// File: /workspace/github_c_makefile_star2_O3/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 0x1474f(%rip), %bpl     # 0x1d690
movzbl %bpl, %ebx
testb $0x2, %bl
jne 0x8f87
cmpq $0x73, %r14
jg 0x8fb9
cmpq $0x3e, %r14
jg 0x9101
cmpq $0x23, %r14
jle 0x918a
cmpq $0x24, %r14
je 0x92f8
cmpq $0x2a, %r14
je 0x926d
cmpq $0x2f, %r14
je 0x9233
jmp 0x938f
cmpq $0xff08, %r14           # imm = 0xFF08
je 0x900a
cmpq $0xff0d, %r14           # imm = 0xFF0D
jne 0x9045
andl $0x7d, %ebx
movb %bl, 0x146ea(%rip)      # 0x1d690
movl %ebx, %edi
movq $-0x2, %rsi
callq 0x9eea
jmp 0x931d
cmpq $0xff54, %r14           # imm = 0xFF54
jg 0x9152
cmpq $0xff0c, %r14           # imm = 0xFF0C
jle 0x91ab
cmpq $0xff0d, %r14           # imm = 0xFF0D
je 0x92b6
cmpq $0xff1b, %r14           # imm = 0xFF1B
je 0x92a8
cmpq $0xff50, %r14           # imm = 0xFF50
jne 0x938f
movq $0x0, 0x124db(%rip) # 0x1b4e0
jmp 0x9306
movslq 0x1466f(%rip), %rcx     # 0x1d680
testq %rcx, %rcx
je 0x931d
movq 0x114ef(%rip), %rdx     # 0x1a510
movslq 0x124d0(%rip), %rax     # 0x1b4f8
movq (%rdx,%rax,8), %rdx
leal -0x1(%rcx), %esi
movl %esi, 0x1464b(%rip)     # 0x1d680
shlq $0x4, %rcx
movl $0x20, (%rdx,%rcx)
jmp 0x90da
xorl %eax, %eax
testl %edx, %edx
jle 0x931f
cmpq $0xff1b, %r14           # imm = 0xFF1B
je 0x931f
movl 0x1461e(%rip), %ecx     # 0x1d680
cmpl 0x114a4(%rip), %ecx     # 0x1a50c
je 0x931f
movslq %ecx, %rax
leal 0x1(%rax), %ebp
leaq 0x14505(%rip), %r14     # 0x1d580
leaq (%r14,%rax,4), %r15
movq %rax, %r12
shlq $0x4, %r12
movl %ebp, 0x145f4(%rip)     # 0x1d680
movl %edx, %edx
movq %rsi, %rdi
movq %r15, %rsi
callq 0xa256
movl (%r15), %eax
movq 0x1146d(%rip), %rcx     # 0x1a510
movslq 0x1244e(%rip), %rdx     # 0x1b4f8
movq (%rcx,%rdx,8), %rcx
movl %eax, 0x10(%r12,%rcx)
movl 0x145cf(%rip), %ecx     # 0x1d688
movl 0x10494(%rip), %r8d     # 0x19554
leaq 0x14491(%rip), %r9      # 0x1d558
movl %ebx, %edi
movq %r14, %rsi
movl %ebp, %edx
callq 0xa158
movslq 0x1241e(%rip), %rax     # 0x1b4f8
movq 0x123e7(%rip), %rcx     # 0x1b4c8
movl $0x1, (%rcx,%rax,4)
movl 0x1240a(%rip), %edi     # 0x1b4f8
movl 0x11418(%rip), %esi     # 0x1a50c
leal 0x1(%rdi), %edx
callq 0x99ca
jmp 0x931d
cmpq $0x5e, %r14
jle 0x91d1
cmpq $0x5f, %r14
je 0x927b
cmpq $0x6e, %r14
je 0x91e1
cmpq $0x73, %r14
jne 0x938f
testb $0x1, %bpl
jne 0x93c2
movl 0x123a7(%rip), %edi     # 0x1b4e0
movl 0x123a5(%rip), %esi     # 0x1b4e4
xorl %edx, %edx
callq 0x7121
movb 0x14543(%rip), %bpl     # 0x1d690
jmp 0x93c7
cmpq $0xffa9, %r14           # imm = 0xFFA9
jg 0x921d
leaq -0xff55(%r14), %rax
cmpq $0x2, %rax
jae 0x928b
xorl %eax, %eax
cmpq $0xff55, %r14           # imm = 0xFF55
je 0x9283
movl 0x143e7(%rip), %eax     # 0x1d56c
jmp 0x9283
cmpq $-0x1, %r14
je 0x9328
cmpq $0x21, %r14
jne 0x938f
movl 0x11368(%rip), %eax     # 0x1a50c
sarl %eax
jmp 0x9300
cmpq $0x74, %r14
je 0x9352
cmpq $0xff08, %r14           # imm = 0xFF08
jne 0x938f
movl $0x0, 0x12314(%rip)     # 0x1b4e0
jmp 0x9306
cmpq $0x3f, %r14
je 0x9233
cmpq $0x4e, %r14
jne 0x938f
movl 0x14499(%rip), %edx     # 0x1d680
testl %edx, %edx
je 0x9313
xorl %eax, %eax
cmpq $0x6e, %r14
setne %al
leal -0x1(%rax,%rax), %ecx
movl 0x10351(%rip), %r8d     # 0x19554
leaq 0x14376(%rip), %rsi     # 0x1d580
leaq 0x14347(%rip), %r9      # 0x1d558
movl %ebx, %edi
callq 0xa158
jmp 0x9313
cmpq $0xffaa, %r14           # imm = 0xFFAA
je 0x926d
cmpq $0xffaf, %r14           # imm = 0xFFAF
jne 0x938f
andl $0x3f, %r14d
xorl %eax, %eax
cmpq $0x2f, %r14
setne %al
leal -0x1(%rax,%rax), %eax
movl %eax, 0x1443e(%rip)     # 0x1d688
movl $0x0, 0x1442c(%rip)     # 0x1d680
movl $0xf, %edi
movq %r14, %rsi
callq 0x9eea
xorb $0x2, 0x14428(%rip)     # 0x1d690
jmp 0x9313
movl 0x11299(%rip), %eax     # 0x1a50c
sarl %eax
movl %eax, 0x12265(%rip)     # 0x1b4e0
movl 0x142eb(%rip), %eax     # 0x1d56c
sarl %eax
movl %eax, 0x1225b(%rip)     # 0x1b4e4
jmp 0x9306
cmpq $0xff57, %r14           # imm = 0xFF57
jne 0x938f
movq 0x142c9(%rip), %rax     # 0x1d568
movq %rax, 0x1223a(%rip)     # 0x1b4e0
jmp 0x9306
cmpb $0x1, 0x143d5(%rip)     # 0x1d684
jne 0x9313
callq 0x71c9
movl $0x4, %edi
movq %r14, %rsi
callq 0x9eea
movq 0x1429e(%rip), %rax     # 0x1d568
movq %rax, 0x1220f(%rip)     # 0x1b4e0
xorl %ebx, %ebx
movb %bl, 0x143b7(%rip)      # 0x1d690
movl 0x10275(%rip), %esi     # 0x19554
xorl %edi, %edi
callq 0xa0d5
movl $0x0, 0x1439c(%rip)     # 0x1d68c
movb %bl, 0x1438e(%rip)      # 0x1d684
jmp 0x934b
movl 0x1120e(%rip), %eax     # 0x1a50c
decl %eax
movl %eax, 0x121da(%rip)     # 0x1b4e0
movl 0x10248(%rip), %esi     # 0x19554
movl %ebx, %edi
callq 0xa0d5
movl $0x0, 0x1436f(%rip)     # 0x1d68c
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x14355(%rip)     # 0x1d684
movq 0x121aa(%rip), %rax     # 0x1b4e0
movq %rax, 0x1422b(%rip)     # 0x1d568
xorl %edi, %edi
movq $-0x1, %rsi
callq 0x9eea
movl $0x40000, %eax          # imm = 0x40000
jmp 0x931f
movl 0x12188(%rip), %edi     # 0x1b4e0
movl 0x12186(%rip), %esi     # 0x1b4e4
movl 0x101f0(%rip), %edx     # 0x19554
xorl $0x3, %edx
movl %edx, 0x101e7(%rip)     # 0x19554
xorl %ecx, %ecx
callq 0x742a
movl 0x12166(%rip), %edi     # 0x1b4e0
movl 0x12164(%rip), %esi     # 0x1b4e4
movl 0x101ce(%rip), %edx     # 0x19554
xorl %ecx, %ecx
callq 0x742a
jmp 0x9313
leaq -0x30(%r14), %rax
cmpq $0x9, %rax
jbe 0x93a6
leaq -0xffb0(%r14), %rax
cmpq $0x9, %rax
ja 0x93e5
movl 0x142e0(%rip), %eax     # 0x1d68c
leal (%rax,%rax,4), %eax
andl $0xf, %r14d
leal (%r14,%rax,2), %eax
movl %eax, 0x142cf(%rip)     # 0x1d68c
jmp 0x931d
callq 0x71c9
xorb $0x1, %bpl
movb %bpl, 0x142be(%rip)     # 0x1d690
movzbl %bpl, %edi
movl $0x73, %esi
callq 0x9eea
jmp 0x9313
leaq -0x68(%r14), %rax
cmpq $0x5, %rax
jae 0x940a
movl $0x1d, %ecx
btl %eax, %ecx
jae 0x940a
leaq 0x91f0(%rip), %rax      # 0x125f0
movl -0x1a0(%rax,%r14,4), %r14d
jmp 0x942e
movl %r14d, %eax
notl %eax
testl $0xff50, %eax           # imm = 0xFF50
jne 0x9313
andl $0xffff00af, %r14d      # imm = 0xFFFF00AF
decl %r14d
cmpl $0x3, %r14d
jg 0x9313
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, 0x14231(%rip)     # 0x1d688
movl %r14d, %esi
andl $-0x2, %esi
cmpl $0x2, %esi
jne 0x9478
cmpl $0x3, %r14d
jne 0x9470
movl 0x1208a(%rip), %ecx     # 0x1b4f8
jmp 0x9478
movl 0x11096(%rip), %ecx     # 0x1a50c
decl %ecx
movl 0x1420e(%rip), %esi     # 0x1d68c
testl %esi, %esi
jne 0x9491
movl $0x1, 0x14200(%rip)     # 0x1d68c
movl $0x1, %esi
leaq 0x11070(%rip), %rdi     # 0x1a508
movl (%rax,%rdi), %r8d
cmpl %ecx, %r8d
jne 0x94c3
xorl %r9d, %r9d
xorl %r10d, %r10d
testl %ecx, %ecx
setne %r9b
setle %r10b
testb $0x2, %r14b
cmovel %r9d, %r10d
cmpb $0x1, %r10b
jne 0x9313
imull %edx, %esi
addl %r8d, %esi
movl %esi, (%rax,%rdi)
js 0x94de
testl %ecx, %ecx
jle 0x9306
cmpl %ecx, %esi
jle 0x9306
movl %ecx, (%rax,%rdi)
jmp 0x9306