// File: /workspace/github_c_makefile_star2_O1/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
je 0x1269
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x131b
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
callq 0x1110
testq %rax, %rax
je 0x1316
movq %rax, %r12
movq %r14, %rdi
callq 0x1070
movq %rax, %r13
movq %r15, %rdi
callq 0x1070
movq %rax, %rbp
subq %r13, %rbp
jbe 0x134c
movq %rbx, %rdi
callq 0x1070
leaq (%rax,%rbp), %rsi
incq %rsi
movq %rbx, %rdi
callq 0x10c0
testq %rax, %rax
je 0x139c
movq %rax, %r13
movq %r15, %rdi
callq 0x1070
leaq (%rax,%r12), %rbx
movq %r14, %rdi
callq 0x1070
movq %r12, %rbp
addq %rax, %rbp
movq %r13, %rdi
callq 0x1070
subq %rbp, %r13
leaq (%rax,%r13), %rdx
incq %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x10e0
movq %r15, %rdi
callq 0x1070
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1080
xorl %ebp, %ebp
jmp 0x133b
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
xorl %eax, %eax
xorl %ebx, %ebx
testq %rbx, %rbx
sete %cl
xorb $0x1, %al
orb %cl, %al
jne 0x133b
movq %rbx, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x1080
movq %r14, %rdi
callq 0x1070
movq %rax, %r14
movq %r15, %rdi
callq 0x1070
xorl %ebp, %ebp
cmpq %rax, %r14
jbe 0x133b
movq %rax, %r15
addq %r12, %r15
addq %r12, %r14
movq %rbx, %rdi
callq 0x1070
subq %r14, %rbx
leaq (%rbx,%rax), %rdx
incq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x10e0
jmp 0x133b
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
movb $0x1, %al
jmp 0x131f