// File: /workspace/github_c_makefile_star2_O1/nerves-project[P]nerves_initramfs/src/cmd.c
// Function: system_cmd
int system_cmd(char * const *argv, char *output_buffer, int length)
{
    int pipefd[2];
    if (pipe(pipefd) < 0)
        return -1;

    pid_t pid = fork();
    if (pid == 0) {
        // child
        int devnull = open("/dev/null", O_RDWR);
        if (devnull < 0)
            exit(EXIT_FAILURE);

        close(pipefd[0]); // no reads from the pipe
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        dup2(devnull, STDIN_FILENO);
        dup2(pipefd[1], STDOUT_FILENO);
        close(devnull);

        execvp(argv[0], argv);

        // Not supposed to reach here.
        exit(EXIT_FAILURE);
    } else {
        // parent
        close(pipefd[1]); // No writes to the pipe

        if (length > 0) {
            memset(output_buffer, 0, length);
            length--; // Save room for a final '\0'
        }

        int index = 0;
        for (;;) {
            char throwaway[256];

            char *p;
            int amount_to_read = length - index;
            if (amount_to_read > 0) {
                p = &output_buffer[index];
            } else {
                p = throwaway;
                amount_to_read = sizeof(throwaway);
            }

            int amount_read = read(pipefd[0], p, amount_to_read);
            if (amount_read <= 0)
                break;

            if (p != throwaway)
                index += amount_read;
        }

        close(pipefd[0]);

        int status;
        if (waitpid(pid, &status, 0) != pid)
            return -1;

        return status;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
leaq 0x4(%rsp), %rdi
callq 0x2290
testl %eax, %eax
js 0x8668
callq 0x2500
testl %eax, %eax
je 0x867f
movl %eax, %ebx
movl 0x8(%rsp), %edi
callq 0x2280
testl %ebp, %ebp
movl %ebx, 0xc(%rsp)
jle 0x85f9
movl %ebp, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2240
decl %ebp
xorl %r12d, %r12d
leaq 0x10(%rsp), %r13
xorl %ebx, %ebx
movl %ebp, %edx
subl %ebx, %edx
movl %ebx, %r15d
addq %r14, %r15
testl %edx, %edx
cmovleq %r13, %r15
movl $0x100, %eax            # imm = 0x100
cmovlel %eax, %edx
movl 0x4(%rsp), %edi
movq %r15, %rsi
callq 0x22c0
testl %eax, %eax
movl %eax, %ecx
cmovlel %r12d, %ecx
cmpq %r13, %r15
cmovel %r12d, %ecx
addl %ecx, %ebx
testl %eax, %eax
jg 0x8603
movl 0x4(%rsp), %edi
callq 0x2280
leaq 0x10(%rsp), %rsi
movl 0xc(%rsp), %ebx
movl %ebx, %edi
xorl %edx, %edx
callq 0x23e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ebx, %ecx
jne 0x866d
movl 0x10(%rsp), %eax
jmp 0x866d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x23b5(%rip), %rdi      # 0xaa3b
movl $0x2, %esi
xorl %eax, %eax
callq 0x2410
testl %eax, %eax
js 0x86db
movl %eax, %ebx
movl 0x4(%rsp), %edi
callq 0x2280
xorl %edi, %edi
callq 0x2280
movl $0x1, %edi
callq 0x2280
movl %ebx, %edi
xorl %esi, %esi
callq 0x21d0
movl 0x8(%rsp), %edi
movl $0x1, %esi
callq 0x21d0
movl %ebx, %edi
callq 0x2280
movq (%r15), %rdi
movq %r15, %rsi
callq 0x2450
movl $0x1, %edi
callq 0x2490
nopl (%rax)