// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
leaq -0x24(%rbp), %rdi
callq 0x2260
cmpl $0x0, %eax
jge 0xb3c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xb52f
callq 0x24c0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0xb446
leaq 0x2878(%rip), %rdi      # 0xdc4d
movl $0x2, %esi
movb $0x0, %al
callq 0x23d0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0xb3f4
movl $0x1, %edi
callq 0x2450
movl -0x24(%rbp), %edi
callq 0x2250
xorl %edi, %edi
callq 0x2250
movl $0x1, %edi
callq 0x2250
movl -0x2c(%rbp), %edi
xorl %esi, %esi
callq 0x21b0
movl -0x20(%rbp), %edi
movl $0x1, %esi
callq 0x21b0
movl -0x2c(%rbp), %edi
callq 0x2250
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x2410
movl $0x1, %edi
callq 0x2450
movl -0x20(%rbp), %edi
callq 0x2250
cmpl $0x0, -0x1c(%rbp)
jle 0xb46c
movq -0x18(%rbp), %rdi
movslq -0x1c(%rbp), %rdx
xorl %esi, %esi
callq 0x2210
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x1c(%rbp), %eax
subl -0x30(%rbp), %eax
movl %eax, -0x13c(%rbp)
cmpl $0x0, -0x13c(%rbp)
jle 0xb49c
movq -0x18(%rbp), %rax
movslq -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x138(%rbp)
jmp 0xb4b4
leaq -0x130(%rbp), %rax
movq %rax, -0x138(%rbp)
movl $0x100, -0x13c(%rbp)    # imm = 0x100
movl -0x24(%rbp), %edi
movq -0x138(%rbp), %rsi
movslq -0x13c(%rbp), %rdx
callq 0x2280
movl %eax, -0x140(%rbp)
cmpl $0x0, -0x140(%rbp)
jg 0xb4db
jmp 0xb4ff
movq -0x138(%rbp), %rax
leaq -0x130(%rbp), %rcx
cmpq %rcx, %rax
je 0xb4fa
movl -0x140(%rbp), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0xb473
movl -0x24(%rbp), %edi
callq 0x2250
movl -0x28(%rbp), %edi
leaq -0x144(%rbp), %rsi
xorl %edx, %edx
callq 0x23a0
cmpl -0x28(%rbp), %eax
je 0xb526
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xb52f
movl -0x144(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopl (%rax,%rax)