String search & replace in C
Since this is about strings, I would rather use strcpy instead of memcpy. This has the benefit, that it saves the unnecessary strlen 

strcpy(buffer, haystack);


You can declare the other parameters const too, e.g. 

char *buffer;
const char *needle, *rep;


The for loop is a bit confusing, because it copies just one character at offset diff, no matter how large diff is. If you want to close a potential gap, you could again use strcpy 

size_t needle_length = strlen(needle);
...
strcpy(buffer + rep_length, buffer + needle_length);


The other way round - if rep is larger than needle, you need to make space for the larger replacement string. Otherwise, you overwrite parts of haystack! (Unless this is what you intended, of course.)



You can avoid this, when you copy from haystack to buffer as you go, something like 

size_t needle_length = strlen(needle);
char *found;
while ((found = strstr(haystack, needle))) {
    /* copy haystack part until needle */
    size_t n = found - haystack;
    strncpy(buffer, haystack, n);
    /* copy replacement string */
    strcpy(buffer + n, rep);
    /* adjust pointers */
    buffer += n + rep_length;
    haystack = found + needle_length;
}

/* copy remaining haystack */
strcpy(buffer, haystack);