// File: /workspace/github_c_makefile_star2_O2/bobrippling[P]tim/keys.c
// Function: keys_filter
int keys_filter(
		enum io io_m,
		char *struc, unsigned n_ents,
		unsigned st_off, unsigned st_sz,
		int mode_off)
{
#define STRUCT_STR(i, st, off) *(const char **)(struc + i * st_sz + st_off)
	int ret = -1;

	bool potential[n_ents];

	if(mode_off >= 0){
		enum buf_mode curmode = windows_cur()->ui_mode;

		for(unsigned i = 0; i < n_ents; i++){
			enum buf_mode mode = *(enum buf_mode *)(
					struc + i * st_sz + mode_off);

			potential[i] = mode & curmode;
		}
	}else{
		memset(potential, true, sizeof potential);
	}

	unsigned ch_idx = 0;
	char *sofar = NULL;
	size_t nsofar = 0;
	for(;; ch_idx++){
		bool raw;
		int ch = io_getch(io_m, &raw, /*map:*/ch_idx == 0); /* dl shouldn't map l */
		if(raw){
			/* raw char, doesn't match any, get out */
			memset(potential, 0, sizeof potential);
		}

		sofar = urealloc(sofar, ++nsofar);
		sofar[nsofar-1] = ch;

		unsigned npotential = 0;
		unsigned last_potential = 0;

		for(unsigned i = 0; i < sizeof potential; i++){
			if(potential[i]){
				const char *const kstr = STRUCT_STR(i, struc, str_off);
				size_t keys_len = strlen(kstr);

				if(ch_idx >= keys_len || kstr[ch_idx] != ch){
					potential[i] = false;
				}else{
					npotential++;
					last_potential = i;
				}
			}
		}

		switch(npotential){
			case 1:
			{
				/* only accept once we have the full string */
				const char *kstr = STRUCT_STR(last_potential, struc, str_off);
				if(ch_idx == strlen(kstr) - 1){
					ret = last_potential;
					goto out;
				}
				break;
			}
			case 0:
				for(size_t i = nsofar; i > 0; i--)
					io_ungetch(sofar[i - 1], false);
				goto out;
		}
	}

out:
	free(sofar);
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %r14d
movl %ecx, %r12d
movq %rsi, %rbx
movl %edi, -0x54(%rbp)
movl %edx, %r13d
movq %rsp, %rdi
leaq 0xf(%r13), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
testl %r9d, %r9d
movq %rdi, -0x38(%rbp)
js 0x68fa
movl %r9d, %r15d
callq 0xd170
movq -0x38(%rbp), %r8
movl 0x40(%rax), %eax
movl %r15d, %ecx
addq %rbx, %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rsi, %r13
je 0x6905
movl %edx, %edi
testl %eax, (%rcx,%rdi)
setne (%r8,%rsi)
incq %rsi
addl %r14d, %edx
jmp 0x68e3
pushq $0x1
popq %rsi
movq %r13, %rdx
callq 0x4240
movl %r12d, %eax
addq %rax, %rbx
movq %rbx, -0x68(%rbp)
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %edx, %edx
testl %r15d, %r15d
sete %dl
movl -0x54(%rbp), %edi
leaq -0x29(%rbp), %rsi
callq 0xbbc8
movl %eax, -0x44(%rbp)
cmpb $0x1, -0x29(%rbp)
jne 0x693f
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x4240
leaq 0x1(%r15), %rsi
movq %rbx, %rdi
movq %rsi, -0x78(%rbp)
callq 0x5660
movq %rax, -0x60(%rbp)
movl -0x44(%rbp), %ecx
movb %cl, (%rax,%r15)
movq %r15, -0x70(%rbp)
movl %r15d, %eax
movq %rax, -0x50(%rbp)
xorl %ebx, %ebx
xorl %r12d, %r12d
movl $0x0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x38(%rbp), %rax
cmpq %r12, %r13
je 0x69de
cmpb $0x1, (%rax,%r12)
jne 0x69d6
movq %r13, %r15
movl %r14d, %r13d
movl %ebx, %eax
movq -0x68(%rbp), %rcx
movq (%rcx,%rax), %r14
movq %r14, %rdi
callq 0x4170
cmpq -0x50(%rbp), %rax
jbe 0x69c7
movq -0x50(%rbp), %rax
movsbl (%r14,%rax), %eax
cmpl %eax, -0x44(%rbp)
jne 0x69c7
incl -0x40(%rbp)
movl %r12d, -0x3c(%rbp)
movl %r13d, %r14d
movq %r15, %r13
movq -0x38(%rbp), %rax
jmp 0x69d6
movq -0x38(%rbp), %rax
movb $0x0, (%rax,%r12)
movl %r13d, %r14d
movq %r15, %r13
incq %r12
addl %r14d, %ebx
jmp 0x697c
movl -0x40(%rbp), %eax
cmpl $0x1, %eax
je 0x69f0
testl %eax, %eax
movq -0x60(%rbp), %rbx
jne 0x6a15
jmp 0x6a1e
movl -0x3c(%rbp), %r15d
movl %r15d, %eax
imull %r14d, %eax
movq -0x68(%rbp), %rcx
movq (%rcx,%rax), %rdi
callq 0x4170
decq %rax
cmpq -0x50(%rbp), %rax
movq -0x60(%rbp), %rbx
je 0x6a3d
movq -0x78(%rbp), %r15
jmp 0x6914
pushq $-0x1
popq %r15
movq -0x70(%rbp), %r14
cmpq $-0x1, %r14
je 0x6a3d
movsbl (%rbx,%r14), %edi
xorl %esi, %esi
callq 0xbcd8
decq %r14
jmp 0x6a26
movq %rbx, %rdi
callq 0x4080
movl %r15d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq