// File: /workspace/github_c_makefile_star2_O1/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 %rdx, %r14
movl %esi, %r13d
movq %rdi, %r12
cmpb $0x1, %r13b
jne 0x401f75
leaq -0x30(%rbp), %rdi
callq 0x45b5e0
cmpl $-0x1, %eax
je 0x402101
callq 0x459ee0
testl %eax, %eax
je 0x4020ab
js 0x402083
cmpb $0x1, %r13b
jne 0x402062
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %edi
callq 0x45b4f0
movl $0x10, %edi
callq 0x420f00
movq %rax, %r15
movl -0x30(%rbp), %edi
movl $0x10, %edx
movq %r15, %rsi
callq 0x45b280
movq %rax, %r12
testl %r12d, %r12d
jle 0x402046
movq (%r14), %rbx
testq %rbx, %rbx
je 0x402014
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 0x421590
movq %rax, %rbx
movq %rax, (%r14)
movq %rax, %rdi
callq 0x4010e0
leaq (%rax,%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x401040
addq %rbx, %r13
jmp 0x40203c
leal 0x1(%r12), %edi
callq 0x420f00
movq %rax, %r13
movq %rax, (%r14)
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x401040
addq %r12, %r13
movb $0x0, (%r13)
jmp 0x401faa
testl %r12d, %r12d
jne 0x401faa
movl -0x30(%rbp), %edi
callq 0x45b4f0
movq %r15, %rdi
callq 0x421240
movl -0x34(%rbp), %eax
leaq -0x38(%rbp), %rbx
movl %eax, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x459e20
movl (%rbx), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebx
leaq 0xa1166(%rip), %rdi     # 0x4a31f2
callq 0x40cad0
cmpb $0x1, %r13b
jne 0x4020a7
movl -0x30(%rbp), %edi
callq 0x45b4f0
movl -0x2c(%rbp), %edi
callq 0x45b4f0
movl %ebx, %eax
jmp 0x402074
movl $0x1, %edi
cmpb $0x1, %r13b
jne 0x4020e7
movl -0x2c(%rbp), %edi
movl $0x1, %esi
callq 0x45b5b0
cmpl $-0x1, %eax
jne 0x4020d2
callq 0x405570
cmpl $0x4, (%rax)
je 0x4020b6
callq 0x405570
cmpl $0x0, (%rax)
jne 0x402106
movl -0x30(%rbp), %edi
callq 0x45b4f0
movl -0x2c(%rbp), %edi
callq 0x45b4f0
movq %r12, %rdi
movq %r15, %rsi
callq 0x45a8e0
movl $0xfe, %edi
callq 0x40bd80
callq 0x4012d6
callq 0x4012be