// File: /workspace/github_c_makefile_star2_O3/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 0x92d0
xorl %ebx, %ebx
jmp 0x944b
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 0x9388
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x30(%rbx)
jne 0x9316
cmpq $0x0, 0x38(%rbx)
jne 0x9316
cmpq $0x0, 0x48(%rbx)
jne 0x9316
cmpq $0x0, 0x10(%rbx)
je 0x958c
movq %rbp, 0x10(%rsp)
xorl %edi, %edi
callq 0x96c2
movq %rax, %r13
movq 0x10(%rbx), %rax
movq 0x40(%rbx), %rbp
testq %rax, %rax
jne 0x9369
cmpb $0x0, 0x27(%rbx)
jne 0x9348
movq %rbp, %rdi
callq 0x923d
testl %eax, %eax
je 0x94b7
movq %r15, %rdi
callq 0x9111
testq %rax, %rax
je 0x935c
movq 0x40(%rax), %rbp
movq %rax, %rbx
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x945d
movq %r13, 0x40(%r14)
testq %rbp, %rbp
je 0x939c
movq 0x40(%rbx), %rcx
testq %rcx, %rcx
je 0x9381
cmpq %rbx, 0x30(%rcx)
je 0x9392
movl $0x38, %ecx
jmp 0x9397
movq %r14, (%rax)
xorl %ebx, %ebx
jmp 0x9415
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 0x93c9
movq 0x10(%rsp), %rbp
je 0x93d6
cmpl $0x3, %eax
jne 0x93ea
movl $0x1, %eax
jmp 0x93d8
movq %r14, %rcx
movq %rbx, %rdx
movq 0x10(%rsp), %rbp
jmp 0x93de
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 0x93f7
movq %r13, (%rax)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x906b
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x9840
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x98ae
cmpq $0x0, 0x38(%r15)
jne 0x943a
movq %r14, %rdi
callq 0x9943
testb %al, %al
je 0x943a
movq %r14, 0x38(%r15)
testq %rbp, %rbp
jne 0x944b
movl $0x1, %edi
xorl %eax, %eax
callq 0x1e6b8
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x27(%rbx)
jne 0x9470
movq 0x40(%rbx), %rdi
callq 0x923d
testl %eax, %eax
je 0x94b7
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x970b
movq %rax, %rcx
shrq $0x20, %rcx
shrq $0x30, %rax
movzwl %cx, %ecx
cmpl %eax, %ecx
movl $0x3, %ecx
sbbl $0x0, %ecx
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x8f4e
movq 0x10(%rbx), %rax
movq %r13, 0x40(%r14)
testq %rax, %rax
jne 0x936d
jmp 0x94bb
movq %r13, 0x40(%r14)
movq 0x48(%rbx), %rax
testq %rax, %rax
je 0x94e7
cmpl $0x1, 0x308(%rax)
ja 0x94e7
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movl $0x1, %esi
callq 0x97a3
cmpl $0x1, %eax
sete %al
jmp 0x94e9
xorl %eax, %eax
testq %rbp, %rbp
sete %dl
leaq 0x208de(%rip), %rcx     # 0x29dd4
movl (%rcx), %ecx
cmpl $0x2, %ecx
setne %sil
orb %al, %sil
orb %dl, %sil
testb $0x1, %sil
je 0x9530
testq %rbp, %rbp
je 0x95f7
movq 0x40(%rbx), %rdx
testq %rdx, %rdx
je 0x9527
cmpq %rbx, 0x30(%rdx)
je 0x95ee
leaq 0x38(%rbp), %rdx
jmp 0x95f2
movq 0x40(%rbp), %rax
movq %rax, 0x40(%r13)
testq %rax, %rax
je 0x9552
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 0x957f
cmpq %rbx, 0x30(%rax)
je 0x95c3
movl $0x10e, %esi            # imm = 0x10E
movq %rbp, %rax
movq %r14, %rcx
jmp 0x95ce
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x95aa
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 0x938b
movl $0x5a, %esi
movq %r14, %rax
movq %rbp, %rcx
movq %rax, 0x30(%r13)
movq %rcx, 0x38(%r13)
cmpb $0x0, 0x24(%r14)
jne 0x9692
movq %rbp, %rdi
callq 0x9815
jmp 0x9692
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 0x207bc(%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 0x964f
movzwl 0x1c(%rbx), %eax
cmpw 0x1e(%rbx), %ax
jbe 0x968a
movl $0x1, 0x4(%r13)
jmp 0x9692
movq %rbp, %rax
movq 0x30(%rax), %rcx
cmpb $0x0, 0x24(%rcx)
jne 0x9666
movq 0x38(%rax), %rcx
cmpb $0x1, 0x24(%rcx)
jne 0x9672
movq 0x40(%rax), %rax
testq %rax, %rax
jne 0x9652
movq %rbp, %rax
cmpl $0x0, 0x4(%rax)
movq 0x10(%rsp), %rbp
je 0x969c
movl $0x0, 0x4(%r13)
jmp 0x9415
movl $0x0, 0x4(%r13)
movq 0x10(%rsp), %rbp
jmp 0x9415
movl $0x1, 0x4(%r13)
jmp 0x9415