// File: /workspace/github_c_makefile_star2_O0/sasairc[P]clangsay/src/libbenly/src/uri.c
// Function: encode_uri
int encode_uri(char* const src, char** dest)
{
    int     status  = 0;

    size_t  bufsiz  = DEFAULT_BUFSIZE,
            current = 0;

    char*   p       = src;

    if ((*dest = (char*)
                malloc(sizeof(char) * bufsiz)) == NULL) {
        status = -1; goto ERR;
    } else {
        memset(*dest, '\0', bufsiz);
    }

    while (*p != '\0') {
        if ((current + 3) >= bufsiz) {
            bufsiz += DEFAULT_BUFSIZE;
            if ((*dest = (char*)
                        realloc(*dest, sizeof(char) * bufsiz)) == NULL) {
                status = -2; goto ERR;
            } else {
                memset(*dest + current, '\0', DEFAULT_BUFSIZE);
            }
        }
        if (
                (*p >= 0x20 && *p <= 0x39)  ||  /* [0-9] */
                (*p >= 0x41 && *p <= 0x5A)  ||  /* [A-Z] */
                (*p >= 0x61 && *p <= 0x7A)  ||  /* [a-z] */
                (*p == 0x2E)                ||  /* . */
                (*p == 0x2F)                ||  /* / */
                (*p == 0x3A)                    /* : */
           ) {
            *(*(dest) + current) = *p;
            current++;
        } else {
            current += sprintf(*dest + current, "%%%02X",
                    *p & 0x000000FF);
        }
        p++;
    }

    return 0;

ERR:
    switch (status) {
        case    -1:
        case    -2:
            fprintf(stderr, "%s\n",
                    strerror(errno));
            break;
    }

    return status;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq $0x400, -0x28(%rbp)     # imm = 0x400
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rdi
shlq $0x0, %rdi
callq 0x20e0
movq -0x18(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0xf535
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
jmp 0xf693
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rdx
xorl %esi, %esi
callq 0x2050
jmp 0xf549
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0xf68a
movq -0x30(%rbp), %rax
addq $0x3, %rax
cmpq -0x28(%rbp), %rax
jb 0xf5bb
movq -0x28(%rbp), %rax
addq $0x400, %rax            # imm = 0x400
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rsi
shlq $0x0, %rsi
callq 0x23c0
movq -0x18(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0xf5a2
movl $0xfffffffe, -0x1c(%rbp) # imm = 0xFFFFFFFE
jmp 0xf693
movq -0x18(%rbp), %rax
movq (%rax), %rdi
addq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x2050
jmp 0xf5bb
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jl 0xf5d3
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x39, %eax
jle 0xf627
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x41, %eax
jl 0xf5eb
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5a, %eax
jle 0xf627
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x61, %eax
jl 0xf603
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x7a, %eax
jle 0xf627
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
je 0xf627
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
je 0xf627
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3a, %eax
jne 0xf649
movq -0x38(%rbp), %rax
movb (%rax), %dl
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xf679
movq -0x18(%rbp), %rax
movq (%rax), %rdi
addq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movsbl (%rax), %edx
andl $0xff, %edx
leaq 0x2aa0(%rip), %rsi      # 0x12108
movb $0x0, %al
callq 0x21e0
cltq
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xf549
movl $0x0, -0x4(%rbp)
jmp 0xf6d5
movl -0x1c(%rbp), %eax
addl $0x2, %eax
subl $0x1, %eax
ja 0xf6cf
jmp 0xf6a0
movq 0x7941(%rip), %rax      # 0x16fe8
movq (%rax), %rax
movq %rax, -0x40(%rbp)
callq 0x2350
movl (%rax), %edi
callq 0x2260
movq -0x40(%rbp), %rdi
movq %rax, %rdx
leaq 0x335f(%rip), %rsi      # 0x12a27
movb $0x0, %al
callq 0x23f0
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop