// File: /workspace/github_c_makefile_star2_O3/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 0x10(%rdi), %rcx
movslq 0x6c(%rcx), %rdx
testq %rdx, %rdx
jle 0x5502
movq %rsi, -0x8(%rsp)
movl 0x28(%rdi), %r8d
movl 0x3c(%rdi), %eax
subl %eax, %r8d
movl 0x2c(%rdi), %r9d
leal (%r8,%rax,2), %r10d
addl 0x30(%rdi), %r10d
subl %eax, %r9d
leal (%r9,%rax,2), %r11d
addl 0x34(%rdi), %r11d
leal (%r10,%r8), %eax
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
movl %esi, -0x14(%rsp)
leal (%r11,%r9), %eax
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
movl %esi, -0x18(%rsp)
movq %rcx, -0x10(%rsp)
movq 0x70(%rcx), %r14
xorpd %xmm0, %xmm0
xorl %eax, %eax
movq $0x0, -0x20(%rsp)
xorl %ebp, %ebp
xorpd %xmm1, %xmm1
movl (%r14), %esi
movl 0x4(%r14), %r12d
movl 0x8(%r14), %ecx
addl %esi, %ecx
movl 0xc(%r14), %r13d
addl %r12d, %r13d
cmpl %r10d, %ecx
movl %r10d, %ebx
cmovll %ecx, %ebx
cmpl %r8d, %esi
movl %r8d, %edi
cmovgl %esi, %edi
subl %edi, %ebx
testl %ebx, %ebx
cmovlel %eax, %ebx
cmpl %r11d, %r13d
movl %r11d, %edi
cmovll %r13d, %edi
cmpl %r9d, %r12d
movl %r9d, %r15d
cmovgl %r12d, %r15d
subl %r15d, %edi
testl %edi, %edi
cmovlel %eax, %edi
imull %ebx, %edi
testl %edi, %edi
je 0x5470
xorps %xmm2, %xmm2
cvtsi2sd %edi, %xmm2
xorps %xmm3, %xmm3
cvtsi2sdl 0x10(%r14), %xmm3
divsd %xmm3, %xmm2
testl %ebp, %ebp
movl $0x1, %ebp
je 0x5465
ucomisd %xmm0, %xmm2
jbe 0x54e1
movapd %xmm2, %xmm0
movq %r14, -0x20(%rsp)
jmp 0x54e1
testl %ebp, %ebp
je 0x547b
movl $0x1, %ebp
jmp 0x54e1
addl %esi, %ecx
movl %ecx, %esi
shrl $0x1f, %esi
addl %ecx, %esi
sarl %esi
movl -0x14(%rsp), %ecx
subl %esi, %ecx
addl %r12d, %r13d
movl %r13d, %esi
shrl $0x1f, %esi
addl %r13d, %esi
sarl %esi
movl -0x18(%rsp), %edi
subl %esi, %edi
imull %ecx, %ecx
imull %edi, %edi
addl %ecx, %edi
xorps %xmm2, %xmm2
cvtsi2sd %edi, %xmm2
sqrtsd %xmm2, %xmm2
movq -0x20(%rsp), %rdi
testq %rdi, %rdi
sete %cl
ucomisd %xmm2, %xmm1
seta %sil
orb %cl, %sil
testb %sil, %sil
jne 0x54d2
movapd %xmm1, %xmm2
cmovneq %r14, %rdi
movq %rdi, -0x20(%rsp)
xorl %ebp, %ebp
movapd %xmm2, %xmm1
addq $0x14, %r14
decq %rdx
jne 0x53f2
cmpq $0x0, -0x20(%rsp)
movq -0x8(%rsp), %rsi
movq -0x10(%rsp), %rcx
jne 0x550d
jmp 0x5504
xorl %ebp, %ebp
movq 0x70(%rcx), %rax
movq %rax, -0x20(%rsp)
testq %rsi, %rsi
je 0x5514
movl %ebp, (%rsi)
movq -0x20(%rsp), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq