// File: alex-courtis[P]xlayoutdisplay/src/layout.cpp
// Function: layout(Settings const&)
int layout(const Settings& settings) {
    // optional wait
    if (settings.wait) {
        if (!settings.quiet) {
            cout << "Waiting " << settings.wait << " seconds..." << endl;
        }
        sleep(settings.wait);
    }

    // discover monitors
    const Monitors monitors = Monitors();

    // discover outputs
    const list<shared_ptr<Output>> currentOutputs = discoverOutputs();
    if (currentOutputs.empty()) {
        throw runtime_error("no outputs found");
    }

    // output verbose information
    if (!settings.quiet || settings.info) {
        cout << renderUserInfo(currentOutputs) << "\n\n";
        cout << "laptop lid ";
        if (monitors.laptopLidClosed) {
            cout << "closed";
        }
        else {
            cout << "open or not present";
        }
        cout << "\n";
    }

    // current info is all output, we're done
    if (settings.info) {
        return EXIT_SUCCESS;
    }

    // order the outputs if the user wishes
    const list<shared_ptr<Output>> outputs = orderOutputs(currentOutputs, settings.order);

    // activate ouputs and determine primary
    const shared_ptr<Output> primary = activateOutputs(outputs, settings.primary, monitors);

    // arrange mirrored or left to right
    if (settings.mirror) {
        mirrorOutputs(outputs);
    }
    else {
        ltrOutputs(outputs);
    }

    // determine DPI from the primary
    string dpiExplaination;
    long dpi = calculateDpi(primary, &dpiExplaination);
    if (!settings.quiet) {
        cout << "\n" << dpiExplaination << "\n";
    }

    // user overrides DPI
    if (settings.dpi) {
        dpi = settings.dpi;
        cout << "overriding with provided DPI " << to_string(dpi) << "\n";
    }

    // user overrides refresh rate
    long rate = 0;
    if (settings.rate) {
        rate = settings.rate;
        cout << "overriding with provided refresh rate " << to_string(rate) << "\n";
    }

    // render desired commands
    const string xrandrCmd = renderXrandrCmd(outputs, primary, dpi, rate);
    const string xrdbCmd = renderXrdbCmd(dpi);
    if (!settings.quiet || settings.noop) {
        cout << "\n" << xrandrCmd << "\n\n" << xrdbCmd << "\n";
    }

    // execute
    if (!settings.noop) {
        // xrandr
        int rc = system(xrandrCmd.c_str());
        if (rc != 0) {
            return rc;
        }

        // xrdb
        rc = system(xrdbCmd.c_str());
        if (rc != 0) {
            return rc;
        }

        // update root window's cursor
        resetRootCursor();
    }
    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x10b24
movq -0x10(%rbp), %rax
testb $0x1, 0x50(%rax)
jne 0x10b15
movq 0x4d4bd(%rip), %rdi     # 0x5df98
leaq 0x2fa86(%rip), %rsi     # 0x40568
callq 0x74a0
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rsi
callq 0x7980
movq %rax, %rdi
leaq 0x2fa70(%rip), %rsi     # 0x40571
callq 0x74a0
movq %rax, %rdi
movq 0x4d440(%rip), %rsi     # 0x5df50
callq 0x7510
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl %eax, %edi
callq 0x73e0
leaq -0x20(%rbp), %rdi
callq 0x11190
leaq -0x38(%rbp), %rdi
callq 0x11a50
leaq -0x38(%rbp), %rdi
callq 0xc2a0
testb $0x1, %al
jne 0x10b45
jmp 0x10bb7
movl $0x10, %edi
callq 0x7260
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x158(%rbp)
leaq 0x2fa1a(%rip), %rsi     # 0x4057d
callq 0x71c0
jmp 0x10b6a
movq -0x158(%rbp), %rdi
movq 0x4d478(%rip), %rsi     # 0x5dff0
movq 0x4d401(%rip), %rdx     # 0x5df80
callq 0x78c0
jmp 0x11187
movq -0x158(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x73c0
jmp 0x11175
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x11175
movq -0x10(%rbp), %rax
testb $0x1, 0x50(%rax)
je 0x10bcf
movq -0x10(%rbp), %rax
testb $0x1, 0x10(%rax)
je 0x10c8f
leaq -0x68(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xb1d0
jmp 0x10bde
movq 0x4d3b3(%rip), %rdi     # 0x5df98
leaq -0x68(%rbp), %rsi
callq 0x7460
movq %rax, -0x160(%rbp)
jmp 0x10bf7
movq -0x160(%rbp), %rdi
leaq 0x2f989(%rip), %rsi     # 0x4058e
callq 0x74a0
jmp 0x10c0c
leaq -0x68(%rbp), %rdi
callq 0x7320
movq 0x4d37c(%rip), %rdi     # 0x5df98
leaq 0x2f96e(%rip), %rsi     # 0x40591
callq 0x74a0
jmp 0x10c2a
testb $0x1, -0x18(%rbp)
je 0x10c61
movq 0x4d361(%rip), %rdi     # 0x5df98
leaq 0x2f5da(%rip), %rsi     # 0x40218
callq 0x74a0
jmp 0x10c45
jmp 0x10c78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x7320
jmp 0x11175
movq 0x4d330(%rip), %rdi     # 0x5df98
leaq 0x2f92e(%rip), %rsi     # 0x4059d
callq 0x74a0
jmp 0x10c76
jmp 0x10c78
movq 0x4d319(%rip), %rdi     # 0x5df98
leaq 0x2f909(%rip), %rsi     # 0x4058f
callq 0x74a0
jmp 0x10c8d
jmp 0x10c8f
movq -0x10(%rbp), %rax
testb $0x1, 0x10(%rax)
je 0x10cac
movl $0x0, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
jmp 0x11160
movq -0x10(%rbp), %rdx
addq $0x18, %rdx
leaq -0x88(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xa760
jmp 0x10cc6
movq -0x10(%rbp), %rdx
addq $0x30, %rdx
leaq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0x20(%rbp), %rcx
callq 0xa9c0
jmp 0x10ce7
movq -0x10(%rbp), %rax
testb $0x1, 0x12(%rax)
je 0x10d23
leaq -0x88(%rbp), %rdi
callq 0xad30
jmp 0x10cff
jmp 0x10d33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x11152
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x11146
leaq -0x88(%rbp), %rdi
callq 0xac20
jmp 0x10d31
jmp 0x10d33
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x7740
movq -0x170(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0xb8c0
movq %rax, -0x168(%rbp)
jmp 0x10d62
movq -0x168(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rax
testb $0x1, 0x50(%rax)
jne 0x10dda
movq 0x4d217(%rip), %rdi     # 0x5df98
leaq 0x2f807(%rip), %rsi     # 0x4058f
callq 0x74a0
movq %rax, -0x178(%rbp)
jmp 0x10d96
movq -0x178(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x7460
movq %rax, -0x180(%rbp)
jmp 0x10db2
movq -0x180(%rbp), %rdi
leaq 0x2f7cf(%rip), %rsi     # 0x4058f
callq 0x74a0
jmp 0x10dc7
jmp 0x10dda
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1113a
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x10e83
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movq 0x4d19b(%rip), %rdi     # 0x5df98
leaq 0x2f7ad(%rip), %rsi     # 0x405b1
callq 0x74a0
movq %rax, -0x188(%rbp)
jmp 0x10e12
movq -0xc0(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x111d0
jmp 0x10e27
movq -0x188(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x7460
movq %rax, -0x190(%rbp)
jmp 0x10e43
movq -0x190(%rbp), %rdi
leaq 0x2f73e(%rip), %rsi     # 0x4058f
callq 0x74a0
jmp 0x10e58
leaq -0xe0(%rbp), %rdi
callq 0x7320
jmp 0x10e83
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x7320
jmp 0x1113a
movq $0x0, -0xe8(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x10f39
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xe8(%rbp)
movq 0x4d0e5(%rip), %rdi     # 0x5df98
leaq 0x2f715(%rip), %rsi     # 0x405cf
callq 0x74a0
movq %rax, -0x198(%rbp)
jmp 0x10ec8
movq -0xe8(%rbp), %rsi
leaq -0x108(%rbp), %rdi
callq 0x111d0
jmp 0x10edd
movq -0x198(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x7460
movq %rax, -0x1a0(%rbp)
jmp 0x10ef9
movq -0x1a0(%rbp), %rdi
leaq 0x2f688(%rip), %rsi     # 0x4058f
callq 0x74a0
jmp 0x10f0e
leaq -0x108(%rbp), %rdi
callq 0x7320
jmp 0x10f39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x7320
jmp 0x1113a
leaq -0x128(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0x98(%rbp), %rdx
leaq -0xc0(%rbp), %rcx
leaq -0xe8(%rbp), %r8
callq 0x113f0
jmp 0x10f63
leaq -0x148(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x14a00
jmp 0x10f78
movq -0x10(%rbp), %rax
testb $0x1, 0x50(%rax)
je 0x10f90
movq -0x10(%rbp), %rax
testb $0x1, 0x11(%rax)
je 0x11045
movq 0x4d001(%rip), %rdi     # 0x5df98
leaq 0x2f5f1(%rip), %rsi     # 0x4058f
callq 0x74a0
movq %rax, -0x1a8(%rbp)
jmp 0x10fac
movq -0x1a8(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x7460
movq %rax, -0x1b0(%rbp)
jmp 0x10fc8
movq -0x1b0(%rbp), %rdi
leaq 0x2f5b8(%rip), %rsi     # 0x4058e
callq 0x74a0
movq %rax, -0x1b8(%rbp)
jmp 0x10fe4
movq -0x1b8(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x7460
movq %rax, -0x1c0(%rbp)
jmp 0x11000
movq -0x1c0(%rbp), %rdi
leaq 0x2f581(%rip), %rsi     # 0x4058f
callq 0x74a0
jmp 0x11015
jmp 0x11045
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1112e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x7320
jmp 0x1112e
movq -0x10(%rbp), %rax
testb $0x1, 0x11(%rax)
jne 0x110e2
leaq -0x128(%rbp), %rdi
callq 0x71e0
movq %rax, %rdi
callq 0x7430
movl %eax, -0x1c4(%rbp)
jmp 0x1106f
movl -0x1c4(%rbp), %eax
movl %eax, -0x14c(%rbp)
cmpl $0x0, -0x14c(%rbp)
je 0x11096
movl -0x14c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
jmp 0x110f0
leaq -0x148(%rbp), %rdi
callq 0x71e0
movq %rax, %rdi
callq 0x7430
movl %eax, -0x1c8(%rbp)
jmp 0x110b2
movl -0x1c8(%rbp), %eax
movl %eax, -0x14c(%rbp)
cmpl $0x0, -0x14c(%rbp)
je 0x110d9
movl -0x14c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
jmp 0x110f0
callq 0x14ae0
jmp 0x110e0
jmp 0x110e2
movl $0x0, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x7320
leaq -0x128(%rbp), %rdi
callq 0x7320
leaq -0xb8(%rbp), %rdi
callq 0x7320
leaq -0x98(%rbp), %rdi
callq 0xc330
leaq -0x88(%rbp), %rdi
callq 0xc280
jmp 0x11160
leaq -0x128(%rbp), %rdi
callq 0x7320
leaq -0xb8(%rbp), %rdi
callq 0x7320
leaq -0x98(%rbp), %rdi
callq 0xc330
leaq -0x88(%rbp), %rdi
callq 0xc280
jmp 0x11175
leaq -0x38(%rbp), %rdi
callq 0xc280
movl -0x4(%rbp), %eax
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0xc280
movq -0x40(%rbp), %rdi
callq 0x7930
nopw (%rax,%rax)