I've been working on a c program called ft_split_whitespaces. 
What the program is supposed to do, is to find words and put them into a string array.
I have to do this for a project and I can't use any function except malloc() and sizeof(). I'm only using printf() and other stuff for debugging purposes. 
So, I ran into a problem that when I would debug the program it would work fine and if I would run it I would get segfault.
I researched this topic and it turned out to be a heisenbug. I tried several ways to find the bug by turning off the debugger optimization but the result is always that if I go over the program step by step it works and otherwise, it won't.
Another weird thing I ran into that the program would work perfectly on my computer but when running on a different computer it would ones again segfault.
So here is my program and any help would be great.
#include <stdlib.h>
#include <stdio.h>
#define W1(c)   (c == ' ' ||  c == '\n' || c == '\t')
#define W2(c)   (c == '\v' || c == '\f' || c == '\r')
int     ft_wordcount(char *str)
{
    int i;
    int ws;
    ws = 1;
    i = 0;
    while (*str)
    {
        if (W1(*str) || W2(*str))
            ws = 1;
        else
        {
            if (ws)
                i++;
            ws = 0;
        }
        str++;
    }
    return (i);
}
int     ft_wordlen(char *str)
{
    int ln;
    ln = 0;
    while (!(W1(*str)) && !(W2(*str)) && *str != '\0')
    {
        ln++;
        str++;
    }
    return (ln);
}
char    **ft_assign(char *str)
{
    int     i;
    char    **res;
    int wordcount;
    int ln;
    i = 0;
    wordcount = ft_wordcount(str);
    res = (char **)malloc(wordcount * sizeof(char));
    while (i < wordcount)
    {
        if (!(W1(*str)) && !(W2(*str)))
        {
            ln = ft_wordlen(str);
            res[i] = malloc((ln + 1) * sizeof(char));
            res[i][ln] = '\0';
            str += ln;
            i++;
        }
        str++;
    }
    return (res);
}
char **ft_split_whitespaces(char *str)
{
    char    **res;
    int     i;
    int     wordcount;
    int     pos;
    wordcount = ft_wordcount(str);
    i = 0;
    res = ft_assign(str);
    while (i < wordcount)
    {
        if (!(W1(*str)) && !(W2(*str)))
        {
            pos = 0;
            while (!(W1(*str)) && !(W2(*str)) && *str != '\0')
                res[i][pos++] = *(str++);
            i++;
        }
        str++;
    }
    return (res);
}
int main(void)
{
    int i;
    int ln;
    i = 0;
    char test[] = "yes  no yuuh     WORD   adjdsfjlksdj   sdfjkdsfjkjsd     sfdkjsdlkjf  sfdds\tfsd";
    char **words;
    words = ft_split_whitespaces(test);
    ln = ft_wordcount(test);
    while (i < ln)
    {
        printf("%s\n", words[i]);
        i++;
    }
}
 
     
     
    