// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl %edi, %ebp
testq %rsi, %rsi
je 0xc031
movq %rsi, %r15
movq %rsi, %rdi
callq 0x4170
movq %rax, %r14
movq %rax, %r13
incq %r13
movq %r13, %rdi
callq 0x5830
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x40e0
jmp 0xc047
movl $0xa, %r13d
movl $0xa, %edi
callq 0x5830
movq %rax, %rbx
xorl %r14d, %r14d
leaq 0x1c(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4b65
callq 0x4cfb
leal -0x1(%rax), %edi
xorl %esi, %esi
callq 0x4b8c
movsbl %bpl, %edi
callq 0x4c56
testq %r14, %r14
je 0xc088
xorl %r15d, %r15d
movsbl (%rbx,%r15), %edi
callq 0x4c56
incq %r15
cmpq %r15, %r14
jne 0xc076
callq 0x4d0f
leaq 0xf(%rsp), %r15
leaq 0x10(%rsp), %r12
movl $0x11, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0xc3de
cmpl $0x1a, %eax
jle 0xc0c6
cmpl $0x7e, %eax
jg 0xc0f3
cmpl $0x1b, %eax
je 0xc18f
cmpl $0x1f, %eax
je 0xc10d
jmp 0xc1ad
cmpl $0x11, %eax
jle 0xc104
cmpl $0x12, %eax
je 0xc135
cmpl $0x15, %eax
jne 0xc1ad
movb $0x0, (%rbx)
callq 0x4cfb
leal -0x1(%rax), %edi
movl $0x1, %esi
callq 0x4b8c
xorl %r14d, %r14d
jmp 0xc097
cmpl $0x107, %eax            # imm = 0x107
je 0xc10d
cmpl $0x7f, %eax
je 0xc10d
jmp 0xc1ad
cmpl $0x8, %eax
jne 0xc19b
testq %r14, %r14
je 0xc28b
movb $0x0, -0x1(%rbx,%r14)
decq %r14
callq 0x4cfb
leal -0x1(%rax), %edi
leal 0x1(%r14), %esi
callq 0x4b8c
jmp 0xc097
movq %r12, %rdi
leaq 0x24(%rsp), %rsi
callq 0x4b65
movl $0x22, %edi
callq 0x4c56
movl 0x10(%rsp), %edi
movl 0x24(%rsp), %esi
callq 0x4b8c
movl $0x1, %edi
leaq 0x17(%rsp), %rsi
movl $0x1, %edx
callq 0xc3de
cmpl $0x25, %eax
je 0xc1f0
cmpl $0x17, %eax
je 0xc1dc
cmpl $0x6, %eax
jne 0xc227
callq 0xdaf0
movq %rax, %rdi
callq 0xd88d
jmp 0xc1e9
cmpb $0x1, 0xf(%rsp)
je 0xc1ad
jmp 0xc28b
cmpl $0xa, %eax
je 0xc2a4
cmpl $0xd, %eax
je 0xc2a4
movb %al, (%rbx,%r14)
incq %r14
movsbl %al, %edi
callq 0x4c56
cmpq %r13, %r14
jne 0xc1d2
addq %r13, %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x584a
movq %rax, %rbx
movb $0x0, (%rbx,%r14)
jmp 0xc097
callq 0xdaf0
movq %rax, %rdi
callq 0xd87d
movq %rax, %rbp
movb $0x1, %al
jmp 0xc202
callq 0xdaf0
movq (%rax), %rdi
callq 0x9216
movq %rax, %rbp
xorl %eax, %eax
testq %rbp, %rbp
je 0xc227
movl %eax, 0x20(%rsp)
movq %rbp, %rdi
callq 0x4170
movq %rax, %r12
leaq (%rax,%r14), %r15
incq %r15
cmpq %r13, %r15
jae 0xc22e
movq %r13, %r15
jmp 0xc23c
callq 0x4d0f
jmp 0xc26c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x584a
movq %rax, %rbx
leaq 0x1(%r12), %rdx
leaq (%rbx,%r14), %rdi
movq %rbp, %rsi
callq 0x4360
addq %r12, %r14
cmpb $0x0, 0x20(%rsp)
je 0xc25f
movq %rbp, %rdi
callq 0x4080
movq %r15, %r13
leaq 0xf(%rsp), %r15
leaq 0x10(%rsp), %r12
callq 0x4cfb
leal -0x1(%rax), %edi
movl $0x1, %esi
callq 0x4b8c
movq %rbx, %rdi
callq 0x4ccf
jmp 0xc097
movl 0x1c(%rsp), %edi
movl 0x18(%rsp), %esi
callq 0x4b8c
movq %rbx, %rdi
callq 0x4080
xorl %ebx, %ebx
jmp 0xc2b1
movl 0x1c(%rsp), %edi
movl 0x18(%rsp), %esi
callq 0x4b8c
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq