// File: /workspace/github_c_makefile_star2_O1/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 0xc3a5
movq %rsi, %r15
movq %rsi, %rdi
callq 0x4170
movq %rax, %r14
movq %rax, %r12
incq %r12
movq %r12, %rdi
callq 0x5830
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x40e0
jmp 0xc3bb
movl $0xa, %r12d
movl $0xa, %edi
callq 0x5830
movq %rax, %rbx
xorl %r14d, %r14d
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4b59
callq 0x4cfa
leal -0x1(%rax), %edi
xorl %esi, %esi
callq 0x4b81
movsbl %bpl, %edi
callq 0x4c4b
testq %r14, %r14
je 0xc3fc
xorl %r15d, %r15d
movsbl (%rbx,%r15), %edi
callq 0x4c4b
incq %r15
cmpq %r15, %r14
jne 0xc3ea
callq 0x4d0e
movb $0x1, %r13b
leaq 0x6(%rsp), %r15
testb $0x1, %r13b
je 0xc637
movl $0x11, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0xc772
movb $0x1, %bpl
cmpl $0x14, %eax
jle 0xc44c
cmpl $0x1e, %eax
jle 0xc4fe
cmpl $0x1f, %eax
je 0xc464
cmpl $0x107, %eax            # imm = 0x107
je 0xc464
cmpl $0x7f, %eax
je 0xc464
jmp 0xc50f
cmpl $0xc, %eax
jg 0xc48c
cmpl $0x8, %eax
je 0xc464
cmpl $0xa, %eax
je 0xc53e
jmp 0xc50f
testq %r14, %r14
je 0xc546
movb $0x0, -0x1(%rbx,%r14)
decq %r14
callq 0x4cfa
leal -0x1(%rax), %edi
leal 0x1(%r14), %esi
callq 0x4b81
jmp 0xc615
cmpl $0xd, %eax
je 0xc53e
cmpl $0x12, %eax
jne 0xc50f
leaq 0x24(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x4b59
movl $0x22, %edi
callq 0x4c4b
movl 0x24(%rsp), %edi
movl 0x20(%rsp), %esi
callq 0x4b81
movl $0x1, %edi
leaq 0x7(%rsp), %rsi
movl $0x1, %edx
callq 0xc772
cmpl $0x25, %eax
je 0xc57b
cmpl $0x17, %eax
je 0xc56a
cmpl $0x6, %eax
jne 0xc5b7
callq 0xde6c
movq %rax, %rdi
callq 0xdc0e
jmp 0xc577
cmpl $0x15, %eax
je 0xc54d
cmpl $0x1b, %eax
jne 0xc50f
cmpb $0x1, 0x6(%rsp)
jne 0xc546
movb %al, (%rbx,%r14)
incq %r14
movsbl %al, %edi
callq 0x4c4b
cmpq %r12, %r14
jne 0xc534
addq %r12, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5851
movq %rax, %rbx
movb $0x0, (%rbx,%r14)
jmp 0xc615
xorl %r13d, %r13d
jmp 0xc615
xorl %ebp, %ebp
jmp 0xc615
movb $0x0, (%rbx)
callq 0x4cfa
leal -0x1(%rax), %edi
movl $0x1, %esi
callq 0x4b81
xorl %r14d, %r14d
jmp 0xc615
callq 0xde6c
movq %rax, %rdi
callq 0xdbfe
movb $0x1, %cl
jmp 0xc58a
callq 0xde6c
movq (%rax), %rdi
callq 0x9348
xorl %ecx, %ecx
testq %rax, %rax
je 0xc5b7
movl %ecx, 0x1c(%rsp)
movq %rax, %r15
movq %rax, %rdi
callq 0x4170
movq %r12, %rcx
movq %rax, %r12
leaq (%rax,%r14), %rsi
incq %rsi
cmpq %rcx, %rsi
jae 0xc5be
movq %rcx, 0x10(%rsp)
jmp 0xc5ce
callq 0x4d0e
jmp 0xc5fb
movq %rbx, %rdi
movq %rsi, 0x10(%rsp)
callq 0x5851
movq %rax, %rbx
leaq 0x1(%r12), %rdx
leaq (%rbx,%r14), %rdi
movq %r15, %rsi
callq 0x4350
addq %r12, %r14
cmpb $0x0, 0x1c(%rsp)
je 0xc5f1
movq %r15, %rdi
callq 0x4080
movq 0x10(%rsp), %r12
leaq 0x6(%rsp), %r15
callq 0x4cfa
leal -0x1(%rax), %edi
movl $0x1, %esi
callq 0x4b81
movq %rbx, %rdi
callq 0x4cce
testb %bpl, %bpl
jne 0xc409
movl 0xc(%rsp), %edi
movl 0x8(%rsp), %esi
callq 0x4b81
movq %rbx, %rdi
callq 0x4080
xorl %ebx, %ebx
jmp 0xc644
movl 0xc(%rsp), %edi
movl 0x8(%rsp), %esi
callq 0x4b81
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq