// File: /workspace/github_c_makefile_star2_O1/mrdotx[P]st/st.c
// Function: trt_kbdselect
int trt_kbdselect(KeySym ksym, char *buf, int len) {
	static TCursor cu;
	static Rune target[64];
	static int type = 1, ptarget, in_use;
	static int sens, quant;
	static char selectsearch_mode;
	int i, bound, *xy;


	if ( selectsearch_mode & 2 ) {
		if ( ksym == XK_Return ) {
			selectsearch_mode ^= 2;
			set_notifmode(selectsearch_mode, -2);
			if ( ksym == XK_Escape )    ptarget = 0;
			return 0;
		}
		else if ( ksym == XK_BackSpace ) {
			if ( !ptarget )     return 0;
			term.line[term.bot][ptarget--].u = ' ';
		}
		else if ( len < 1 ) {
			return 0;
		}
		else if ( ptarget == term.col  || ksym == XK_Escape ) {
			return 0;
		}
		else {
			utf8decode(buf, &target[ptarget++], len);
			term.line[term.bot][ptarget].u = target[ptarget - 1];
		}

		if ( ksym != XK_BackSpace )
			search(selectsearch_mode, &target[0], ptarget, sens, type, &cu);

		term.dirty[term.bot] = 1;
		drawregion(0, term.bot, term.col, term.bot + 1);
		return 0;
	}

	switch ( ksym ) {
	case -1 :
		in_use = 1;
		cu.x = term.c.x, cu.y = term.c.y;
		set_notifmode(0, ksym);
		return MODE_KBDSELECT;
	case XK_s :
		if ( selectsearch_mode & 1 )
			selclear();
		else
			selstart(term.c.x, term.c.y, 0);
		set_notifmode(selectsearch_mode ^= 1, ksym);
		break;
	case XK_t :
		selextend(term.c.x, term.c.y, type ^= 3, i = 0);  /* 2 fois */
		selextend(term.c.x, term.c.y, type, i = 0);
		break;
	case XK_slash :
	case XK_KP_Divide :
	case XK_question :
		ksym &= XK_question;                /* Divide to slash */
		sens = (ksym == XK_slash) ? -1 : 1;
		ptarget = 0;
		set_notifmode(15, ksym);
		selectsearch_mode ^= 2;
		break;
	case XK_Escape :
		if ( !in_use )  break;
		selclear();
	case XK_Return :
		set_notifmode(4, ksym);
		term.c.x = cu.x, term.c.y = cu.y;
		select_or_drawcursor(selectsearch_mode = 0, type);
		in_use = quant = 0;
		return MODE_KBDSELECT;
	case XK_n :
	case XK_N :
		if ( ptarget )
			search(selectsearch_mode, &target[0], ptarget, (ksym == XK_n) ? -1 : 1, type, &cu);
		break;
	case XK_BackSpace :
		term.c.x = 0;
		select_or_drawcursor(selectsearch_mode, type);
		break;
	case XK_dollar :
		term.c.x = term.col - 1;
		select_or_drawcursor(selectsearch_mode, type);
		break;
	case XK_Home :
		term.c.x = 0, term.c.y = 0;
		select_or_drawcursor(selectsearch_mode, type);
		break;
	case XK_End :
		term.c.x = cu.x, term.c.y = cu.y;
		select_or_drawcursor(selectsearch_mode, type);
		break;
	case XK_Page_Up :
	case XK_Page_Down :
		term.c.y = (ksym == XK_Prior ) ? 0 : cu.y;
		select_or_drawcursor(selectsearch_mode, type);
		break;
	case XK_exclam :
		term.c.x = term.col >> 1;
		select_or_drawcursor(selectsearch_mode, type);
		break;
	case XK_asterisk :
	case XK_KP_Multiply :
		term.c.x = term.col >> 1;
	case XK_underscore :
		term.c.y = cu.y >> 1;
		select_or_drawcursor(selectsearch_mode, type);
		break;
	default :
		if ( ksym >= XK_0 && ksym <= XK_9 ) {               /* 0-9 keyboard */
			quant = (quant * 10) + (ksym ^ XK_0);
			return 0;
		}
		else if ( ksym >= XK_KP_0 && ksym <= XK_KP_9 ) {    /* 0-9 numpad */
			quant = (quant * 10) + (ksym ^ XK_KP_0);
			return 0;
		}
		else if ( ksym == XK_k || ksym == XK_h )
			i = ksym & 1;
		else if ( ksym == XK_l || ksym == XK_j )
			i = ((ksym & 6) | 4) >> 1;
		else if ( (XK_Home & ksym) != XK_Home || (i = (ksym ^ XK_Home) - 1) > 3 )
			break;

		xy = (i & 1) ? &term.c.y : &term.c.x;
		sens = (i & 2) ? 1 : -1;
		bound = (i >> 1 ^ 1) ? 0 : (i ^ 3) ? term.col - 1 : term.bot;

		if ( quant == 0 )
			quant++;

		if ( *xy == bound && ((sens < 0 && bound == 0) || (sens > 0 && bound > 0)) )
			break;

		*xy += quant * sens;
		if ( *xy < 0 || ( bound > 0 && *xy > bound) )
			*xy = bound;

		select_or_drawcursor(selectsearch_mode, type);
	}
	quant = 0;
	return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
movzbl 0x14a3a(%rip), %ebx     # 0x1d6a0
testb $0x2, %bl
jne 0x8ca8
cmpq $0x73, %r14
jg 0x8cde
cmpq $0x3e, %r14
jg 0x8e30
cmpq $0x23, %r14
jle 0x8eb1
cmpq $0x24, %r14
je 0x9025
cmpq $0x2a, %r14
je 0x8f9a
cmpq $0x2f, %r14
je 0x8f5d
jmp 0x90bf
cmpq $0xff08, %r14           # imm = 0xFF08
je 0x8d2f
cmpq $0xff0d, %r14           # imm = 0xFF0D
jne 0x8d63
movl %ebx, %eax
andb $0x7d, %al
movb %al, 0x149d8(%rip)      # 0x1d6a0
andl $0x7d, %ebx
movl %ebx, %edi
movq $-0x2, %rsi
callq 0x9a58
jmp 0x904a
cmpq $0xff54, %r14           # imm = 0xFF54
jg 0x8e79
cmpq $0xff0c, %r14           # imm = 0xFF0C
jle 0x8ed2
cmpq $0xff0d, %r14           # imm = 0xFF0D
je 0x8fe3
cmpq $0xff1b, %r14           # imm = 0xFF1B
je 0x8fd5
cmpq $0xff50, %r14           # imm = 0xFF50
jne 0x90bf
movq $0x0, 0x127c6(%rip) # 0x1b4f0
jmp 0x9033
movl 0x1495a(%rip), %r12d    # 0x1d690
testl %r12d, %r12d
je 0x904a
movq 0x117da(%rip), %rax     # 0x1a520
movslq 0x127bb(%rip), %rcx     # 0x1b508
movq (%rax,%rcx,8), %rax
leal -0x1(%r12), %ecx
movl %ecx, 0x14934(%rip)     # 0x1d690
movl $0x20, %ecx
jmp 0x8dc7
xorl %eax, %eax
testl %edx, %edx
jle 0x904c
cmpq $0xff1b, %r14           # imm = 0xFF1B
je 0x904c
movl 0x14910(%rip), %ecx     # 0x1d690
cmpl 0x11796(%rip), %ecx     # 0x1a51c
je 0x904c
movslq %ecx, %rax
leal 0x1(%rax), %r12d
movl %r12d, 0x148f6(%rip)    # 0x1d690
leaq 0x147ef(%rip), %rcx     # 0x1d590
leaq (%rcx,%rax,4), %r15
movl %edx, %edx
movq %rsi, %rdi
movq %r15, %rsi
callq 0x9dd7
movl (%r15), %ecx
movq 0x11764(%rip), %rax     # 0x1a520
movslq 0x12745(%rip), %rdx     # 0x1b508
movq (%rax,%rdx,8), %rax
movslq %r12d, %rdx
shlq $0x4, %rdx
movl %ecx, (%rax,%rdx)
cmpq $0xff08, %r14           # imm = 0xFF08
je 0x8e02
movl 0x148b0(%rip), %edx     # 0x1d690
movl 0x148b2(%rip), %ecx     # 0x1d698
movl 0x10777(%rip), %r8d     # 0x19564
leaq 0x1479c(%rip), %rsi     # 0x1d590
leaq 0x1476d(%rip), %r9      # 0x1d568
movl %ebx, %edi
callq 0x9cbb
movq 0x126cf(%rip), %rax     # 0x1b4d8
movslq 0x126f8(%rip), %rcx     # 0x1b508
movl $0x1, (%rax,%rcx,4)
movl 0x126eb(%rip), %edi     # 0x1b508
movl 0x116f9(%rip), %esi     # 0x1a51c
leal 0x1(%rdi), %edx
callq 0x9619
jmp 0x904a
cmpq $0x5e, %r14
jle 0x8ef8
cmpq $0x5f, %r14
je 0x8fa8
cmpq $0x6e, %r14
je 0x8f08
cmpq $0x73, %r14
jne 0x90bf
testb $0x1, %bl
jne 0x90f2
movl 0x12689(%rip), %edi     # 0x1b4f0
movl 0x12687(%rip), %esi     # 0x1b4f4
xorl %edx, %edx
callq 0x7131
jmp 0x90f7
cmpq $0xffa9, %r14           # imm = 0xFFA9
jg 0x8f47
leaq -0xff55(%r14), %rax
cmpq $0x2, %rax
jae 0x8fb8
xorl %eax, %eax
cmpq $0xff55, %r14           # imm = 0xFF55
je 0x8fb0
movl 0x146d0(%rip), %eax     # 0x1d57c
jmp 0x8fb0
cmpq $-0x1, %r14
je 0x9058
cmpq $0x21, %r14
jne 0x90bf
movl 0x11651(%rip), %eax     # 0x1a51c
sarl %eax
jmp 0x902d
cmpq $0x74, %r14
je 0x9082
cmpq $0xff08, %r14           # imm = 0xFF08
jne 0x90bf
movl $0x0, 0x125fd(%rip)     # 0x1b4f0
jmp 0x9033
cmpq $0x3f, %r14
je 0x8f5d
cmpq $0x4e, %r14
jne 0x90bf
movl 0x14782(%rip), %edx     # 0x1d690
testl %edx, %edx
je 0x9040
xorl %eax, %eax
cmpq $0x6e, %r14
setne %al
leal -0x1(,%rax,2), %ecx
movl 0x10637(%rip), %r8d     # 0x19564
leaq 0x1465c(%rip), %rsi     # 0x1d590
leaq 0x1462d(%rip), %r9      # 0x1d568
movl %ebx, %edi
callq 0x9cbb
jmp 0x9040
cmpq $0xffaa, %r14           # imm = 0xFFAA
je 0x8f9a
cmpq $0xffaf, %r14           # imm = 0xFFAF
jne 0x90bf
andl $0x3f, %r14d
xorl %eax, %eax
cmpq $0x2f, %r14
setne %al
leal -0x1(,%rax,2), %eax
movl %eax, 0x14721(%rip)     # 0x1d698
movl $0x0, 0x1470f(%rip)     # 0x1d690
movl $0xf, %edi
movq %r14, %rsi
callq 0x9a58
xorb $0x2, 0x1470b(%rip)     # 0x1d6a0
jmp 0x9040
movl 0x1157c(%rip), %eax     # 0x1a51c
sarl %eax
movl %eax, 0x12548(%rip)     # 0x1b4f0
movl 0x145ce(%rip), %eax     # 0x1d57c
sarl %eax
movl %eax, 0x1253e(%rip)     # 0x1b4f4
jmp 0x9033
cmpq $0xff57, %r14           # imm = 0xFF57
jne 0x90bf
movq 0x145ac(%rip), %rax     # 0x1d578
movq %rax, 0x1251d(%rip)     # 0x1b4f0
jmp 0x9033
cmpb $0x1, 0x146b8(%rip)     # 0x1d694
jne 0x9040
callq 0x71d9
movl $0x4, %edi
movq %r14, %rsi
callq 0x9a58
movq 0x14581(%rip), %rax     # 0x1d578
movq %rax, 0x124f2(%rip)     # 0x1b4f0
xorl %ebx, %ebx
movb %bl, 0x1469a(%rip)      # 0x1d6a0
movl 0x10558(%rip), %esi     # 0x19564
xorl %edi, %edi
callq 0x9c38
movl $0x0, 0x1467f(%rip)     # 0x1d69c
movb %bl, 0x14671(%rip)      # 0x1d694
jmp 0x907b
movl 0x114f1(%rip), %eax     # 0x1a51c
decl %eax
movl %eax, 0x124bd(%rip)     # 0x1b4f0
movl 0x1052b(%rip), %esi     # 0x19564
movl %ebx, %edi
callq 0x9c38
movl $0x0, 0x14652(%rip)     # 0x1d69c
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movb $0x1, 0x14635(%rip)     # 0x1d694
movq 0x1248a(%rip), %rax     # 0x1b4f0
movq %rax, 0x1450b(%rip)     # 0x1d578
xorl %edi, %edi
movq $-0x1, %rsi
callq 0x9a58
movl $0x40000, %eax          # imm = 0x40000
jmp 0x904c
movl 0x12468(%rip), %edi     # 0x1b4f0
movl 0x12466(%rip), %esi     # 0x1b4f4
movl 0x104d0(%rip), %edx     # 0x19564
xorl $0x3, %edx
movl %edx, 0x104c7(%rip)     # 0x19564
xorl %ecx, %ecx
callq 0x7354
movl 0x12446(%rip), %edi     # 0x1b4f0
movl 0x12444(%rip), %esi     # 0x1b4f4
movl 0x104ae(%rip), %edx     # 0x19564
xorl %ecx, %ecx
callq 0x7354
jmp 0x9040
leaq -0x30(%r14), %rax
cmpq $0x9, %rax
jbe 0x90d6
leaq -0xffb0(%r14), %rax
cmpq $0x9, %rax
ja 0x9117
movl 0x145c0(%rip), %eax     # 0x1d69c
leal (%rax,%rax,4), %eax
andl $0xf, %r14d
leal (%r14,%rax,2), %eax
movl %eax, 0x145af(%rip)     # 0x1d69c
jmp 0x904a
callq 0x71d9
movzbl 0x145a2(%rip), %edi     # 0x1d6a0
xorl $0x1, %edi
movb %dil, 0x14598(%rip)     # 0x1d6a0
movl $0x73, %esi
callq 0x9a58
jmp 0x9040
leaq -0x68(%r14), %rax
cmpq $0x5, %rax
jae 0x913c
movl $0x1d, %ecx
btl %eax, %ecx
jae 0x913c
leaq 0x948e(%rip), %rax      # 0x125c0
movl -0x1a0(%rax,%r14,4), %r14d
jmp 0x9160
movl %r14d, %eax
notl %eax
testl $0xff50, %eax           # imm = 0xFF50
jne 0x9040
andl $0xffff00af, %r14d      # imm = 0xFFFF00AF
decl %r14d
cmpl $0x3, %r14d
jg 0x9040
movl %r14d, %eax
andl $0x1, %eax
leaq 0xfd8(,%rax,4), %rax
movl %r14d, %edx
andl $0x2, %edx
xorl %ecx, %ecx
cmpl $0x1, %edx
movl $0x0, %edx
sbbl %edx, %edx
orl $0x1, %edx
movl %edx, 0x1450f(%rip)     # 0x1d698
movl %r14d, %esi
andl $-0x2, %esi
cmpl $0x2, %esi
jne 0x91aa
cmpl $0x3, %r14d
jne 0x91a2
movl 0x12368(%rip), %ecx     # 0x1b508
jmp 0x91aa
movl 0x11374(%rip), %ecx     # 0x1a51c
decl %ecx
cmpl $0x0, 0x144eb(%rip)     # 0x1d69c
jne 0x91bd
movl $0x1, 0x144df(%rip)     # 0x1d69c
leaq 0x11354(%rip), %rsi     # 0x1a518
movl (%rax,%rsi), %edi
cmpl %ecx, %edi
jne 0x91ed
xorl %r8d, %r8d
xorl %r9d, %r9d
testl %ecx, %ecx
setne %r8b
setle %r9b
testb $0x2, %r14b
cmovel %r8d, %r9d
cmpb $0x1, %r9b
jne 0x9040
imull 0x144a8(%rip), %edx     # 0x1d69c
addl %edi, %edx
movl %edx, (%rax,%rsi)
js 0x920b
testl %ecx, %ecx
jle 0x9033
cmpl %ecx, %edx
jle 0x9033
movl %ecx, (%rax,%rsi)
jmp 0x9033