// File: /workspace/github_c_makefile_star2_O1/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 0x5d1b(%rip), %rax      # 0xb950
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq $0x0, (%rax)
je 0x61dc
movq %rsi, %r13
movq %rdi, %rbx
movq 0x28(%rdi), %rdi
xorl %eax, %eax
callq 0x340c
testq %rax, %rax
je 0x61dc
movq %rax, %r12
movq 0x20(%rbx), %rax
cmpq 0x10(%r12), %rax
je 0x61dc
movq %rbx, 0x20(%rsp)
movq (%r13), %rax
movq (%rax), %r15
movl $0x0, %edx
movl $0x0, %ebp
testq %r15, %r15
movq %r12, 0x30(%rsp)
je 0x5cd5
xorl %ebp, %ebp
xorl %edx, %edx
leaq 0x5cb5(%rip), %r14      # 0xb950
movl %edx, %ebx
movq (%r14), %r12
movq %r15, %rdi
callq 0x2140
movq %r12, %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0x24d0
movl %eax, %edx
addl $0x4, %edx
cmpl %ebx, %edx
cmovlel %ebx, %edx
movq (%r13), %rax
movq 0x8(%rax,%rbp,8), %r15
incq %rbp
testq %r15, %r15
jne 0x5c9b
movq 0x30(%rsp), %r12
movq %r13, 0x38(%rsp)
movl 0x10(%r13), %eax
leal -0x1(%rbp), %ebx
cmpl %ebp, %eax
cmovll %eax, %ebx
leaq 0x5c63(%rip), %rax      # 0xb950
movq (%rax), %rax
movl 0x58(%rax), %ecx
movl 0x5c(%rax), %eax
leal (%rcx,%rax), %esi
leal (%rcx,%rax), %r14d
incl %r14d
movq %rdx, %r11
shrl %edx
movq 0x20(%rsp), %r10
movl 0x44(%r10), %ecx
movl %r14d, %eax
imull %ebx, %eax
movl %ecx, %r13d
subl %eax, %r13d
movl %r14d, %eax
shrl $0x1f, %eax
leal (%rax,%rsi), %r8d
incl %r8d
movslq (%r12), %rax
shlq $0x7, %rax
leaq 0x5c42(%rip), %rsi      # 0xb978
movq (%rsi), %rdi
movq 0xe8(%rdi), %r9
movl 0x18(%r9,%rax), %esi
movl 0x1c(%r9,%rax), %eax
movl 0x40(%r10), %r10d
sarl %r8d
subl %edx, %r10d
movq %rbp, %r15
movl $0x0, %r9d
jns 0x5d71
movq 0x20(%rsp), %r9
movl %edx, 0x40(%r9)
xorl %r10d, %r10d
movl $0x1, %r9d
subl %r8d, %r13d
leal (%r10,%r11), %r8d
movl %esi, %edx
subl %r8d, %edx
movq %r11, 0x28(%rsp)
jg 0x5d96
movq 0x20(%rsp), %r8
addl %edx, 0x40(%r8)
subl %r11d, %esi
incl %r9d
movl %esi, %r10d
movl %r14d, %ebp
imull %r15d, %ebp
testl %r13d, %r13d
jns 0x5db3
subl %r13d, %ecx
movq 0x20(%rsp), %rdx
movl %ecx, 0x44(%rdx)
incl %r9d
xorl %r13d, %r13d
leal (%r13,%rbp), %edx
movl %eax, %ecx
subl %edx, %ecx
movl %r14d, 0x1c(%rsp)
jg 0x5dd3
movq 0x20(%rsp), %rdx
addl %ecx, 0x44(%rdx)
subl %ebp, %eax
incl %r9d
movl %eax, %r13d
testl %r9d, %r9d
movq 0x20(%rsp), %r14
je 0x5e13
movl 0x40(%r14), %eax
movl 0x44(%r14), %ecx
movq 0x8(%r12), %rdx
movl %ecx, 0x10(%rsp)
movl %eax, 0x8(%rsp)
movl $0x0, (%rsp)
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %r10, %r14
callq 0x2230
movq %r14, %r10
movq 0x20(%rsp), %r14
leaq 0x5b5e(%rip), %rax      # 0xb978
movq (%rax), %rdi
movq 0x10(%r12), %rsi
movl %r10d, %edx
movl %r13d, %ecx
movq %rax, %r13
movq 0x28(%rsp), %r8
movl %ebp, %r9d
callq 0x21a0
movq (%r13), %rdi
movq 0x10(%r12), %rsi
movl $0xa00c, %edx           # imm = 0xA00C
callq 0x23b0
movq (%r13), %rdi
movq 0x10(%r12), %rsi
callq 0x2340
movq 0x10(%r12), %rdi
movl 0x38(%r14), %r8d
xorl %esi, %esi
movl $0x2200c, %edx          # imm = 0x2200C
xorl %ecx, %ecx
callq 0x5b2e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x61ce
movl %r15d, %eax
movq %rax, 0x50(%rsp)
xorl %ebp, %ebp
movq 0x38(%rsp), %r13
movq %r15, 0x40(%rsp)
leaq 0x5ae0(%rip), %rax      # 0xb978
movq (%rax), %rdi
movl $0xa00c, %esi           # imm = 0xA00C
leaq 0x68(%rsp), %rdx
callq 0x25f0
movq %rbx, %r8
movl 0x68(%rsp), %ecx
leal -0x4(%rcx), %eax
cmpl $0x8, %eax
ja 0x611c
leaq 0x1890(%rip), %rdx      # 0x7754
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
movl %r8d, %ebx
jmpq *%rax
movl 0xbc(%rsp), %eax
movl %r8d, %ebx
movq 0x20(%rsp), %rdx
cmpl 0x54(%rdx), %eax
jne 0x5e91
jmp 0x613e
movq %r8, 0x48(%rsp)
leaq 0x5a83(%rip), %rax      # 0xb978
movq (%rax), %rdi
movq 0x10(%r12), %rsi
callq 0x2090
testl %r15d, %r15d
je 0x5fc3
movl $0x1, %r15d
xorl %ebx, %ebx
movq (%r13), %rax
movq (%rax,%rbx,8), %r12
movl $0x1, %r13d
cmpq $0x4, %rbx
leaq 0x5a24(%rip), %r14      # 0xb950
ja 0x5f5a
movq (%r14), %r13
movq %r12, %rdi
callq 0x2140
movq %r13, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x24d0
movq 0x28(%rsp), %rcx
subl %eax, %ecx
movl %ecx, %r13d
shrl $0x1f, %r13d
addl %ecx, %r13d
sarl %r13d
movq (%r14), %rax
movl 0x58(%rax), %r14d
addl %r15d, %r14d
leaq 0x5a0d(%rip), %rax      # 0xb978
movq (%rax), %rbp
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 %rbp, %rdi
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
movl %r13d, %ecx
movl %r14d, %r8d
movq %r12, %r9
callq 0x23f0
incq %rbx
addl 0x1c(%rsp), %r15d
cmpq %rbx, 0x50(%rsp)
movq 0x38(%rsp), %r13
jne 0x5f13
movl $0x1, %ebp
movq 0x48(%rsp), %r9
movl %r9d, %ebx
movq 0x40(%rsp), %r15
cmpl %r15d, %r9d
movq 0x30(%rsp), %r12
jae 0x5e91
leaq 0x598e(%rip), %rax      # 0xb978
movq (%rax), %rdi
movq 0x10(%r12), %rsi
movq 0x20(%r12), %rdx
movl %r9d, %r8d
movl 0x1c(%rsp), %eax
imull %eax, %r8d
movl %eax, (%rsp)
xorl %ecx, %ecx
movq %r9, %rbx
movq 0x28(%rsp), %r9
callq 0x2150
jmp 0x5e91
movl %r8d, %ebx
testl %ebp, %ebp
movl $0x0, %ebp
je 0x5e91
movl 0xac(%rsp), %ecx
movl %ecx, %eax
cltd
movl 0x1c(%rsp), %esi
idivl %esi
testl %r8d, %r8d
js 0x605b
movl %r8d, %edx
imull %esi, %edx
addl $-0x3, %edx
cmpl %edx, %ecx
jl 0x605b
leal 0x1(%r8), %edx
imull %esi, %edx
addl $0x3, %edx
cmpl %edx, %ecx
cmovll %r8d, %eax
movl 0xa8(%rsp), %edx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testl %edx, %edx
js 0x608b
cmpl 0x28(%rsp), %edx
setg %dl
cmpl $-0x3, %ecx
setl %cl
orb %dl, %cl
jne 0x608b
cmpl %r15d, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovael %ecx, %eax
movl %eax, %r14d
movl $0x1, %ebp
movl %r14d, %ebx
cmpl %r8d, %r14d
je 0x5e91
cmpl %r15d, %r8d
jae 0x60cc
leaq 0x58d0(%rip), %rax      # 0xb978
movq (%rax), %rdi
movq 0x10(%r12), %rsi
movq 0x20(%r12), %rdx
movl 0x1c(%rsp), %eax
imull %eax, %r8d
movl %eax, (%rsp)
xorl %ecx, %ecx
movq 0x28(%rsp), %r9
callq 0x2150
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %r14d, %r14d
js 0x5e91
movl %r14d, %ebx
cmpl %r15d, %r14d
jge 0x5e91
leaq 0x588b(%rip), %rax      # 0xb978
movq (%rax), %rdi
movq 0x10(%r12), %rsi
movq 0x20(%r12), %rdx
movl %r14d, %r8d
movl 0x1c(%rsp), %eax
imull %eax, %r8d
movl %eax, (%rsp)
xorl %ecx, %ecx
movq 0x28(%rsp), %r9
callq 0x2150
movl %r14d, %ebx
jmp 0x5e91
movq 0x4ed5(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x164b(%rip), %rsi      # 0x7778
movl %ecx, %edx
xorl %eax, %eax
movq %r8, %rbx
callq 0x22b0
jmp 0x5e91
movl 0xa8(%rsp), %edi
movl 0xac(%rsp), %esi
movl %esi, %eax
cltd
movl 0x1c(%rsp), %r9d
idivl %r9d
testl %r8d, %r8d
js 0x617b
movl %r8d, %edx
imull %r9d, %edx
addl $-0x3, %edx
cmpl %edx, %esi
jl 0x617b
leal 0x1(%r8), %edx
imull %r9d, %edx
addl $0x3, %edx
cmpl %edx, %esi
cmovll %r8d, %eax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %edi, %edi
js 0x619f
cmpl 0x28(%rsp), %edi
jg 0x619f
cmpl $-0x3, %esi
jl 0x619f
cmpl %r15d, %eax
jae 0x619f
movl %eax, 0x10(%r13)
movl %eax, %edx
cmpl $0x5, %ecx
jne 0x61bd
movl $0x1f00, %eax           # imm = 0x1F00
andl 0xb8(%rsp), %eax
leal -0x1(%rax), %ecx
andl %eax, %ecx
xorl %ebp, %ebp
negl %ecx
sbbl %ebp, %ebp
orl %edx, %ebp
leaq 0x68(%rsp), %rdi
callq 0x5b87
leaq 0x57aa(%rip), %r13      # 0xb978
movq (%r13), %rdi
movq 0x10(%r12), %rsi
callq 0x2420
movl %ebp, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq