// File: /workspace/github_c_makefile_star2_O1/mati75[P]evilwm/client.c
// Function: client_monitor
struct monitor *client_monitor(struct client *c, Bool *intersects) {
	int cx1 = c->x - c->border;
	int cy1 = c->y - c->border;
	int cx2 = cx1 + c->width + c->border*2;
	int cy2 = cy1 + c->height + c->border*2;
	int cmidx = (cx1 + cx2)/2;
	int cmidy = (cy1 + cy2)/2;

	struct monitor *best = NULL;
	Bool have_intersection = 0;
	double best_area_ratio = 0.0;
	double best_distance = 0.0;;

	for (int i = 0; i < c->screen->nmonitors; i++) {
		struct monitor *m = &c->screen->monitors[i];
		int mx2 = m->x + m->width;
		int my2 = m->y + m->height;

		int iw = imax(0, imin(mx2, cx2) - imax(m->x, cx1));
		int ih = imax(0, imin(my2, cy2) - imax(m->y, cy1));
		int iarea = iw * ih;

		// XXX if you're building on a platform without any floating
		// point, you could consider simply returning the first monitor
		// for which an intersection is found.

		if (iarea > 0) {
			// Found an intersection.  Higher ratio wins.
			double iarea_ratio = (double)iarea / (double)m->area;
			if (!have_intersection || iarea_ratio > best_area_ratio) {
				have_intersection = 1;
				best_area_ratio = iarea_ratio;
				best = m;
				continue;
			}
		}

		// Otherwise, any previous intersection trumps distance.
		if (have_intersection)
			continue;

#ifdef HAVE_MATH_H
		// No intersections yet, calculate distance between midpoints.
		int mmidx = (m->x + mx2)/2;
		int mmidy = (m->y + my2)/2;
		int dx = abs(cmidx - mmidx);
		int dy = abs(cmidy - mmidy);
		double d = sqrt(dx*dx + dy*dy);

		if (!best || d < best_distance) {
			best_distance = d;
			best = m;
		}
#endif
	}

	if (!best) {
		best = &c->screen->monitors[0];
	}
	if (intersects) {
		*intersects = have_intersection;
	}
	return best;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, 0x10(%rsp)
movq 0x10(%rdi), %rcx
cmpl $0x0, 0x6c(%rcx)
jle 0x55ce
movq %rdi, %rbp
movl 0x28(%rdi), %edx
movl 0x3c(%rdi), %eax
subl %eax, %edx
movl 0x2c(%rdi), %esi
subl %eax, %esi
leal (%rdx,%rax,2), %r8d
addl 0x30(%rdi), %r8d
leal (%rsi,%rax,2), %r9d
addl 0x34(%rdi), %r9d
movq %rdx, 0x40(%rsp)
movq %r8, 0x30(%rsp)
leal (%r8,%rdx), %eax
movl %eax, %edx
shrl $0x1f, %edx
addl %eax, %edx
sarl %edx
movl %edx, 0xc(%rsp)
movq %rsi, 0x38(%rsp)
movq %r9, 0x28(%rsp)
leal (%r9,%rsi), %eax
movl %eax, %edx
shrl $0x1f, %edx
addl %eax, %edx
sarl %edx
movl %edx, 0x8(%rsp)
xorpd %xmm2, %xmm2
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %eax, %eax
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
movq 0x70(%rcx), %r8
leaq (%r8,%r14), %r13
movl (%r8,%r14), %edi
movl 0x4(%r8,%r14), %ecx
movl 0x8(%r8,%r14), %esi
addl %edi, %esi
movl 0xc(%r8,%r14), %edx
addl %ecx, %edx
movq 0x30(%rsp), %r9
cmpl %r9d, %esi
movl %r9d, %r10d
cmovll %esi, %r10d
movq 0x40(%rsp), %r9
cmpl %r9d, %edi
cmovgl %edi, %r9d
subl %r9d, %r10d
testl %r10d, %r10d
movl $0x0, %ebx
cmovlel %ebx, %r10d
movq 0x28(%rsp), %r9
cmpl %r9d, %edx
cmovll %edx, %r9d
movq 0x38(%rsp), %r11
cmpl %r11d, %ecx
cmovgl %ecx, %r11d
subl %r11d, %r9d
testl %r9d, %r9d
cmovlel %ebx, %r9d
imull %r10d, %r9d
testl %r9d, %r9d
je 0x5528
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x10(%r8,%r14), %xmm1
divsd %xmm1, %xmm0
testl %eax, %eax
setne %al
ucomisd %xmm3, %xmm0
setbe %r8b
andb %al, %r8b
testb %r8b, %r8b
jne 0x5515
movapd %xmm0, %xmm3
cmoveq %r13, %r15
movl $0x1, %eax
cmpb $0x1, %r8b
jne 0x55b4
movl %eax, %r8d
movl $0x1, %eax
testl %r8d, %r8d
jne 0x55b4
addl %edi, %esi
movl %esi, %eax
shrl $0x1f, %eax
addl %esi, %eax
sarl %eax
movl 0xc(%rsp), %esi
subl %eax, %esi
addl %ecx, %edx
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
movl 0x8(%rsp), %ecx
subl %eax, %ecx
imull %esi, %esi
imull %ecx, %ecx
addl %esi, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
ucomisd %xmm2, %xmm0
jb 0x5572
sqrtsd %xmm0, %xmm0
jmp 0x5593
movsd %xmm3, 0x20(%rsp)
movsd %xmm4, 0x18(%rsp)
callq 0x4720
movsd 0x18(%rsp), %xmm4
movsd 0x20(%rsp), %xmm3
xorpd %xmm2, %xmm2
testq %r15, %r15
sete %al
ucomisd %xmm0, %xmm4
seta %cl
orb %al, %cl
testb %cl, %cl
jne 0x55aa
movapd %xmm4, %xmm0
cmovneq %r13, %r15
xorl %eax, %eax
movapd %xmm0, %xmm4
incq %r12
movq 0x10(%rbp), %rcx
movslq 0x6c(%rcx), %rdx
addq $0x14, %r14
cmpq %rdx, %r12
jl 0x5472
jmp 0x55d3
xorl %eax, %eax
xorl %r15d, %r15d
testq %r15, %r15
jne 0x55dc
movq 0x70(%rcx), %r15
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x55e8
movl %eax, (%rcx)
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq