// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
andb $0x1, %al
movb %al, -0x25(%rbp)
movl $0x0, -0x38(%rbp)
movb $0x0, -0x3d(%rbp)
cmpl $-0x1, -0x24(%rbp)
setne %al
andb $0x1, %al
movb %al, -0x3e(%rbp)
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x48(%rbp) # imm = 0xFFFFFFFF
testb $0x1, -0x3e(%rbp)
je 0x10bf9
testb $0x1, -0x25(%rbp)
je 0x10bf9
movb $0x0, -0x1(%rbp)
jmp 0x10db1
testb $0x1, -0x3e(%rbp)
jne 0x10c2e
testb $0x1, -0x25(%rbp)
je 0x10c2e
callq 0xddd0
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0x10c1c
movb $0x0, -0x1(%rbp)
jmp 0x10db1
movl $0x0, -0x44(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x10cbc
testb $0x1, -0x3e(%rbp)
je 0x10c43
movl $0x0, -0x44(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x10cba
leaq -0x34(%rbp), %rdi
callq 0x43e0
cmpl $-0x1, %eax
jne 0x10c5b
movl $0x1252, %edi           # imm = 0x1252
callq 0xeb50
movl $0x0, -0x38(%rbp)
cmpl $0x2, -0x38(%rbp)
jge 0x10cae
movslq -0x38(%rbp), %rax
movl -0x34(%rbp,%rax,4), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x46d0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
orl $0x800, %eax            # imm = 0x800
movl %eax, -0x3c(%rbp)
movslq -0x38(%rbp), %rax
movl -0x34(%rbp,%rax,4), %edi
movl -0x3c(%rbp), %edx
movl $0x4, %esi
movb $0x0, %al
callq 0x46d0
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x10c62
movl -0x34(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x10cbc
callq 0x48c0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x10d16
movl -0x44(%rbp), %edi
callq 0x43c0
movl -0x48(%rbp), %edi
movl $0x1, %esi
callq 0x42f0
movl -0x48(%rbp), %edi
movl $0x2, %esi
callq 0x42f0
movl -0x48(%rbp), %edi
callq 0x43c0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
movl $0x1, %r8d
callq 0xde30
xorl %edi, %edi
callq 0x4110
movl -0x2c(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x4710
testb $0x1, -0x3e(%rbp)
jne 0x10d6a
testb $0x1, -0x25(%rbp)
je 0x10d6a
movl -0x24(%rbp), %edi
callq 0x43c0
movq 0xfcb1(%rip), %rdi      # 0x209f0
leaq 0xfcba(%rip), %rsi      # 0x20a00
xorl %eax, %eax
movl %eax, %ecx
movl $0x109, %r8d            # imm = 0x109
movq %rcx, %rdx
callq 0xde30
leaq 0xfca1(%rip), %rdi      # 0x20a00
callq 0x40d0
movb $0x1, -0x1(%rbp)
jmp 0x10db1
testb $0x1, -0x3e(%rbp)
je 0x10d76
movb $0x1, -0x1(%rbp)
jmp 0x10db1
movl -0x34(%rbp), %edi
leaq 0xfcd0(%rip), %rsi      # 0x20a50
movl $0x11ff, %edx           # imm = 0x11FF
callq 0x4460
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jle 0x10d99
movb $0x1, -0x3d(%rbp)
movl -0x34(%rbp), %edi
callq 0x43c0
movl -0x30(%rbp), %edi
callq 0x43c0
movb -0x3d(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)