// File: /workspace/github_c_makefile_star2_O1/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 0x93cd
xorl %ebx, %ebx
jmp 0x9769
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbp
leaq 0x30(%rsi), %r12
movq 0x30(%rsi), %r13
testq %rbx, %rbx
cmoveq %r13, %rbx
testq %rbx, %rbx
je 0x94db
cmpq $0x0, 0x30(%rbx)
jne 0x940e
cmpq $0x0, 0x38(%rbx)
jne 0x940e
cmpq $0x0, 0x48(%rbx)
jne 0x940e
cmpq $0x0, 0x10(%rbx)
je 0x977b
movq %rbp, 0x8(%rsp)
movq %r13, 0x10(%rsp)
xorl %edi, %edi
callq 0x97c2
movq %rax, %r13
movq 0x40(%rbx), %rbp
cmpq $0x0, 0x10(%rbx)
jne 0x94a5
cmpb $0x0, 0x27(%rbx)
jne 0x943f
movq %rbp, %rdi
callq 0x9331
testl %eax, %eax
je 0x94a5
movq %r15, %rdi
callq 0x9226
testq %rax, %rax
je 0x9453
movq 0x40(%rax), %rbp
movq %rax, %rbx
cmpq $0x0, 0x10(%rbx)
jne 0x94a5
cmpb $0x0, 0x27(%rbx)
jne 0x946d
movq 0x40(%rbx), %rdi
callq 0x9331
testl %eax, %eax
je 0x94a5
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x982d
movq %rax, %rcx
shrq $0x20, %rcx
shrq $0x30, %rax
movzwl %cx, %ecx
cmpl %eax, %ecx
movl $0x3, %ecx
sbbl $0x0, %ecx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x9059
movq %r12, %rdi
movq %r13, 0x40(%r14)
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x94e6
testq %rbp, %rbp
je 0x9591
movq 0x40(%rbx), %rcx
testq %rcx, %rcx
je 0x94d1
cmpq %rbx, 0x30(%rcx)
je 0x9587
movl $0x38, %ecx
jmp 0x958c
movq %r14, (%r12)
xorl %ebx, %ebx
jmp 0x9733
movq 0x48(%rbx), %rax
testq %rax, %rax
je 0x9511
cmpl $0x1, 0x308(%rax)
ja 0x9511
movq (%r12), %rdi
movl $0x1, %esi
callq 0x98c5
movq %r12, %rdi
cmpl $0x1, %eax
sete %al
jmp 0x9513
xorl %eax, %eax
testq %rbp, %rbp
sete %dl
leaq 0x208b4(%rip), %rcx     # 0x29dd4
movl (%rcx), %ecx
cmpl $0x2, %ecx
setne %sil
orb %al, %sil
orb %dl, %sil
testb $0x1, %sil
je 0x955a
testq %rbp, %rbp
je 0x9613
movq 0x40(%rbx), %rdx
testq %rdx, %rdx
je 0x9551
cmpq %rbx, 0x30(%rdx)
je 0x960f
leaq 0x38(%rbp), %rdi
jmp 0x9613
movq 0x40(%rbp), %rax
movq %rax, 0x40(%r13)
testq %rax, %rax
je 0x96c2
movq 0x40(%rbp), %rcx
testq %rcx, %rcx
je 0x957e
cmpq %rbp, 0x30(%rcx)
je 0x96bb
addq $0x38, %rax
jmp 0x96bf
movl $0x30, %ecx
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 0x95be
movq 0x8(%rsp), %rbp
je 0x95cb
cmpl $0x3, %eax
jne 0x95df
movl $0x1, %eax
jmp 0x95cd
movq %r14, %rcx
movq %rbx, %rdx
movq 0x8(%rsp), %rbp
jmp 0x95d3
xorl %eax, %eax
movq %rbx, %rcx
movq %r14, %rdx
movl %eax, 0x4(%r13)
movq %rcx, 0x30(%r13)
movq %rdx, 0x38(%r13)
cmpq %rbx, (%rdi)
jne 0x95e7
movq %r13, (%rdi)
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x9188
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x9937
movq 0x10(%rsp), %r13
jmp 0x9733
leaq 0x30(%rbp), %rdi
testq %rbp, %rbp
sete %dl
movq %r13, (%rdi)
movq %rbp, 0x40(%r13)
movq %r13, 0x40(%rbx)
leaq 0x207a5(%rip), %rsi     # 0x29dd0
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 0x9669
movzwl 0x1c(%rbx), %eax
cmpw 0x1e(%rbx), %ax
jbe 0x96a4
movl $0x1, 0x4(%r13)
jmp 0x9729
movq %rbp, %rax
movq 0x30(%rax), %rcx
cmpb $0x0, 0x24(%rcx)
jne 0x9680
movq 0x38(%rax), %rcx
cmpb $0x1, 0x24(%rcx)
jne 0x968c
movq 0x40(%rax), %rax
testq %rax, %rax
jne 0x966c
movq %rbp, %rax
cmpl $0x0, 0x4(%rax)
movq 0x8(%rsp), %rbp
je 0x96ae
movl $0x0, 0x4(%r13)
jmp 0x9605
movl $0x0, 0x4(%r13)
jmp 0x9729
movl $0x1, 0x4(%r13)
jmp 0x9605
addq $0x30, %rax
movq %rax, %rdi
movq %r13, (%rdi)
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 0x96ea
cmpq %rbx, 0x30(%rax)
je 0x96f7
movl $0x10e, %esi            # imm = 0x10E
movq %rbp, %rax
movq %r14, %rcx
jmp 0x9702
movl $0x5a, %esi
movq %r14, %rax
movq %rbp, %rcx
movq %rax, 0x30(%r13)
movq %rcx, 0x38(%r13)
cmpb $0x0, 0x24(%r14)
jne 0x9729
movq %rbp, %rdi
callq 0xb298
movq %rbp, %rdi
callq 0xb362
movq %rbp, %rdi
callq 0x901e
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x99a5
cmpq $0x0, 0x38(%r15)
jne 0x9758
movq %r14, %rdi
callq 0x9a26
testb %al, %al
je 0x9758
movq %r14, 0x38(%r15)
testq %r13, %r13
jne 0x9769
movl $0x1, %edi
xorl %eax, %eax
callq 0x1eb0b
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x9794
cmpq %rbx, 0x30(%rax)
je 0x9790
leaq 0x38(%rax), %r12
jmp 0x9794
leaq 0x30(%rax), %r12
movq %r14, (%r12)
movq %rax, 0x40(%r14)
movq %rbx, %rdi
callq 0x4460
jmp 0x94df