// File: /workspace/github_c_makefile_star2_O3/sasairc[P]yasuna/src/libbenly/src/string.c
// Function: strrep
int strrep(char* src, char* haystack, char* needle)
{
    short   status  = 0;

    char*   bak     = NULL,
        *   find    = NULL;

    if (src == NULL || haystack == NULL || needle == NULL) {
        status = -1; goto ERR;
    }

    /* seach strings */
    if ((find = strstr(src, haystack)) == NULL) {
        status = -2; goto ERR;
    }

    if (strlen(haystack) < strlen(needle)) {
        /* reallocate memory */
        bak = src;
        if ((src = (char*)
                    realloc(src, strlen(src) + strlen(needle) + 1 - strlen(haystack))) == NULL) {
            status = -3; goto ERR;
        }

        /* move match word to specified location in memory */
        memmove(
            find + strlen(needle),
            find + strlen(haystack),
            strlen(src) - strlen(haystack) - (find - src) + 1
        );
        memcpy(find, haystack, strlen(needle));
    } else {
        memcpy(find, needle, strlen(needle));
        /* move match word to specified location in memory */
        if (strlen(haystack) > strlen(needle)) {
            memmove(
                find + strlen(needle),
                find + strlen(haystack),
                strlen(src) - strlen(haystack) - (find - src) + 1
            );
        }
    }

    return 0;

ERR:
    switch (status) {
        case    -1:
        case    -2:
            break;
        case    -3:
            if (bak != NULL) {
                free(bak);
                free(bak);
            }
            break;
    }

    return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
testq %rdx, %rdx
sete %al
orb %cl, %al
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jne 0x137d
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
callq 0x1110
testq %rax, %rax
je 0x1311
movq %rax, %r12
movq %rbx, %rdi
callq 0x1070
movq %rax, %r13
movq %r14, %rdi
callq 0x1070
movq %rax, %rbp
subq %r13, %rbp
jbe 0x1318
movq %r15, %rdi
callq 0x1070
leaq (%rax,%rbp), %rsi
incq %rsi
movq %r15, %rdi
callq 0x10c0
testq %rax, %rax
je 0x1368
movq %rax, %r13
movq %r14, %rdi
callq 0x1070
leaq (%rax,%r12), %r15
movq %rbx, %rdi
callq 0x1070
movq %r12, %rbp
addq %rax, %rbp
movq %r13, %rdi
callq 0x1070
subq %rbp, %r13
leaq (%rax,%r13), %rdx
incq %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x10e0
movq %r14, %rdi
callq 0x1070
movq %r12, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x1080
xorl %ebp, %ebp
jmp 0x137d
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x137d
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1080
movq %rbx, %rdi
callq 0x1070
movq %rax, %rbx
movq %r14, %rdi
callq 0x1070
xorl %ebp, %ebp
cmpq %rax, %rbx
jbe 0x137d
movq %rax, %r14
addq %r12, %r14
addq %r12, %rbx
movq %r15, %rdi
callq 0x1070
subq %rbx, %r15
leaq (%r15,%rax), %rdx
incq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10e0
jmp 0x137d
movq %r15, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq