// File: /workspace/github_c_makefile_star2_O2/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, %ebx
movq %rsi, %r14
movq %rdi, %r15
leaq 0x4(%rsp), %rdi
callq 0x2290
testl %eax, %eax
js 0x7873
callq 0x2510
testl %eax, %eax
je 0x7888
movl %eax, %ebp
movl 0x8(%rsp), %edi
callq 0x2280
testl %ebx, %ebx
movl %ebp, 0xc(%rsp)
jle 0x780d
movl %ebx, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2240
decl %ebx
leaq 0x10(%rsp), %r13
movl $0x100, %ebp            # imm = 0x100
xorl %r12d, %r12d
movl %ebx, %edx
subl %r12d, %edx
movl %r12d, %r15d
addq %r14, %r15
testl %edx, %edx
cmovleq %r13, %r15
cmovlel %ebp, %edx
movl 0x4(%rsp), %edi
movq %r15, %rsi
callq 0x22c0
cmpq %r13, %r15
movl %eax, %ecx
movl $0x0, %edx
cmovel %edx, %ecx
addl %ecx, %r12d
testl %eax, %eax
jg 0x781a
movl 0x4(%rsp), %edi
callq 0x2280
leaq 0x10(%rsp), %rsi
movl 0xc(%rsp), %ebx
movl %ebx, %edi
xorl %edx, %edx
callq 0x23f0
cmpl %ebx, %eax
jne 0x7873
movl 0x10(%rsp), %eax
jmp 0x7876
pushq $-0x1
popq %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x21dc(%rip), %rdi      # 0x9a6b
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x2420
testl %eax, %eax
jns 0x78a5
pushq $0x1
popq %rdi
callq 0x24a0
movl %eax, %ebx
movl 0x4(%rsp), %edi
callq 0x2280
xorl %edi, %edi
callq 0x2280
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x2280
movl %ebx, %edi
xorl %esi, %esi
callq 0x21d0
movl 0x8(%rsp), %edi
movl %ebp, %esi
callq 0x21d0
movl %ebx, %edi
callq 0x2280
movq (%r15), %rdi
movq %r15, %rsi
callq 0x2460
movl %ebp, %edi
callq 0x24a0
nop