// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movsbl -0x11(%rbp), %eax
cmpl $0x1, %eax
jne 0x402119
leaq -0x30(%rbp), %rdi
callq 0x45b8e0
cmpl $-0x1, %eax
jne 0x402117
leaq 0xa20b9(%rip), %rdi     # 0x4a41c1
callq 0x40cf70
movl $0x1, %edi
callq 0x40c220
jmp 0x402119
callq 0x45a1e0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x4021bb
movsbl -0x11(%rbp), %eax
cmpl $0x1, %eax
jne 0x40219a
jmp 0x402136
movl -0x2c(%rbp), %edi
movl $0x1, %esi
callq 0x45b8b0
movl %eax, %ecx
xorl %eax, %eax
cmpl $-0x1, %ecx
movb %al, -0x4d(%rbp)
jne 0x40215d
callq 0x405a10
cmpl $0x4, (%rax)
sete %al
movb %al, -0x4d(%rbp)
movb -0x4d(%rbp), %al
testb $0x1, %al
jne 0x402166
jmp 0x402168
jmp 0x402136
callq 0x405a10
cmpl $0x0, (%rax)
je 0x402188
leaq 0xa205c(%rip), %rdi     # 0x4a41d5
callq 0x40cf70
movl $0x1, %edi
callq 0x40c220
movl -0x30(%rbp), %edi
callq 0x45b7f0
movl -0x2c(%rbp), %edi
callq 0x45b7f0
jmp 0x4021a4
movl $0x1, %edi
callq 0x45b7f0
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x45abe0
movl $0xfe, %edi
callq 0x40c220
cmpl $0x0, -0x34(%rbp)
jge 0x4021f1
leaq 0xa202c(%rip), %rdi     # 0x4a41f4
callq 0x40cf70
movsbl -0x11(%rbp), %eax
cmpl $0x1, %eax
jne 0x4021e6
movl -0x30(%rbp), %edi
callq 0x45b7f0
movl -0x2c(%rbp), %edi
callq 0x45b7f0
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x402326
jmp 0x4021f3
movsbl -0x11(%rbp), %eax
cmpl $0x1, %eax
jne 0x402312
movl -0x2c(%rbp), %edi
callq 0x45b7f0
movl $0x10, %edi
callq 0x421210
movq %rax, -0x40(%rbp)
jmp 0x402218
movl -0x30(%rbp), %edi
movq -0x40(%rbp), %rsi
movl $0x10, %edx
callq 0x45b580
movl %eax, -0x4c(%rbp)
cmpl $0x0, %eax
jle 0x4022f4
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x40227c
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x421210
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
movslq -0x4c(%rbp), %rdx
callq 0x401040
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x4c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
jmp 0x4022ef
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x4010e0
movslq -0x4c(%rbp), %rcx
addq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x4218a0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x4010e0
movq -0x58(%rbp), %rdi
addq %rax, %rdi
movq -0x40(%rbp), %rsi
movslq -0x4c(%rbp), %rdx
callq 0x401040
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x402218
cmpl $0x0, -0x4c(%rbp)
jne 0x4022fc
jmp 0x402301
jmp 0x402216
movl -0x30(%rbp), %edi
callq 0x45b7f0
movq -0x40(%rbp), %rdi
callq 0x421550
movl -0x34(%rbp), %edi
leaq -0x38(%rbp), %rsi
xorl %edx, %edx
callq 0x45a120
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop