// File: /workspace/github_c_makefile_star2_O0/bobrippling[P]tim/window.c
// Function: window_add_neighbour
void window_add_neighbour(window *to, enum neighbour dir, window *new)
{
	window_evict(new);

	switch(dir){
		case neighbour_left:
		case neighbour_right:
		{
			window *topmost;
			for(topmost = to; topmost->neighbours.above; topmost = topmost->neighbours.above);

			window **pdest = (dir == neighbour_right
					? &topmost->neighbours.right
					: &topmost->neighbours.left);

			window *dest = *pdest;

			if(!dest){
				*pdest = new;
				if(dir == neighbour_right)
					new->neighbours.left = topmost;
				else
					new->neighbours.right = topmost;
			}else{
				/* walk down until we find an entry where we left */
				while(dest->neighbours.below
				&& dest->ui_start.y <= new->ui_start.y)
				{
					dest = dest->neighbours.below;
				}

				window *replace = dest->neighbours.below;
				dest->neighbours.below = new;
				if(replace)
					replace->neighbours.above = new;

				new->neighbours.above = dest;
				new->neighbours.below = replace;
			}
			break;
		}
		case neighbour_down:
		{
			window *evicted = to->neighbours.below;

			to->neighbours.below = new;
			new->neighbours.above = to;

			new->neighbours.below = evicted;
			if(evicted)
				evicted->neighbours.above = new;
			break;
		}
		case neighbour_up:
		{
			window *up = to->neighbours.above;
			if(up){
				/* insert in the chain */
				new->neighbours.above = up;
				up->neighbours.below = new;

				to->neighbours.above = new;
				new->neighbours.below = to;
			}else{
				/* rewrite left and right pointers */
				new->neighbours.left = to->neighbours.left;
				to->neighbours.left = NULL;
				new->neighbours.right = to->neighbours.right;
				to->neighbours.right = NULL;

				if(new->neighbours.right)
					new->neighbours.right->neighbours.left = new;
				if(new->neighbours.left)
					new->neighbours.left->neighbours.right = new;

				new->neighbours.below = to;
				to->neighbours.above = new;
			}
			break;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x130f0
movl -0xc(%rbp), %eax
movl %eax, -0x4c(%rbp)
testl %eax, %eax
je 0x13549
jmp 0x133cc
movl -0x4c(%rbp), %eax
subl $0x1, %eax
je 0x13501
jmp 0x133da
movl -0x4c(%rbp), %eax
addl $-0x2, %eax
subl $0x1, %eax
ja 0x13619
jmp 0x133eb
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x1340e
jmp 0x13400
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x133f3
cmpl $0x3, -0xc(%rbp)
jne 0x13426
movq -0x20(%rbp), %rax
addq $0x48, %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
jmp 0x13432
movq -0x20(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x1347c
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
cmpl $0x3, -0xc(%rbp)
jne 0x1346b
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
jmp 0x13477
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
jmp 0x134fc
jmp 0x1347e
movq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x58(%rcx)
movb %al, -0x59(%rbp)
je 0x134a2
movq -0x30(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0xc(%rcx), %eax
setle %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x134ab
jmp 0x134b9
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x1347e
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x58(%rax)
cmpq $0x0, -0x38(%rbp)
je 0x134e4
movq -0x18(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
jmp 0x13619
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
cmpq $0x0, -0x40(%rbp)
je 0x13544
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x60(%rax)
jmp 0x13619
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x13591
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
jmp 0x13617
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x135e4
movq -0x18(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x48(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x135ff
movq -0x18(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x60(%rax)
jmp 0x13619
addq $0x60, %rsp
popq %rbp
retq
nop