// File: /workspace/github_c_makefile_star2_O2/baskerville[P]bspwm/src/tree.c
// Function: insert_node
node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
{
	if (d == NULL || n == NULL) {
		return NULL;
	}

	bool d_was_not_occupied = d->root == NULL;

	/* n: inserted node */
	/* c: new internal node */
	/* f: focus or insertion anchor */
	/* p: parent of focus */
	/* g: grand parent of focus */

	if (f == NULL) {
		f = d->root;
	}

	if (f == NULL) {
		d->root = n;
	} else if (IS_RECEPTACLE(f) && f->presel == NULL) {
		node_t *p = f->parent;
		if (p != NULL) {
			if (is_first_child(f)) {
				p->first_child = n;
			} else {
				p->second_child = n;
			}
		} else {
			d->root = n;
		}
		n->parent = p;
		free(f);
		f = NULL;
	} else {
		node_t *c = make_node(XCB_NONE);
		node_t *p = f->parent;
		if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
			node_t *k = find_public(d);
			if (k != NULL) {
				f = k;
				p = f->parent;
			}
			if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
				xcb_rectangle_t rect = get_rectangle(m, d, f);
				presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
			}
		}
		n->parent = c;
		if (f->presel == NULL) {
			bool single_tiled = f->client != NULL && IS_TILED(f->client) && tiled_count(d->root, true) == 1;
			if (p == NULL || automatic_scheme != SCHEME_SPIRAL || single_tiled) {
				if (p != NULL) {
					if (is_first_child(f)) {
						p->first_child = c;
					} else {
						p->second_child = c;
					}
				} else {
					d->root = c;
				}
				c->parent = p;
				f->parent = c;
				if (initial_polarity == FIRST_CHILD) {
					c->first_child = n;
					c->second_child = f;
				} else {
					c->first_child = f;
					c->second_child = n;
				}
				if (p == NULL || automatic_scheme == SCHEME_LONGEST_SIDE || single_tiled) {
					if (f->rectangle.width > f->rectangle.height) {
						c->split_type = TYPE_VERTICAL;
					} else {
						c->split_type = TYPE_HORIZONTAL;
					}
				} else {
					node_t *q = p;
					while (q != NULL && (q->first_child->vacant || q->second_child->vacant)) {
						q = q->parent;
					}
					if (q == NULL) {
						q = p;
					}
					if (q->split_type == TYPE_HORIZONTAL) {
						c->split_type = TYPE_VERTICAL;
					} else {
						c->split_type = TYPE_HORIZONTAL;
					}
				}
			} else {
				node_t *g = p->parent;
				c->parent = g;
				if (g != NULL) {
					if (is_first_child(p)) {
						g->first_child = c;
					} else {
						g->second_child = c;
					}
				} else {
					d->root = c;
				}
				c->split_type = p->split_type;
				c->split_ratio = p->split_ratio;
				p->parent = c;
				int rot;
				if (is_first_child(f)) {
					c->first_child = n;
					c->second_child = p;
					rot = 90;
				} else {
					c->first_child = p;
					c->second_child = n;
					rot = 270;
				}
				if (!n->vacant) {
					rotate_tree(p, rot);
				}
			}
		} else {
			if (p != NULL) {
				if (is_first_child(f)) {
					p->first_child = c;
				} else {
					p->second_child = c;
				}
			}
			c->split_ratio = f->presel->split_ratio;
			c->parent = p;
			f->parent = c;
			switch (f->presel->split_dir) {
				case DIR_WEST:
					c->split_type = TYPE_VERTICAL;
					c->first_child = n;
					c->second_child = f;
					break;
				case DIR_EAST:
					c->split_type = TYPE_VERTICAL;
					c->first_child = f;
					c->second_child = n;
					break;
				case DIR_NORTH:
					c->split_type = TYPE_HORIZONTAL;
					c->first_child = n;
					c->second_child = f;
					break;
				case DIR_SOUTH:
					c->split_type = TYPE_HORIZONTAL;
					c->first_child = f;
					c->second_child = n;
					break;
			}
			if (d->root == f) {
				d->root = c;
			}
			cancel_presel(m, d, f);
			set_marked(m, d, n, false);
		}
	}

	propagate_flags_upward(m, d, n);

	if (d->focus == NULL && is_focusable(n)) {
		d->focus = n;
	}

	if (d_was_not_occupied) {
		put_status(SBSC_MASK_REPORT);
	}

	return f;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
testq %rsi, %rsi
sete %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
je 0x8f29
xorl %ebx, %ebx
jmp 0x9098
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x30(%rsi), %rax
movq 0x30(%rsi), %rbp
testq %rbx, %rbx
cmoveq %rbp, %rbx
testq %rbx, %rbx
je 0x8fde
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x30(%rbx)
jne 0x8f6f
cmpq $0x0, 0x38(%rbx)
jne 0x8f6f
cmpq $0x0, 0x48(%rbx)
jne 0x8f6f
cmpq $0x0, 0x10(%rbx)
je 0x91d5
movq %rbp, 0x10(%rsp)
xorl %edi, %edi
callq 0x92e9
movq %rax, %r13
movq 0x10(%rbx), %rax
movq 0x40(%rbx), %rbp
testq %rax, %rax
jne 0x8fc2
cmpb $0x0, 0x27(%rbx)
jne 0x8fa1
movq %rbp, %rdi
callq 0x8ea3
testl %eax, %eax
je 0x9102
movq %r15, %rdi
callq 0x8db9
testq %rax, %rax
je 0x8fb5
movq 0x40(%rax), %rbp
movq %rax, %rbx
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x90aa
movq %r13, 0x40(%r14)
testq %rbp, %rbp
je 0x8fed
movq 0x40(%rbx), %rcx
testq %rcx, %rcx
je 0x8fda
cmpq %rbx, 0x30(%rcx)
je 0x8fe5
pushq $0x38
jmp 0x8fe7
movq %r14, (%rax)
xorl %ebx, %ebx
jmp 0x9064
pushq $0x30
popq %rcx
movq %r13, (%rbp,%rcx)
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%r13)
movq %rbp, 0x40(%r13)
movq %r13, 0x40(%rbx)
movl 0x8(%rax), %eax
cmpl $0x2, %eax
jb 0x9018
movq 0x10(%rsp), %rbp
je 0x9025
cmpl $0x3, %eax
jne 0x9039
pushq $0x1
popq %rax
jmp 0x9027
movq %r14, %rcx
movq %rbx, %rdx
movq 0x10(%rsp), %rbp
jmp 0x902d
xorl %eax, %eax
movq %rbx, %rcx
movq %r14, %rdx
movl %eax, 0x4(%r13)
movq %rcx, 0x30(%r13)
movq %rdx, 0x38(%r13)
movq 0x8(%rsp), %rax
cmpq %rbx, (%rax)
jne 0x9046
movq %r13, (%rax)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x8d19
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x9435
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x94a1
cmpq $0x0, 0x38(%r15)
jne 0x9089
movq %r14, %rdi
callq 0x9519
testb %al, %al
je 0x9089
movq %r14, 0x38(%r15)
testq %rbp, %rbp
jne 0x9098
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x1d07d
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x27(%rbx)
jne 0x90bd
movq 0x40(%rbx), %rdi
callq 0x8ea3
testl %eax, %eax
je 0x9102
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x932e
movq %rax, %rcx
shrq $0x20, %rcx
shrq $0x30, %rax
movzwl %cx, %ecx
cmpl %eax, %ecx
pushq $0x3
popq %rcx
sbbl $0x0, %ecx
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x8c3c
movq 0x10(%rbx), %rax
movq %r13, 0x40(%r14)
testq %rax, %rax
jne 0x8fc6
jmp 0x9106
movq %r13, 0x40(%r14)
movq 0x48(%rbx), %rax
testq %rax, %rax
je 0x9130
cmpl $0x1, 0x308(%rax)
ja 0x9130
movq 0x8(%rsp), %rax
movq (%rax), %rdi
pushq $0x1
popq %rsi
callq 0x93c6
cmpl $0x1, %eax
sete %al
jmp 0x9132
xorl %eax, %eax
testq %rbp, %rbp
sete %dl
leaq 0x1fc95(%rip), %rcx     # 0x28dd4
movl (%rcx), %ecx
cmpl $0x2, %ecx
setne %sil
orb %al, %sil
orb %dl, %sil
testb $0x1, %sil
je 0x9179
testq %rbp, %rbp
je 0x923e
movq 0x40(%rbx), %rdx
testq %rdx, %rdx
je 0x9170
cmpq %rbx, 0x30(%rdx)
je 0x9235
leaq 0x38(%rbp), %rdx
jmp 0x9239
movq 0x40(%rbp), %rax
movq %rax, 0x40(%r13)
testq %rax, %rax
je 0x919b
leaq 0x30(%rax), %rcx
leaq 0x38(%rax), %rdx
cmpq %rbp, 0x30(%rax)
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq %r13, (%rax)
movl 0x4(%rbp), %eax
movl %eax, 0x4(%r13)
movsd 0x8(%rbp), %xmm0
movsd %xmm0, 0x8(%r13)
movq %r13, 0x40(%rbp)
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x91c8
cmpq %rbx, 0x30(%rax)
je 0x920c
movl $0x10e, %esi            # imm = 0x10E
movq %rbp, %rax
movq %r14, %rcx
jmp 0x9215
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x91f3
leaq 0x30(%rax), %rcx
leaq 0x38(%rax), %rdx
cmpq %rbx, 0x30(%rax)
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movq %r14, (%rcx)
movq %rax, 0x40(%r14)
movq %rbx, %rdi
callq 0x4460
jmp 0x8fe1
pushq $0x5a
popq %rsi
movq %r14, %rax
movq %rbp, %rcx
movq %rax, 0x30(%r13)
movq %rcx, 0x38(%r13)
cmpb $0x0, 0x24(%r14)
jne 0x92c6
movq %rbp, %rdi
callq 0x941b
jmp 0x92c6
leaq 0x30(%rbp), %rdx
movq %rdx, 0x8(%rsp)
testq %rbp, %rbp
sete %dl
movq 0x8(%rsp), %rsi
movq %r13, (%rsi)
movq %rbp, 0x40(%r13)
movq %r13, 0x40(%rbx)
leaq 0x1fb75(%rip), %rsi     # 0x28dd0
cmpl $0x0, (%rsi)
movq %rbx, %rsi
cmoveq %r14, %rsi
movq %r14, %rdi
cmoveq %rbx, %rdi
movq %rsi, 0x30(%r13)
movq %rdi, 0x38(%r13)
testl %ecx, %ecx
sete %cl
orb %al, %cl
orb %dl, %cl
testb $0x1, %cl
je 0x9296
movzwl 0x1c(%rbx), %eax
cmpw 0x1e(%rbx), %ax
jbe 0x92c1
movl $0x1, 0x4(%r13)
jmp 0x92c6
movq %rbp, %rax
testq %rax, %rax
je 0x92bb
movq 0x30(%rax), %rcx
cmpb $0x0, 0x24(%rcx)
jne 0x92b2
movq 0x38(%rax), %rcx
cmpb $0x1, 0x24(%rcx)
jne 0x92b8
movq 0x40(%rax), %rax
jmp 0x9299
movq %rax, %rbp
cmpl $0x0, 0x4(%rbp)
je 0x928c
andl $0x0, 0x4(%r13)
movq 0x10(%rsp), %rbp
jmp 0x9064