// File: /workspace/github_cpp_makefile_star2_O1/nishalkulkarni[P]fluid-wm/fluid_wm.cpp
// Function: WindowManager::Frame(unsigned long, bool)
void WindowManager::Frame(Window w, bool was_created_before_window_manager)
{
    assert(!clients_.count(w));

    XWindowAttributes x_window_attrs;
    assert(XGetWindowAttributes(display_, w, &x_window_attrs));

    if (was_created_before_window_manager) {
        if (x_window_attrs.override_redirect || x_window_attrs.map_state != IsViewable) {
            return;
        }
    }

    Node* parent_window;
    Node* new_window;
    if (clients_.size() == 0) {
        Window returned_root;
        int root_x, root_y;
        unsigned root_width, root_height, root_border_width, root_depth;

        assert(XGetGeometry(
            display_,
            root_,
            &returned_root,
            &root_x, &root_y,
            &root_width, &root_height,
            &root_border_width,
            &root_depth));

        std::tie(parent_window, new_window) = bsp->init_root(w, root_width, root_height, root_x, root_y);
    } else {
        std::tie(parent_window, new_window) = bsp->create_node(w);
    }

    int window_width, window_height, window_x, window_y;
    std::tie(window_width, window_height, window_x, window_y) = new_window->get_dimensions();
    std::cout << "\n\nhello" << window_width << " 0 " << window_height << "\n";
    std::cout << "bye" << window_x << " 0 " << window_y << "\n\n";

    bsp->display_tree();

    if (parent_window != nullptr) {
        int parent_width, parent_height, parent_x, parent_y;
        std::tie(parent_width, parent_height, parent_x, parent_y) = parent_window->get_dimensions();
        XResizeWindow(
            display_,
            clients_[parent_window->get_window()],
            parent_width - 2 * (BORDER_WIDTH + GAP), parent_height - 2 * (BORDER_WIDTH + GAP));
        XResizeWindow(
            display_,
            parent_window->get_window(),
            parent_width - 2 * (BORDER_WIDTH + GAP), parent_height - 2 * (BORDER_WIDTH + GAP));
    }

    XResizeWindow(
        display_,
        w,
        window_width - 2 * (BORDER_WIDTH + GAP), window_height - 2 * (BORDER_WIDTH + GAP));

    const Window frame = XCreateSimpleWindow(
        display_,
        root_,
        window_x + GAP,
        window_y + GAP,
        window_width - 2 * (BORDER_WIDTH + GAP),
        window_height - 2 * (BORDER_WIDTH + GAP),
        BORDER_WIDTH,
        BORDER_ACTIVE_COLOR,
        BG_COLOR);

    XSelectInput(
        display_,
        frame,
        SubstructureRedirectMask | SubstructureNotifyMask);

    XAddToSaveSet(display_, w);

    XReparentWindow(
        display_,
        w,
        frame,
        0, 0);

    XMapWindow(display_, frame);

    clients_[w] = frame;
    std::cout << " Map size " << clients_.size() << std::endl;
    for (auto i : clients_) {
        std::cout << i.first << " " << i.second << std::endl;
    }

    XSync(display_,false);
    //   Kill windows with alt + f4.
    XGrabKey(
        display_,
        XKeysymToKeycode(display_, XK_F4),
        MODKEY,
        w,
        false,
        GrabModeAsync,
        GrabModeAsync);
    //   Switch windows with alt + tab.
    XGrabKey(
        display_,
        XKeysymToKeycode(display_, XK_Tab),
        MODKEY,
        w,
        false,
        GrabModeAsync,
        GrabModeAsync);

    std::cout << "Framed window " << w << " [" << frame << "]\n\n";
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %edx, %ebp
movq %rdi, %rbx
leaq 0x20(%rsp), %rax
movq %rsi, (%rax)
addq $0x10, %rdi
movq %rdi, 0x28(%rsp)
movq %rax, %rsi
callq 0x93fc
testq %rax, %rax
jne 0x8bc7
movq (%rbx), %rdi
movq 0x20(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x3440
testl %eax, %eax
je 0x8be6
testb %bpl, %bpl
je 0x8778
cmpl $0x0, 0xf8(%rsp)
jne 0x8bb5
cmpl $0x2, 0xdc(%rsp)
jne 0x8bb5
cmpq $0x0, 0x28(%rbx)
je 0x8797
movq 0x48(%rbx), %rsi
movq 0x20(%rsp), %rdx
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x71c8
jmp 0x8807
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x74(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, (%rsp)
leaq 0x78(%rsp), %rdx
leaq 0x4c(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x44(%rsp), %r9
callq 0x3360
testl %eax, %eax
je 0x8c05
movq 0x48(%rbx), %rsi
movq 0x20(%rsp), %rdx
movl 0x44(%rsp), %ecx
movl 0x40(%rsp), %r8d
movl 0x4c(%rsp), %r9d
movl 0x48(%rsp), %eax
movl %eax, (%rsp)
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x70e4
movq (%r14), %rsi
movq 0x8(%r14), %rax
movq %rax, 0x50(%rsp)
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x6f1a
movl 0xc(%r14), %r12d
movl 0x8(%r14), %r15d
movl (%r14), %r13d
movl 0x4(%r14), %ebp
movq 0x67a2(%rip), %r14      # 0xefd8
leaq 0x26f2(%rip), %rsi      # 0xaf2f
movl $0x7, %edx
movq %r14, %rdi
callq 0x32a0
movq %r14, %rdi
movl %r12d, 0x34(%rsp)
movl %r12d, %esi
callq 0x3410
movq %rax, %r12
leaq 0x26d3(%rip), %rsi      # 0xaf37
movl $0x3, %edx
movq %rax, %rdi
callq 0x32a0
movq %r12, %rdi
movl %r15d, %esi
callq 0x3410
leaq 0x2898(%rip), %rsi      # 0xb11b
movl $0x1, %edx
movq %rax, %rdi
callq 0x32a0
leaq 0x26a4(%rip), %rsi      # 0xaf3b
movl $0x3, %edx
movq %r14, %rdi
callq 0x32a0
movq %r14, %rdi
movl %ebp, 0x38(%rsp)
movl %ebp, %esi
callq 0x3410
movq %rax, %r14
movl $0x3, %edx
movq %rax, %rdi
leaq 0x2673(%rip), %rsi      # 0xaf37
callq 0x32a0
movq %r14, %rdi
movl %r13d, 0x3c(%rsp)
movl %r13d, %esi
callq 0x3410
leaq 0x283a(%rip), %rsi      # 0xb11a
movl $0x2, %edx
movq %rax, %rdi
callq 0x32a0
movq 0x48(%rbx), %rdi
callq 0x73fa
movq 0x50(%rsp), %rbp
testq %rbp, %rbp
je 0x8972
leaq 0x58(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6f1a
movl 0x8(%r13), %r14d
movl 0xc(%r13), %r12d
movq (%rbx), %rax
movq %rax, 0x68(%rsp)
movq %rbp, %rdi
callq 0x6f34
movq %rax, (%r13)
leaq 0x58(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x9478
movq (%rax), %rsi
addl $-0xe, %r12d
addl $-0xe, %r14d
movq 0x68(%rsp), %rdi
movl %r12d, %edx
movl %r14d, %ecx
callq 0x3350
movq (%rbx), %r13
movq %rbp, %rdi
callq 0x6f34
movq %r13, %rdi
movq %rax, %rsi
movl %r12d, %edx
movl %r14d, %ecx
callq 0x3350
movq (%rbx), %rdi
leaq 0x20(%rsp), %r14
movq (%r14), %rsi
movl 0x34(%rsp), %ebp
addl $-0xe, %ebp
addl $-0xe, %r15d
movl %ebp, %edx
movl %r15d, %ecx
callq 0x3350
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movl 0x38(%rsp), %edx
addl $0x5, %edx
movl 0x3c(%rsp), %ecx
addl $0x5, %ecx
movq $0x2e3440, 0x10(%rsp)   # imm = 0x2E3440
movq $0x81a1c1, 0x8(%rsp)    # imm = 0x81A1C1
movl $0x2, (%rsp)
movl %ebp, %r8d
movl %r15d, %r9d
callq 0x3070
movq %rax, %r15
movq (%rbx), %rdi
movl $0x180000, %edx         # imm = 0x180000
movq %rax, %rsi
callq 0x3150
movq (%rbx), %rdi
movq (%r14), %rsi
callq 0x32d0
movq (%rbx), %rdi
movq (%r14), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3030
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x3280
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x96f8
movq %r15, 0x28(%rsp)
movq %r15, (%rax)
movq 0x65b5(%rip), %r14      # 0xefd8
leaq 0x2515(%rip), %rsi      # 0xaf3f
movl $0xa, %edx
movq %r14, %rdi
callq 0x32a0
movq 0x28(%rbx), %rsi
movq %r14, %rdi
callq 0x3170
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3180
movq 0x20(%rbx), %r15
testq %r15, %r15
je 0x8ae4
movq 0x655b(%rip), %r14      # 0xefd8
leaq 0x19fe(%rip), %r12      # 0xa482
movq 0x8(%r15), %rsi
movq 0x10(%r15), %r13
movq %r14, %rdi
callq 0x3170
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x32a0
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3170
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3180
movq (%r15), %r15
testq %r15, %r15
jne 0x8a84
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x3480
movq (%rbx), %r14
movl $0xffc1, %esi           # imm = 0xFFC1
movq %r14, %rdi
callq 0x3140
movzbl %al, %esi
movq 0x20(%rsp), %rcx
movl $0x1, %ebp
movl %ebp, (%rsp)
movq %r14, %rdi
movl $0x8, %edx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x3210
movq (%rbx), %rbx
movl $0xff09, %esi           # imm = 0xFF09
movq %rbx, %rdi
callq 0x3140
movzbl %al, %esi
movq 0x20(%rsp), %rcx
movl %ebp, (%rsp)
movq %rbx, %rdi
movl $0x8, %edx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x3210
movq 0x647c(%rip), %rbx      # 0xefd8
leaq 0x23e7(%rip), %rsi      # 0xaf4a
movl $0xe, %edx
movq %rbx, %rdi
callq 0x32a0
movq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x3170
movq %rax, %rbx
leaq 0x2474(%rip), %rsi      # 0xaffb
movl $0x2, %edx
movq %rax, %rdi
callq 0x32a0
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x3170
leaq 0x23b1(%rip), %rsi      # 0xaf59
movl $0x3, %edx
movq %rax, %rdi
callq 0x32a0
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2276(%rip), %rdi      # 0xae44
leaq 0x2212(%rip), %rsi      # 0xade7
leaq 0x227b(%rip), %rcx      # 0xae57
movl $0x82, %edx
callq 0x3160
leaq 0x2292(%rip), %rdi      # 0xae7f
leaq 0x21f3(%rip), %rsi      # 0xade7
leaq 0x225c(%rip), %rcx      # 0xae57
movl $0x85, %edx
callq 0x3160
leaq 0x22a6(%rip), %rdi      # 0xaeb2
leaq 0x21d4(%rip), %rsi      # 0xade7
leaq 0x223d(%rip), %rcx      # 0xae57
movl $0x9b, %edx
callq 0x3160