// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movb %dil, %al
movb %al, -0x9(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x10e02
movq -0x18(%rbp), %rdi
callq 0x4170
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5f20
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x40e0
jmp 0x10e1f
movq $0xa, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5f20
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
leaq -0x34(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x4b10
callq 0x4e70
movl %eax, %edi
subl $0x1, %edi
xorl %esi, %esi
callq 0x4b90
movsbl -0x9(%rbp), %edi
callq 0x4cf0
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x10e77
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x4cf0
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x10e4e
callq 0x4e90
movb $0x1, -0x41(%rbp)
testb $0x1, -0x41(%rbp)
je 0x11022
movl $0x11, %edi
leaq -0x42(%rbp), %rsi
movl $0x1, %edx
callq 0x11310
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x4c(%rbp)
subl $0x8, %eax
je 0x10f17
jmp 0x10ead
movl -0x4c(%rbp), %eax
subl $0xa, %eax
je 0x10f59
jmp 0x10ebb
movl -0x4c(%rbp), %eax
subl $0xd, %eax
je 0x10f59
jmp 0x10ec9
movl -0x4c(%rbp), %eax
subl $0x12, %eax
je 0x10f62
jmp 0x10ed7
movl -0x4c(%rbp), %eax
subl $0x15, %eax
je 0x10f95
jmp 0x10ee5
movl -0x4c(%rbp), %eax
subl $0x1b, %eax
je 0x10fbf
jmp 0x10ef3
movl -0x4c(%rbp), %eax
subl $0x1f, %eax
je 0x10f17
jmp 0x10efd
movl -0x4c(%rbp), %eax
subl $0x7f, %eax
je 0x10f17
jmp 0x10f07
movl -0x4c(%rbp), %eax
subl $0x107, %eax            # imm = 0x107
jne 0x10fc9
jmp 0x10f17
cmpq $0x0, -0x30(%rbp)
jne 0x10f23
jmp 0x11037
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rdx
addq $-0x1, %rdx
movq %rdx, -0x30(%rbp)
movb $0x0, -0x1(%rax,%rcx)
callq 0x4e70
movl %eax, %edi
subl $0x1, %edi
movq -0x30(%rbp), %rax
addq $0x1, %rax
movl %eax, %esi
callq 0x4b90
jmp 0x1101d
movb $0x0, -0x41(%rbp)
jmp 0x1101d
leaq -0x28(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0x11060
callq 0x4e70
movl %eax, %edi
subl $0x1, %edi
movl $0x1, %esi
callq 0x4b90
movq -0x28(%rbp), %rdi
callq 0x4e10
jmp 0x1101d
movq -0x28(%rbp), %rax
movq $0x0, -0x30(%rbp)
movb $0x0, (%rax)
callq 0x4e70
movl %eax, %edi
subl $0x1, %edi
movq -0x30(%rbp), %rax
addq $0x1, %rax
movl %eax, %esi
callq 0x4b90
jmp 0x1101d
testb $0x1, -0x42(%rbp)
jne 0x10fc7
jmp 0x11037
jmp 0x10fc9
movl -0x48(%rbp), %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x30(%rbp)
movb %dl, (%rax,%rcx)
movl -0x48(%rbp), %eax
movsbl %al, %edi
callq 0x4cf0
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x11011
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
shlq %rsi
movq %rsi, -0x20(%rbp)
callq 0x5f60
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movb $0x0, (%rax,%rcx)
jmp 0x10e80
movl -0x34(%rbp), %edi
movl -0x38(%rbp), %esi
callq 0x4b90
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x11053
movl -0x34(%rbp), %edi
movl -0x38(%rbp), %esi
callq 0x4b90
movq -0x28(%rbp), %rdi
callq 0x4080
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)