// File: /workspace/github_c_makefile_star2_O1/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 0x4236
testq %rcx, %rcx
je 0x4251
movq %rcx, %rax
movq 0x18(%rcx), %rcx
testq %rcx, %rcx
jne 0x4203
movl 0x28(%rax), %ebp
movq 0x10(%rax), %rcx
movq 0x20(%rax), %r14
cmpq %rsi, %rcx
je 0x4270
testq %rcx, %rcx
je 0x428f
leaq 0x18(%rcx), %rdi
leaq 0x20(%rcx), %rdx
cmpq %rax, 0x18(%rcx)
cmoveq %rdi, %rdx
jmp 0x4293
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x427a
leaq 0x18(%rax), %rdi
leaq 0x20(%rax), %rdx
cmpq %rsi, 0x18(%rax)
cmoveq %rdi, %rdx
jmp 0x427e
movq 0x10(%rsi), %rax
testq %rax, %rax
je 0x42e5
leaq 0x18(%rax), %rdx
leaq 0x20(%rax), %rcx
cmpq %rsi, 0x18(%rax)
cmoveq %rdx, %rcx
jmp 0x42e9
movq %r14, %rcx
testq %r14, %r14
jne 0x42a7
jmp 0x42ab
leaq 0x28(%rbx), %rdx
movq %rcx, (%rdx)
testq %rcx, %rcx
je 0x428a
movq %rax, 0x10(%rcx)
movq %rcx, %r14
jmp 0x42f0
leaq 0x28(%rbx), %rdx
movq %r14, (%rdx)
testq %r14, %r14
je 0x429f
movq %rcx, 0x10(%r14)
movq 0x20(%rsi), %rcx
movq %rcx, 0x20(%rax)
movq %rax, 0x10(%rcx)
movq 0x10(%rsi), %rcx
testq %rcx, %rcx
je 0x42c6
leaq 0x18(%rcx), %rdi
leaq 0x20(%rcx), %rdx
cmpq %rsi, 0x18(%rcx)
cmoveq %rdi, %rdx
jmp 0x42ca
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 0x42f0
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 0x44a6
movl $0x1, %ebp
cmpq 0x28(%rbx), %r14
je 0x449e
cmpl $0x1, 0x28(%r14)
jne 0x449e
movq 0x10(%r14), %rax
movq 0x18(%rax), %rsi
cmpq %rsi, %r14
je 0x4371
cmpl $0x0, 0x28(%rsi)
jne 0x435f
movl $0x1, 0x28(%rsi)
movq 0x10(%r14), %rax
movl $0x0, 0x28(%rax)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x454a
movq 0x10(%r14), %rax
movq 0x18(%rax), %rsi
movq 0x20(%rsi), %rax
testq %rax, %rax
je 0x43b3
cmpl $0x1, 0x28(%rax)
setne %cl
jmp 0x43b5
movq 0x20(%rax), %rsi
cmpl $0x0, 0x28(%rsi)
jne 0x43a1
movl $0x1, 0x28(%rsi)
movq 0x10(%r14), %rax
movl $0x0, 0x28(%rax)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x4502
movq 0x10(%r14), %rax
movq 0x20(%rax), %rsi
movq 0x18(%rsi), %rax
testq %rax, %rax
je 0x441f
cmpl $0x1, 0x28(%rax)
setne %cl
jmp 0x4421
xorl %ecx, %ecx
movq 0x18(%rsi), %rdx
testq %rdx, %rdx
je 0x43c7
cmpl $0x1, 0x28(%rdx)
sete %dl
jmp 0x43c9
movb $0x1, %dl
movl %edx, %edi
notb %dil
orb %dil, %cl
testb $0x1, %cl
je 0x445e
testb %dl, %dl
je 0x43f9
movl $0x1, 0x28(%rax)
movl $0x0, 0x28(%rsi)
movq %rbx, %rdi
callq 0x4502
jmp 0x4493
movq 0x10(%r14), %rax
movl 0x28(%rax), %eax
movl %eax, 0x28(%rsi)
movq 0x10(%r14), %rax
movl %ebp, 0x28(%rax)
movq 0x18(%rsi), %rax
movl %ebp, 0x28(%rax)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x454a
jmp 0x448f
xorl %ecx, %ecx
movq 0x20(%rsi), %rdx
testq %rdx, %rdx
je 0x4433
cmpl $0x1, 0x28(%rdx)
sete %dl
jmp 0x4435
movb $0x1, %dl
movl %edx, %edi
notb %dil
orb %dil, %cl
testb $0x1, %cl
je 0x445e
testb %dl, %dl
je 0x446b
movl $0x1, 0x28(%rax)
movl $0x0, 0x28(%rsi)
movq %rbx, %rdi
callq 0x454a
jmp 0x4493
movl $0x0, 0x28(%rsi)
movq 0x10(%r14), %r14
jmp 0x4493
movq 0x10(%r14), %rax
movl 0x28(%rax), %eax
movl %eax, 0x28(%rsi)
movq 0x10(%r14), %rax
movl %ebp, 0x28(%rax)
movq 0x20(%rsi), %rax
movl %ebp, 0x28(%rax)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x4502
movq 0x28(%rbx), %r14
testq %r14, %r14
jne 0x4311
jmp 0x44a6
movl $0x1, 0x28(%r14)
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq