// File: /workspace/github_c_makefile_star2_O3/dasJ[P]sd-zfs/src/zfs-util.c
// Function: execute
int execute(char *command, char needOutput, char **output, char *param[]) {
	int pip[2];
	pid_t pid;
	int status;
	char *linebuffer;
	size_t size;
	int nRead;

	// Execute
	if (needOutput == 1) {
		if (pipe(pip) == -1) {
			perror("Cannot create pipe\n");
			exit(1);
		}
	}
	pid = fork();
	if (pid == 0) {
		// Set up pipe
		if (needOutput == 1) {
			// Redirect stdout to pipe
			while ((dup2(pip[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {}
			if (errno != 0) {
				perror("Cannot connect to parent pipe\n");
				exit(1);
			}
			// Close pipe, we won't need it anymore
			close(pip[0]);
			close(pip[1]);
		} else {
			// Close stdout so it doesn't get written to the journal
			close(1);
		}
		// Execute
		execv(command, param);
		exit(254);
	} else if (pid < 0) {
		perror("Cannot fork\n");
		if (needOutput == 1) {
			close(pip[0]);
			close(pip[1]);
		}
		return pid;
	}
	// Capture output
	if (needOutput == 1) {
		// We don't need to write to the pipe
		close(pip[1]);
		// Read lines
		linebuffer = malloc(BUFSIZE * sizeof(char));
		while (1) {
			while ((nRead = read(pip[0], linebuffer, BUFSIZE)) > 0) {
				// Append buffer to output
				if (*output == NULL) {
					*output = malloc(nRead + 1);
					memcpy(*output, linebuffer, nRead);
					(*output)[nRead] = '\0';
				} else {
					size = strlen(*output) + nRead + 1;
					*output = realloc(*output, size);
					memcpy(&(*output)[strlen(*output)], linebuffer, nRead);
					(*output)[size - 1] = '\0';
				}
			}
			if (nRead == 0) {
				break;
			}
		}
		close(pip[0]);
		free(linebuffer);
	}
	// Wait for quit
	waitpid(pid, &status, 0);
	return status;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r15
movq %rdi, %r12
cmpb $0x1, %sil
jne 0x4020c4
movq %rdx, %r14
leaq -0x30(%rbp), %rdi
callq 0x45b620
cmpl $-0x1, %eax
je 0x402142
callq 0x459f20
testl %eax, %eax
je 0x4020fd
movl %eax, %r13d
js 0x402137
movl %r13d, -0x34(%rbp)
movl -0x2c(%rbp), %edi
callq 0x45b530
movl $0x10, %edi
callq 0x420f50
movq %rax, %r15
movl -0x30(%rbp), %edi
movl $0x10, %edx
movq %r15, %rsi
callq 0x45b2c0
movq %rax, %r12
testl %r12d, %r12d
jle 0x4020a5
movq (%r14), %rbx
testq %rbx, %rbx
je 0x402070
movq %rbx, %rdi
callq 0x4010e0
movq %rax, %r13
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
leaq (%rax,%r12), %rsi
incq %rsi
addq %r12, %r13
movq %rbx, %rdi
callq 0x4215e0
movq %rax, %rbx
movq %rax, %rdi
callq 0x4010e0
leaq (%rax,%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x401040
addq %rbx, %r13
jmp 0x402098
leal 0x1(%r12), %edi
callq 0x420f50
movq %rax, %rbx
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x401040
movq %rbx, %r13
addq %r12, %r13
movb $0x0, (%r13)
movq %rbx, (%r14)
jmp 0x402009
testl %r12d, %r12d
jne 0x402009
movl -0x30(%rbp), %edi
callq 0x45b530
movq %r15, %rdi
callq 0x421290
movl -0x34(%rbp), %r13d
jmp 0x4020d2
callq 0x459f20
testl %eax, %eax
je 0x402147
movl %eax, %r13d
js 0x402129
leaq -0x38(%rbp), %rbx
movl %r13d, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x459e60
movl (%rbx), %r13d
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x4, %eax
jne 0x402118
movl -0x2c(%rbp), %edi
movl $0x1, %esi
callq 0x45b5f0
movl %eax, %ebx
callq 0x4055c0
movl (%rax), %eax
cmpl $-0x1, %ebx
je 0x4020f8
testl %eax, %eax
jne 0x402166
movl -0x30(%rbp), %edi
callq 0x45b530
movl -0x2c(%rbp), %edi
jmp 0x40214c
leaq 0xa20bf(%rip), %rdi     # 0x4a41ef
callq 0x40cb20
jmp 0x4020e6
leaq -0x30(%rbp), %rdi
callq 0x4012be
jmp 0x4020e6
callq 0x401301
movl $0x1, %edi
callq 0x45b530
movq %r12, %rdi
movq %r15, %rsi
callq 0x45a920
movl $0xfe, %edi
callq 0x40bdd0
callq 0x4012e9