// File: /workspace/github_c_makefile_star2_O3/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, %r15d
movl %ecx, %r13d
movl %edx, %ebx
movq %rsi, -0x50(%rbp)
movl %edi, -0x54(%rbp)
movl %edx, %r12d
movq %rsp, %rdi
leaq 0xf(%r12), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
testl %r9d, %r9d
movq %rdi, -0x38(%rbp)
js 0x6d02
movl %r9d, %r14d
callq 0xdaf0
movq -0x38(%rbp), %r8
testl %ebx, %ebx
je 0x6d0f
movl 0x40(%rax), %eax
movl %r14d, %ecx
addq -0x50(%rbp), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl %edx, %edi
testl %eax, (%rcx,%rdi)
setne (%r8,%rsi)
incq %rsi
addl %r15d, %edx
cmpq %rsi, %r12
jne 0x6ceb
jmp 0x6d0f
movl $0x1, %esi
movq %r12, %rdx
callq 0x4240
movl %r13d, %eax
addq %rax, -0x50(%rbp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movl %ebx, -0x3c(%rbp)
xorl %edx, %edx
testl %r13d, %r13d
sete %dl
movl -0x54(%rbp), %edi
leaq -0x29(%rbp), %rsi
callq 0xc3de
movl %eax, -0x48(%rbp)
cmpb $0x1, -0x29(%rbp)
jne 0x6d4a
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4240
leaq 0x1(%r13), %rsi
movq %r14, %rdi
movq %rsi, -0x70(%rbp)
callq 0x584a
movq %rax, %r14
movl -0x48(%rbp), %eax
movb %al, (%r14,%r13)
testl %ebx, %ebx
je 0x6e3e
movq %r14, -0x68(%rbp)
movq %r13, -0x60(%rbp)
movl %r13d, %eax
movq %rax, -0x78(%rbp)
xorl %ebx, %ebx
xorl %r13d, %r13d
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x38(%rbp), %rax
cmpb $0x1, (%rax,%r13)
jne 0x6de7
movq %r12, %r14
movl %r15d, %r12d
movl %ebx, %eax
movq -0x50(%rbp), %rcx
movq (%rcx,%rax), %r15
movq %r15, %rdi
callq 0x4170
cmpq -0x78(%rbp), %rax
jbe 0x6dd8
movq -0x78(%rbp), %rax
movsbl (%r15,%rax), %eax
cmpl %eax, -0x48(%rbp)
jne 0x6dd8
incl -0x44(%rbp)
movl %r13d, -0x40(%rbp)
movl %r12d, %r15d
movq %r14, %r12
movq -0x38(%rbp), %rax
jmp 0x6de7
movq -0x38(%rbp), %rax
movb $0x0, (%rax,%r13)
movl %r12d, %r15d
movq %r14, %r12
incq %r13
addl %r15d, %ebx
cmpq %r13, %r12
jne 0x6d92
movl -0x44(%rbp), %eax
cmpl $0x1, %eax
je 0x6e0b
testl %eax, %eax
movl -0x3c(%rbp), %ebx
movq -0x68(%rbp), %r14
movq -0x60(%rbp), %r13
jne 0x6e35
jmp 0x6e3e
movl -0x40(%rbp), %r13d
movl %r13d, %eax
imull %r15d, %eax
movq -0x50(%rbp), %rcx
movq (%rcx,%rax), %rdi
movl -0x60(%rbp), %ebx
callq 0x4170
decq %rax
cmpq %rbx, %rax
movl -0x3c(%rbp), %ebx
movq -0x68(%rbp), %r14
je 0x6e5d
movq -0x70(%rbp), %r13
jmp 0x6d1f
cmpq $0x0, -0x70(%rbp)
je 0x6e57
movsbl (%r14,%r13), %edi
xorl %esi, %esi
callq 0xc50d
addq $-0x1, %r13
jb 0x6e45
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %r14, %rdi
callq 0x4080
movl %r13d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq