// File: nishalkulkarni[P]fluid-wm/bsp.cpp
// Function: BSP::delete_node(unsigned long)
std::tuple<Node*, Node*> BSP::delete_node(Window w)
{
    Node* foundWindow;
    Node* foundParentWindow;
    std::tie(foundWindow, foundParentWindow) = find_to_delete_node(w, root);

    if (foundWindow == nullptr && foundParentWindow == nullptr) {
        std::cout << "No node found :((.\n";
        return std::make_tuple(nullptr, nullptr);
    } else if (foundWindow != nullptr && foundParentWindow == nullptr) {
        delete root;
        root = nullptr;
        return std::make_tuple(nullptr, nullptr);
    } else {
        if (foundWindow == foundParentWindow->get_left_child()) {
            delete foundWindow;
            foundParentWindow = foundParentWindow->get_right_child();
        } else if (foundWindow == foundParentWindow->get_right_child()) {
            delete foundWindow;
            foundParentWindow = foundParentWindow->get_left_child();
        }
    }

    return std::make_tuple(nullptr, nullptr);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0xc0(%rbp)
movq %rdi, %rax
movq %rax, -0xb8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0xb0(%rbp)
movq -0x18(%rbp), %rdx
movq (%rsi), %rcx
leaq -0x38(%rbp), %rdi
callq 0xc540
leaq -0x48(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x28(%rbp), %rdx
callq 0xccd0
leaq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xcd10
cmpq $0x0, -0x20(%rbp)
jne 0xc898
cmpq $0x0, -0x28(%rbp)
jne 0xc898
movq 0x1677a(%rip), %rdi     # 0x22fd0
leaq 0xb1b3(%rip), %rsi      # 0x17a10
callq 0x4240
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0xcbb0
movq -0xc0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xcbf0
jmp 0xca07
cmpq $0x0, -0x20(%rbp)
je 0xc918
cmpq $0x0, -0x28(%rbp)
jne 0xc918
movq -0xb0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0xc8ce
movq -0xc8(%rbp), %rdi
movl $0x28, %esi
callq 0x4270
movq -0xb0(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x88(%rbp), %rdx
callq 0xcbb0
movq -0xc0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xcbf0
jmp 0xca07
movq -0x20(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x28(%rbp), %rdi
callq 0xbf90
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jne 0xc96c
movq -0x20(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, %rax
je 0xc95d
movq -0xd8(%rbp), %rdi
movl $0x28, %esi
callq 0x4270
movq -0x28(%rbp), %rdi
callq 0xbfb0
movq %rax, -0x28(%rbp)
jmp 0xc9c0
movq -0x20(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x28(%rbp), %rdi
callq 0xbfb0
movq %rax, %rcx
movq -0xe0(%rbp), %rax
cmpq %rcx, %rax
jne 0xc9be
movq -0x20(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, %rax
je 0xc9b1
movq -0xe8(%rbp), %rdi
movl $0x28, %esi
callq 0x4270
movq -0x28(%rbp), %rdi
callq 0xbf90
movq %rax, -0x28(%rbp)
jmp 0xc9c0
jmp 0xc9c2
jmp 0xc9c4
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
callq 0xcbb0
movq -0xc0(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xcbf0
movq -0xb8(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
nopw (%rax,%rax)