// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %r14
movb 0x140c8(%rip), %bpl     # 0x1c690
movzbl %bpl, %ebx
testb $0x2, %bl
jne 0x86d3
leaq -0xff55(%r14), %rax
cmpq $0x2, %rax
jb 0x8880
cmpq $0xffaf, %r14           # imm = 0xFFAF
je 0x87f4
cmpq $0x21, %r14
je 0x892a
cmpq $0x24, %r14
je 0x889c
cmpq $0x2a, %r14
je 0x882c
cmpq $0x2f, %r14
je 0x87f4
cmpq $0x3f, %r14
je 0x87f4
cmpq $0x4e, %r14
je 0x8844
cmpq $0x5f, %r14
je 0x883a
cmpq $0x6e, %r14
je 0x8844
cmpq $0x73, %r14
je 0x88b6
cmpq $0x74, %r14
je 0x89a7
cmpq $0xff08, %r14           # imm = 0xFF08
je 0x88ef
cmpq $0xff0d, %r14           # imm = 0xFF0D
je 0x8965
cmpq $0xff1b, %r14           # imm = 0xFF1B
je 0x8957
cmpq $0xff50, %r14           # imm = 0xFF50
je 0x88a9
cmpq $0xff57, %r14           # imm = 0xFF57
je 0x88df
cmpq $0xffaa, %r14           # imm = 0xFFAA
je 0x882c
cmpq $-0x1, %r14
jne 0x88f8
movb $0x1, 0x13fce(%rip)     # 0x1c684
movq 0x11e23(%rip), %rax     # 0x1a4e0
movq %rax, 0x13ea4(%rip)     # 0x1c568
pushq $-0x1
popq %rsi
xorl %edi, %edi
callq 0x9231
jmp 0x89a0
cmpq $0xff08, %r14           # imm = 0xFF08
je 0x86fd
cmpq $0xff0d, %r14           # imm = 0xFF0D
jne 0x8738
andl $0x7d, %ebx
movb %bl, 0x13fa2(%rip)      # 0x1c690
pushq $-0x2
popq %rsi
movl %ebx, %edi
callq 0x9231
jmp 0x894c
movslq 0x13f7c(%rip), %rcx     # 0x1c680
testq %rcx, %rcx
je 0x894c
movq 0x10dfc(%rip), %rdx     # 0x19510
movslq 0x11ddd(%rip), %rax     # 0x1a4f8
movq (%rdx,%rax,8), %rdx
leal -0x1(%rcx), %esi
movl %esi, 0x13f58(%rip)     # 0x1c680
shlq $0x4, %rcx
movl $0x20, (%rdx,%rcx)
jmp 0x87cd
xorl %eax, %eax
testl %edx, %edx
jle 0x894e
cmpq $0xff1b, %r14           # imm = 0xFF1B
je 0x894e
movl 0x13f2b(%rip), %ecx     # 0x1c680
cmpl 0x10db1(%rip), %ecx     # 0x1950c
je 0x894e
movslq %ecx, %rax
leal 0x1(%rax), %ebp
leaq 0x13e12(%rip), %r14     # 0x1c580
leaq (%r14,%rax,4), %r15
movq %rax, %r12
shlq $0x4, %r12
movl %ebp, 0x13f01(%rip)     # 0x1c680
movl %edx, %edx
movq %rsi, %rdi
movq %r15, %rsi
callq 0x9589
movl (%r15), %eax
movq 0x10d7a(%rip), %rcx     # 0x19510
movslq 0x11d5b(%rip), %rdx     # 0x1a4f8
movq (%rcx,%rdx,8), %rcx
movl %eax, 0x10(%r12,%rcx)
movl 0x13edc(%rip), %ecx     # 0x1c688
movl 0xfda1(%rip), %r8d      # 0x18554
leaq 0x13d9e(%rip), %r9      # 0x1c558
movl %ebx, %edi
movq %r14, %rsi
movl %ebp, %edx
callq 0x9494
movslq 0x11d2b(%rip), %rax     # 0x1a4f8
movq 0x11cf4(%rip), %rcx     # 0x1a4c8
movl $0x1, (%rcx,%rax,4)
movl 0x11d17(%rip), %edi     # 0x1a4f8
movl 0x10d25(%rip), %esi     # 0x1950c
leal 0x1(%rdi), %edx
callq 0x8ebb
jmp 0x894c
andl $0x3f, %r14d
xorl %eax, %eax
cmpq $0x2f, %r14
setne %al
leal -0x1(,%rax,2), %eax
movl %eax, 0x13e7a(%rip)     # 0x1c688
andl $0x0, 0x13e6b(%rip)     # 0x1c680
pushq $0xf
popq %rdi
movq %r14, %rsi
callq 0x9231
xorb $0x2, 0x13e69(%rip)     # 0x1c690
jmp 0x8945
movl 0x10cda(%rip), %eax     # 0x1950c
sarl %eax
movl %eax, 0x11ca6(%rip)     # 0x1a4e0
movl 0x13d2c(%rip), %eax     # 0x1c56c
sarl %eax
jmp 0x8891
movl 0x13e36(%rip), %edx     # 0x1c680
testl %edx, %edx
je 0x8945
xorl %eax, %eax
cmpq $0x6e, %r14
setne %al
leal -0x1(%rax,%rax), %ecx
movl 0xfcee(%rip), %r8d      # 0x18554
leaq 0x13d13(%rip), %rsi     # 0x1c580
leaq 0x13ce4(%rip), %r9      # 0x1c558
movl %ebx, %edi
callq 0x9494
jmp 0x8945
xorl %eax, %eax
cmpq $0xff55, %r14           # imm = 0xFF55
je 0x8891
movl 0x13cdb(%rip), %eax     # 0x1c56c
movl %eax, 0x11c4d(%rip)     # 0x1a4e4
jmp 0x8938
movl 0x10c6a(%rip), %eax     # 0x1950c
decl %eax
jmp 0x8932
andq $0x0, 0x11c2f(%rip)     # 0x1a4e0
jmp 0x8938
testb $0x1, %bpl
jne 0x89e7
movl 0x11c1a(%rip), %edi     # 0x1a4e0
movl 0x11c18(%rip), %esi     # 0x1a4e4
xorl %edx, %edx
callq 0x6d1a
movb 0x13db6(%rip), %bpl     # 0x1c690
jmp 0x89ec
movq 0x13c82(%rip), %rax     # 0x1c568
movq %rax, 0x11bf3(%rip)     # 0x1a4e0
jmp 0x8938
andl $0x0, 0x11bea(%rip)     # 0x1a4e0
jmp 0x8938
leaq -0x30(%r14), %rax
cmpq $0x9, %rax
jbe 0x8913
leaq -0xffb0(%r14), %rax
cmpq $0x9, %rax
ja 0x8a08
imull $0xa, 0x13d72(%rip), %eax # 0x1c68c
andl $0xf, %r14d
addl %eax, %r14d
movl %r14d, 0x13d64(%rip)    # 0x1c68c
jmp 0x894c
movl 0x10bdc(%rip), %eax     # 0x1950c
sarl %eax
movl %eax, 0x11ba8(%rip)     # 0x1a4e0
movl 0xfc16(%rip), %esi      # 0x18554
movl %ebx, %edi
callq 0x9411
andl $0x0, 0x13d40(%rip)     # 0x1c68c
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1, 0x13d26(%rip)     # 0x1c684
jne 0x8945
callq 0x6d9a
pushq $0x4
popq %rdi
movq %r14, %rsi
callq 0x9231
movq 0x13bf1(%rip), %rax     # 0x1c568
movq %rax, 0x11b62(%rip)     # 0x1a4e0
xorl %ebx, %ebx
movb %bl, 0x13d0a(%rip)      # 0x1c690
movl 0xfbc8(%rip), %esi      # 0x18554
xorl %edi, %edi
callq 0x9411
andl $0x0, 0x13cf2(%rip)     # 0x1c68c
movb %bl, 0x13ce4(%rip)      # 0x1c684
movl $0x40000, %eax          # imm = 0x40000
jmp 0x894e
movl 0x11b33(%rip), %edi     # 0x1a4e0
movl 0x11b31(%rip), %esi     # 0x1a4e4
movl 0xfb9b(%rip), %edx      # 0x18554
xorl $0x3, %edx
movl %edx, 0xfb92(%rip)      # 0x18554
xorl %ecx, %ecx
callq 0x6f03
movl 0x11b11(%rip), %edi     # 0x1a4e0
movl 0x11b0f(%rip), %esi     # 0x1a4e4
movl 0xfb79(%rip), %edx      # 0x18554
xorl %ecx, %ecx
callq 0x6f03
jmp 0x8945
callq 0x6d9a
xorb $0x1, %bpl
movb %bpl, 0x13c99(%rip)     # 0x1c690
movzbl %bpl, %edi
pushq $0x73
popq %rsi
callq 0x9231
jmp 0x8945
leaq -0x68(%r14), %rax
cmpq $0x5, %rax
jae 0x8a2b
pushq $0x1d
popq %rcx
btl %eax, %ecx
jae 0x8a2b
leaq 0x8b9f(%rip), %rax      # 0x115c0
movl -0x1a0(%rax,%r14,4), %r14d
jmp 0x8a4e
movl %r14d, %eax
notl %eax
testw $0xff50, %ax            # imm = 0xFF50
jne 0x8945
andl $0xffff00af, %r14d      # imm = 0xFFFF00AF
decl %r14d
cmpl $0x3, %r14d
jg 0x8945
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, 0x13c11(%rip)     # 0x1c688
movl %r14d, %esi
andl $-0x2, %esi
cmpl $0x2, %esi
jne 0x8a98
cmpl $0x3, %r14d
jne 0x8a90
movl 0x11a6a(%rip), %ecx     # 0x1a4f8
jmp 0x8a98
movl 0x10a76(%rip), %ecx     # 0x1950c
decl %ecx
movl 0x13bee(%rip), %esi     # 0x1c68c
testl %esi, %esi
jne 0x8aaf
movl $0x1, 0x13be0(%rip)     # 0x1c68c
pushq $0x1
popq %rsi
leaq 0x10a52(%rip), %rdi     # 0x19508
movl (%rax,%rdi), %r8d
cmpl %ecx, %r8d
jne 0x8ae1
xorl %r9d, %r9d
xorl %r10d, %r10d
testl %ecx, %ecx
setne %r9b
setle %r10b
testb $0x2, %r14b
cmovel %r9d, %r10d
cmpb $0x1, %r10b
jne 0x8945
imull %edx, %esi
addl %r8d, %esi
movl %esi, (%rax,%rdi)
js 0x8afc
testl %ecx, %ecx
jle 0x8938
cmpl %ecx, %esi
jle 0x8938
movl %ecx, (%rax,%rdi)
jmp 0x8938