// File: /workspace/github_c_makefile_star2_O3/sasairc[P]clangsay/src/libbenly/src/cmd.c
// Function: set_io_val
int set_io_val(char* str, int flag, cmd_t** cmd)
{
    int     msc = 0,
            off = 0,
            len = 0;

    short   unt = 0;

    char*   tmp = NULL,
        *   bak = NULL;

    io_t*   io  = NULL;

    /*
     * # io_unit
     * 1. N> file or N>> (N = STDIN, STDOUT, STDERR)
     * 2. > or >>
     * 3. < or <<
     */
    if (isdigit(*str) && *(str + 1) == '>') {
        unt = atoi(&*str);
        if (*(str + 1) == '>' && *(str + 2) == '>')
            str += 3;
        else
            str += 2;
    } else if (*(str + 1) == '>') {
        unt = 1;
        if (*(str + 2) == '>')
            str += 3;
        else
            str += 2;
    } else {
        unt = 0;
        if (*(str + 2) == '<')
            str += 3;
        else
            str += 2;
    }

    bak = str;
    while ( *str != ';'     &&
            *str != '|'     &&
            *str != '&'     &&
            *str != '<'     &&
            *str != '>'     &&
            *str != '\0'    &&
            *str != '\n') {
        str++;
        len++;
    }
    while (*str != '\0' || *str != '\n') {
        if (*str == '&' && *(str + 1) == '&') {
            (*cmd)->type = TAND;
            off++;
            break;
        } else if (*str == '|' && *(str + 1) == '|') {
            (*cmd)->type = TOR;
            off++;
            break;
        } else if (*str == '|') {
            (*cmd)->type = TPIPE;
            off++;
            break;
        } else if (!isspace(*str)) {
            break;
        }
        str++;
    }

    str = bak;
    if ((tmp = (char*)
                malloc(sizeof(char) * (len + 1))) == NULL)
        return -2;

    memcpy(tmp, str, len);
    tmp[len] = '\0';

    if ((msc = trim(tmp)) > 0)
        msc--;

    if ((io = (io_t*)
                malloc(sizeof(io_t))) == NULL)
        return -2;

    if ((io->io_name = (char*)
                malloc(sizeof(char) * (len + 1))) == NULL)
        return -3;

    memcpy(io->io_name, tmp, strlen(tmp) + 1);
    io->io_unit = unt;
    io->io_flag = flag;
    free(tmp);

    (*cmd)->io = io;

    return len + off + 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %r15
callq 0x21e0
movq (%rax), %r13
movsbq (%r15), %rax
testb $0x8, 0x1(%r13,%rax,2)
movb 0x1(%r15), %al
jne 0x2b14
cmpb $0x3e, %al
jne 0x2b26
movw $0x1, %bx
jmp 0x2b22
cmpb $0x3e, %al
jne 0x2b26
movq %r15, %rdi
callq 0x22f0
movl %eax, %ebx
movb $0x3e, %dl
jmp 0x2b2a
movb $0x3c, %dl
xorl %ebx, %ebx
leaq 0x2(%r15), %r12
leaq 0x3(%r15), %rcx
cmpb %dl, 0x2(%r15)
cmoveq %rcx, %r12
leaq 0x1(%r12), %rsi
movl $0x1, %r15d
movabsq $0x5800004000000401, %rdx # imm = 0x5800004000000401
movq %r14, 0x10(%rsp)
movl %ebp, 0xc(%rsp)
movzbl -0x1(%rsi), %ecx
cmpq $0x3e, %rcx
ja 0x2b68
btq %rcx, %rdx
jb 0x2b7a
cmpl $0x7c, %ecx
je 0x2b7a
incq %rsi
incq %r15
jmp 0x2b58
movb (%rsi), %cl
incq %rsi
cmpb $0x26, %cl
je 0x2b89
movzbl %cl, %edx
cmpl $0x7c, %edx
jne 0x2b8e
jmp 0x2b9f
cmpb $0x26, (%rsi)
je 0x2baf
movsbq %cl, %rcx
testb $0x20, 0x1(%r13,%rcx,2)
jne 0x2b75
xorl %r14d, %r14d
jmp 0x2bc6
cmpb $0x7c, (%rsi)
movq (%r14), %rcx
jne 0x2bba
movl $0x5, (%rcx)
jmp 0x2bc0
movq (%r14), %rax
movl $0x4, (%rax)
jmp 0x2bc0
movl $0x3, (%rcx)
movl $0x1, %r14d
movq %r15, %rdi
callq 0x20e0
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
testq %rax, %rax
je 0x2c6a
movq %rax, %r13
leaq -0x1(%r15), %rdx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2470
movb $0x0, -0x1(%r13,%r15)
movq %r13, %rdi
callq 0x2c7b
movl $0x10, %edi
callq 0x20e0
testq %rax, %rax
je 0x2c6a
movq %rax, %r12
movq %r15, %rdi
callq 0x20e0
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x2c65
movq %rax, %rbp
movq %r13, %rdi
callq 0x21b0
leaq 0x1(%rax), %rdx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2470
movw %bx, (%r12)
movl 0xc(%rsp), %eax
movw %ax, 0x2(%r12)
movq %r13, %rdi
callq 0x21a0
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %r12, 0x10(%rax)
addl %r15d, %r14d
movl %r14d, %ebp
jmp 0x2c6a
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq