// File: /workspace/github_c_makefile_star2_O2/baskerville[P]bspwm/src/window.c
// Function: apply_size_hints
void apply_size_hints(client_t *c, uint16_t *width, uint16_t *height)
{
	if (!SHOULD_HONOR_SIZE_HINTS(c->honor_size_hints, c->state)) {
		return;
	}

	int32_t minw = 0, minh = 0;
	int32_t basew = 0, baseh = 0, real_basew = 0, real_baseh = 0;

	if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
		basew = c->size_hints.base_width;
		baseh = c->size_hints.base_height;
		real_basew = basew;
		real_baseh = baseh;
	} else if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
		/* base size is substituted with min size if not specified */
		basew = c->size_hints.min_width;
		baseh = c->size_hints.min_height;
	}

	if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
		minw = c->size_hints.min_width;
		minh = c->size_hints.min_height;
	} else if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
		/* min size is substituted with base size if not specified */
		minw = c->size_hints.base_width;
		minh = c->size_hints.base_height;
	}

	/* Handle the size aspect ratio */
	if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT &&
	    c->size_hints.min_aspect_den > 0 &&
	    c->size_hints.max_aspect_den > 0 &&
	    *height > real_baseh &&
	    *width > real_basew) {
		/* ICCCM mandates:
		 * If a base size is provided along with the aspect ratio fields, the base size should be subtracted from the
		 * window size prior to checking that the aspect ratio falls in range. If a base size is not provided, nothing
		 * should be subtracted from the window size. (The minimum size is not to be used in place of the base size for
		 * this purpose.)
		 */
		double dx = *width - real_basew;
		double dy = *height - real_baseh;
		double ratio = dx / dy;
		double min = c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
		double max = c->size_hints.max_aspect_num / (double) c->size_hints.max_aspect_den;

		if (max > 0 && min > 0 && ratio > 0) {
			if (ratio < min) {
				/* dx is lower than allowed, make dy lower to compensate this (+ 0.5 to force proper rounding). */
				dy = dx / min + 0.5;
				*width  = dx + real_basew;
				*height = dy + real_baseh;
			} else if (ratio > max) {
				/* dx is too high, lower it (+0.5 for proper rounding) */
				dx = dy * max + 0.5;
				*width  = dx + real_basew;
				*height = dy + real_baseh;
			}
		}
	}

	/* Handle the minimum size */
	*width = MAX(*width, minw);
	*height = MAX(*height, minh);

	/* Handle the maximum size */
	if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
	{
		if (c->size_hints.max_width > 0) {
			*width = MIN(*width, c->size_hints.max_width);
		}
		if (c->size_hints.max_height > 0) {
			*height = MIN(*height, c->size_hints.max_height);
		}
	}

	/* Handle the size increment */
	if (c->size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_RESIZE_INC | XCB_ICCCM_SIZE_HINT_BASE_SIZE) &&
	    c->size_hints.width_inc > 0 && c->size_hints.height_inc > 0) {
		uint16_t t1 = *width, t2 = *height;
		unsigned_subtract(t1, basew);
		unsigned_subtract(t2, baseh);
		*width -= t1 % c->size_hints.width_inc;
		*height -= t2 % c->size_hints.height_inc;
	}
}

// Assembly:
movq %rdx, %rcx
movl 0x328(%rdi), %eax
cmpl $0x3, %eax
je 0x10106
cmpl $0x2, %eax
je 0x100ae
cmpl $0x1, %eax
jne 0x1010f
cmpl $0x3, 0x308(%rdi)
jne 0x100bb
jmp 0x1010f
movl 0x308(%rdi), %eax
decl %eax
cmpl $0x1, %eax
ja 0x1010f
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl 0x32c(%rdi), %eax
btl $0x8, %eax
jb 0x10110
xorl %edx, %edx
movl $0x0, %r9d
movl $0x0, %ebx
movl $0x0, %r8d
movl $0x0, %r10d
movl $0x0, %r11d
testb $0x10, %al
je 0x1013a
movl 0x340(%rdi), %edx
movl 0x344(%rdi), %r8d
xorl %r9d, %r9d
xorl %ebx, %ebx
jmp 0x1012c
cmpl $0x0, 0x308(%rdi)
je 0x100bb
retq
movl 0x368(%rdi), %edx
movl 0x36c(%rdi), %r9d
movl %edx, %ebx
movl %r9d, %r8d
movl %r9d, %r10d
movl %edx, %r11d
testb $0x10, %al
je 0x1013a
movl 0x340(%rdi), %r11d
movl 0x344(%rdi), %r10d
testb %al, %al
jns 0x10241
movl 0x35c(%rdi), %ebp
testl %ebp, %ebp
jle 0x10241
movl 0x364(%rdi), %r15d
testl %r15d, %r15d
jle 0x10241
movzwl (%rcx), %r14d
subl %r9d, %r14d
jle 0x10241
movzwl (%rsi), %r12d
subl %ebx, %r12d
jle 0x10241
cvtsi2sdl 0x360(%rdi), %xmm0
cvtsi2sd %r15d, %xmm1
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x10241
cvtsi2sd %r14d, %xmm1
cvtsi2sd %ebp, %xmm3
movd %r12d, %xmm2
movd 0x358(%rdi), %xmm4
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
cvtdq2pd %xmm2, %xmm2
movapd %xmm1, %xmm4
unpcklpd %xmm3, %xmm4            # xmm4 = xmm4[0],xmm3[0]
movapd %xmm2, %xmm3
divpd %xmm4, %xmm3
xorpd %xmm4, %xmm4
cmpltpd %xmm3, %xmm4
movmskpd %xmm4, %ebp
movl %ebp, %r14d
shrb %r14b
je 0x10241
testb $0x1, %bpl
je 0x10241
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
ucomisd %xmm3, %xmm4
jbe 0x1020e
movapd %xmm2, %xmm1
divsd %xmm4, %xmm1
addsd 0xe9a8(%rip), %xmm1     # 0x1eba8
cvtsi2sd %ebx, %xmm0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %ebx
jmp 0x1022c
ucomisd %xmm0, %xmm3
jbe 0x10241
mulsd %xmm1, %xmm0
addsd 0xe988(%rip), %xmm0     # 0x1eba8
cvtsi2sd %ebx, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %ebx
movw %bx, (%rsi)
cvtsi2sd %r9d, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r9d
movw %r9w, (%rcx)
movzwl (%rsi), %r9d
cmpl %r9d, %r11d
cmovll %r9d, %r11d
movw %r11w, (%rsi)
movzwl (%rcx), %r9d
cmpl %r9d, %r10d
cmovll %r9d, %r10d
movw %r10w, (%rcx)
testb $0x20, %al
je 0x10299
movl 0x348(%rdi), %r9d
testl %r9d, %r9d
jle 0x1027e
movzwl (%rsi), %r10d
cmpl %r10d, %r9d
cmoval %r10d, %r9d
movw %r9w, (%rsi)
movl 0x34c(%rdi), %r9d
testl %r9d, %r9d
jle 0x10299
movzwl (%rcx), %r10d
cmpl %r10d, %r9d
cmoval %r10d, %r9d
movw %r9w, (%rcx)
testw $0x140, %ax             # imm = 0x140
je 0x102f5
movl 0x350(%rdi), %r10d
testl %r10d, %r10d
jle 0x102f5
movl 0x354(%rdi), %r9d
testl %r9d, %r9d
jle 0x102f5
movzwl (%rsi), %r11d
movl %r11d, %eax
subl %edx, %eax
movl $0xffff, %ebx           # imm = 0xFFFF
andl %ebx, %eax
xorl %ebp, %ebp
cmpl %r11d, %edx
cmovgl %ebp, %eax
movzwl (%rcx), %edx
movl %edx, %edi
subl %r8d, %edi
andl %ebx, %edi
cmpl %edx, %r8d
cmovgl %ebp, %edi
xorl %edx, %edx
divl %r10d
subl %edx, %r11d
movw %r11w, (%rsi)
movl %edi, %eax
xorl %edx, %edx
divl %r9d
subw %dx, (%rcx)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq