// File: /workspace/github_c_makefile_star2_O3/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 0x82d2
callq 0x2510
testl %eax, %eax
je 0x82e9
movl %eax, %ebx
movl 0x8(%rsp), %edi
callq 0x2280
testl %ebp, %ebp
movl %ebx, 0xc(%rsp)
jle 0x8265
movl %ebp, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2240
decl %ebp
leaq 0x10(%rsp), %r13
movl $0x100, %ebx            # imm = 0x100
xorl %r12d, %r12d
movl %ebp, %edx
subl %r12d, %edx
movl %r12d, %r15d
addq %r14, %r15
testl %edx, %edx
cmovleq %r13, %r15
cmovlel %ebx, %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 0x8272
movl 0x4(%rsp), %edi
callq 0x2280
leaq 0x10(%rsp), %rsi
movl 0xc(%rsp), %ebx
movl %ebx, %edi
xorl %edx, %edx
callq 0x23f0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ebx, %ecx
jne 0x82d7
movl 0x10(%rsp), %eax
jmp 0x82d7
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x276b(%rip), %rdi      # 0xaa5b
movl $0x2, %esi
xorl %eax, %eax
callq 0x2420
testl %eax, %eax
js 0x8345
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 0x2460
movl $0x1, %edi
callq 0x24a0
nop