// File: /workspace/github_c_makefile_star2_O1/baskerville[P]sxhkd/src/types.c
// Function: find_hotkey
hotkey_t *find_hotkey(xcb_keysym_t keysym, xcb_button_t button, uint16_t modfield, uint8_t event_type, bool *replay_event)
{
	int num_active = 0;
	int num_locked = 0;
	hotkey_t *result = NULL;

	for (hotkey_t *hk = hotkeys_head; hk != NULL; hk = hk->next) {
		chain_t *c = hk->chain;
		if ((chained && c->state == c->head) || (locked && c->state != c->tail))
			continue;
		if (match_chord(c->state, event_type, keysym, button, modfield)) {
			if (status_fifo != NULL && num_active == 0) {
				if (!chained) {
					snprintf(progress, sizeof(progress), "%s", c->state->repr);
				} else {
					strncat(progress, ";", sizeof(progress) - strlen(progress) - 1);
					strncat(progress, c->state->repr, sizeof(progress) - strlen(progress) - 1);
				}
				put_status(HOTKEY_PREFIX, progress);
			}
			if (replay_event != NULL && c->state->replay_event)
				*replay_event = true;
			if (c->state->lock_chain) {
				num_locked += 1;
				if (timeout > 0)
					alarm(0);
			}
			if (c->state == c->tail) {
				if (hk->cycle != NULL) {
					unsigned char delay = hk->cycle->delay;
					hk->cycle->delay = (delay == 0 ? hk->cycle->period - 1 : delay - 1);
					if (delay == 0)
						result = hk;
					continue;
				}
				if (chained && !locked)
					abort_chain();
				return hk;
			} else {
				c->state = c->state->next;
				num_active++;
				grab_chord(c->state);
			}
		} else if (chained) {
			if (!locked && c->state->event_type == event_type)
				c->state = c->head;
			else
				num_active++;
		}
	}

	if (result != NULL)
		return result;

	if (num_locked > 0) {
		locked = true;
	}

	if (!chained) {
		if (num_active > 0) {
			chained = true;
			put_status(BEGIN_CHAIN_PREFIX, "Begin chain");
			grab_chord(abort_chord);
		}
	} else if (num_active == 0 || match_chord(abort_chord, event_type, keysym, button, modfield)) {
		abort_chain();
		return find_hotkey(keysym, button, modfield, event_type, replay_event);
	}
	if (chained && !locked && timeout > 0)
		alarm(timeout);
	PRINTF("num active %i\n", num_active);

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x28(%rsp)
movl %ecx, %ebp
movl %edi, %r12d
leaq 0x12ba2(%rip), %r9      # 0x23481
movl %edx, 0x4(%rsp)
movl %esi, (%rsp)
leaq 0x12bc3(%rip), %rax     # 0x234b0
movq (%rax), %r14
testq %r14, %r14
sete %bl
je 0x10b99
movq $0x0, 0x8(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movq (%r14), %r13
movb (%r9), %al
cmpb $0x1, %al
jne 0x1092e
movq 0x10(%r13), %rcx
cmpq (%r13), %rcx
je 0x10b2a
leaq 0x12b4b(%rip), %rcx     # 0x23480
movb (%rcx), %r10b
cmpb $0x1, %r10b
jne 0x10952
movq 0x10(%r13), %rcx
movl $0x4, %r15d
cmpq 0x8(%r13), %rcx
jne 0x10b30
movq 0x10(%r13), %rcx
testq %rcx, %rcx
setne %r11b
je 0x109a4
movq %rcx, %rdi
cmpb %bpl, 0x108(%rdi)
jne 0x10994
cmpl %r12d, 0x100(%rdi)
jne 0x10994
cmpb %sil, 0x104(%rdi)
jne 0x10994
movzwl 0x106(%rdi), %r8d
cmpl $0x8000, %r8d           # imm = 0x8000
je 0x109a4
cmpw %dx, %r8w
je 0x109a4
movq 0x118(%rdi), %rdi
testq %rdi, %rdi
setne %r11b
jne 0x10962
testb $0x1, %r11b
je 0x10a21
leaq 0x1298f(%rip), %rdi     # 0x23340
cmpq $0x0, (%rdi)
setne %dil
cmpl $0x0, 0x8(%rsp)
sete %r8b
andb %dil, %r8b
cmpb $0x1, %r8b
jne 0x10a82
testb %al, %al
leaq 0x12ae8(%rip), %rdi     # 0x234c0
je 0x10a58
movq %rdi, %r15
callq 0xe130
movl $0x2ff, %edx            # imm = 0x2FF
subq %rax, %rdx
movq %r15, %rdi
leaq 0x7a2c(%rip), %rsi      # 0x18420
callq 0xe1a0
movq 0x10(%r13), %rax
movq %rax, 0x30(%rsp)
movq %r15, %rdi
callq 0xe130
movl $0x2ff, %edx            # imm = 0x2FF
subq %rax, %rdx
movq %r15, %rdi
movq 0x30(%rsp), %rsi
callq 0xe1a0
jmp 0x10a6e
xorl %r15d, %r15d
testb %al, %al
je 0x10b30
testb %r10b, %r10b
jne 0x10a47
cmpb %bpl, 0x108(%rcx)
jne 0x10a47
movq (%r13), %rax
movq %rax, 0x10(%r13)
jmp 0x10b30
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
jmp 0x10b30
movl $0x300, %esi            # imm = 0x300
leaq 0x16c3(%rip), %rdx      # 0x12127
xorl %eax, %eax
movq %rdi, %r15
callq 0xe170
movl $0x48, %edi
movq %r15, %rsi
callq 0xee73
movl 0x4(%rsp), %edx
movl (%rsp), %esi
cmpq $0x0, 0x28(%rsp)
je 0x10a9f
movq 0x10(%r13), %rax
cmpb $0x1, 0x109(%rax)
jne 0x10a9f
movq 0x28(%rsp), %rax
movb $0x1, (%rax)
movq 0x10(%r13), %rax
cmpb $0x1, 0x10a(%rax)
jne 0x10aca
incl 0x14(%rsp)
leaq 0x1289d(%rip), %rax     # 0x23354
cmpl $0x0, (%rax)
jle 0x10aca
xorl %edi, %edi
callq 0xe1c0
movl (%rsp), %esi
movl 0x4(%rsp), %edx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rax
je 0x10b03
movq 0x110(%rax), %rdi
movq %rdi, 0x10(%r13)
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
callq 0xef37
movl (%rsp), %esi
movl 0x4(%rsp), %edx
xorl %r15d, %r15d
leaq 0x12980(%rip), %r9      # 0x23481
jmp 0x10b30
movq 0x210(%r14), %rax
testq %rax, %rax
je 0x10b4b
movzbl 0x4(%rax), %ecx
testl %ecx, %ecx
leaq 0x12965(%rip), %r9      # 0x23481
jne 0x10b25
movl (%rax), %ecx
movq %r14, 0x20(%rsp)
decl %ecx
movl %ecx, 0x4(%rax)
movl $0x4, %r15d
testb $0x3, %r15b
jne 0x10b84
movq 0x218(%r14), %r14
testq %r14, %r14
sete %bl
jne 0x10916
jmp 0x10b84
movl $0x1, %r15d
leaq 0x12929(%rip), %r9      # 0x23481
cmpb $0x1, (%r9)
jne 0x10b7d
leaq 0x1291b(%rip), %rax     # 0x23480
cmpb $0x0, (%rax)
jne 0x10b7d
callq 0x10ce8
movl (%rsp), %esi
movl 0x4(%rsp), %edx
leaq 0x12904(%rip), %r9      # 0x23481
movq %r14, 0x18(%rsp)
jmp 0x10b30
cmpl $0x0, 0x14(%rsp)
setg %dil
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %rax
jmp 0x10ba6
xorl %edi, %edi
xorl %ecx, %ecx
movq $0x0, 0x8(%rsp)
testb $0x1, %bl
je 0x10c95
movq %rcx, %rax
testq %rcx, %rcx
jne 0x10c95
testb %dil, %dil
je 0x10bca
leaq 0x128b9(%rip), %rax     # 0x23480
movb $0x1, (%rax)
cmpb $0x0, (%r9)
je 0x10c3d
cmpl $0x0, 0x8(%rsp)
je 0x10c25
leaq 0x12892(%rip), %rax     # 0x23470
movq (%rax), %rcx
testq %rcx, %rcx
setne %al
je 0x10c21
cmpb %bpl, 0x108(%rcx)
jne 0x10c18
cmpl %r12d, 0x100(%rcx)
jne 0x10c18
cmpb %sil, 0x104(%rcx)
jne 0x10c18
movzwl 0x106(%rcx), %edi
cmpl $0x8000, %edi           # imm = 0x8000
je 0x10c21
cmpw %dx, %di
je 0x10c21
movq 0x118(%rcx), %rcx
jmp 0x10be1
testb $0x1, %al
je 0x10c6f
callq 0x10ce8
movl (%rsp), %esi
movl 0x4(%rsp), %edx
leaq 0x12849(%rip), %r9      # 0x23481
jmp 0x108e6
cmpl $0x0, 0x8(%rsp)
jle 0x10c6f
movb $0x1, (%r9)
leaq 0x77d3(%rip), %rsi      # 0x18422
movl $0x42, %edi
callq 0xee73
leaq 0x12810(%rip), %rax     # 0x23470
movq (%rax), %rdi
callq 0xef37
leaq 0x12812(%rip), %r9      # 0x23481
cmpb $0x1, (%r9)
jne 0x10c93
leaq 0x126d8(%rip), %rax     # 0x23354
movl (%rax), %edi
testl %edi, %edi
setle %al
leaq 0x127f6(%rip), %rcx     # 0x23480
orb (%rcx), %al
jne 0x10c93
callq 0xe1c0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq