// File: /workspace/github_c_makefile_star2_O2/jarun[P]nnn/src/nnn.c
// Function: xreadline
static char *xreadline(const char *prefill, const char *prompt)
{
	size_t len, pos, lpos;
	int x, r;
	const int WCHAR_T_WIDTH = sizeof(wchar_t);
	wint_t ch[1];
	wchar_t * const buf = malloc(sizeof(wchar_t) * READLINE_MAX);

	if (!buf)
		errexit();

	cleartimeout();
	printmsg(prompt);

	if (prefill) {
		DPRINTF_S(prefill);
		len = pos = mbstowcs(buf, prefill, READLINE_MAX);
	} else
		len = (size_t)-1;

	if (len == (size_t)-1) {
		buf[0] = '\0';
		len = pos = 0;
	}

	x = getcurx(stdscr);
	curs_set(TRUE);

	while (1) {
		buf[len] = ' ';
		attron(COLOR_PAIR(cfg.curctx + 1));
		if (pos > (size_t)(xcols - x)) {
			mvaddnwstr(xlines - 1, x, buf + (pos - (xcols - x) + 1), xcols - x);
			move(xlines - 1, xcols - 1);
		} else {
			mvaddnwstr(xlines - 1, x, buf, len + 1);
			move(xlines - 1, x + wcswidth(buf, pos));
		}
		attroff(COLOR_PAIR(cfg.curctx + 1));

		r = get_wch(ch);
		if (r == ERR)
			continue;

		if (r == OK) {
			switch (*ch) {
			case KEY_ENTER: // fallthrough
			case '\n': // fallthrough
			case '\r':
				goto END;
			case CONTROL('D'):
				if (pos < len)
					++pos;
				else if (!(pos || len)) { /* Exit on ^D at empty prompt */
					len = 0;
					goto END;
				} else
					continue;
				// fallthrough
			case DEL: // fallthrough
			case '\b': /* rhel25 sends '\b' for backspace */
				if (pos > 0) {
					memmove(buf + pos - 1, buf + pos,
						(len - pos) * WCHAR_T_WIDTH);
					--len, --pos;
				}
				continue;
			case '\t':
				if ((len == pos) && ndents && (pos < (READLINE_MAX - xstrlen(pdents[cur].name)))) {
					buf[pos] = '\0';
					lpos = mbstowcs(NULL, pdents[cur].name, MB_CUR_MAX);
					pos += mbstowcs(buf + wcslen(buf), pdents[cur].name, lpos);
					len = pos;
				}
				continue;
			case CONTROL('F'):
				if (pos < len)
					++pos;
				continue;
			case CONTROL('B'):
				if (pos > 0)
					--pos;
				continue;
			case CONTROL('W'):
				printmsg(prompt);
				lpos = pos;
				while (pos > 0 && ISSPACE(buf[pos-1]))
					--pos;
				while (pos > 0 && !ISSPACE(buf[pos-1]))
					--pos;
				memmove(buf + pos, buf + lpos, (len - lpos) * WCHAR_T_WIDTH);
				len -= lpos - pos;
				continue;
			case CONTROL('K'):
				printmsg(prompt);
				len = pos;
				continue;
			case CONTROL('L'):
				printmsg(prompt);
				len = pos = 0;
				continue;
			case CONTROL('A'):
				pos = 0;
				continue;
			case CONTROL('E'):
				pos = len;
				continue;
			case CONTROL('U'):
				printmsg(prompt);
				memmove(buf, buf + pos, (len - pos) * WCHAR_T_WIDTH);
				len -= pos;
				pos = 0;
				continue;
			case ESC: /* Exit prompt on Esc, but just filter out Alt+key */
				if (handle_alt_key(ch) != ERR) {
					switch (*ch) {
					case 'd':
						printmsg(prompt);
						lpos = pos;
						while (pos < len && !iswalnum(buf[pos + 1]))
							++pos;
						while (pos < len && iswalnum(buf[++pos]));
						memmove(buf + lpos, buf + pos, (len - pos) * WCHAR_T_WIDTH);
						len -= pos - lpos;
						pos = lpos;
						continue;
					case KEY_BACKSPACE:
						printmsg(prompt);
						lpos = pos;
						while (pos > 0 && !iswalnum(buf[pos - 1]))
							--pos;
						while (pos > 0 && iswalnum(buf[pos - 1]))
							--pos;
						memmove(buf + pos, buf + lpos, (len - lpos) * WCHAR_T_WIDTH);
						len -= lpos - pos;
						continue;
					case 'f':
						while (pos < len && !iswalnum(buf[pos + 1]))
							++pos;
						while (pos < len && iswalnum(buf[++pos]));
						continue;
					case 'b':
						while (pos > 0 && !iswalnum(buf[pos - 1]))
							--pos;
						while (pos > 0 && iswalnum(buf[pos - 1]))
							--pos;
						continue;
					}
					continue;
				}

				len = 0;
				goto END;
			}

			/* Filter out all other control chars */
			if (*ch < ASCII_MAX && keyname(*ch)[0] == '^')
				continue;

			if (pos < READLINE_MAX - 1) {
				memmove(buf + pos + 1, buf + pos,
					(len - pos) * WCHAR_T_WIDTH);
				buf[pos] = *ch;
				++len, ++pos;
				continue;
			}
		} else {
			switch (*ch) {
#ifdef KEY_RESIZE
			case KEY_RESIZE:
				clearoldprompt();
				xlines = LINES;
				printmsg(prompt);
				break;
#endif
			case KEY_LEFT:
				if (pos > 0)
					--pos;
				break;
			case KEY_RIGHT:
				if (pos < len)
					++pos;
				break;
			case KEY_BACKSPACE:
				if (pos > 0) {
					memmove(buf + pos - 1, buf + pos,
						(len - pos) * WCHAR_T_WIDTH);
					--len, --pos;
				}
				break;
			case KEY_DC:
				if (pos < len) {
					memmove(buf + pos, buf + pos + 1,
						(len - pos - 1) * WCHAR_T_WIDTH);
					--len;
				}
				break;
			case KEY_END:
				pos = len;
				break;
			case KEY_HOME:
				pos = 0;
				break;
			case KEY_UP: // fallthrough
			case KEY_DOWN:
			{
				char *cmd = getcmdfromhist(*ch == KEY_UP);

				if (prompt && cmd && (xstrcmp(prompt, PROMPT) == 0)) {
					printmsg(prompt);
					len = pos = mbstowcs(buf, cmd, READLINE_MAX); // fallthrough
				}
			}
			default:
				break;
			}
		}
	}

END:
	curs_set(FALSE);
	settimeout();
	printmsg("");

	buf[len] = '\0';

	pos = wcstombs(g_buf, buf, READLINE_MAX - 1);
	if (pos >= READLINE_MAX - 1)
		g_buf[READLINE_MAX - 1] = '\0';

	free(buf);
	return g_buf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
movq %rdi, %r14
movl $0x400, %edi            # imm = 0x400
callq 0x45c0
testq %rax, %rax
je 0x10a76
movq %rax, %rbx
movq 0x8ca2(%rip), %r15      # 0x18f90
movq (%r15), %rdi
pushq $-0x1
popq %rsi
movq %rsi, 0x18(%rsp)
callq 0x4560
movq %r12, (%rsp)
movq %r12, %rdi
callq 0xd5b9
testq %r14, %r14
je 0x10328
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
movq %r14, %rsi
callq 0x41d0
movq %rax, %rbp
cmpq $-0x1, %rax
jne 0x1032d
andl $0x0, (%rbx)
xorl %ebp, %ebp
movq (%r15), %rax
testq %rax, %rax
je 0x1033e
movswl 0x2(%rax), %eax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %rdi
callq 0x4140
leaq 0x4(%rbx), %rax
movq %rax, 0x10(%rsp)
movslq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rbp, %r15
movq %rbp, %r14
movl $0x20, (%rbx,%r15,4)
movq 0x8c22(%rip), %r13      # 0x18f90
movq (%r13), %rdi
movl 0x9150(%rip), %esi      # 0x194c8
shrl $0x5, %esi
andl $0x700, %esi            # imm = 0x700
addl $0x100, %esi            # imm = 0x100
xorl %edx, %edx
callq 0x4120
movzwl 0x12f02(%rip), %r12d    # 0x23298
movq 0x20(%rsp), %rdx
subq %rdx, %r12
movq (%r13), %rdi
movzwl 0xa7df(%rip), %esi      # 0x1ab88
decl %esi
callq 0x4070
cmpq %r12, %rbp
jbe 0x103f6
cmpl $-0x1, %eax
je 0x103de
movq (%r13), %rdi
movzwl 0x12ed3(%rip), %edx     # 0x23298
subq 0x20(%rsp), %rdx
movq %r14, %rax
subq %rdx, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,4), %rsi
callq 0x4370
movq (%r13), %rdi
movzwl 0xa79f(%rip), %esi      # 0x1ab88
decl %esi
movzwl 0x12ea6(%rip), %edx     # 0x23298
decl %edx
jmp 0x1043d
cmpl $-0x1, %eax
je 0x1040b
movq (%r13), %rdi
leal 0x1(%r15), %edx
movq %rbx, %rsi
callq 0x4370
movq 0x8b7e(%rip), %rax      # 0x18f90
movq (%rax), %r13
movzwl 0xa76c(%rip), %ebp      # 0x1ab88
decl %ebp
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4190
movq 0x18(%rsp), %rcx
leal (%rax,%rcx), %edx
movq %r13, %rdi
movq 0x8b55(%rip), %r13      # 0x18f90
movl %ebp, %esi
callq 0x4070
movq (%r13), %rdi
movl 0x907c(%rip), %esi      # 0x194c8
shrl $0x5, %esi
andl $0x700, %esi            # imm = 0x700
addl $0x100, %esi            # imm = 0x100
xorl %edx, %edx
callq 0x4730
movq (%r13), %rdi
leaq 0xc(%rsp), %rsi
callq 0x48d0
testl %eax, %eax
je 0x104ea
movq %r14, %rbp
cmpl $-0x1, %eax
je 0x1035c
movl 0xc(%rsp), %eax
leal -0x102(%rax), %ecx
cmpl $0x5, %ecx
ja 0x10520
leaq 0x2e96(%rip), %rdx      # 0x13330
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movb 0x902f(%rip), %dl       # 0x194d8
movq %r14, %rbp
cmpb $-0x1, %dl
je 0x1035c
movb 0x9044(%rip), %sil      # 0x19500
movzbl %dl, %ecx
cmpb $-0x1, %sil
je 0x10865
cmpl $0x103, %eax            # imm = 0x103
movq (%rsp), %rdi
jne 0x108b5
subb $0x1, %sil
movzbl %sil, %eax
cmovbl %ecx, %eax
movl %eax, %ecx
jmp 0x108c6
movl 0xc(%rsp), %r13d
leal -0x1(%r13), %eax
cmpl $0x1a, %eax
ja 0x1057d
leaq 0x2dc1(%rip), %rcx      # 0x132c4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
xorl %ebp, %ebp
jmpq *%rax
subq $0x1, %r14
movl $0x0, %ebp
cmovaeq %r14, %rbp
jmp 0x1035c
cmpl $0x14a, %eax            # imm = 0x14A
je 0x105d7
movq %r15, %rbp
cmpl $0x168, %eax            # imm = 0x168
je 0x1035c
movq %r14, %rbp
cmpl $0x19a, %eax            # imm = 0x19A
jne 0x1035c
callq 0x11b0f
movq 0x8a7d(%rip), %rax      # 0x18fd0
movzwl (%rax), %eax
movw %ax, 0xa62b(%rip)       # 0x1ab88
movq (%rsp), %rdi
callq 0xd5b9
movq %r14, %rbp
jmp 0x1035c
cmpq %r15, %r14
adcq $0x0, %r14
movq %r14, %rbp
jmp 0x1035c
cmpl $0x7f, %r13d
je 0x105c8
cmpl $0x157, %r13d           # imm = 0x157
je 0x10a10
cmpl $0x7f, %r13d
ja 0x10878
movl %r13d, %edi
callq 0x48a0
cmpb $0x5e, (%rax)
movq %r14, %rbp
je 0x1035c
movq %r14, %rbp
cmpq $0xfe, %r14
ja 0x1035c
movl 0xc(%rsp), %r13d
jmp 0x10888
movl $0x0, %ebp
testq %r14, %r14
jne 0x10634
jmp 0x1035c
movq %r14, %rbp
cmpq %r14, %r15
jbe 0x1035c
leaq (%rbx,%r14,4), %rdi
leaq (%rbx,%r14,4), %rsi
addq $0x4, %rsi
movq %r14, %rdx
notq %rdx
addq %r15, %rdx
shlq $0x2, %rdx
callq 0x46d0
decq %r15
movq %r14, %rbp
jmp 0x1035c
movq (%rsp), %rdi
callq 0xd5b9
movq %r14, %rbp
movq %r14, %r15
jmp 0x1035c
movq %r15, %rbp
jmp 0x1035c
cmpq %r14, %r15
jbe 0x10941
incq %r14
leaq (%rbx,%r14,4), %rsi
leaq (%rbx,%r14,4), %rdi
addq $-0x4, %rdi
movq %r15, %rdx
subq %r14, %rdx
shlq $0x2, %rdx
callq 0x46d0
decq %r15
decq %r14
movq %r14, %rbp
jmp 0x1035c
leaq 0xc(%rsp), %rdi
callq 0x11e57
cmpl $-0x1, %eax
je 0x10a0d
movl 0xc(%rsp), %eax
cmpl $0x62, %eax
je 0x10709
cmpl $0x64, %eax
je 0x10955
cmpl $0x66, %eax
je 0x106ca
movq %r14, %rbp
cmpl $0x107, %eax            # imm = 0x107
jne 0x1035c
movq (%rsp), %rdi
callq 0xd5b9
movq %r15, %r13
movq %r14, %rbp
movq %r14, %r12
subq $0x1, %r12
jb 0x10740
movl (%rbx,%r12,4), %edi
callq 0x45b0
testl %eax, %eax
jne 0x10740
decq %r13
movq %r12, %rbp
jmp 0x106ab
movq %r14, %rbp
cmpq %r15, %r14
jae 0x106e7
leaq 0x1(%rbp), %r14
movq 0x10(%rsp), %rax
movl (%rax,%rbp,4), %edi
callq 0x45b0
testl %eax, %eax
je 0x106ca
cmpq %r15, %rbp
jae 0x1035c
movq 0x10(%rsp), %rax
movl (%rax,%rbp,4), %edi
incq %rbp
callq 0x45b0
testl %eax, %eax
jne 0x106e7
jmp 0x1035c
movq %r14, %r13
subq $0x1, %r14
jb 0x1071f
movl (%rbx,%r14,4), %edi
callq 0x45b0
testl %eax, %eax
je 0x10709
movq %r13, %rbp
subq $0x1, %r13
jb 0x1077f
movl (%rbx,%r13,4), %edi
callq 0x45b0
testl %eax, %eax
jne 0x1071f
jmp 0x1035c
decq %r13
movq %r12, %rbp
movq %rbp, %r12
subq $0x1, %r12
jb 0x10833
movl (%rbx,%r12,4), %edi
callq 0x45b0
testl %eax, %eax
jne 0x1073a
jmp 0x10833
movq (%rsp), %rdi
callq 0xd5b9
leaq (%rbx,%r14,4), %rsi
subq %r14, %r15
leaq (,%r15,4), %rdx
movq %rbx, %rdi
callq 0x46d0
xorl %ebp, %ebp
jmp 0x1035c
movq %r14, %rbp
cmpq %r14, %r15
jne 0x1035c
cmpl $0x0, 0xa3f3(%rip)      # 0x1ab8c
movq %r14, %rbp
je 0x1035c
movq 0x128c7(%rip), %rax     # 0x23070
movslq 0x128c8(%rip), %rcx     # 0x23078
imulq $0x30, %rcx, %rcx
movq (%rax,%rcx), %r13
movq %r13, %rdi
callq 0x5d70
movl $0x100, %ecx            # imm = 0x100
subq %rax, %rcx
movq %r14, %rbp
movq %r14, %r15
cmpq %rcx, %r14
jae 0x1035c
jmp 0x109be
movq (%rsp), %rdi
callq 0xd5b9
movq %r15, %r13
movq %r14, %rbp
movq %r14, %rax
movabsq $0x100003e00, %rdx      # imm = 0x100003E00
subq $0x1, %rax
jb 0x1081b
movl (%rbx,%rax,4), %ecx
cmpq $0x20, %rcx
ja 0x1081b
btq %rcx, %rdx
jae 0x1081b
decq %r13
movq %rax, %rbp
jmp 0x107f8
decq %r13
movq %rax, %rbp
movq %rbp, %rax
subq $0x1, %rax
jb 0x10833
movl (%rbx,%rax,4), %ecx
cmpq $0x20, %rcx
ja 0x10815
btq %rcx, %rdx
jae 0x10815
leaq (%rbx,%rbp,4), %rdi
leaq (%rbx,%r14,4), %rsi
subq %r14, %r15
shlq $0x2, %r15
movq %r15, %rdx
callq 0x46d0
movq %r13, %r15
jmp 0x1035c
movq (%rsp), %rdi
callq 0xd5b9
xorl %ebp, %ebp
xorl %r15d, %r15d
jmp 0x1035c
cmpl $0x103, %eax            # imm = 0x103
movl $0x0, %eax
cmovnel %eax, %ecx
movq (%rsp), %rdi
jmp 0x108c6
movq %r14, %rbp
cmpq $0xfe, %r14
ja 0x1035c
leaq (%rbx,%r14,4), %rsi
leaq (%rbx,%r14,4), %rdi
addq $0x4, %rdi
movq %r15, %rdx
subq %r14, %rdx
shlq $0x2, %rdx
callq 0x46d0
movl %r13d, (%rbx,%r14,4)
incq %r15
incq %r14
movq %r14, %rbp
jmp 0x1035c
leal 0x1(%rsi), %eax
cmpb %dl, %sil
movzbl %al, %ecx
movl $0x0, %eax
cmovel %eax, %ecx
movb %cl, 0x8c34(%rip)       # 0x19500
movq %r14, %rbp
testq %rdi, %rdi
je 0x1035c
movzbl %cl, %eax
leaq 0xa1de(%rip), %rcx      # 0x1aac0
movq (%rcx,%rax,8), %r13
movq %r14, %rbp
testq %r13, %r13
je 0x1035c
movq (%rsp), %rax
cmpb $0x3e, (%rax)
movq %r14, %rbp
jne 0x1035c
movq (%rsp), %rdi
leaq 0x3d03(%rip), %rsi      # 0x14610
callq 0x44d0
movq %r14, %rbp
testl %eax, %eax
jne 0x1035c
movq (%rsp), %rdi
callq 0xd5b9
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
movq %r13, %rsi
callq 0x41d0
movq %rax, %rbp
movq %rax, %r15
jmp 0x1035c
movq %r15, %rax
orq %r14, %rax
movq %r14, %rbp
jne 0x1035c
jmp 0x10a0d
movq (%rsp), %rdi
callq 0xd5b9
movq %r14, %r12
movq %r12, %r13
cmpq %r15, %r12
jae 0x1097f
leaq 0x1(%r13), %r12
movq 0x10(%rsp), %rax
movl (%rax,%r13,4), %edi
callq 0x45b0
testl %eax, %eax
je 0x10961
cmpq %r15, %r13
jae 0x10999
movq 0x10(%rsp), %rax
movl (%rax,%r13,4), %edi
incq %r13
callq 0x45b0
testl %eax, %eax
jne 0x1097f
leaq (%rbx,%r14,4), %rdi
leaq (%rbx,%r13,4), %rsi
movq %r15, %rdx
subq %r13, %rdx
shlq $0x2, %rdx
callq 0x46d0
addq %r14, %r15
subq %r13, %r15
movq %r14, %rbp
jmp 0x1035c
andl $0x0, (%rbx,%r14,4)
callq 0x42b0
xorl %edi, %edi
movq %r13, %rsi
movq %rax, %rdx
callq 0x41d0
movq %rax, %r15
movq %rbx, %rdi
callq 0x4330
leaq (%rbx,%rax,4), %rdi
movq 0x12685(%rip), %rax     # 0x23070
movslq 0x12686(%rip), %rcx     # 0x23078
imulq $0x30, %rcx, %rcx
movq (%rax,%rcx), %rsi
movq %r15, %rdx
callq 0x41d0
movq %rax, %rbp
addq %r14, %rbp
jmp 0x10359
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x4140
movq 0x8572(%rip), %rax      # 0x18f90
movq (%rax), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x4560
leaq 0x38f8(%rip), %rdi      # 0x1432a
callq 0xd5b9
andl $0x0, (%rbx,%r15,4)
leaq 0x8d0d(%rip), %rdi      # 0x19750
movl $0xff, %edx
movq %rbx, %rsi
callq 0x47c0
cmpq $0xff, %rax
jb 0x10a5f
movb $0x0, 0x8df0(%rip)      # 0x1984f
movq %rbx, %rdi
callq 0x4090
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xe6d, %edi            # imm = 0xE6D
callq 0xe555