// File: /workspace/github_c_makefile_star2_O2/jarun[P]nnn/src/nnn.c
// Function: get_output
static bool get_output(char *file, char *arg1, char *arg2, int fdout, bool page)
{
	pid_t pid;
	int pipefd[2];
	int index = 0, flags;
	bool ret = FALSE;
	bool have_file = fdout != -1;
	int cmd_in_fd = -1;
	int cmd_out_fd = -1;
	ssize_t len;

	/*
	 * In this case the logic of the function dictates that we should write the output of the command
	 * to `fd` and show it in the pager. But since we didn't open the file descriptor we have no right
	 * to close it, the caller must do it. We don't even know the path to pass to the pager and
	 * it's a real hassle to get it. In general this just invites problems so we are blocking it.
	 */
	if (have_file && page) {
		DPRINTF_S("invalid get_ouptput() call");
		return FALSE;
	}

	/* Setup file descriptors for child command */
	if (!have_file && page) {
		// Case 2
		fdout = create_tmp_file();
		if (fdout == -1)
			return FALSE;

		cmd_in_fd = STDIN_FILENO;
		cmd_out_fd = fdout;
	} else if (have_file) {
		// Case 3
		cmd_in_fd = STDIN_FILENO;
		cmd_out_fd = fdout;
	} else {
		// Case 1
		if (pipe(pipefd) == -1)
			errexit();

		for (index = 0; index < 2; ++index) {
			/* Get previous flags */
			flags = fcntl(pipefd[index], F_GETFL, 0);

			/* Set bit for non-blocking flag */
			flags |= O_NONBLOCK;

			/* Change flags on fd */
			fcntl(pipefd[index], F_SETFL, flags);
		}

		cmd_in_fd = pipefd[0];
		cmd_out_fd = pipefd[1];
	}

	pid = fork();
	if (pid == 0) {
		/* In child */
		close(cmd_in_fd);
		dup2(cmd_out_fd, STDOUT_FILENO);
		dup2(cmd_out_fd, STDERR_FILENO);
		close(cmd_out_fd);

		spawn(file, arg1, arg2, NULL, F_MULTI);
		_exit(EXIT_SUCCESS);
	}

	/* In parent */
	waitpid(pid, NULL, 0);

	/* Do what each case should do */
	if (!have_file && page) {
		// Case 2
		close(fdout);

		spawn(pager, g_tmpfpath, NULL, NULL, F_CLI | F_TTY);

		unlink(g_tmpfpath);
		return TRUE;
	}

	if (have_file)
		// Case 3
		return TRUE;

	// Case 1
	len = read(pipefd[0], g_buf, CMD_LEN_MAX - 1);
	if (len > 0)
		ret = TRUE;

	close(pipefd[0]);
	close(pipefd[1]);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, %r12d
cmpl $-0x1, %ecx
setne %al
testb %r12b, %al
je 0xf98d
xorl %r13d, %r13d
jmp 0xfab3
movl %ecx, %ebp
movq %rdx, %r14
movq %rdi, %r15
movq %rsi, 0x10(%rsp)
cmpl $-0x1, %ecx
setne %al
xorb $0x1, %r12b
orb %al, %r12b
je 0xfa05
movq %r14, 0x8(%rsp)
xorl %r13d, %r13d
movl %ebp, %ebx
movl %ebp, %r14d
cmpl $-0x1, %ebp
jne 0xfa2e
movq %rsp, %rdi
callq 0x43e0
cmpl $-0x1, %eax
je 0xfb0b
pushq $0x3
popq %rbx
pushq $0x4
popq %r14
xorl %r13d, %r13d
cmpq $0x2, %r13
je 0xfa22
movl (%rsp,%r13,4), %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x46c0
orl $0x800, %eax            # imm = 0x800
movl (%rsp,%r13,4), %edi
movl %r14d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x46c0
incq %r13
jmp 0xf9d6
callq 0xdc85
xorl %r13d, %r13d
cmpl $-0x1, %eax
je 0xfab3
movl %eax, %ebx
movq %r14, 0x8(%rsp)
movl %eax, %r14d
jmp 0xfa2e
movl (%rsp), %r13d
movl 0x4(%rsp), %r14d
pushq $-0x1
popq %rbx
callq 0x48c0
testl %eax, %eax
je 0xfac5
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4700
testb %r12b, %r12b
je 0xfa81
movb $0x1, %r13b
cmpl $-0x1, %ebp
jne 0xfab3
movl (%rsp), %edi
leaq 0x9cf3(%rip), %rsi      # 0x19750
movl $0x11ff, %edx           # imm = 0x11FF
callq 0x4460
testq %rax, %rax
setg %r13b
movl (%rsp), %edi
callq 0x43c0
movl 0x4(%rsp), %edi
callq 0x43c0
jmp 0xfab3
movl %ebx, %edi
callq 0x43c0
movq 0x9c61(%rip), %rdi      # 0x196f0
leaq 0x9c6a(%rip), %rbx      # 0x19700
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x109, %r8d            # imm = 0x109
callq 0xdcb4
movq %rbx, %rdi
callq 0x40d0
movb $0x1, %r13b
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r13d, %edi
callq 0x43c0
pushq $0x1
popq %rbx
movl %r14d, %edi
movl %ebx, %esi
callq 0x42f0
pushq $0x2
popq %rsi
movl %r14d, %edi
callq 0x42f0
movl %r14d, %edi
callq 0x43c0
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
xorl %ecx, %ecx
movl %ebx, %r8d
callq 0xdcb4
xorl %edi, %edi
callq 0x4110
movl $0x127a, %edi           # imm = 0x127A
callq 0xe555