// File: /workspace/github_c_makefile_star2_O3/tudurom[P]windowchef/wm.c
// Function: ipc_window_cardinal_focus
static void
cardinal_focus(uint32_t dir)
{
	/* Don't focus if we don't have a current focus! */
	if (focused_win == NULL)
		return;

	struct list_item *valid_windows = NULL;
	struct list_item *desired_window = NULL;
	struct list_item *valid_window;
	struct list_item *win;

	struct win_position focus_win_pos = get_window_position(CENTER, focused_win);

	float closest_distance = -1;

	win = win_list;

	while(win != NULL) {
		/* Skip focused window */
		if (((struct client *)win->data)->window == focused_win->window) {
			win = win->next;
			continue;
		}

		/* Skip unmapped windows */
		if (!((struct client *)win->data)->mapped) {
			win = win->next;
			continue;
		}

		struct win_position win_pos = get_window_position(CENTER, (struct client *)win->data);

		valid_window = NULL;

		switch (dir) {
			case NORTH:
				if (win_pos.y < focus_win_pos.y)
					valid_window = list_add_item(&valid_windows);
				break;
			case SOUTH:
				if (win_pos.y >= focus_win_pos.y)
					valid_window = list_add_item(&valid_windows);
				break;
			case WEST:
				if (win_pos.x < focus_win_pos.x)
					valid_window = list_add_item(&valid_windows);
				break;
			case EAST:
				if (win_pos.x >= focus_win_pos.x)
					valid_window = list_add_item(&valid_windows);
				break;
		}

		if (valid_window != NULL)
			valid_window->data = win->data;

		win = win->next;
	}

	win = valid_windows;
	while(win != NULL) {
		float cur_distance;
		float cur_angle;

		cur_distance = get_distance_between_windows(focused_win, (struct client *)win->data);
		cur_angle = get_angle_between_windows(focused_win, (struct client *)win->data);

		if (is_in_valid_direction(dir, cur_angle, 10)) {
			if (is_overlapping(focused_win, (struct client *)win->data))
				cur_distance = cur_distance * 0.1;
			cur_distance = cur_distance * 0.80;
		}
		else if (is_in_valid_direction(dir, cur_angle, 25)) {
			if (is_overlapping(focused_win, (struct client *)win->data))
				cur_distance = cur_distance * 0.1;
			cur_distance = cur_distance * 0.85;
		}
		else if (is_in_valid_direction(dir, cur_angle, 35)) {
			if (is_overlapping(focused_win, (struct client *)win->data))
				cur_distance = cur_distance * 0.1;
			cur_distance = cur_distance * 0.9;
		}
		else if (is_in_valid_direction(dir, cur_angle, 50)) {
			if (is_overlapping(focused_win, (struct client *)win->data))
				cur_distance = cur_distance * 0.1;
			cur_distance = cur_distance * 3;
		}
		else {
			win = win->next;
			continue;
		}

		if (is_in_cardinal_direction(dir, focused_win, (struct client *)win->data))
			cur_distance = cur_distance * 0.9;


		if (closest_distance == -1 || (cur_distance < closest_distance)) {
			closest_distance = cur_distance;
			desired_window = win;
		}

		win = win->next;
	}

	if (desired_window != NULL)
		set_focused(desired_window->data);

	if (valid_windows != NULL)
		list_delete_all_items(&valid_windows, false);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x515e(%rip), %r14      # 0xc7e0
testq %r14, %r14
je 0x7af1
movl (%rdi), %ebx
movq $0x0, 0x28(%rsp)
movq 0x4d2b(%rip), %r12      # 0xc3c8
testq %r12, %r12
je 0x7add
movzwl 0xa(%r14), %eax
shrl %eax
addw 0x6(%r14), %ax
movl %eax, 0x10(%rsp)
movzwl 0x8(%r14), %r13d
shrl %r13d
addw 0x4(%r14), %r13w
shll $0x10, %r13d
movl %ebx, %r15d
leaq 0x19c7(%rip), %rbp      # 0x9098
movq (%r12), %rax
movl (%rax), %ecx
cmpl (%r14), %ecx
je 0x7749
cmpb $0x1, 0x54(%rax)
jne 0x7749
cmpl $0x3, %ebx
ja 0x7749
movzwl 0x8(%rax), %edx
shrl %edx
addw 0x4(%rax), %dx
movzwl 0xa(%rax), %ecx
shrl %ecx
addw 0x6(%rax), %cx
movzwl %dx, %eax
movslq (%rbp,%r15,4), %rdx
addq %rbp, %rdx
jmpq *%rdx
cmpw 0x10(%rsp), %cx
jl 0x772c
jmp 0x7749
shll $0x10, %eax
cmpl %r13d, %eax
jge 0x772c
jmp 0x7749
shll $0x10, %eax
cmpl %r13d, %eax
jl 0x772c
jmp 0x7749
cmpw 0x10(%rsp), %cx
jl 0x7749
leaq 0x28(%rsp), %rdi
callq 0x85d2
movq 0x50a3(%rip), %r14      # 0xc7e0
testq %rax, %rax
je 0x7749
movq (%r12), %rcx
movq %rcx, (%rax)
movq 0x10(%r12), %r12
testq %r12, %r12
jne 0x76d1
movq 0x28(%rsp), %r12
testq %r12, %r12
je 0x7af1
movss 0x19bb(%rip), %xmm0     # 0x9128
xorl %r13d, %r13d
leal -0x2(%rbx), %ebp
movaps %xmm0, 0x10(%rsp)
movq (%r12), %rax
movzwl 0x8(%r14), %ecx
shrl %ecx
addw 0x4(%r14), %cx
movzwl 0xa(%r14), %edx
shrl %edx
addw 0x6(%r14), %dx
movzwl 0x8(%rax), %esi
shrl %esi
addw 0x4(%rax), %si
movzwl 0xa(%rax), %edi
shrl %edi
movswl %si, %esi
movswl %cx, %ecx
subl %ecx, %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
addw 0x6(%rax), %di
movswl %di, %eax
movswl %dx, %ecx
subl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
callq 0x3670
movaps %xmm0, 0x30(%rsp)
movq 0x5009(%rip), %r14      # 0xc7e0
movq (%r12), %rax
movd 0x4(%r14), %xmm1
movd 0x8(%r14), %xmm3
psrlw $0x1, %xmm3
paddw %xmm1, %xmm3
movd 0x4(%rax), %xmm2
movd 0x8(%rax), %xmm1
psrlw $0x1, %xmm1
paddw %xmm2, %xmm1
movdqa %xmm1, %xmm2
pcmpeqw %xmm3, %xmm2
pshuflw $0x50, %xmm2, %xmm2     # xmm2 = xmm2[0,0,1,1,4,5,6,7]
pshufd $0x50, %xmm2, %xmm2     # xmm2 = xmm2[0,0,1,1]
movmskpd %xmm2, %eax
xorps %xmm0, %xmm0
cmpl $0x3, %eax
je 0x7869
pextrw $0x1, %xmm3, %eax
cwtl
pextrw $0x1, %xmm1, %ecx
movswl %cx, %ecx
subl %eax, %ecx
movd %xmm3, %eax
cwtl
movd %xmm1, %edx
movswl %dx, %edx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
subl %eax, %edx
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
callq 0x3270
cvtss2sd %xmm0, %xmm0
mulsd 0x18f2(%rip), %xmm0     # 0x9150
cvtsd2ss %xmm0, %xmm0
movq 0x4f77(%rip), %r14      # 0xc7e0
movl %ebx, %edi
movss %xmm0, 0xc(%rsp)
movss 0x18b3(%rip), %xmm1     # 0x912c
callq 0x812e
testb %al, %al
je 0x78c9
movq (%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x81b4
movaps 0x30(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x18b3(%rip), %xmm0     # 0x9158
testb %al, %al
je 0x78b0
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movaps 0x10(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x18ac(%rip), %xmm0     # 0x9170
jmp 0x7985
movl %ebx, %edi
movss 0xc(%rsp), %xmm0
movss 0x1857(%rip), %xmm1     # 0x9130
callq 0x812e
testb %al, %al
je 0x7926
movq (%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x81b4
movaps 0x30(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x1853(%rip), %xmm0     # 0x9158
testb %al, %al
je 0x7910
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movaps 0x10(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x1844(%rip), %xmm0     # 0x9168
jmp 0x7985
movl %ebx, %edi
movss 0xc(%rsp), %xmm0
movss 0x17fe(%rip), %xmm1     # 0x9134
callq 0x812e
testb %al, %al
je 0x7a4b
movq (%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x81b4
movaps 0x30(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x17f2(%rip), %xmm0     # 0x9158
testb %al, %al
je 0x7971
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movaps 0x10(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x17db(%rip), %xmm0     # 0x9160
cvtsd2ss %xmm0, %xmm4
cmpl $0x2, %ebx
jb 0x7a14
cmpl $0x1, %ebp
ja 0x79d6
movzwl 0xa(%r15), %eax
shrl %eax
addw 0x6(%r15), %ax
movzwl 0x6(%r14), %ecx
movzwl 0xa(%r14), %edx
addw %cx, %dx
cmpw %ax, %cx
setle %cl
cmpw %ax, %dx
setge %al
testb %al, %cl
je 0x79d6
xorps %xmm0, %xmm0
cvtss2sd %xmm4, %xmm0
mulsd 0x1791(%rip), %xmm0     # 0x9160
xorps %xmm4, %xmm4
cvtsd2ss %xmm0, %xmm4
movaps %xmm4, %xmm0
cmpltps %xmm2, %xmm0
movaps %xmm2, %xmm1
movss 0x1740(%rip), %xmm3     # 0x9128
cmpeqps %xmm3, %xmm1
orps %xmm0, %xmm1
movd %xmm1, %eax
testb $0x1, %al
jne 0x79fa
movaps %xmm2, %xmm4
cmovneq %r12, %r13
movaps %xmm4, %xmm0
movq 0x10(%r12), %r12
testq %r12, %r12
jne 0x7773
jmp 0x7aae
movzwl 0x8(%r15), %eax
shrl %eax
addw 0x4(%r15), %ax
movl 0x4(%r14), %ecx
movzwl 0x8(%r14), %edx
addw %cx, %dx
shll $0x10, %ecx
shll $0x10, %eax
cmpl %eax, %ecx
setle %cl
shll $0x10, %edx
cmpl %eax, %edx
setge %al
andb %cl, %al
cmpb $0x1, %al
je 0x79c0
jmp 0x79d6
movl %ebx, %edi
movss 0xc(%rsp), %xmm0
movss 0x16dd(%rip), %xmm1     # 0x9138
callq 0x812e
testb %al, %al
je 0x7aa4
movq (%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x81b4
movaps 0x30(%rsp), %xmm4
xorps %xmm0, %xmm0
cvtss2sd %xmm4, %xmm0
mulsd 0x16d1(%rip), %xmm0     # 0x9158
testb %al, %al
je 0x7a92
xorps %xmm4, %xmm4
cvtsd2ss %xmm0, %xmm4
mulss 0x16a2(%rip), %xmm4     # 0x913c
movaps 0x10(%rsp), %xmm2
jmp 0x7989
movaps 0x10(%rsp), %xmm0
jmp 0x7a01
testq %r13, %r13
je 0x7add
movq (%r13), %rbx
movq %rbx, %rdi
callq 0x4f9e
movl (%rbx), %esi
leaq 0x44(%rsp), %rcx
movl $0x0, (%rcx)
movq 0x48e5(%rip), %rdi      # 0xc3b8
movl $0x40, %edx
callq 0x3200
cmpq $0x0, 0x28(%rsp)
je 0x7af1
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x8649
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq