// File: harry830622[P]floorplanner/src/b_star_tree.cpp
// Function: BStarTree::Delete(int)
void BStarTree::Delete(int deleted_node_id) {
  Node& deleted_node = node(deleted_node_id);

  int child_id = -1;
  if (deleted_node.left_child_id_ != -1 && deleted_node.right_child_id_ != -1) {
    child_id = deleted_node.left_child_id_;
    int current_node_id = child_id;
    while (left_child_id(current_node_id) != -1 &&
           right_child_id(current_node_id) != -1) {
      current_node_id = left_child_id(current_node_id);
    }
    if (right_child_id(current_node_id) != -1) {
      Node& current_node = node(current_node_id);
      current_node.left_child_id_ = current_node.right_child_id_;
      current_node.right_child_id_ = -1;
    }
    while (current_node_id != deleted_node_id) {
      Node& current_node = node(current_node_id);
      Node& current_parent = node(current_node.parent_id_);
      Node& current_parent_right_child = node(current_parent.right_child_id_);
      current_node.right_child_id_ = current_parent.right_child_id_;
      current_parent_right_child.parent_id_ = current_node_id;
      current_node_id = current_node.parent_id_;
    }
  } else if (deleted_node.left_child_id_ != -1) {
    child_id = deleted_node.left_child_id_;
  } else if (deleted_node.right_child_id_ != -1) {
    child_id = deleted_node.right_child_id_;
  }

  if (child_id != -1) {
    node(child_id).parent_id_ = deleted_node.parent_id_;
  }

  if (deleted_node.parent_id_ == -1) {
    root_id_ = child_id;
  } else {
    Node& parent = node(deleted_node.parent_id_);
    if (parent.left_child_id_ == deleted_node_id) {
      parent.left_child_id_ = child_id;
    } else {
      parent.right_child_id_ = child_id;
    }
  }

  deleted_node.parent_id_ = -1;
  deleted_node.left_child_id_ = -1;
  deleted_node.right_child_id_ = -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x50(%rbp)
movl -0xc(%rbp), %esi
callq 0xa7c0
movq %rax, -0x18(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
je 0xa9ec
movq -0x18(%rbp), %rax
cmpl $-0x1, 0x8(%rax)
je 0xa9ec
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x50(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0xa700
movl %eax, %ecx
xorl %eax, %eax
cmpl $-0x1, %ecx
movb %al, -0x51(%rbp)
je 0xa933
movq -0x50(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0xa730
cmpl $-0x1, %eax
setne %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0xa93c
jmp 0xa94d
movq -0x50(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0xa700
movl %eax, -0x20(%rbp)
jmp 0xa906
movq -0x50(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0xa730
cmpl $-0x1, %eax
je 0xa987
movq -0x50(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0xa7c0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
jmp 0xa989
movl -0x20(%rbp), %eax
cmpl -0xc(%rbp), %eax
je 0xa9ea
movq -0x50(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0xa7c0
movq -0x50(%rbp), %rdi
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %esi
callq 0xa7c0
movq -0x50(%rbp), %rdi
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl 0x8(%rax), %esi
callq 0xa7c0
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x20(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x20(%rbp)
jmp 0xa989
jmp 0xaa1a
movq -0x18(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
je 0xaa02
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x1c(%rbp)
jmp 0xaa18
movq -0x18(%rbp), %rax
cmpl $-0x1, 0x8(%rax)
je 0xaa16
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x1c(%rbp)
jmp 0xaa18
jmp 0xaa1a
cmpl $-0x1, -0x1c(%rbp)
je 0xaa3a
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x1c(%rbp), %esi
callq 0xa7c0
movl -0x58(%rbp), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
cmpl $-0x1, (%rax)
jne 0xaa4e
movq -0x50(%rbp), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xaa85
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
callq 0xa7c0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0xaa79
movl -0x1c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0xaa83
movl -0x1c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0xaa85
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)