// File: /workspace/github_c_makefile_star2_O0/sasairc[P]clangsay/src/libbenly/src/cmd.c
// Function: file_redirect
int file_redirect(cmd_t* cmd)
{
    int fd  = 0;
    
    switch (cmd->io->io_flag) {
        case    IOREAD:
        case    IOHERE:
            if (check_file_stat(cmd, 1, S_IREAD) < 0)
                return -1;
            if ((fd = open(cmd->io->io_name,
                            O_RDONLY)) < 0) {
                perror("ysh: fopen");
                close(fd);

                return -1;
            }
            close(0);
            dup2(fd, 0);
            break;
        case    IOWRITE:
            if (check_file_stat(cmd, 1, S_IWRITE) < 0)
                return -1;
            if ((fd = open(cmd->io->io_name,
                            O_WRONLY    |
                            O_CREAT     |
                            O_TRUNC, 0666)) < 0) {
                perror("ysh: fopen");
                close(fd);

                return -1;
            }
            close(cmd->io->io_unit);
            dup2(fd, cmd->io->io_unit);
            break;
        case    IOCAT:
            if (check_file_stat(cmd, 1, S_IWRITE) < 0)
                return -1;
            if ((fd = open(cmd->io->io_name,
                            O_WRONLY    |
                            O_CREAT     |
                            O_APPEND, 0666)) < 0) {
                perror("ysh: fopen");
                close(fd);

                return -1;
            }
            close(cmd->io->io_unit);
            dup2(fd, cmd->io->io_unit);
            break;
    }
    close(fd);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movswl 0x2(%rax), %eax
movl %eax, -0x18(%rbp)
decl %eax
subl $0x2, %eax
jb 0x35ba
jmp 0x359b
movl -0x18(%rbp), %eax
subl $0x3, %eax
je 0x3631
jmp 0x35a9
movl -0x18(%rbp), %eax
subl $0x4, %eax
je 0x36c2
jmp 0x3748
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x3760
cmpl $0x0, %eax
jge 0x35de
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3757
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x2420
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
jge 0x361b
leaq 0xea14(%rip), %rdi      # 0x12016
callq 0x23e0
movl -0x14(%rbp), %edi
callq 0x2060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3757
xorl %edi, %edi
callq 0x2060
movl -0x14(%rbp), %edi
xorl %esi, %esi
callq 0x2040
jmp 0x3748
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x80, %edx
callq 0x3760
cmpl $0x0, %eax
jge 0x3655
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3757
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x2420
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
jge 0x369a
leaq 0xe995(%rip), %rdi      # 0x12016
callq 0x23e0
movl -0x14(%rbp), %edi
callq 0x2060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3757
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movswl (%rax), %edi
callq 0x2060
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movswl (%rax), %esi
callq 0x2040
jmp 0x3748
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x80, %edx
callq 0x3760
cmpl $0x0, %eax
jge 0x36e3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3757
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x2420
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
jge 0x3725
leaq 0xe907(%rip), %rdi      # 0x12016
callq 0x23e0
movl -0x14(%rbp), %edi
callq 0x2060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3757
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movswl (%rax), %edi
callq 0x2060
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movswl (%rax), %esi
callq 0x2040
movl -0x14(%rbp), %edi
callq 0x2060
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq