// File: /workspace/github_c_makefile_star2_O0/tudurom[P]windowchef/wm.c
// Function: 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
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
cmpq $0x0, 0x53ed(%rip)      # 0xf800
jne 0xa41a
jmp 0xa834
movq $0x0, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq 0x53cf(%rip), %rsi      # 0xf800
movl $0x4, %edi
callq 0xa840
movl %eax, -0x2c(%rbp)
movss 0x1caa(%rip), %xmm0     # 0xc0f0
movss %xmm0, -0x30(%rbp)
movq 0x4f96(%rip), %rax      # 0xf3e8
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa571
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
movq 0x538f(%rip), %rcx      # 0xf800
cmpl (%rcx), %eax
jne 0xa483
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0xa456
movq -0x28(%rbp), %rax
movq (%rax), %rax
testb $0x1, 0x54(%rax)
jne 0xa49e
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0xa456
movq -0x28(%rbp), %rax
movq (%rax), %rsi
movl $0x4, %edi
callq 0xa840
movl %eax, -0x34(%rbp)
movq $0x0, -0x20(%rbp)
movl -0x4(%rbp), %eax
movq %rax, -0x48(%rbp)
subq $0x3, %rax
ja 0xa54b
movq -0x48(%rbp), %rax
leaq 0x1bda(%rip), %rcx      # 0xc0b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movswl -0x32(%rbp), %eax
movswl -0x2a(%rbp), %ecx
cmpl %ecx, %eax
jge 0xa4f8
leaq -0x10(%rbp), %rdi
callq 0xb750
movq %rax, -0x20(%rbp)
jmp 0xa54b
movswl -0x32(%rbp), %eax
movswl -0x2a(%rbp), %ecx
cmpl %ecx, %eax
jl 0xa513
leaq -0x10(%rbp), %rdi
callq 0xb750
movq %rax, -0x20(%rbp)
jmp 0xa54b
movswl -0x34(%rbp), %eax
movswl -0x2c(%rbp), %ecx
cmpl %ecx, %eax
jge 0xa52e
leaq -0x10(%rbp), %rdi
callq 0xb750
movq %rax, -0x20(%rbp)
jmp 0xa54b
movswl -0x34(%rbp), %eax
movswl -0x2c(%rbp), %ecx
cmpl %ecx, %eax
jl 0xa549
leaq -0x10(%rbp), %rdi
callq 0xb750
movq %rax, -0x20(%rbp)
jmp 0xa54b
cmpq $0x0, -0x20(%rbp)
je 0xa560
movq -0x28(%rbp), %rax
movq (%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0xa456
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa80f
movq 0x5275(%rip), %rdi      # 0xf800
movq -0x28(%rbp), %rax
movq (%rax), %rsi
callq 0xa960
movss %xmm0, -0x38(%rbp)
movq 0x525d(%rip), %rdi      # 0xf800
movq -0x28(%rbp), %rax
movq (%rax), %rsi
callq 0xa9e0
movss %xmm0, -0x3c(%rbp)
movl -0x4(%rbp), %edi
movss -0x3c(%rbp), %xmm0
movss 0x1b30(%rip), %xmm1     # 0xc0f4
callq 0xaac0
testb $0x1, %al
jne 0xa5cf
jmp 0xa629
movq 0x522a(%rip), %rdi      # 0xf800
movq -0x28(%rbp), %rax
movq (%rax), %rsi
callq 0xabe0
testb $0x1, %al
jne 0xa5e8
jmp 0xa606
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1b1f(%rip), %xmm1     # 0xc118
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1b19(%rip), %xmm1     # 0xc130
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
jmp 0xa78d
movl -0x4(%rbp), %edi
movss -0x3c(%rbp), %xmm0
movss 0x1abf(%rip), %xmm1     # 0xc0f8
callq 0xaac0
testb $0x1, %al
jne 0xa644
jmp 0xa69e
movq 0x51b5(%rip), %rdi      # 0xf800
movq -0x28(%rbp), %rax
movq (%rax), %rsi
callq 0xabe0
testb $0x1, %al
jne 0xa65d
jmp 0xa67b
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1aaa(%rip), %xmm1     # 0xc118
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1a9c(%rip), %xmm1     # 0xc128
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
jmp 0xa78b
movl -0x4(%rbp), %edi
movss -0x3c(%rbp), %xmm0
movss 0x1a4e(%rip), %xmm1     # 0xc0fc
callq 0xaac0
testb $0x1, %al
jne 0xa6b9
jmp 0xa710
movq 0x5140(%rip), %rdi      # 0xf800
movq -0x28(%rbp), %rax
movq (%rax), %rsi
callq 0xabe0
testb $0x1, %al
jne 0xa6d2
jmp 0xa6f0
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1a35(%rip), %xmm1     # 0xc118
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1a1f(%rip), %xmm1     # 0xc120
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
jmp 0xa789
movl -0x4(%rbp), %edi
movss -0x3c(%rbp), %xmm0
movss 0x19e0(%rip), %xmm1     # 0xc100
callq 0xaac0
testb $0x1, %al
jne 0xa72b
jmp 0xa776
movq 0x50ce(%rip), %rdi      # 0xf800
movq -0x28(%rbp), %rax
movq (%rax), %rsi
callq 0xabe0
testb $0x1, %al
jne 0xa744
jmp 0xa762
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x19c3(%rip), %xmm1     # 0xc118
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
movss 0x199a(%rip), %xmm0     # 0xc104
mulss -0x38(%rbp), %xmm0
movss %xmm0, -0x38(%rbp)
jmp 0xa787
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0xa579
jmp 0xa789
jmp 0xa78b
jmp 0xa78d
movl -0x4(%rbp), %edi
movq 0x5069(%rip), %rsi      # 0xf800
movq -0x28(%rbp), %rax
movq (%rax), %rdx
callq 0xad30
testb $0x1, %al
jne 0xa7a9
jmp 0xa7c7
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1966(%rip), %xmm1     # 0xc120
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
movss -0x30(%rbp), %xmm0
movss 0x191c(%rip), %xmm1     # 0xc0f0
ucomiss %xmm1, %xmm0
jne 0xa7dd
jp 0xa7dd
jmp 0xa7ec
movss -0x38(%rbp), %xmm1
movss -0x30(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xa7fe
movss -0x38(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0xa579
cmpq $0x0, -0x18(%rbp)
je 0xa822
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x6a10
cmpq $0x0, -0x10(%rbp)
je 0xa834
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xb8a0
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)