// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rsi, %r15
movq %rdi, %rbx
movl 0x28(%rsi), %ebp
movq 0x18(%rsi), %r14
movq 0x20(%rsi), %r12
testq %r14, %r14
je 0x3d73
testq %r12, %r12
je 0x3d86
movq %r12, %rdi
callq 0x3b1a
movq %rax, %r12
movl 0x28(%rax), %ebp
movq 0x20(%rax), %r14
cmpq %r15, 0x10(%rax)
je 0x3da1
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x4079
movq 0x20(%r15), %rax
movq %rax, 0x20(%r12)
jmp 0x3da9
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4079
movq %r12, %r14
jmp 0x3dde
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x3dd3
leaq 0x18(%rax), %rdx
leaq 0x20(%rax), %rcx
cmpq %r15, 0x18(%rax)
cmoveq %rdx, %rcx
jmp 0x3dd7
movq %r14, %rax
testq %r14, %r14
je 0x3dad
movq %r12, 0x10(%rax)
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4079
movq 0x18(%r15), %rax
movq %rax, 0x18(%r12)
movq %r12, 0x10(%rax)
movl 0x28(%r15), %eax
movl %eax, 0x28(%r12)
jmp 0x3dde
leaq 0x28(%rbx), %rcx
movq %r14, (%rcx)
movq %rax, 0x10(%r14)
movq %r15, %rdi
callq 0x1030
cmpl $0x1, %ebp
jne 0x3f8f
leaq 0x28(%rbx), %r15
pushq $0x1
popq %rbp
leaq 0x10(%r14), %r12
testq %r14, %r14
je 0x3f8f
cmpq (%r15), %r14
je 0x3f87
cmpl $0x1, 0x28(%r14)
jne 0x3f87
movq 0x10(%r14), %rax
movq 0x18(%rax), %rsi
cmpq %rsi, %r14
je 0x3e76
cmpl $0x0, 0x28(%rsi)
jne 0x3e4d
movl $0x1, 0x28(%rsi)
movq (%r12), %rax
andl $0x0, 0x28(%rax)
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x4035
movq (%r12), %rax
movq 0x18(%rax), %rsi
movq 0x20(%rsi), %rcx
testq %rcx, %rcx
je 0x3ec9
movl 0x28(%rcx), %r8d
cmpl $0x1, %r8d
setne %dil
movq 0x18(%rsi), %rdx
testq %rdx, %rdx
jne 0x3ed4
cmpl $0x1, %r8d
jne 0x3ee6
jmp 0x3f33
movq 0x20(%rax), %rsi
cmpl $0x0, 0x28(%rsi)
jne 0x3ea3
movl $0x1, 0x28(%rsi)
movq (%r12), %rax
andl $0x0, 0x28(%rax)
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x3ff1
movq (%r12), %rax
movq 0x20(%rax), %rsi
movq 0x18(%rsi), %rcx
testq %rcx, %rcx
je 0x3efe
movl 0x28(%rcx), %r8d
cmpl $0x1, %r8d
setne %dil
movq 0x20(%rsi), %rdx
testq %rdx, %rdx
jne 0x3f09
cmpl $0x1, %r8d
jne 0x3f1b
jmp 0x3f33
movq 0x18(%rsi), %rdx
testq %rdx, %rdx
je 0x3f33
xorl %edi, %edi
movl 0x28(%rdx), %edx
testb %dil, %dil
jne 0x3ee1
cmpl $0x1, %edx
je 0x3f33
cmpl $0x1, %edx
jne 0x3f39
movl $0x1, 0x28(%rcx)
andl $0x0, 0x28(%rsi)
movq %rbx, %rdi
callq 0x3ff1
jmp 0x3dfa
movq 0x20(%rsi), %rdx
testq %rdx, %rdx
je 0x3f33
xorl %edi, %edi
movl 0x28(%rdx), %edx
testb %dil, %dil
jne 0x3f16
cmpl $0x1, %edx
je 0x3f33
cmpl $0x1, %edx
jne 0x3f5b
movl $0x1, 0x28(%rcx)
andl $0x0, 0x28(%rsi)
movq %rbx, %rdi
callq 0x4035
jmp 0x3dfa
andl $0x0, 0x28(%rsi)
jmp 0x3f7e
movl 0x28(%rax), %eax
movl %eax, 0x28(%rsi)
movq (%r12), %rax
movl %ebp, 0x28(%rax)
movq 0x18(%rsi), %rax
movl %ebp, 0x28(%rax)
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x4035
jmp 0x3f7b
movl 0x28(%rax), %eax
movl %eax, 0x28(%rsi)
movq (%r12), %rax
movl %ebp, 0x28(%rax)
movq 0x20(%rsi), %rax
movl %ebp, 0x28(%rax)
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x3ff1
movq %r15, %r12
movq (%r12), %r14
jmp 0x3df6
movl $0x1, 0x28(%r14)
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq