// File: /workspace/github_c_makefile_star2_O3/9wm[P]9wm/grab.c
// Function: menuhit
int
menuhit(XButtonEvent * e, Menu * m)
{
	XEvent ev;
	int i, n, cur, old, wide, high, status, drawn, warp;
	int x, y, dx, dy, xmax, ymax;
	int tx, ty;
	ScreenInfo *s;

	if (font == 0)
		return -1;
	s = getscreen(e->root);
	if (s == 0 || e->window == s->menuwin)	/* ugly event mangling */
		return -1;

	dx = 0;
	for (n = 0; m->item[n]; n++) {
		wide = XTextWidth(font, m->item[n], strlen(m->item[n])) + 4;
		if (wide > dx)
			dx = wide;
	}
	wide = dx;
	cur = m->lasthit;
	if (cur >= n)
		cur = n - 1;

	high = font->ascent + font->descent + 1;
	dy = n * high;
	x = e->x - wide / 2;
	y = e->y - cur * high - high / 2;
	warp = 0;
	xmax = DisplayWidth(dpy, s->num);
	ymax = DisplayHeight(dpy, s->num);
	if (x < 0) {
		e->x -= x;
		x = 0;
		warp++;
	}
	if (x + wide >= xmax) {
		e->x -= x + wide - xmax;
		x = xmax - wide;
		warp++;
	}
	if (y < 0) {
		e->y -= y;
		y = 0;
		warp++;
	}
	if (y + dy >= ymax) {
		e->y -= y + dy - ymax;
		y = ymax - dy;
		warp++;
	}
	if (warp)
		setmouse(e->x, e->y, s);
	XMoveResizeWindow(dpy, s->menuwin, x, y, dx, dy);
	XSelectInput(dpy, s->menuwin, MenuMask);
	XMapRaised(dpy, s->menuwin);
	status = grab(s->menuwin, None, MenuGrabMask, None, e->time);
	if (status != GrabSuccess) {
		/*
		 * graberror("menuhit", status); 
		 */
		XUnmapWindow(dpy, s->menuwin);
		return -1;
	}
	drawn = 0;
	for (;;) {
		XMaskEvent(dpy, MenuMask, &ev);
		switch (ev.type) {
		default:
			fprintf(stderr, "9wm: menuhit: unknown ev.type %d\n", ev.type);
			break;
		case ButtonPress:
			break;
		case ButtonRelease:
			if (ev.xbutton.button != e->button)
				break;
			x = ev.xbutton.x;
			y = ev.xbutton.y;
			i = y / high;
			if (cur >= 0 && y >= cur * high - 3 && y < (cur + 1) * high + 3)
				i = cur;
			if (x < 0 || x > wide || y < -3)
				i = -1;
			else if (i < 0 || i >= n)
				i = -1;
			else
				m->lasthit = i;
			if (!nobuttons(&ev.xbutton))
				i = -1;
			ungrab(&ev.xbutton);
			XUnmapWindow(dpy, s->menuwin);
			return i;
		case MotionNotify:
			if (!drawn)
				break;
			x = ev.xbutton.x;
			y = ev.xbutton.y;
			old = cur;
			cur = y / high;
			if (old >= 0 && y >= old * high - 3 && y < (old + 1) * high + 3)
				cur = old;
			if (x < 0 || x > wide || y < -3)
				cur = -1;
			else if (cur < 0 || cur >= n)
				cur = -1;
			if (cur == old)
				break;
			if (old >= 0 && old < n)
				XFillRectangle(dpy, s->menuwin, s->gc, 0, old * high, wide, high);
			if (cur >= 0 && cur < n)
				XFillRectangle(dpy, s->menuwin, s->gc, 0, cur * high, wide, high);
			break;
		case Expose:
			XClearWindow(dpy, s->menuwin);
			for (i = 0; i < n; i++) {
				char *item = m->item[i];

				if (i < 5) {
					tx = (wide - XTextWidth(font, item, strlen(item))) / 2;
				} else {
					tx = 1;
				}
				ty = i * high + font->ascent + 1;
				XDrawString(dpy, s->menuwin, s->gc, tx, ty, item, strlen(item));
			}
			if (cur >= 0 && cur < n)
				XFillRectangle(dpy, s->menuwin, s->gc, 0, cur * high, wide, high);
			drawn = 1;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
leaq 0x5da6(%rip), %rax      # 0xb960
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq $0x0, (%rax)
je 0x6125
movq %rsi, %r13
movq %rdi, %rbx
movq 0x28(%rdi), %rdi
xorl %eax, %eax
callq 0x3381
testq %rax, %rax
je 0x6125
movq 0x20(%rbx), %rcx
cmpq 0x10(%rax), %rcx
je 0x6125
movq %rax, 0x30(%rsp)
movq (%r13), %rax
movq (%rax), %r15
xorl %r14d, %r14d
movl $0x0, %ecx
movl $0x0, %r12d
testq %r15, %r15
movq %rbx, 0x38(%rsp)
je 0x5c64
xorl %r12d, %r12d
xorl %ecx, %ecx
leaq 0x5d3f(%rip), %r14      # 0xb960
movl %ecx, %ebp
movq %r12, %rbx
movq (%r14), %r12
movq %r15, %rdi
callq 0x2140
movq %r12, %rdi
movq %rbx, %r12
movq %r15, %rsi
movl %eax, %edx
callq 0x24e0
movl %eax, %ecx
addl $0x4, %ecx
cmpl %ebp, %ecx
cmovlel %ebp, %ecx
movq (%r13), %rax
movq 0x8(%rax,%rbx,8), %r15
incq %r12
testq %r15, %r15
jne 0x5c21
movq 0x38(%rsp), %rbx
xorl %r14d, %r14d
movl 0x10(%r13), %eax
leal -0x1(%r12), %r9d
cmpl %r12d, %eax
cmovll %eax, %r9d
leaq 0x5ce5(%rip), %rax      # 0xb960
movq (%rax), %rax
movq %rcx, %r11
movl 0x58(%rax), %ecx
movl 0x5c(%rax), %eax
leal (%rcx,%rax), %edx
leal 0x1(%rcx,%rax), %edi
movl 0x40(%rbx), %ecx
movl 0x44(%rbx), %eax
movl %r11d, %r8d
shrl %r8d
movl %edi, %esi
movq %r9, 0x20(%rsp)
imull %r9d, %esi
movl %eax, %ebp
subl %esi, %ebp
movl %edi, 0x1c(%rsp)
movl %edi, %esi
shrl $0x1f, %esi
leal (%rsi,%rdx), %r9d
incl %r9d
sarl %r9d
movq 0x30(%rsp), %rdx
movslq (%rdx), %rdx
shlq $0x7, %rdx
leaq 0x5cb9(%rip), %rsi      # 0xb988
movq (%rsi), %rdi
movq 0xe8(%rdi), %r10
movl 0x18(%r10,%rdx), %esi
movl 0x1c(%r10,%rdx), %edx
movl %ecx, %r10d
subl %r8d, %r10d
jns 0x5cfb
movl %r8d, 0x40(%rbx)
xorl %r10d, %r10d
movl $0x1, %r14d
movl %r8d, %ecx
subl %r9d, %ebp
leal (%r10,%r11), %r9d
movl %esi, %r8d
subl %r9d, %r8d
movq %r11, 0x28(%rsp)
jg 0x5d21
addl %r8d, %ecx
movl %ecx, 0x40(%rbx)
subl %r11d, %esi
movl $0x1, %r14d
movl %esi, %r10d
movl 0x1c(%rsp), %esi
movl %esi, %r9d
imull %r12d, %r9d
testl %ebp, %ebp
js 0x5d4b
leal (%r9,%rbp), %esi
cmpl %edx, %esi
jge 0x5d5a
testl %r14d, %r14d
jne 0x5d6b
movq 0x30(%rsp), %r15
leaq 0x5c3f(%rip), %r14      # 0xb988
jmp 0x5db3
subl %ebp, %eax
movl %eax, 0x44(%rbx)
xorl %ebp, %ebp
movl %r9d, %esi
cmpl %r9d, %edx
jg 0x5d6b
movl %edx, %r8d
subl %esi, %r8d
addl %r8d, %eax
movl %eax, 0x44(%rbx)
subl %r9d, %edx
movl %edx, %ebp
movq 0x30(%rsp), %r15
movq 0x8(%r15), %rdx
movl %eax, 0x10(%rsp)
movl %ecx, 0x8(%rsp)
movl $0x0, (%rsp)
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbp, %rbx
movq %r9, %rbp
xorl %r9d, %r9d
movq %r10, %r14
callq 0x2230
movq %rbp, %r9
movq %rbx, %rbp
movq 0x38(%rsp), %rbx
movq %r14, %r10
leaq 0x5bd8(%rip), %r14      # 0xb988
movq (%r14), %rdi
movq 0x10(%r15), %rsi
movl %r10d, %edx
movl %ebp, %ecx
movq 0x28(%rsp), %r8
callq 0x21a0
movq (%r14), %rdi
movq 0x10(%r15), %rsi
movl $0xa00c, %edx           # imm = 0xA00C
callq 0x23c0
movq (%r14), %rdi
movq 0x10(%r15), %rsi
callq 0x2350
movq 0x10(%r15), %rdi
movl 0x38(%rbx), %r8d
xorl %esi, %esi
movl $0x2200c, %edx          # imm = 0x2200C
xorl %ecx, %ecx
callq 0x5aba
testl %eax, %eax
je 0x5e13
movq (%r14), %rdi
movq 0x10(%r15), %rsi
callq 0x2430
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x6125
movl %r12d, %eax
movq %rax, 0x48(%rsp)
xorl %ebx, %ebx
movq %r13, 0x50(%rsp)
movq %r12, 0x40(%rsp)
movq (%r14), %rdi
movl $0xa00c, %esi           # imm = 0xA00C
leaq 0x68(%rsp), %rdx
callq 0x2600
movl 0x68(%rsp), %edx
leal -0x4(%rdx), %eax
cmpl $0x8, %eax
ja 0x6080
leaq 0x1904(%rip), %rcx      # 0x7754
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0xbc(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl 0x54(%rcx), %eax
jne 0x5e27
jmp 0x609d
movq (%r14), %rdi
movq 0x10(%r15), %rsi
callq 0x2090
testl %r12d, %r12d
je 0x5f36
movl $0x1, %r15d
xorl %ebx, %ebx
movq (%r13), %rax
movq (%rax,%rbx,8), %r12
movl $0x1, %ebp
cmpq $0x4, %rbx
leaq 0x5abc(%rip), %r14      # 0xb960
ja 0x5ece
movq (%r14), %rbp
movq %r12, %rdi
callq 0x2140
movq %rbp, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x24e0
movq 0x28(%rsp), %rcx
subl %eax, %ecx
movl %ecx, %ebp
shrl $0x1f, %ebp
addl %ecx, %ebp
sarl %ebp
movq (%r14), %rax
movl 0x58(%rax), %r13d
addl %r15d, %r13d
leaq 0x5aa9(%rip), %rax      # 0xb988
movq (%rax), %r14
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x60(%rsp)
movq 0x20(%rax), %rax
movq %rax, 0x58(%rsp)
movq %r12, %rdi
callq 0x2140
movl %eax, (%rsp)
movq %r14, %rdi
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
movl %ebp, %ecx
movl %r13d, %r8d
movq %r12, %r9
callq 0x2400
incq %rbx
addl 0x1c(%rsp), %r15d
cmpq %rbx, 0x48(%rsp)
movq 0x50(%rsp), %r13
jne 0x5e8c
movl $0x1, %ebx
movq 0x40(%rsp), %r12
cmpl %r12d, 0x20(%rsp)
movq 0x30(%rsp), %r15
leaq 0x5a37(%rip), %r14      # 0xb988
jae 0x5e27
movq (%r14), %rdi
movq 0x10(%r15), %rsi
movq 0x20(%r15), %rdx
movq 0x20(%rsp), %rax
movl %eax, %r8d
movl 0x1c(%rsp), %eax
imull %eax, %r8d
movl %eax, (%rsp)
xorl %ecx, %ecx
movq 0x28(%rsp), %r9
callq 0x2150
jmp 0x5e27
testl %ebx, %ebx
movl $0x0, %ebx
je 0x5e27
movl 0xac(%rsp), %ecx
movl %ecx, %eax
cltd
movl 0x1c(%rsp), %esi
idivl %esi
movq 0x20(%rsp), %rdi
testl %edi, %edi
js 0x5fc6
movl %edi, %edx
imull %esi, %edx
addl $-0x3, %edx
cmpl %edx, %ecx
jl 0x5fc6
leal 0x1(%rdi), %edx
imull %esi, %edx
addl $0x3, %edx
cmpl %edx, %ecx
cmovll %edi, %eax
movl 0xa8(%rsp), %edx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %edx, %edx
js 0x5ff4
cmpl 0x28(%rsp), %edx
setg %dl
cmpl $-0x3, %ecx
setl %cl
orb %dl, %cl
jne 0x5ff4
cmpl %r12d, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovael %ecx, %eax
movl %eax, %ebp
movl $0x1, %ebx
cmpl %edi, %ebp
je 0x5e27
movq 0x20(%rsp), %r8
cmpl %r12d, %r8d
jae 0x602d
movq (%r14), %rdi
movq 0x10(%r15), %rsi
movq 0x20(%r15), %rdx
movl 0x1c(%rsp), %eax
imull %eax, %r8d
movl %eax, (%rsp)
xorl %ecx, %ecx
movq 0x28(%rsp), %r9
callq 0x2150
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x20(%rsp)
testl %ebp, %ebp
js 0x5e27
movl %ebp, %eax
movq %rax, 0x20(%rsp)
cmpl %r12d, %ebp
jge 0x5e27
movq (%r14), %rdi
movq 0x10(%r15), %rsi
movq 0x20(%r15), %rdx
movl %ebp, %r8d
movl 0x1c(%rsp), %eax
imull %eax, %r8d
movl %eax, (%rsp)
xorl %ecx, %ecx
movq 0x28(%rsp), %r9
callq 0x2150
movl %ebp, %eax
movq %rax, 0x20(%rsp)
jmp 0x5e27
movq 0x4f71(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x16e7(%rip), %rsi      # 0x7778
xorl %eax, %eax
callq 0x22c0
jmp 0x5e27
movl 0xa8(%rsp), %esi
movl 0xac(%rsp), %ecx
movl %ecx, %eax
cltd
movl 0x1c(%rsp), %edi
idivl %edi
movq 0x20(%rsp), %r8
testl %r8d, %r8d
js 0x60db
movl %r8d, %edx
imull %edi, %edx
addl $-0x3, %edx
cmpl %edx, %ecx
jl 0x60db
leal 0x1(%r8), %edx
imull %edi, %edx
addl $0x3, %edx
cmpl %edx, %ecx
cmovll %r8d, %eax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %esi, %esi
js 0x60fa
cmpl 0x28(%rsp), %esi
jg 0x60fa
cmpl $-0x3, %ecx
jl 0x60fa
cmpl %r12d, %eax
jae 0x60fa
movl %eax, 0x10(%r13)
movl %eax, %edx
movl $0x1f00, %eax           # imm = 0x1F00
leaq 0x68(%rsp), %rdi
andl 0x50(%rdi), %eax
leal -0x1(%rax), %ecx
andl %eax, %ecx
xorl %ebp, %ebp
negl %ecx
sbbl %ebp, %ebp
orl %edx, %ebp
callq 0x5b13
movq (%r14), %rdi
movq 0x10(%r15), %rsi
callq 0x2430
movl %ebp, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq