// File: /workspace/github_c_makefile_star2_O1/sasairc[P]yasuna/src/libbenly/src/string.c
// Function: str_to_args
char** str_to_args(char* str)
{
    /*
     * # note
     *
     *  sx   : current strings array pointer
     *  xt   : temporary string array pointer
     *  ax   : args array pointer (X)
     *  ay   : args array pointer (Y)
     *  elmc : elements counter
     *  dspf : double space flag
     *  sqtf : single quote flag
     *  dqtf : double quote flag
     */
    int     i,
            ax, ay,
            elmc,
            dspf,
            sqtf,
            dqtf;

    size_t  sx, xt;

    char**  args    = NULL;
    
    /* count elements */
    for (i = dspf = sqtf = dqtf = 0, elmc = 1; str[i] != '\0'; i++) {
        if (str[i] == '\'') {
            if (sqtf == 0)
                sqtf = 1;
            else
                sqtf = 0;
        }
        if (str[i] == '\"') {
            if (dqtf == 0)
                dqtf = 1;
            else
                dqtf = 0;
        }
        if (sqtf == 1 || dqtf == 1)
            continue;

        if (str[i] == ' ' || str[i] == '\t') {
            if (dspf == 1)
                continue;
            elmc++;
        } else {
            dspf = 0;
        }
    }
    if (elmc > 0) {
        if ((args = (char**)
                    malloc(sizeof(char*) * (elmc + 1))) == NULL)
            return NULL;
    } else {
        return NULL;
    }

    /* string to args */
    for (dspf = sqtf = dqtf = sx = ay = ax = xt = 0; sx <= strlen(str); sx++) {
        if (str[sx] == ' ' || str[sx] == '\t' || str[sx] == '\0' || str[sx] == '\'' || str[sx] == '\"') {
            if (str[sx] == '\'') {
                if (sqtf == 0) {
                    xt++;
                    sqtf = 1;
                } else {
                    sqtf = 0;
                }
            }
            if (str[sx] == '\"') {
                if (dqtf == 0) {
                    xt++;
                    dqtf = 1;
                } else {
                    dqtf = 0;
                }
            }
            if (sqtf == 1 || dqtf == 1)
                continue;

            if (dspf == 1) {
                xt++;
                continue;
            }
            if ((args[ay] = (char*)
                        malloc(sizeof(char) * (sx - xt + 1))) == NULL)
                goto ERR;

            for (ax = 0; xt < sx; xt++, ax++)
                    args[ay][ax] = str[xt];

            args[ay][ax] = '\0';
            xt++;
            ay++;
            dspf = 1;
        } else {
            dspf = 0;
        }
    }
    /* null-terminated */
    args[elmc] = NULL;

#ifdef  DEBUG
    fprintf(stderr, "DEBUG: str_to_args(): args(%p)\n", args);
    for (i = 0; i <= elmc; i++)
        fprintf(stderr, "DEBUG: str_to_args(): args[%d](%p) = %s\n", i, args[i], args[i]);
/* DEBUG */
#endif

    return args;


ERR:
    for (i = 0; i < elmc; i++) {
        if (args[i] != NULL) {
            free(args[i]);
            args[i] = NULL;
        }
    }
    free(args);

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
xorl %edx, %edx
movl $0x1, %eax
movq %rdi, %rcx
xorl %esi, %esi
movl %esi, %r8d
movzbl (%rcx), %r9d
cmpl $0x27, %r9d
je 0x16ae
testl %r9d, %r9d
jne 0x16b8
jmp 0x16e6
xorl %esi, %esi
testl %edx, %edx
sete %sil
movl %esi, %edx
xorl %esi, %esi
testl %r8d, %r8d
sete %sil
cmpl $0x22, %r9d
cmovnel %r8d, %esi
cmpl $0x1, %edx
je 0x16e1
cmpl $0x1, %esi
je 0x16e1
cmpl $0x20, %r9d
je 0x16df
cmpl $0x9, %r9d
jne 0x16e1
incl %eax
incq %rcx
jmp 0x169a
testl %eax, %eax
jle 0x18cd
movq %rdi, 0x10(%rsp)
movl %eax, %ebx
leaq 0x8(,%rbx,8), %rdi
callq 0x1090
xorl %r12d, %r12d
testq %rax, %rax
je 0x18cd
movq %rbx, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1070
movq %rbx, %r8
movabsq $0x8500000201, %r10     # imm = 0x8500000201
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r9d, %r9d
xorl %ecx, %ecx
xorl %r11d, %r11d
movq %rax, 0x28(%rsp)
movzbl (%r8,%r13), %esi
xorl %edx, %edx
cmpq $0x27, %rsi
ja 0x1879
btq %rsi, %r10
jae 0x1879
xorl %edx, %edx
testl %r9d, %r9d
sete %dl
cmpb $0x27, %sil
sete %dil
cmovel %edx, %r9d
andb %dil, %dl
movzbl %dl, %edi
leaq (%r14,%rdi), %rbx
xorl %edx, %edx
testl %r12d, %r12d
sete %dl
cmpb $0x22, %sil
sete %sil
cmovel %edx, %r12d
andb %sil, %dl
movzbl %dl, %r15d
leaq (%rbx,%r15), %rbp
cmpl $0x1, %r9d
je 0x17b6
cmpl $0x1, %r12d
je 0x17b6
cmpl $0x1, %ecx
jne 0x17bd
incq %rbp
jmp 0x186b
movl %ecx, %edx
jmp 0x1876
movq %rdi, 0x30(%rsp)
movq %r13, %rdi
subq %rbp, %rdi
incq %rdi
movl %r11d, 0xc(%rsp)
callq 0x1090
movl 0xc(%rsp), %r11d
movq %rax, %r12
movslq %r11d, %rax
movq 0x18(%rsp), %rcx
movq %r12, (%rcx,%rax,8)
testq %r12, %r12
je 0x189b
cmpq %r13, %rbp
jae 0x184a
addq %r15, %r14
addq 0x30(%rsp), %r14
movq 0x10(%rsp), %rax
leaq (%r14,%rax), %rsi
addq %r15, %rbx
movq %r13, %rdx
subq %rbx, %rdx
movq %r12, %rdi
callq 0x1080
xorl %eax, %eax
leaq (%r14,%rax), %rcx
incq %rcx
incq %rax
cmpq %r13, %rcx
jb 0x181b
movl %eax, %ecx
movq %r13, %rbp
movq 0x10(%rsp), %r8
movq 0x28(%rsp), %rax
movabsq $0x8500000201, %r10     # imm = 0x8500000201
movl 0xc(%rsp), %r11d
jmp 0x1860
xorl %ecx, %ecx
movq 0x10(%rsp), %r8
movq 0x28(%rsp), %rax
movabsq $0x8500000201, %r10     # imm = 0x8500000201
movb $0x0, (%r12,%rcx)
incq %rbp
incl %r11d
xorl %r9d, %r9d
movl $0x1, %edx
xorl %r12d, %r12d
movq %rbp, %r14
incq %r13
movl %edx, %ecx
cmpq %rax, %r13
jbe 0x1745
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x18cf
xorl %ebx, %ebx
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r15
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x18bd
callq 0x1030
movq $0x0, (%r14,%rbx,8)
incq %rbx
cmpq %rbx, %r15
jne 0x18a7
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq