// File: nishalkulkarni[P]fluid-wm/bsp.cpp
// Function: BSP::find_to_delete_node(unsigned long, Node*)
std::tuple<Node*, Node*> BSP::find_to_delete_node(Window w, Node* current)
{
    if (current == nullptr) {
        std::cout << "Node to delete not found.\n";
        return std::make_tuple(nullptr, nullptr);
    }

    if (current->is_leaf() && current == root && current->get_window() == w) {
        return std::make_tuple(current, nullptr);
    }

    if (current->is_leaf() == false && current->get_left_child()->is_leaf()
        && current->get_left_child()->get_window() == w) {
        return std::make_tuple(current->get_left_child(), current);
    } else if (current->is_leaf() == false && current->get_right_child()->is_leaf()
        && current->get_right_child()->get_window() == w) {
        return std::make_tuple(current->get_right_child(), current);
    } else {
        Node* foundWindow;
        Node* foundParentWindow;
        std::tie(foundWindow, foundParentWindow) = find_to_delete_node(w, current->get_right_child());
        if (foundWindow == nullptr && foundParentWindow == nullptr) {
            std::tie(foundWindow, foundParentWindow) = find_to_delete_node(w, current->get_left_child());
        }
        return std::make_tuple(foundWindow, foundParentWindow);
    }
    return std::make_tuple(nullptr, nullptr);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0xd0(%rbp)
movq %rdi, %rax
movq %rax, -0xc8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xc5c7
movq 0x16a4b(%rip), %rdi     # 0x22fd0
leaq 0xb469(%rip), %rsi      # 0x179f5
callq 0x4240
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0xcbb0
movq -0xd0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0xcbf0
jmp 0xc7c3
movq -0x20(%rbp), %rdi
callq 0xbfd0
testb $0x1, %al
jne 0xc5d6
jmp 0xc623
movq -0xc0(%rbp), %rcx
movq -0x20(%rbp), %rax
cmpq (%rcx), %rax
jne 0xc623
movq -0x20(%rbp), %rdi
callq 0xbf70
cmpq -0x18(%rbp), %rax
jne 0xc623
movq $0x0, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0xcc20
movq -0xd0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0xcc60
jmp 0xc7c3
movq -0x20(%rbp), %rdi
callq 0xbfd0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xc68a
movq -0x20(%rbp), %rdi
callq 0xbf90
movq %rax, %rdi
callq 0xbfd0
testb $0x1, %al
jne 0xc64d
jmp 0xc68a
movq -0x20(%rbp), %rdi
callq 0xbf90
movq %rax, %rdi
callq 0xbf70
cmpq -0x18(%rbp), %rax
jne 0xc68a
movq -0x20(%rbp), %rdi
callq 0xbf90
movq -0xd0(%rbp), %rdi
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rsi
leaq -0x20(%rbp), %rdx
callq 0xcc90
jmp 0xc7c3
movq -0x20(%rbp), %rdi
callq 0xbfd0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xc6f1
movq -0x20(%rbp), %rdi
callq 0xbfb0
movq %rax, %rdi
callq 0xbfd0
testb $0x1, %al
jne 0xc6b4
jmp 0xc6f1
movq -0x20(%rbp), %rdi
callq 0xbfb0
movq %rax, %rdi
callq 0xbf70
cmpq -0x18(%rbp), %rax
jne 0xc6f1
movq -0x20(%rbp), %rdi
callq 0xbfb0
movq -0xd0(%rbp), %rdi
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %rsi
leaq -0x20(%rbp), %rdx
callq 0xcc90
jmp 0xc7c3
movq -0x18(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rdi
callq 0xbfb0
movq -0xc0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
movq %rax, %rcx
leaq -0x88(%rbp), %rdi
callq 0xc540
leaq -0x98(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x78(%rbp), %rdx
callq 0xccd0
leaq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0xcd10
cmpq $0x0, -0x70(%rbp)
jne 0xc7af
cmpq $0x0, -0x78(%rbp)
jne 0xc7af
movq -0x18(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x20(%rbp), %rdi
callq 0xbf90
movq -0xc0(%rbp), %rsi
movq -0xe0(%rbp), %rdx
movq %rax, %rcx
leaq -0xa8(%rbp), %rdi
callq 0xc540
leaq -0xb8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x78(%rbp), %rdx
callq 0xccd0
leaq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0xcd10
movq -0xd0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x78(%rbp), %rdx
callq 0xcb00
movq -0xc8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)