// File: /workspace/github_c_makefile_star2_O2/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, %ebx
movq %rdi, %r12
cmpb $0x1, %bl
jne 0x401dff
leaq -0x30(%rbp), %rdi
callq 0x45b3a0
cmpl $-0x1, %eax
je 0x401f61
callq 0x459ca0
testl %eax, %eax
je 0x401f29
movl %eax, %r13d
js 0x401f06
cmpb $0x1, %bl
jne 0x401ee0
movl %r13d, -0x34(%rbp)
movl -0x2c(%rbp), %edi
callq 0x45b2b0
pushq $0x10
popq %rdi
callq 0x420cc0
movq %rax, %r15
movl -0x30(%rbp), %edi
movq %r15, %rsi
pushq $0x10
popq %rdx
callq 0x45b040
movq %rax, %r13
testl %r13d, %r13d
jle 0x401e96
movq (%r14), %r12
testq %r12, %r12
je 0x401e9a
movq %r12, %rdi
callq 0x4010e0
movq %rax, %rbx
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq (%rax,%r13), %rsi
incq %rsi
addq %r13, %rbx
movq %r12, %rdi
callq 0x421350
movq %rax, %r12
movq %rax, %rdi
callq 0x4010e0
leaq (%rax,%r12), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x401040
addq %r12, %rbx
jmp 0x401ec1
jne 0x401e35
jmp 0x401ecc
leal 0x1(%r13), %edi
callq 0x420cc0
movq %rax, %r12
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x401040
movq %r12, %rbx
addq %r13, %rbx
movb $0x0, (%rbx)
movq %r12, (%r14)
jmp 0x401e35
movl -0x30(%rbp), %edi
callq 0x45b2b0
movq %r15, %rdi
callq 0x421000
movl -0x34(%rbp), %r13d
leaq -0x38(%rbp), %rbx
movl %r13d, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x459be0
movl (%rbx), %r13d
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa12e2(%rip), %rdi     # 0x4a31ef
callq 0x40c890
cmpb $0x1, %bl
jne 0x401ef4
movl -0x30(%rbp), %edi
callq 0x45b2b0
movl -0x2c(%rbp), %edi
callq 0x45b2b0
jmp 0x401ef4
cmpb $0x1, %bl
jne 0x401f6a
pushq $0x1
popq %rbx
movl -0x2c(%rbp), %edi
movl %ebx, %esi
callq 0x45b370
movl %eax, %r14d
callq 0x405330
movl (%rax), %eax
cmpl $-0x1, %r14d
jne 0x401f50
cmpl $0x4, %eax
je 0x401f31
testl %eax, %eax
jne 0x401f87
movl -0x30(%rbp), %edi
callq 0x45b2b0
movl -0x2c(%rbp), %edi
jmp 0x401f6d
leaq 0xa1254(%rip), %rdi     # 0x4a31bc
jmp 0x401f8e
pushq $0x1
popq %rdi
callq 0x45b2b0
movq %r12, %rdi
movq %r15, %rsi
callq 0x45a6a0
movl $0xfe, %edi
callq 0x40bb40
leaq 0xa1242(%rip), %rdi     # 0x4a31d0
callq 0x40c890
pushq $0x1
popq %rdi
callq 0x40bb40