// File: /workspace/github_c_makefile_star2_O3/cyb70289[P]tt/src/common/tree.c
// Function: tt_rbtree_delete
static int tt_rbtree_delete(struct tt_bintree *tree, struct tt_bintree_node *z)
{
	struct tt_bintree_node *w, *x, *y = z;
	enum tt_bintree_color ycolor = y->ext.color;

	/* Delete node */
	if (!z->left) {
		x = z->right;
		tt_rbtree_trans(tree, z, z->right);
	} else if (!z->right) {
		x = z->left;
		tt_rbtree_trans(tree, z, z->left);
	} else {
		y = tt_rbtree_min(z->right);
		ycolor = y->ext.color;
		x = y->right;
		if (y->parent == z) {
			if (x)
				x->parent = y;
		} else {
			tt_rbtree_trans(tree, y, y->right);
			y->right = z->right;
			y->right->parent = y;
		}
		tt_rbtree_trans(tree, z, y);
		y->left = z->left;
		y->left->parent = y;
		y->ext.color = z->ext.color;
	}
	free(z);

	if (ycolor != TT_BINTREE_BLACK)
		return 0;

	/* Rotate to maintain red-black property */
	while (x && x != tree->_root && x->ext.color == TT_BINTREE_BLACK) {
		if (x == x->parent->left) {
			w = x->parent->right;
			if (w->ext.color == TT_BINTREE_RED) {
				w->ext.color = TT_BINTREE_BLACK;
				x->parent->ext.color = TT_BINTREE_RED;
				tt_rbtree_rotate_left(tree, x->parent);
				w = x->parent->right;
			}
			bool l = !w->left ||
				w->left->ext.color == TT_BINTREE_BLACK;
			bool r = !w->right ||
				w->right->ext.color == TT_BINTREE_BLACK;
			if (l && r) {
				w->ext.color = TT_BINTREE_RED;
				x = x->parent;
			} else if (r) {
				w->left->ext.color = TT_BINTREE_BLACK;
				w->ext.color = TT_BINTREE_RED;
				tt_rbtree_rotate_right(tree, w);
				w = x->parent->right;
			} else {
				w->ext.color = x->parent->ext.color;
				x->parent->ext.color = TT_BINTREE_BLACK;
				w->right->ext.color = TT_BINTREE_BLACK;
				tt_rbtree_rotate_left(tree, x->parent);
				x = tree->_root;
			}
		} else {
			w = x->parent->left;
			if (w->ext.color == TT_BINTREE_RED) {
				w->ext.color = TT_BINTREE_BLACK;
				x->parent->ext.color = TT_BINTREE_RED;
				tt_rbtree_rotate_right(tree, x->parent);
				w = x->parent->left;
			}
			bool r = !w->right ||
				w->right->ext.color == TT_BINTREE_BLACK;
			bool l = !w->left ||
				w->left->ext.color == TT_BINTREE_BLACK;
			if (r && l) {
				w->ext.color = TT_BINTREE_RED;
				x = x->parent;
			} else if (l) {
				w->right->ext.color = TT_BINTREE_BLACK;
				w->ext.color = TT_BINTREE_RED;
				tt_rbtree_rotate_left(tree, w);
				w = x->parent->left;
			} else {
				w->ext.color = x->parent->ext.color;
				x->parent->ext.color = TT_BINTREE_BLACK;
				w->left->ext.color = TT_BINTREE_BLACK;
				tt_rbtree_rotate_right(tree, x->parent);
				x = tree->_root;
			}
		}
	}
	if (x)
		x->ext.color = TT_BINTREE_BLACK;

	return 0;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movq %rdi, %rbx
movl 0x28(%rsi), %ebp
movq 0x18(%rsi), %r14
movq 0x20(%rsi), %rcx
testq %r14, %r14
je 0x42b9
testq %rcx, %rcx
je 0x42d4
movq %rcx, %rax
movq 0x18(%rcx), %rcx
testq %rcx, %rcx
jne 0x4286
movl 0x28(%rax), %ebp
movq 0x10(%rax), %rcx
movq 0x20(%rax), %r14
cmpq %rsi, %rcx
je 0x42f3
testq %rcx, %rcx
je 0x4317
leaq 0x18(%rcx), %rdi
leaq 0x20(%rcx), %rdx
cmpq %rax, 0x18(%rcx)
cmoveq %rdi, %rdx
jmp 0x431b
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x42fd
leaq 0x18(%rax), %rdi
leaq 0x20(%rax), %rdx
cmpq %rsi, 0x18(%rax)
cmoveq %rdi, %rdx
jmp 0x4301
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x436d
leaq 0x18(%rax), %rdx
leaq 0x20(%rax), %rcx
cmpq %rsi, 0x18(%rax)
cmoveq %rdx, %rcx
jmp 0x4371
movq %r14, %rcx
testq %r14, %r14
jne 0x432f
jmp 0x4333
leaq 0x28(%rbx), %rdx
movq %rcx, (%rdx)
testq %rcx, %rcx
je 0x4312
movq %rax, 0x10(%rcx)
movq %rcx, %r14
jmp 0x4378
xorl %r14d, %r14d
jmp 0x4378
leaq 0x28(%rbx), %rdx
movq %r14, (%rdx)
testq %r14, %r14
je 0x4327
movq %rcx, 0x10(%r14)
movq 0x20(%rsi), %rcx
movq %rcx, 0x20(%rax)
movq %rax, 0x10(%rcx)
movq 0x10(%rsi), %rcx
testq %rcx, %rcx
je 0x434e
leaq 0x18(%rcx), %rdi
leaq 0x20(%rcx), %rdx
cmpq %rsi, 0x18(%rcx)
cmoveq %rdi, %rdx
jmp 0x4352
leaq 0x28(%rbx), %rdx
movq %rax, (%rdx)
movq %rcx, 0x10(%rax)
movq 0x18(%rsi), %rcx
movq %rcx, 0x18(%rax)
movq %rax, 0x10(%rcx)
movl 0x28(%rsi), %ecx
movl %ecx, 0x28(%rax)
jmp 0x4378
leaq 0x28(%rbx), %rcx
movq %r14, (%rcx)
movq %rax, 0x10(%r14)
movq %rsi, %rdi
callq 0x1030
cmpl $0x1, %ebp
setne %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x46b2
addq $0x28, %rbx
movl $0x1, %eax
cmpq (%rbx), %r14
je 0x46aa
cmpl $0x1, 0x28(%r14)
jne 0x46aa
movq 0x10(%r14), %rcx
movq 0x18(%rcx), %rdx
cmpq %rdx, %r14
je 0x445b
cmpl $0x0, 0x28(%rdx)
jne 0x4426
movl $0x1, 0x28(%rdx)
movq 0x10(%r14), %rcx
movl $0x0, 0x28(%rcx)
movq 0x10(%r14), %rcx
movq 0x18(%rcx), %rdx
movq 0x20(%rdx), %rsi
movq %rsi, 0x18(%rcx)
testq %rsi, %rsi
je 0x43f3
movq %rcx, 0x10(%rsi)
movq 0x10(%rcx), %rdi
movq %rdi, 0x10(%rdx)
movq %rbx, %rsi
testq %rdi, %rdi
je 0x4413
leaq 0x18(%rdi), %r8
leaq 0x20(%rdi), %rsi
cmpq %rcx, 0x18(%rdi)
cmoveq %r8, %rsi
movq %rdx, (%rsi)
movq %rcx, 0x20(%rdx)
movq %rdx, 0x10(%rcx)
movq 0x10(%r14), %rcx
movq 0x18(%rcx), %rdx
movq 0x20(%rdx), %rsi
testq %rsi, %rsi
je 0x44f8
movl 0x28(%rsi), %r9d
cmpl $0x1, %r9d
setne %r8b
movq 0x18(%rdx), %rdi
testq %rdi, %rdi
jne 0x4508
cmpl $0x1, %r9d
je 0x45df
jmp 0x4577
movq 0x20(%rcx), %rdx
cmpl $0x0, 0x28(%rdx)
jne 0x44c3
movl $0x1, 0x28(%rdx)
movq 0x10(%r14), %rcx
movl $0x0, 0x28(%rcx)
movq 0x10(%r14), %rcx
movq 0x20(%rcx), %rdx
movq 0x18(%rdx), %rsi
movq %rsi, 0x20(%rcx)
testq %rsi, %rsi
je 0x4490
movq %rcx, 0x10(%rsi)
movq 0x10(%rcx), %rdi
movq %rdi, 0x10(%rdx)
movq %rbx, %rsi
testq %rdi, %rdi
je 0x44b0
leaq 0x18(%rdi), %r8
leaq 0x20(%rdi), %rsi
cmpq %rcx, 0x18(%rdi)
cmoveq %r8, %rsi
movq %rdx, (%rsi)
movq %rcx, 0x18(%rdx)
movq %rdx, 0x10(%rcx)
movq 0x10(%r14), %rcx
movq 0x20(%rcx), %rdx
movq 0x18(%rdx), %rsi
testq %rsi, %rsi
je 0x45c6
movl 0x28(%rsi), %r9d
cmpl $0x1, %r9d
setne %r8b
movq 0x20(%rdx), %rdi
testq %rdi, %rdi
jne 0x45d2
cmpl $0x1, %r9d
je 0x45df
jmp 0x4651
movq 0x18(%rdx), %rdi
testq %rdi, %rdi
je 0x45df
xorl %r8d, %r8d
movl 0x28(%rdi), %edi
testb %r8b, %r8b
jne 0x4519
cmpl $0x1, %edi
je 0x45df
cmpl $0x1, %edi
je 0x4577
movl 0x28(%rcx), %ecx
movl %ecx, 0x28(%rdx)
movq 0x10(%r14), %rcx
movl %eax, 0x28(%rcx)
movq 0x18(%rdx), %rcx
movl %eax, 0x28(%rcx)
movq 0x10(%r14), %rcx
movq 0x18(%rcx), %rdx
movq 0x20(%rdx), %rsi
movq %rsi, 0x18(%rcx)
testq %rsi, %rsi
je 0x454b
movq %rcx, 0x10(%rsi)
movq 0x10(%rcx), %rdi
movq %rdi, 0x10(%rdx)
movq %rbx, %rsi
testq %rdi, %rdi
je 0x456b
leaq 0x18(%rdi), %r8
leaq 0x20(%rdi), %rsi
cmpq %rcx, 0x18(%rdi)
cmoveq %r8, %rsi
movq %rdx, (%rsi)
movq %rcx, 0x20(%rdx)
jmp 0x4648
movl $0x1, 0x28(%rsi)
movl $0x0, 0x28(%rdx)
movq 0x20(%rdx), %rcx
movq 0x18(%rcx), %rsi
movq %rsi, 0x20(%rdx)
testq %rsi, %rsi
je 0x459a
movq %rdx, 0x10(%rsi)
movq 0x10(%rdx), %rdi
movq %rdi, 0x10(%rcx)
movq %rbx, %rsi
testq %rdi, %rdi
je 0x45ba
leaq 0x18(%rdi), %r8
leaq 0x20(%rdi), %rsi
cmpq %rdx, 0x18(%rdi)
cmoveq %r8, %rsi
movq %rcx, (%rsi)
movq %rdx, 0x18(%rcx)
jmp 0x469b
movq 0x20(%rdx), %rdi
testq %rdi, %rdi
je 0x45df
xorl %r8d, %r8d
movl 0x28(%rdi), %edi
testb %r8b, %r8b
jne 0x45ef
cmpl $0x1, %edi
jne 0x45ef
movl $0x0, 0x28(%rdx)
movq 0x10(%r14), %r14
jmp 0x469f
cmpl $0x1, %edi
je 0x4651
movl 0x28(%rcx), %ecx
movl %ecx, 0x28(%rdx)
movq 0x10(%r14), %rcx
movl %eax, 0x28(%rcx)
movq 0x20(%rdx), %rcx
movl %eax, 0x28(%rcx)
movq 0x10(%r14), %rcx
movq 0x20(%rcx), %rdx
movq 0x18(%rdx), %rsi
movq %rsi, 0x20(%rcx)
testq %rsi, %rsi
je 0x4621
movq %rcx, 0x10(%rsi)
movq 0x10(%rcx), %rdi
movq %rdi, 0x10(%rdx)
movq %rbx, %rsi
testq %rdi, %rdi
je 0x4641
leaq 0x18(%rdi), %r8
leaq 0x20(%rdi), %rsi
cmpq %rcx, 0x18(%rdi)
cmoveq %r8, %rsi
movq %rdx, (%rsi)
movq %rcx, 0x18(%rdx)
movq %rdx, 0x10(%rcx)
movq (%rbx), %r14
jmp 0x469f
movl $0x1, 0x28(%rsi)
movl $0x0, 0x28(%rdx)
movq 0x18(%rdx), %rcx
movq 0x20(%rcx), %rsi
movq %rsi, 0x18(%rdx)
testq %rsi, %rsi
je 0x4674
movq %rdx, 0x10(%rsi)
movq 0x10(%rdx), %rdi
movq %rdi, 0x10(%rcx)
movq %rbx, %rsi
testq %rdi, %rdi
je 0x4694
leaq 0x18(%rdi), %r8
leaq 0x20(%rdi), %rsi
cmpq %rdx, 0x18(%rdi)
cmoveq %r8, %rsi
movq %rcx, (%rsi)
movq %rdx, 0x20(%rcx)
movq %rcx, 0x10(%rdx)
testq %r14, %r14
jne 0x439d
jmp 0x46b2
movl $0x1, 0x28(%r14)
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq