// File: /workspace/github_c_makefile_star2_O3/kyx0r[P]xkeyboard/xkeyboard.c
// Function: do_button_event
void do_button_event(void)
{
	int i, j, mod, sc, lockmap = 0;
	char *name;
	static Window lastkeywin = 0;
	static Window lastmodkeywin = 0;
	Window win = myevent.xbutton.window;
	static Time laststamp = 0;
	Time stamp = myevent.xbutton.time;

	if (lastkeywin && myevent.type == ButtonRelease) {
		XSetWindowBorderWidth(dpy, lastkeywin, BORDER);
		win = lastkeywin;
		lastkeywin = 0;
	}
	if (lastkeywin) {	/* sanity */
		XSetWindowBorderWidth(dpy, lastkeywin, BORDER);
		lastkeywin = 0;
	}
	for (i = 0; i < MAX_KEYS; i++) {
		if (win == mykey[i])
			break;
	}
	if (i == MAX_KEYS) {
		/* unexpected event window */
		return;
	}
	sc = xk[i].scancode;
	for (j = 0; j < 8; j++) {
		if (modifiermap[j][0] == sc)
			break;
		if (modifiermap[j][1] == sc)
			break;
		if (modifiermap[j][2] == sc)
			break;
		if (modifiermap[j][3] == sc)
			break;
	}
	mod = 0;
	if (j < 8) {
		if (myevent.type == ButtonRelease)
			return;
		if(win == lastmodkeywin && stamp - laststamp < 500)
		{
			lockmod = !lockmod ? 1 : 0;
			GC tmp = mygcled;
			mygcled = mygcred;
			mygcred = tmp;
			if(lockmod)
			{
				name = scanname[0][sc];
				if(name)
					XDrawString(dpy, mykey[i], mygcled,
							8 * sn / sd, 26 * sn / sd,
							name, strlen(name));
			}
			do_control();
			return;
		} 
		laststamp = stamp;
		lastmodkeywin = mykey[i];
		mod = (1 << j);
		if (keystate & mod) {
                       keystate &= ~mod;
                       lockmap |= mod;
                       myevent.type = ButtonRelease;
                       i = modindex[j];
                       sc = xk[i].scancode;
                       name = scanname[0][sc];
                       if (name)
                               XDrawString(dpy, mykey[i], mygc,
                                       8 * sn / sd, 26 * sn / sd,
                                       name, strlen(name));
		} else { 
			keystate |= mod; 
			lockmap |= mod;
			modindex[j] = i;
			name = scanname[0][sc];
			if (name)
			{
				XDrawString(dpy, mykey[i], mygcled,
						8 * sn / sd, 26 * sn / sd,
						name, strlen(name));
			}
		}
	}
	if (myevent.type == ButtonPress) {
		if (!mod)
			lastkeywin = mykey[i];
		myevent.type = KeyPress;
		XSetWindowBorderWidth(dpy, mykey[i], 2);
	} else {
		myevent.type = KeyRelease;
		XSetWindowBorderWidth(dpy, mykey[i], BORDER);
	}
	if ((lockmap & 0x92) && myevent.type == KeyRelease)
		myevent.type = KeyPress;

	if(sendkey)
	{
		int revert;
		Window focus;
		int statemask = 0;

		if (keystate & 0x01) { 	/* shift */
			statemask |= ShiftMask;
		}
		if (keystate & 0x02) { 	/* caps */
			statemask |= LockMask;
		}
		if (keystate & 0x04) {	/* control */
			statemask |= ControlMask;
		}
		if (keystate & 0x08) { 	/* alt */
			statemask |= Mod1Mask;
		}
		if (keystate & 0x0f) {
			statemask |= Mod2Mask;
		}
		if (keystate & 0x20) { 
			statemask |= Mod3Mask;
		}
		if (keystate & 0x40) { 	/* win key */
			statemask |= Mod4Mask;
		}
		if (keystate & 0x80) { 	
			statemask |= Mod5Mask;
		}

		XGetInputFocus(dpy, &focus, &revert);

		XEvent event;
		event.xkey.type = myevent.type;
		event.xkey.window = focus;
		event.xkey.root = event.xkey.subwindow = None;
		event.xkey.time = 0;
		event.xkey.send_event = 1;
		event.xkey.keycode = sc;
		event.xkey.state = statemask;
		XSendEvent(dpy, focus, True, KeyPressMask | KeyReleaseMask, &event);
		XSync(dpy, 1);
	}
	else
	{
		XTestFakeKeyEvent(dpy, sc, (myevent.type == KeyPress), 0);
	}
	if (myevent.type == KeyPress && !(mod)) {
		/* automagically turn off some modifiers */
		if(!lockmod)
		{
			if (keystate & 0x01) { 	/* shift */
				i = modindex[0];
				sc = xk[i].scancode;
				XSetWindowBorderWidth(dpy, mykey[i], BORDER);
				XTestFakeKeyEvent(dpy, sc, 0, 0);
				name = scanname[0][sc];
				if (name)
					XDrawString(dpy, mykey[i], mygc,
							8 * sn / sd, 26 * sn / sd,
							name, strlen(name));
			}
			if (keystate & 0x04) {	/* control */
				i = modindex[2];
				sc = xk[i].scancode;
				XSetWindowBorderWidth(dpy, mykey[i], BORDER);
				XTestFakeKeyEvent(dpy, sc, 0, 0);
				name = scanname[0][sc];
				if (name)
					XDrawString(dpy, mykey[i], mygc,
							8 * sn / sd, 26 * sn / sd,
							name, strlen(name));
			}
			if (keystate & 0x08) { 	/* alt */
				i = modindex[3];
				sc = xk[i].scancode;
				XSetWindowBorderWidth(dpy, mykey[i], BORDER);
				XTestFakeKeyEvent(dpy, sc, 0, 0);
				name = scanname[0][sc];
				if (name)
					XDrawString(dpy, mykey[i], mygc,
							8 * sn / sd, 26 * sn / sd,
							name, strlen(name));
			}
			keystate &= ~0x0d;
		}
	}
	if (lockmap & 0x92) {	/* Need to generate keyup events */
		if (lockmap & 0x02) {	/* Lock */
			XTestFakeKeyEvent(dpy,
				xk[modindex[1]].scancode, 0, 0);
		}
		if (lockmap & 0x10) {	/* Mod1 */
			XTestFakeKeyEvent(dpy,
				xk[modindex[4]].scancode, 0, 0);
		}
		if (lockmap & 0x80) {	/* Mod5 */
			XTestFakeKeyEvent(dpy,
				xk[modindex[7]].scancode, 0, 0);
		}
	}
	XFlush(dpy);
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq 0x195ce(%rip), %r15     # 0x1d190
movq 0x195df(%rip), %r14     # 0x1d1a8
movq 0x19660(%rip), %rsi     # 0x1d230
testq %rsi, %rsi
je 0x3bf5
cmpl $0x5, 0x19594(%rip)     # 0x1d170
jne 0x3bf5
movq 0x14c13(%rip), %rdi     # 0x187f8
xorl %edx, %edx
callq 0x21c0
movq 0x1963d(%rip), %r15     # 0x1d230
jmp 0x3c08
testq %rsi, %rsi
je 0x3c13
movq 0x14bf7(%rip), %rdi     # 0x187f8
xorl %edx, %edx
callq 0x21c0
movq $0x0, 0x1961d(%rip) # 0x1d230
leaq 0x14f46(%rip), %rax     # 0x18b60
leaq 0x471f(%rip), %rcx      # 0x8340
xorl %r12d, %r12d
cmpq (%rax), %r15
je 0x3c3f
incq %r12
addq $0x8, %rax
addq $0x18, %rcx
cmpq $0x69, %r12
jne 0x3c24
jmp 0x4337
movl (%rcx), %ebx
cmpl %ebx, 0x19329(%rip)     # 0x1cf70
jne 0x3d2d
movb $0x1, %r13b
xorl %ecx, %ecx
cmpl $0x5, 0x19517(%rip)     # 0x1d170
je 0x4337
movslq %ebx, %rdx
cmpq 0x195cf(%rip), %r15     # 0x1d238
jne 0x3d75
movq %r14, %rsi
subq 0x195c7(%rip), %rsi     # 0x1d240
cmpq $0x1f3, %rsi            # imm = 0x1F3
ja 0x3d75
xorl %ecx, %ecx
cmpl $0x0, 0x195b9(%rip)     # 0x1d248
sete %cl
movl %ecx, 0x195b0(%rip)     # 0x1d248
movq 0x14d81(%rip), %rcx     # 0x18a20
movq 0x14d82(%rip), %rbx     # 0x18a28
movq %rbx, 0x14d73(%rip)     # 0x18a20
movq %rcx, 0x14d74(%rip)     # 0x18a28
jne 0x3d17
leaq 0x15293(%rip), %rcx     # 0x18f50
movq (%rcx,%rdx,8), %r14
testq %r14, %r14
je 0x3d17
movq 0x14b2b(%rip), %r15     # 0x187f8
movq (%rax), %r12
movl 0x14d5a(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x14d51(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, %ebp
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %ecx, %eax
cltd
idivl %esi
movl %eax, %r13d
movq %r14, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
movl %r13d, %r8d
movq %r14, %r9
callq 0x2270
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3a5e
movb $0x1, %r13b
leaq 0x1923d(%rip), %rdx     # 0x1cf74
xorl %ecx, %ecx
cmpl %ebx, (%rdx)
je 0x3c52
cmpl %ebx, 0x4(%rdx)
je 0x3c52
cmpl %ebx, 0x8(%rdx)
je 0x3c52
cmpq $0x7, %rcx
je 0x3f0a
setb %r13b
leaq 0x40(%rdx), %rsi
incq %rcx
cmpl %ebx, 0x3c(%rdx)
movq %rsi, %rdx
jne 0x3d39
jmp 0x3c52
movq %r14, 0x194c4(%rip)     # 0x1d240
movl $0x1, %esi
shll %cl, %esi
movq (%rax), %r14
movq %r14, 0x194ab(%rip)     # 0x1d238
movl 0x151b5(%rip), %eax     # 0x18f48
btl %ecx, %eax
movl %esi, 0xc(%rsp)
jae 0x3e5c
movl %esi, %edx
notl %edx
andl %edx, %eax
movl %eax, 0x1519c(%rip)     # 0x18f48
movl $0x5, 0x193ba(%rip)     # 0x1d170
leaq 0x19193(%rip), %rax     # 0x1cf50
movl (%rax,%rcx,4), %r12d
movslq %r12d, %rax
leaq (%rax,%rax,2), %rcx
leaq 0x4571(%rip), %rdx      # 0x8340
movslq (%rdx,%rcx,8), %rbx
leaq 0x15176(%rip), %rcx     # 0x18f50
movq (%rcx,%rbx,8), %r14
testq %r14, %r14
je 0x3ee5
movq 0x14a0a(%rip), %rcx     # 0x187f8
movq %rcx, 0x20(%rsp)
leaq 0x14d66(%rip), %rcx     # 0x18b60
movq (%rcx,%rax,8), %rax
movq %rax, 0x18(%rsp)
movq 0x14c06(%rip), %rax     # 0x18a10
movq %rax, 0x10(%rsp)
movl 0x14c1b(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x14c12(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, %ebp
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %ecx, %eax
cltd
idivl %esi
movl %eax, %r15d
movq %r14, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
movl %ebp, %ecx
movl %r15d, %r8d
movq %r14, %r9
jmp 0x3ee0
orl %esi, %eax
movl %eax, 0x150e4(%rip)     # 0x18f48
leaq 0x190e5(%rip), %rax     # 0x1cf50
movl %r12d, (%rax,%rcx,4)
leaq 0x150da(%rip), %rax     # 0x18f50
movq (%rax,%rdx,8), %r15
testq %r15, %r15
je 0x3ee5
movq 0x14972(%rip), %rax     # 0x187f8
movq %rax, 0x18(%rsp)
movq 0x14b8e(%rip), %rax     # 0x18a20
movq %rax, 0x20(%rsp)
movl 0x14b93(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x14b8a(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, 0x10(%rsp)
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %ecx, %eax
cltd
idivl %esi
movl %eax, %ebp
movq %r15, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movq 0x20(%rsp), %rdx
movl 0x10(%rsp), %ecx
movl %ebp, %r8d
movq %r15, %r9
callq 0x2270
cmpl $0x4, 0x19284(%rip)     # 0x1d170
jne 0x3f03
movslq %r12d, %rax
leaq 0x14c68(%rip), %rcx     # 0x18b60
movq (%rcx,%rax,8), %rsi
movl 0xc(%rsp), %r15d
jmp 0x3f23
movl 0xc(%rsp), %r15d
jmp 0x3f41
cmpl $0x4, 0x1925f(%rip)     # 0x1d170
jne 0x3f3b
movq (%rax), %rsi
movq %rsi, 0x19313(%rip)     # 0x1d230
xorl %r13d, %r13d
xorl %r15d, %r15d
movl $0x2, 0x19243(%rip)     # 0x1d170
movq 0x148c4(%rip), %rdi     # 0x187f8
movl $0x2, %edx
jmp 0x3f62
xorl %r13d, %r13d
xorl %r15d, %r15d
movl $0x3, 0x19225(%rip)     # 0x1d170
movq 0x148a6(%rip), %rdi     # 0x187f8
movslq %r12d, %rax
leaq 0x14c04(%rip), %rcx     # 0x18b60
movq (%rcx,%rax,8), %rsi
xorl %edx, %edx
callq 0x21c0
testb $-0x6e, %r15b
setne %cl
movl 0x191fc(%rip), %eax     # 0x1d170
cmpl $0x3, %eax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3f90
movl $0x2, 0x191e5(%rip)     # 0x1d170
movl $0x2, %eax
cmpl $0x0, 0x192b5(%rip)     # 0x1d24c
je 0x402f
movl 0x14fa5(%rip), %ebp     # 0x18f48
movl %ebp, %eax
andl $0xf, %eax
leal 0x10(%rax), %ecx
testl %eax, %eax
cmovel %eax, %ecx
andl $0xe0, %ebp
orl %ecx, %ebp
movq 0x14839(%rip), %rdi     # 0x187f8
leaq 0x30(%rsp), %r14
leaq 0x2c(%rsp), %rdx
movq %r14, %rsi
callq 0x23c0
movl 0x19199(%rip), %eax     # 0x1d170
leaq 0x38(%rsp), %r8
movl %eax, (%r8)
movq (%r14), %rsi
movq %rsi, 0x20(%r8)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r8)
movq $0x0, 0x38(%r8)
movl $0x1, 0x10(%r8)
movl %ebx, 0x54(%r8)
movl %ebp, 0x50(%r8)
movq 0x147eb(%rip), %rdi     # 0x187f8
movl $0x3, %ecx
movl $0x1, %edx
callq 0x2030
movq 0x147d5(%rip), %rdi     # 0x187f8
movl $0x1, %esi
callq 0x2250
jmp 0x4047
movq 0x147c2(%rip), %rdi     # 0x187f8
xorl %edx, %edx
cmpl $0x2, %eax
sete %dl
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x22a0
cmpl $0x2, 0x19122(%rip)     # 0x1d170
setne %al
orb %al, %r13b
testb $0x1, %r13b
jne 0x42a1
cmpl $0x0, 0x191e3(%rip)     # 0x1d248
jne 0x42a1
movl %r15d, 0xc(%rsp)
testb $0x1, 0x14ed1(%rip)     # 0x18f48
je 0x4127
movslq 0x18ecc(%rip), %r15     # 0x1cf50
leaq (%r15,%r15,2), %rax
leaq 0x42b1(%rip), %rcx      # 0x8340
movslq (%rcx,%rax,8), %rbx
movq 0x1475e(%rip), %rdi     # 0x187f8
leaq 0x14abf(%rip), %r12     # 0x18b60
movq (%r12,%r15,8), %rsi
xorl %edx, %edx
callq 0x21c0
movq 0x14745(%rip), %rdi     # 0x187f8
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
leaq 0x14e8b(%rip), %rax     # 0x18f50
movq (%rax,%rbx,8), %rbx
testq %rbx, %rbx
je 0x4127
movq 0x14723(%rip), %r14     # 0x187f8
movq (%r12,%r15,8), %r15
movq 0x14930(%rip), %r12     # 0x18a10
movl 0x1494a(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x14941(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, %ebp
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %ecx, %eax
cltd
idivl %esi
movl %eax, %r13d
movq %rbx, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %ebp, %ecx
movl %r13d, %r8d
movq %rbx, %r9
callq 0x2270
testb $0x4, 0x14e1a(%rip)     # 0x18f48
je 0x41de
movslq 0x18e1d(%rip), %r15     # 0x1cf58
leaq (%r15,%r15,2), %rax
leaq 0x41fa(%rip), %rcx      # 0x8340
movslq (%rcx,%rax,8), %rbx
movq 0x146a7(%rip), %rdi     # 0x187f8
leaq 0x14a08(%rip), %r12     # 0x18b60
movq (%r12,%r15,8), %rsi
xorl %edx, %edx
callq 0x21c0
movq 0x1468e(%rip), %rdi     # 0x187f8
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
leaq 0x14dd4(%rip), %rax     # 0x18f50
movq (%rax,%rbx,8), %rbx
testq %rbx, %rbx
je 0x41de
movq 0x1466c(%rip), %r14     # 0x187f8
movq (%r12,%r15,8), %r15
movq 0x14879(%rip), %r12     # 0x18a10
movl 0x14893(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x1488a(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, %ebp
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %ecx, %eax
cltd
idivl %esi
movl %eax, %r13d
movq %rbx, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %ebp, %ecx
movl %r13d, %r8d
movq %rbx, %r9
callq 0x2270
testb $0x8, 0x14d63(%rip)     # 0x18f48
je 0x4295
movslq 0x18d6a(%rip), %r15     # 0x1cf5c
leaq (%r15,%r15,2), %rax
leaq 0x4143(%rip), %rcx      # 0x8340
movslq (%rcx,%rax,8), %rbx
movq 0x145f0(%rip), %rdi     # 0x187f8
leaq 0x14951(%rip), %r12     # 0x18b60
movq (%r12,%r15,8), %rsi
xorl %edx, %edx
callq 0x21c0
movq 0x145d7(%rip), %rdi     # 0x187f8
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
leaq 0x14d1d(%rip), %rax     # 0x18f50
movq (%rax,%rbx,8), %rbx
testq %rbx, %rbx
je 0x4295
movq 0x145b5(%rip), %r14     # 0x187f8
movq (%r12,%r15,8), %r15
movq 0x147c2(%rip), %r12     # 0x18a10
movl 0x147dc(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x147d3(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, %ebp
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %ecx, %eax
cltd
idivl %esi
movl %eax, %r13d
movq %rbx, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %ebp, %ecx
movl %r13d, %r8d
movq %rbx, %r9
callq 0x2270
andb $-0xe, 0x14cac(%rip)    # 0x18f48
movl 0xc(%rsp), %r15d
testb $-0x6e, %r15b
je 0x432b
testb $0x2, %r15b
je 0x42d6
movq 0x14540(%rip), %rdi     # 0x187f8
movslq 0x18c95(%rip), %rax     # 0x1cf54
leaq (%rax,%rax,2), %rax
leaq 0x4076(%rip), %rcx      # 0x8340
movl (%rcx,%rax,8), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
testb $0x10, %r15b
je 0x4301
movq 0x14515(%rip), %rdi     # 0x187f8
movslq 0x18c76(%rip), %rax     # 0x1cf60
leaq (%rax,%rax,2), %rax
leaq 0x404b(%rip), %rcx      # 0x8340
movl (%rcx,%rax,8), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
testb %r15b, %r15b
jns 0x432b
movq 0x144eb(%rip), %rdi     # 0x187f8
movslq 0x18c58(%rip), %rax     # 0x1cf6c
leaq (%rax,%rax,2), %rax
leaq 0x4021(%rip), %rcx      # 0x8340
movl (%rcx,%rax,8), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
movq 0x144c6(%rip), %rdi     # 0x187f8
callq 0x22f0
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq