// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movw $0x0, -0x22(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x12b2
cmpq $0x0, -0x18(%rbp)
je 0x12b2
cmpq $0x0, -0x20(%rbp)
jne 0x12bd
movw $0xffff, -0x22(%rbp)    # imm = 0xFFFF
jmp 0x1523
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1110
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x12df
movw $0xfffe, -0x22(%rbp)    # imm = 0xFFFE
jmp 0x1523
movq -0x18(%rbp), %rdi
callq 0x1070
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1070
movq %rax, %rcx
movq -0x40(%rbp), %rax
cmpq %rcx, %rax
jae 0x141f
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1070
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1070
movq %rax, %rcx
movq -0x58(%rbp), %rax
addq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1070
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdi
subq %rax, %rsi
callq 0x10c0
movq %rax, -0x10(%rbp)
cmpq $0x0, %rax
jne 0x136b
movw $0xfffd, -0x22(%rbp)    # imm = 0xFFFD
jmp 0x1523
movq -0x38(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1070
movq %rax, %rcx
movq -0x90(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1070
movq %rax, %rcx
movq -0x88(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1070
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1070
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
subq %rax, %rdx
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
subq %rax, %rdx
addq $0x1, %rdx
callq 0x10e0
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1070
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq %rax, %rdx
callq 0x1080
jmp 0x151a
movq -0x38(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1070
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq %rax, %rdx
callq 0x1080
movq -0x18(%rbp), %rdi
callq 0x1070
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1070
movq %rax, %rcx
movq -0x98(%rbp), %rax
cmpq %rcx, %rax
jbe 0x1518
movq -0x38(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1070
movq %rax, %rcx
movq -0xd0(%rbp), %rax
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1070
movq %rax, %rcx
movq -0xc8(%rbp), %rax
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1070
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1070
movq -0xc0(%rbp), %rdx
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
subq %rax, %rdx
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
subq %rax, %rdx
addq $0x1, %rdx
callq 0x10e0
jmp 0x151a
movl $0x0, -0x4(%rbp)
jmp 0x1568
movswl -0x22(%rbp), %eax
movl %eax, -0xd4(%rbp)
subl $-0x3, %eax
je 0x1546
jmp 0x1534
movl -0xd4(%rbp), %eax
addl $0x2, %eax
subl $0x1, %eax
ja 0x1561
jmp 0x1544
jmp 0x1561
cmpq $0x0, -0x30(%rbp)
je 0x155f
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x30(%rbp), %rdi
callq 0x1030
jmp 0x1561
movswl -0x22(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)