// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rdi, %rbx
xorl %edx, %edx
pushq $0x1
popq %rax
movq %rdi, %rcx
xorl %esi, %esi
movl %esi, %r8d
movzbl (%rcx), %edi
cmpl $0x27, %edi
je 0x164f
testl %edi, %edi
jne 0x1659
jmp 0x1684
xorl %esi, %esi
testl %edx, %edx
sete %sil
movl %esi, %edx
xorl %esi, %esi
testl %r8d, %r8d
sete %sil
cmpl $0x22, %edi
cmovnel %r8d, %esi
cmpl $0x1, %edx
je 0x167f
cmpl $0x1, %esi
je 0x167f
cmpl $0x20, %edi
je 0x167d
cmpl $0x9, %edi
jne 0x167f
incl %eax
incq %rcx
jmp 0x163e
testl %eax, %eax
jle 0x17ed
movl %eax, %r15d
leaq 0x8(,%r15,8), %rdi
callq 0x1090
testq %rax, %rax
je 0x17ed
movq %rax, (%rsp)
movq $0x0, 0x10(%rsp)
movq %r15, 0x8(%rsp)
movq %rbx, %rdi
callq 0x1070
movq %rax, %r15
movabsq $0x8500000201, %r13     # imm = 0x8500000201
pushq $0x1
popq %r14
xorl %edx, %edx
xorl %eax, %eax
xorl %ecx, %ecx
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq %r15, %r12
ja 0x17ad
movzbl (%rbx,%r12), %edi
xorl %esi, %esi
cmpq $0x27, %rdi
ja 0x17a3
btq %rdi, %r13
jae 0x17a3
xorl %esi, %esi
testl %eax, %eax
sete %sil
cmpb $0x27, %dil
cmovel %esi, %eax
sete %r8b
andb %r8b, %sil
movzbl %sil, %esi
movq %rbp, %r8
addq %rsi, %r8
xorl %esi, %esi
testl %ecx, %ecx
sete %sil
cmpb $0x22, %dil
sete %dil
cmovel %esi, %ecx
andb %dil, %sil
movzbl %sil, %ebp
addq %r8, %rbp
cmpl $0x1, %eax
je 0x1750
cmpl $0x1, %ecx
je 0x1750
cmpl $0x1, %edx
jne 0x1754
incq %rbp
jmp 0x179c
movl %edx, %esi
jmp 0x17a3
movq %r12, %rdi
subq %rbp, %rdi
incq %rdi
callq 0x1090
movq 0x10(%rsp), %rsi
movslq %esi, %rcx
movq (%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
testq %rax, %rax
je 0x17bd
xorl %ecx, %ecx
cmpq %r12, %rbp
jae 0x178c
movb (%rbx,%rbp), %dl
movb %dl, (%rax,%rcx)
incq %rbp
incq %rcx
jmp 0x1779
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
incq %rbp
incl %esi
movq %rsi, 0x10(%rsp)
xorl %eax, %eax
movl %r14d, %esi
xorl %ecx, %ecx
incq %r12
movl %esi, %edx
jmp 0x16db
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
andq $0x0, (%rcx,%rax,8)
jmp 0x17ef
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
movq (%rsp), %r14
cmpq %rbx, %r15
je 0x17e5
movq (%r14,%rbx,8), %rdi
testq %rdi, %rdi
je 0x17e0
callq 0x1030
andq $0x0, (%r14,%rbx,8)
incq %rbx
jmp 0x17c8
movq %r14, %rdi
callq 0x1030
xorl %ecx, %ecx
movq %rcx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq