// File: /workspace/github_c_makefile_star2_O2/jarun[P]nnn/src/nnn.c
// Function: load_input
static char *load_input(int fd, const char *path)
{
	size_t i, chunk_count = 1, chunk = 512UL * 1024 /* 512 KiB chunk size */, entries = 0;
	char *input = malloc(sizeof(char) * chunk), *tmpdir = NULL;
	char cwd[PATH_MAX], *next;
	size_t offsets[LIST_FILES_MAX];
	char **paths = NULL;
	ssize_t input_read, total_read = 0, off = 0;
	int msgnum = 0;

	if (!input) {
		DPRINTF_S(strerror(errno));
		return NULL;
	}

	if (!path) {
		if (!getcwd(cwd, PATH_MAX)) {
			free(input);
			return NULL;
		}
	} else
		xstrsncpy(cwd, path, PATH_MAX);

	while (chunk_count < LIST_INPUT_MAX / chunk && !msgnum) {
		input_read = read(fd, input + total_read, chunk);
		if (input_read < 0) {
			if (errno == EINTR)
				continue;

			DPRINTF_S(strerror(errno));
			goto malloc_1;
		}

		if (input_read == 0)
			break;

		total_read += input_read;

		while (off < total_read) {
			next = memchr(input + off, '\0', total_read - off);
			if (!next)
				break;
			++next;

			if (next - input == off + 1) {
				off = next - input;
				continue;
			}

			if (entries == LIST_FILES_MAX) {
				msgnum = MSG_FILE_LIMIT;
				break;
			}

			offsets[entries++] = off;
			off = next - input;
		}

		/* We don't need to allocate another chunk */
		if (chunk_count > (total_read + chunk - 1) / chunk)
			continue;

		/* We can never need more than one additional chunk */
		++chunk_count;
		if (chunk_count > LIST_INPUT_MAX / chunk) {
			msgnum = MSG_SIZE_LIMIT;
			break;
		}

		input = xrealloc(input, chunk_count * chunk);
		if (!input)
			goto malloc_1;
	}

	/* We close fd outside this function. Any extra data is left to the kernel to handle */

	if (off != total_read) {
		if (entries == LIST_FILES_MAX)
			msgnum = MSG_FILE_LIMIT;
		else
			offsets[entries++] = off;
	}

	DPRINTF_D(entries);
	DPRINTF_D(total_read);
	DPRINTF_D(chunk_count);

	if (!entries) {
		msgnum = MSG_0_ENTRIES;
		goto malloc_1;
	}

	input[total_read] = '\0';

	paths = malloc(entries * sizeof(char *));
	if (!paths)
		goto malloc_1;

	for (i = 0; i < entries; ++i)
		paths[i] = input + offsets[i];

	listroot = malloc(sizeof(char) * PATH_MAX);
	if (!listroot)
		goto malloc_1;
	listroot[0] = '\0';

	DPRINTF_S(paths[0]);

	for (i = 0; i < entries; ++i) {
		if (paths[i][0] == '\n' || selforparent(paths[i])) {
			paths[i] = NULL;
			continue;
		}

		paths[i] = abspath(paths[i], cwd, NULL);
		if (!paths[i]) {
			entries = i; // free from the previous entry
			goto malloc_2;
		}

		DPRINTF_S(paths[i]);

		xstrsncpy(g_buf, paths[i], PATH_MAX);
		if (!common_prefix(xdirname(g_buf), listroot)) {
			entries = i + 1; // free from the current entry
			goto malloc_2;
		}

		DPRINTF_S(listroot);
	}

	DPRINTF_S(listroot);

	if (listroot[0])
		tmpdir = make_tmp_tree(paths, entries, listroot);

malloc_2:
	for (i = 0; i < entries; ++i)
		free(paths[i]);
malloc_1:
	if (msgnum) { /* Check if we are past init stage and show msg */
		if (home) {
			printmsg(messages[msgnum]);
			xdelay(XDELAY_INTERVAL_MS << 2);
		} else {
			msg(messages[msgnum]);
			usleep(XDELAY_INTERVAL_MS << 2);
		}
	}

	free(input);
	free(paths);
	return tmpdir;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x210d8, %rsp          # imm = 0x210D8
movq %rsi, %r14
movl %edi, %ebp
movl $0x80000, %edi          # imm = 0x80000
callq 0x45c0
testq %rax, %rax
je 0x632d
movq %rax, %r12
leaq 0xd0(%rsp), %rdi
testq %r14, %r14
je 0x61cd
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rsi
callq 0x6016
jmp 0x61e0
movl $0x1000, %esi           # imm = 0x1000
callq 0x43a0
testq %rax, %rax
je 0x6325
pushq $0x1
popq %r13
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
addq %r15, %r12
cmpq $0x7f, %r13
ja 0x62ee
testl %r14d, %r14d
jne 0x62ee
movl $0x80000, %edx          # imm = 0x80000
movl %ebp, %edi
movq %r12, %rsi
callq 0x4460
testq %rax, %rax
jns 0x623b
callq 0x40c0
xorl %r14d, %r14d
cmpl $0x4, (%rax)
je 0x6202
jmp 0x62fc
je 0x62ee
movq %rax, %r15
addq 0x18(%rsp), %r15
movq %rbx, %rcx
movq 0x10(%rsp), %r12
movq %rcx, %rbx
movq %r15, %rdx
subq %rcx, %rdx
jle 0x629a
leaq (%r12,%rbx), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x4450
testq %rax, %rax
je 0x629d
subq %r12, %rax
leaq 0x1(%rax), %rcx
cmpq %rbx, %rax
je 0x6251
movq 0x8(%rsp), %rax
cmpq $0x4000, %rax           # imm = 0x4000
je 0x62b3
movq %rbx, 0x10d0(%rsp,%rax,8)
incq %rax
movq %rax, 0x8(%rsp)
jmp 0x6251
xorl %r14d, %r14d
leaq 0x7ffff(%r15), %rax
shrq $0x13, %rax
cmpq %rax, %r13
ja 0x61fa
jmp 0x62c5
movl $0x4000, %eax           # imm = 0x4000
movq %rax, 0x8(%rsp)
pushq $0x9
popq %rax
movl %eax, %r14d
jmp 0x629d
incq %r13
movq %r13, %rsi
shlq $0x13, %rsi
movq %r12, %rdi
callq 0xd572
movq %rax, %r12
testq %rax, %rax
jne 0x61f5
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0x6690
cmpq 0x18(%rsp), %rbx
jne 0x630c
movq 0x8(%rsp), %rbp
jmp 0x6340
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x10(%rsp), %r12
jmp 0x66d0
movq 0x8(%rsp), %rbp
cmpq $0x4000, %rbp           # imm = 0x4000
jne 0x6335
movl $0x4000, %ebp           # imm = 0x4000
pushq $0x9
popq %r14
jmp 0x6349
movq %r12, %rdi
callq 0x4090
xorl %r13d, %r13d
jmp 0x66e0
movq %rbx, 0x10d0(%rsp,%rbp,8)
incq %rbp
testq %rbp, %rbp
je 0x64b9
movb $0x0, (%r12)
leaq (,%rbp,8), %rdi
callq 0x45c0
testq %rax, %rax
je 0x64cd
movq %rax, %r15
xorl %eax, %eax
movq 0x10(%rsp), %r12
cmpq %rax, %rbp
je 0x6387
movq 0x10d0(%rsp,%rax,8), %rcx
addq %r12, %rcx
movq %rcx, (%r15,%rax,8)
incq %rax
jmp 0x636e
movl $0x1000, %edi           # imm = 0x1000
callq 0x45c0
movq %rax, 0x14710(%rip)     # 0x1aaa8
testq %rax, %rax
je 0x6690
movb $0x0, (%rax)
leaq 0x133a5(%rip), %r12     # 0x19750
xorl %r13d, %r13d
movq %rbp, %rbx
cmpq %rbp, %r13
je 0x64dd
movq (%r15,%r13,8), %rbp
cmpb $0xa, (%rbp)
je 0x63d0
movq %rbp, %rdi
callq 0xd599
testb %al, %al
je 0x63dd
andq $0x0, (%r15,%r13,8)
incq %r13
movq %rbx, %rbp
jmp 0x63ae
movq %rbp, %rdi
leaq 0xd0(%rsp), %rsi
xorl %edx, %edx
callq 0x5e18
movq %rax, (%r15,%r13,8)
testq %rax, %rax
je 0x670e
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %rax, %rsi
callq 0x6016
movq %r12, %rdi
callq 0x6985
movq 0x1468d(%rip), %rbp     # 0x1aaa8
testq %rbp, %rbp
je 0x671b
movb 0x13326(%rip), %al      # 0x19750
testb %al, %al
je 0x671b
cmpb $0x0, (%rbp)
je 0x6451
xorl %edx, %edx
movzbl (%rbp,%rdx), %ecx
testb %al, %al
je 0x645e
cmpb %cl, %al
jne 0x6487
movb 0x1(%rdx,%r12), %al
incq %rdx
jmp 0x643a
movl $0x1000, %edx           # imm = 0x1000
movq %rbp, %rdi
movq %r12, %rsi
jmp 0x647d
testl %ecx, %ecx
je 0x63d5
cmpl $0x2f, %ecx
jne 0x6493
movq %rbp, %rax
subq %r12, %rax
addq %rdx, %rax
movq %rbp, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x6016
jmp 0x63d5
cmpb $0x2f, %al
jne 0x6493
testb %cl, %cl
je 0x63d5
movb $0x0, (%rbp,%rdx)
movq %rbp, %rdi
pushq $0x2f
popq %rsi
callq 0x46f0
cmpq %rbp, %rax
je 0x64b0
movb $0x0, (%rax)
jmp 0x63d5
movb $0x0, 0x1(%rbp)
jmp 0x63d5
pushq $0x1
popq %r14
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x10(%rsp), %r12
jmp 0x6698
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x10(%rsp), %r12
jmp 0x6693
movq 0x145c4(%rip), %rbp     # 0x1aaa8
movb (%rbp), %r13b
testb %r13b, %r13b
je 0x66f5
movq %rbp, %rdi
callq 0x5d70
movq %rax, 0x20(%rsp)
movl $0x1000, %edi           # imm = 0x1000
callq 0x45c0
testq %rax, %rax
movq 0x10(%rsp), %r12
je 0x6733
movzbl 0x13223(%rip), %edx     # 0x19740
leaq (%rax,%rdx), %rcx
decq %rcx
movq %rcx, 0x18(%rsp)
leaq 0x131d0(%rip), %rsi     # 0x19700
movq %rax, %rdi
movq %rax, 0x8(%rsp)
movq %rdx, 0x28(%rsp)
callq 0x6016
leaq 0xd889(%rip), %rsi      # 0x13dd2
pushq $0xb
popq %rdx
movq 0x18(%rsp), %rdi
callq 0x6016
xorb $0x2f, %r13b
xorl %eax, %eax
orb 0x1(%rbp), %r13b
movq 0x20(%rsp), %rbp
cmoveq %rax, %rbp
movq 0x8(%rsp), %rdi
movq %rdi, %r13
callq 0x4670
testq %rax, %rax
je 0x672b
movq %rbp, 0x20(%rsp)
movq 0x28(%rsp), %rax
addq %r13, %rax
addq $0x9, %rax
movq %rax, 0x30(%rsp)
movq %rbx, %rbp
testq %rbx, %rbx
movl $0x0, %eax
cmovgq %rbx, %rax
movq %rax, 0x18(%rsp)
movq %r13, 0x145d0(%rip)     # 0x1ab80
xorl %r13d, %r13d
cmpq %r13, 0x18(%rsp)
je 0x66ff
movq (%r15,%r13,8), %rbp
testq %rbp, %rbp
je 0x6685
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x4300
testl %eax, %eax
je 0x65ea
callq 0x40c0
cmpl $0x2, (%rax)
je 0x6685
movq %r13, 0x28(%rsp)
movq 0x20(%rsp), %r13
movq %rbp, %rax
addq %r13, %rax
movq %rax, 0x38(%rsp)
movq %rbp, %rdi
callq 0x5d70
subq %r13, %rax
leaq 0x1(%rax), %rdx
movq 0x30(%rsp), %r12
movq %r12, %rdi
movq 0x38(%rsp), %rsi
callq 0x6016
movq %rbp, %rdi
callq 0x5d70
subq %r13, %rax
movq %r12, %rdi
pushq $0x2f
popq %rsi
movq %rax, %rdx
callq 0x46f0
movq %rax, %r12
testq %rax, %rax
je 0x6646
movb $0x0, (%r12)
movq 0x8(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x4710
testl %eax, %eax
je 0x6664
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x69e3
testq %r12, %r12
je 0x666e
movb $0x2f, (%r12)
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0x4910
movq 0x10(%rsp), %r12
movq 0x28(%rsp), %r13
incq %r13
movq %rbx, %rbp
jmp 0x65b3
xorl %r13d, %r13d
testl %r14d, %r14d
je 0x66d0
cmpq $0x0, 0x12fa0(%rip)     # 0x19640
movl %r14d, %eax
leaq 0x12466(%rip), %rcx     # 0x18b10
movq (%rcx,%rax,8), %rdi
je 0x66c1
callq 0xd5b9
movl $0x155cc0, %edi         # imm = 0x155CC0
callq 0xd617
jmp 0x66d0
callq 0x6752
movl $0x155cc0, %edi         # imm = 0x155CC0
callq 0x48f0
movq %r12, %rdi
callq 0x4090
movq %r15, %rdi
callq 0x4090
movq %r13, %rax
addq $0x210d8, %rsp          # imm = 0x210D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
movq 0x10(%rsp), %r12
jmp 0x6736
movq 0x30(%rsp), %rax
movb $0x0, (%rax)
movq 0x8(%rsp), %r13
jmp 0x6739
movq %r13, %rbp
movq 0x10(%rsp), %r12
xorl %r13d, %r13d
jmp 0x6739
incq %r13
movq %r13, %rbp
xorl %r13d, %r13d
movq 0x10(%rsp), %r12
jmp 0x6739
movq %r13, %rdi
callq 0x4090
xorl %r13d, %r13d
movq %rbx, %rbp
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x6693
movq (%r15,%rbx,8), %rdi
callq 0x4090
incq %rbx
jmp 0x673b