// File: /workspace/github_c_makefile_star2_O2/bobrippling[P]tim/prompt.c
// Function: prompt
char *prompt(char promp, const char *initial)
{
	size_t len;
	char *buf;
	size_t i;

	if(initial){
		len = strlen(initial) + 1;

		buf = umalloc(len);
		i = len - 1;

		strcpy(buf, initial);

	}else{
		len = 10;
		buf = umalloc(len);
		i = 0;
	}

	int y, x;
	nc_get_yx(&y, &x);
	nc_set_yx(nc_LINES() - 1, 0);
	nc_addch(promp);

	for(size_t x = 0; x < i; x++)
		nc_addch(buf[x]);

	nc_clrtoeol();

	bool reading = true;

	while(reading){
		bool wasraw;
		int ch = io_getch(IO_NOMAP | IO_MAPRAW, &wasraw, true);

		switch(ch){
			case CTRL_AND('?'):
			case CTRL_AND('H'):
			case 263:
			case 127:
				/* backspace */
				if(i == 0)
					goto cancel;
				buf[--i] = '\0';
				nc_set_yx(nc_LINES() - 1, i + 1);
				break;

			case '\n':
			case '\r':
				reading = false;
				break;

			case CTRL_AND('r'):
				prompt_ctrl_r(&buf, &len, &i);
				nc_set_yx(nc_LINES() - 1, 1);
				nc_addstr(buf);
				break;

			case CTRL_AND('u'):
				buf[i = 0] = '\0';
				nc_set_yx(nc_LINES() - 1, i + 1);
				break;

			case K_ESC:
				if(!wasraw)
					goto cancel;
				/* fall */

			default:
				buf[i++] = ch;
				nc_addch(ch);
				if(i == len)
					buf = urealloc(buf, len *= 2);
				buf[i] = '\0';
				break;
		}
	}

	nc_set_yx(y, x);
	return buf;
cancel:
	nc_set_yx(y, x);
	free(buf);
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %ebp
testq %rsi, %rsi
je 0xb835
movq %rsi, %r15
movq %rsi, %rdi
callq 0x4170
movq %rax, %r14
movq %rax, %r12
incq %r12
movq %r12, %rdi
callq 0x5648
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x40e0
jmp 0xb847
pushq $0xa
popq %r12
movq %r12, %rdi
callq 0x5648
movq %rax, %rbx
xorl %r14d, %r14d
leaq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x49c3
callq 0x4b4e
leal -0x1(%rax), %edi
xorl %r15d, %r15d
xorl %esi, %esi
callq 0x49e5
movsbl %bpl, %edi
callq 0x4ab2
cmpq %r15, %r14
je 0xb885
movsbl (%rbx,%r15), %edi
callq 0x4ab2
incq %r15
jmp 0xb871
callq 0x4b62
movb $0x1, %cl
pushq $0x11
popq %r15
leaq 0x2(%rsp), %rbp
pushq $0x1
popq %r13
testb $0x1, %cl
je 0xba79
movl %r15d, %edi
movq %rbp, %rsi
movl %r13d, %edx
callq 0xbbc8
cmpl $0x8, %eax
je 0xb8ed
xorl %ecx, %ecx
cmpl $0xa, %eax
je 0xb899
cmpl $0xd, %eax
je 0xb899
cmpl $0x12, %eax
je 0xb915
cmpl $0x15, %eax
je 0xb96f
cmpl $0x1b, %eax
je 0xb98c
cmpl $0x1f, %eax
je 0xb8ed
cmpl $0x107, %eax            # imm = 0x107
je 0xb8ed
cmpl $0x7f, %eax
jne 0xb997
testq %r14, %r14
je 0xba88
movb $0x0, -0x1(%rbx,%r14)
decq %r14
callq 0x4b4e
leal -0x1(%rax), %edi
leal 0x1(%r14), %esi
callq 0x49e5
jmp 0xb9c1
leaq 0x14(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x49c3
pushq $0x22
popq %rdi
callq 0x4ab2
movl 0x14(%rsp), %edi
movl 0x10(%rsp), %esi
callq 0x49e5
movl %r13d, %edi
leaq 0x3(%rsp), %rsi
movl %r13d, %edx
callq 0xbbc8
cmpl $0x25, %eax
je 0xb9dc
cmpl $0x17, %eax
je 0xb9c8
cmpl $0x6, %eax
jne 0xba16
callq 0xd170
movq %rax, %rdi
callq 0xcf1b
jmp 0xb9d5
movb $0x0, (%rbx)
callq 0x4b4e
leal -0x1(%rax), %edi
movl %r13d, %esi
callq 0x49e5
movb $0x1, %cl
xorl %r14d, %r14d
jmp 0xb899
cmpb $0x1, 0x2(%rsp)
jne 0xba88
movb %al, (%rbx,%r14)
incq %r14
movsbl %al, %edi
callq 0x4ab2
cmpq %r12, %r14
jne 0xb9bc
addq %r12, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5660
movq %rax, %rbx
movb $0x0, (%rbx,%r14)
movb $0x1, %cl
jmp 0xb899
callq 0xd170
movq %rax, %rdi
callq 0xcf0b
movq %rax, %rbp
movb $0x1, %al
jmp 0xb9ee
callq 0xd170
movq (%rax), %rdi
callq 0x8cc6
movq %rax, %rbp
xorl %eax, %eax
testq %rbp, %rbp
je 0xba16
movl %eax, 0xc(%rsp)
movq %rbp, %rdi
callq 0x4170
movq %r12, %rcx
movq %rax, %r12
leaq (%rax,%r14), %r15
incq %r15
cmpq %rcx, %r15
jae 0xba1d
movq %rcx, %r15
jmp 0xba2b
callq 0x4b62
jmp 0xba55
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5660
movq %rax, %rbx
leaq 0x1(%r12), %rdx
leaq (%rbx,%r14), %rdi
movq %rbp, %rsi
callq 0x4360
addq %r12, %r14
cmpb $0x0, 0xc(%rsp)
je 0xba4e
movq %rbp, %rdi
callq 0x4080
movq %r15, %r12
pushq $0x11
popq %r15
callq 0x4b4e
leal -0x1(%rax), %edi
movl %r13d, %esi
callq 0x49e5
movq %rbx, %rdi
callq 0x4b24
movb $0x1, %cl
leaq 0x2(%rsp), %rbp
jmp 0xb899
movl 0x8(%rsp), %edi
movl 0x4(%rsp), %esi
callq 0x49e5
jmp 0xba9f
movl 0x8(%rsp), %edi
movl 0x4(%rsp), %esi
callq 0x49e5
movq %rbx, %rdi
callq 0x4080
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq