// File: /workspace/github_c_makefile_star2_O2/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
movq %rsi, -0x10(%rsp)
movl 0x28(%rdi), %r11d
movl 0x3c(%rdi), %eax
subl %eax, %r11d
movl 0x2c(%rdi), %ebx
subl %eax, %ebx
leal (%r11,%rax,2), %r14d
addl 0x30(%rdi), %r14d
leal (%rbx,%rax,2), %r15d
addl 0x34(%rdi), %r15d
leal (%r14,%r11), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x1c(%rsp)
leal (%r15,%rbx), %eax
cltd
idivl %ecx
movl %eax, -0x20(%rsp)
movq 0x10(%rdi), %rax
movq %rax, -0x18(%rsp)
movl 0x6c(%rax), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
imulq $0x14, %rax, %rax
movq %rax, -0x8(%rsp)
xorpd %xmm0, %xmm0
xorl %esi, %esi
xorpd %xmm1, %xmm1
xorl %edx, %edx
movq $0x0, -0x28(%rsp)
cmpq %rsi, -0x8(%rsp)
je 0x52aa
movq -0x18(%rsp), %rax
movq 0x70(%rax), %r10
movl (%r10,%rsi), %edi
movl 0x4(%r10,%rsi), %ecx
movl 0x8(%r10,%rsi), %eax
addl %edi, %eax
movl 0xc(%r10,%rsi), %r9d
addl %ecx, %r9d
cmpl %r14d, %eax
movl %r14d, %r8d
cmovll %eax, %r8d
cmpl %r11d, %edi
movl %r11d, %r13d
cmovgl %edi, %r13d
subl %r13d, %r8d
testl %r8d, %r8d
movl $0x0, %ebp
cmovlel %ebp, %r8d
cmpl %r15d, %r9d
movl %r15d, %r13d
cmovll %r9d, %r13d
cmpl %ebx, %ecx
movl %ebx, %r12d
cmovgl %ecx, %r12d
subl %r12d, %r13d
testl %r13d, %r13d
cmovlel %ebp, %r13d
imull %r8d, %r13d
leaq (%r10,%rsi), %r8
testl %r13d, %r13d
je 0x523b
cvtsi2sd %r13d, %xmm2
cvtsi2sdl 0x10(%r10,%rsi), %xmm3
divsd %xmm3, %xmm2
testl %edx, %edx
je 0x522b
ucomisd %xmm1, %xmm2
jbe 0x523f
movapd %xmm2, %xmm1
movl $0x1, %edx
movq %r8, -0x28(%rsp)
jmp 0x52a1
testl %edx, %edx
je 0x5246
movl $0x1, %edx
jmp 0x52a1
addl %edi, %eax
cltd
movl $0xfffffffe, %r10d      # imm = 0xFFFFFFFE
idivl %r10d
movl %eax, %edi
addl %ecx, %r9d
movl %r9d, %eax
cltd
idivl %r10d
addl -0x1c(%rsp), %edi
addl -0x20(%rsp), %eax
imull %edi, %edi
imull %eax, %eax
addl %edi, %eax
cvtsi2sd %eax, %xmm2
sqrtsd %xmm2, %xmm2
movq -0x28(%rsp), %rdx
testq %rdx, %rdx
sete %al
ucomisd %xmm2, %xmm0
seta %cl
orb %al, %cl
testb %cl, %cl
jne 0x5292
movapd %xmm0, %xmm2
cmovneq %r8, %rdx
movq %rdx, -0x28(%rsp)
xorl %edx, %edx
movapd %xmm2, %xmm0
addq $0x14, %rsi
jmp 0x5198
cmpq $0x0, -0x28(%rsp)
jne 0x52c0
movq -0x18(%rsp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x28(%rsp)
movq -0x10(%rsp), %rax
testq %rax, %rax
je 0x52cc
movl %edx, (%rax)
movq -0x28(%rsp), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq