// File: alex-courtis[P]xlayoutdisplay/src/xrandrrutil.cpp
// Function: discoverOutputs[abi:cxx11]()
const list<shared_ptr<Output>> discoverOutputs() {
    list<shared_ptr<Output>> outputs;

    // get the display
    Display *dpy = XOpenDisplay(nullptr);
    if (!dpy)
        throw domain_error(string("unable to open display '") + XDisplayName(nullptr) + "'");

    // get the root window
    int screen = DefaultScreen(dpy);
    Window rootWindow = RootWindow(dpy, screen);

    // get RandR resources
    XRRScreenResources *screenResources = XRRGetScreenResources(dpy, rootWindow);

    // iterate outputs
    for (int i = 0; i < screenResources->noutput; i++) {
        Output::State state;
        list<std::shared_ptr<const Mode>> modes;
        std::shared_ptr<Mode> currentMode, preferredMode;
        shared_ptr<Pos> currentPos;
        shared_ptr<Edid> edid;

        // current state
        const RROutput rrOutput = screenResources->outputs[i];
        const XRROutputInfo *outputInfo = XRRGetOutputInfo(dpy, screenResources, rrOutput);
        const char *name = outputInfo->name;
        RRMode rrMode = 0;
        if (outputInfo->crtc != 0) {
            // active outputs have CRTC info
            state = Output::active;

            // current position and mode
            XRRCrtcInfo *crtcInfo = XRRGetCrtcInfo(dpy, screenResources, outputInfo->crtc);
            currentPos = make_shared<Pos>(crtcInfo->x, crtcInfo->y);
            rrMode = crtcInfo->mode;
            currentMode = shared_ptr<Mode>(modeFromXRR(rrMode, screenResources));

            if (outputInfo->nmode == 0) {
                // output is active but has been disconnected
                state = Output::disconnected;
            }
        } else if (outputInfo->nmode != 0) {
            // inactive connected outputs have modes available
            state = Output::connected;
        } else {
            state = Output::disconnected;
        }

        // iterate all properties to find EDID; XRRQueryOutputProperty fails when queried with XInternAtom
        int nprop;
        Atom *atoms = XRRListOutputProperties(dpy, rrOutput, &nprop);
        for (int j = 0; j < nprop; j++) {
            Atom atom = atoms[j];
            char *atomName = XGetAtomName(dpy, atom);

            // drill down on Edid
            if (strcmp(atomName, RR_PROPERTY_RANDR_EDID) == 0) {

                // retrieve property specifics
                Atom actualType;
                int actualFormat;
                unsigned long nitems, bytesAfter;
                unsigned char *prop;
                XRRGetOutputProperty(dpy, rrOutput, atom,
                                     0, // offset
                                     64, // length in CARD32 - EDID 2.0 max length is 256 bytes
                                     false, // delete
                                     false, // pending
                                     AnyPropertyType, &actualType, &actualFormat, &nitems, &bytesAfter, &prop
                );

                // record Edid
                edid = make_shared<Edid>(prop, nitems, name);
            }
        }

        // add available modes
        for (int j = 0; j < outputInfo->nmode; j++) {

            // add to modes
            const auto &mode = shared_ptr<Mode>(modeFromXRR(outputInfo->modes[j], screenResources));
            modes.push_back(mode);

            // (optional) preferred mode based on outputInfo->modes indexed by 1
            if (outputInfo->npreferred == j + 1)
                preferredMode = mode;

            // replace currentMode with the one from the list
            if (mode->rrMode == rrMode)
                currentMode = mode;
        }

        // add the output
        outputs.push_back(make_shared<Output>(name, state, modes, currentMode, preferredMode, currentPos, edid));
    }

    return outputs;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x200(%rbp)
movq %rdi, %rax
movq %rax, -0x1f8(%rbp)
movq %rdi, -0x8(%rbp)
movb $0x0, -0x9(%rbp)
callq 0x12300
xorl %eax, %eax
movl %eax, %edi
callq 0x7730
movq %rax, -0x1f0(%rbp)
jmp 0x11a8b
movq -0x1f0(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x11bee
movb $0x1, -0x8a(%rbp)
movl $0x10, %edi
callq 0x7260
movq %rax, -0x210(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x7940
movq -0x208(%rbp), %rdx
leaq 0x2ec10(%rip), %rsi     # 0x406ea
leaq -0x88(%rbp), %rdi
callq 0x8150
jmp 0x11ae8
xorl %eax, %eax
movl %eax, %edi
callq 0x77e0
movq %rax, -0x218(%rbp)
jmp 0x11afa
movq -0x218(%rbp), %rdx
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x8100
jmp 0x11b13
leaq 0x2ebce(%rip), %rdx     # 0x406e8
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x8100
jmp 0x11b29
movq -0x210(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x7630
jmp 0x11b3b
movq -0x210(%rbp), %rdi
movb $0x0, -0x8a(%rbp)
movq 0x4c408(%rip), %rsi     # 0x5df58
movq 0x4c421(%rip), %rdx     # 0x5df78
callq 0x78c0
jmp 0x12251
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x1223c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x11bc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x11bba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x11bb1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x7320
leaq -0x68(%rbp), %rdi
callq 0x7320
leaq -0x88(%rbp), %rdi
callq 0x7320
leaq -0x89(%rbp), %rdi
callq 0x7550
testb $0x1, -0x8a(%rbp)
jne 0x11bdd
jmp 0x11be9
movq -0x210(%rbp), %rdi
callq 0x73c0
jmp 0x1223c
movq -0x18(%rbp), %rax
movl 0xe0(%rax), %eax
movl %eax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq 0xe8(%rax), %rax
movslq -0x90(%rbp), %rcx
shlq $0x7, %rcx
movq 0x10(%rax,%rcx), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x74c0
movq %rax, -0x220(%rbp)
jmp 0x11c39
movq -0x220(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movq -0xa0(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x12216
leaq -0xc0(%rbp), %rdi
callq 0xfd00
leaq -0xd0(%rbp), %rdi
callq 0x12320
leaq -0xe0(%rbp), %rdi
callq 0x12320
leaq -0xf0(%rbp), %rdi
callq 0x12340
leaq -0x100(%rbp), %rdi
callq 0x12360
movq -0xa0(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0xa4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x108(%rbp)
movq -0x18(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x108(%rbp), %rdx
callq 0x7100
movq %rax, -0x228(%rbp)
jmp 0x11ce0
movq -0x228(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x118(%rbp)
movq $0x0, -0x120(%rbp)
movq -0x110(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x11e2d
movl $0x0, -0xa8(%rbp)
movq -0x18(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x110(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x76b0
movq %rax, -0x230(%rbp)
jmp 0x11d4b
movq -0x230(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rdx
movq %rdx, %rsi
addq $0x8, %rsi
addq $0xc, %rdx
leaq -0x138(%rbp), %rdi
callq 0xc380
jmp 0x11d79
leaq -0xf0(%rbp), %rdi
leaq -0x138(%rbp), %rsi
movq %rsi, -0x240(%rbp)
callq 0x12380
movq -0x240(%rbp), %rdi
callq 0xc430
movq -0x128(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x117d0
movq %rax, -0x238(%rbp)
jmp 0x11dcd
movq -0x238(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x123b0
jmp 0x11de2
leaq -0xd0(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x123e0
leaq -0x148(%rbp), %rdi
callq 0x12410
movq -0x110(%rbp), %rax
cmpl $0x0, 0x50(%rax)
jne 0x11e2b
movl $0x2, -0xa8(%rbp)
jmp 0x11e2b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x121d8
jmp 0x11e52
movq -0x110(%rbp), %rax
cmpl $0x0, 0x50(%rax)
je 0x11e46
movl $0x1, -0xa8(%rbp)
jmp 0x11e50
movl $0x2, -0xa8(%rbp)
jmp 0x11e52
movq -0x18(%rbp), %rdi
movq -0x108(%rbp), %rsi
leaq -0x14c(%rbp), %rdx
callq 0x7590
movq %rax, -0x248(%rbp)
jmp 0x11e72
movq -0x248(%rbp), %rax
movq %rax, -0x158(%rbp)
movl $0x0, -0x15c(%rbp)
movl -0x15c(%rbp), %eax
cmpl -0x14c(%rbp), %eax
jge 0x11fbd
movq -0x158(%rbp), %rax
movslq -0x15c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x168(%rbp)
movq -0x18(%rbp), %rdi
movq -0x168(%rbp), %rsi
callq 0x79f0
movq %rax, -0x250(%rbp)
jmp 0x11ece
movq -0x250(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rdi
leaq 0x2e819(%rip), %rsi     # 0x40703
callq 0x7750
cmpl $0x0, %eax
jne 0x11fa7
movq -0x18(%rbp), %rdi
movq -0x108(%rbp), %rsi
movq -0x168(%rbp), %rdx
movq %rsp, %rax
leaq -0x198(%rbp), %rcx
movq %rcx, 0x30(%rax)
leaq -0x190(%rbp), %rcx
movq %rcx, 0x28(%rax)
leaq -0x188(%rbp), %rcx
movq %rcx, 0x20(%rax)
leaq -0x17c(%rbp), %rcx
movq %rcx, 0x18(%rax)
leaq -0x178(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq $0x0, 0x8(%rax)
movl $0x0, (%rax)
xorl %r9d, %r9d
movl %r9d, %ecx
movl $0x40, %r8d
callq 0x7530
jmp 0x11f65
leaq -0x1a8(%rbp), %rdi
leaq -0x198(%rbp), %rsi
leaq -0x188(%rbp), %rdx
leaq -0x118(%rbp), %rcx
callq 0x12430
jmp 0x11f88
leaq -0x100(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x124b0
leaq -0x1a8(%rbp), %rdi
callq 0x124e0
jmp 0x11fa9
movl -0x15c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x15c(%rbp)
jmp 0x11e8a
movl $0x0, -0x1ac(%rbp)
movl -0x1ac(%rbp), %eax
movq -0x110(%rbp), %rcx
cmpl 0x50(%rcx), %eax
jge 0x12106
movq -0x110(%rbp), %rax
movq 0x58(%rax), %rax
movslq -0x1ac(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0xa0(%rbp), %rsi
callq 0x117d0
movq %rax, -0x258(%rbp)
jmp 0x12008
movq -0x258(%rbp), %rsi
leaq -0x1c8(%rbp), %rdi
callq 0x123b0
jmp 0x1201d
leaq -0x1c8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rsi
leaq -0x1d8(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x12540
movq -0x260(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x12500
jmp 0x1205a
leaq -0x1d8(%rbp), %rdi
callq 0x9820
movq -0x110(%rbp), %rax
movl 0x54(%rax), %eax
movl -0x1ac(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x120bb
movq -0x1b8(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x12570
jmp 0x120bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x9820
leaq -0x1c8(%rbp), %rdi
callq 0x12410
jmp 0x121d8
movq -0x1b8(%rbp), %rdi
callq 0x125a0
movq (%rax), %rax
cmpq -0x120(%rbp), %rax
jne 0x120e6
movq -0x1b8(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x12570
leaq -0x1c8(%rbp), %rdi
callq 0x12410
movl -0x1ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ac(%rbp)
jmp 0x11fc7
movq %rsp, %rax
leaq -0x100(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0xf0(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x1e8(%rbp), %rdi
leaq -0x118(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
leaq -0xc0(%rbp), %rcx
leaq -0xd0(%rbp), %r8
leaq -0xe0(%rbp), %r9
callq 0x12600
jmp 0x1214f
movq -0x200(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x125c0
jmp 0x12164
leaq -0x1e8(%rbp), %rdi
callq 0xc330
leaq -0x100(%rbp), %rdi
callq 0x124e0
leaq -0xf0(%rbp), %rdi
callq 0xc430
leaq -0xe0(%rbp), %rdi
callq 0x12410
leaq -0xd0(%rbp), %rdi
callq 0x12410
leaq -0xc0(%rbp), %rdi
callq 0x9860
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x11c51
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0xc330
leaq -0x100(%rbp), %rdi
callq 0x124e0
leaq -0xf0(%rbp), %rdi
callq 0xc430
leaq -0xe0(%rbp), %rdi
callq 0x12410
leaq -0xd0(%rbp), %rdi
callq 0x12410
leaq -0xc0(%rbp), %rdi
callq 0x9860
jmp 0x1223c
movb $0x1, -0x9(%rbp)
testb $0x1, -0x9(%rbp)
jne 0x1222c
movq -0x200(%rbp), %rdi
callq 0xc280
movq -0x1f8(%rbp), %rax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
movq -0x200(%rbp), %rdi
callq 0xc280
movq -0x20(%rbp), %rdi
callq 0x7930
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)