// File: alex-courtis[P]xlayoutdisplay/src/calculations.cpp
// Function: mirrorOutputs(std::__cxx11::list<std::shared_ptr<Output>, std::allocator<std::shared_ptr<Output>>> const&)
void mirrorOutputs(const list<shared_ptr<Output>> &outputs) {

    // find the first active output
    shared_ptr<Output> firstOutput;
    for (const auto &output : outputs) {
        if (output->desiredActive) {
            firstOutput = output;
            break;
        }
    }
    if (!firstOutput)
        return;

    // iterate through first active output's modes
    for (const auto &possibleMode : reverseSort(firstOutput->modes)) {
        bool matched = true;

        // attempt to match mode to each active output
        for (const auto &output : outputs) {
            if (!output->desiredActive)
                continue;

            // reset failed matches
            shared_ptr<const Mode> desiredMode;

            // match height and width
            for (const auto &mode : reverseSort(output->modes)) {
                if (mode->width == possibleMode->width && mode->height == possibleMode->height) {

                    // select best refresh
                    desiredMode = mode;
                    break;
                }
            }

            // match a mode for every output; root it at 0, 0
            matched = matched && desiredMode;
            if (matched) {
                output->desiredMode = desiredMode;
                output->desiredPos = make_shared<Pos>(0, 0);
                continue;
            }
        }

        // we've set desiredMode and desiredPos (zero) for all outputs, all done
        if (matched)
            return;
    }

    // couldn't find a common mode, exit
    throw runtime_error("unable to find common width/height for mirror");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
leaq -0x18(%rbp), %rdi
callq 0xc2c0
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0xbf80
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0xbfb0
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0xbfe0
testb $0x1, %al
jne 0xad7d
jmp 0xadb8
leaq -0x28(%rbp), %rdi
callq 0xc010
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0xc040
testb $0x1, 0x90(%rax)
je 0xadab
movq -0x38(%rbp), %rsi
leaq -0x18(%rbp), %rdi
callq 0xc300
jmp 0xadb8
jmp 0xadad
leaq -0x28(%rbp), %rdi
callq 0xc090
jmp 0xad6a
leaq -0x18(%rbp), %rdi
callq 0xc2e0
testb $0x1, %al
jne 0xadd1
movl $0x1, -0x3c(%rbp)
jmp 0xb1ab
leaq -0x18(%rbp), %rdi
callq 0xc040
movq %rax, %rsi
addq $0x28, %rsi
leaq -0x60(%rbp), %rdi
callq 0xc450
jmp 0xadec
leaq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x97a0
movq %rax, -0x78(%rbp)
movq -0x48(%rbp), %rdi
callq 0x97d0
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xa070
testb $0x1, %al
jne 0xae3c
movl $0x4, -0x3c(%rbp)
jmp 0xb13d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xb1bd
leaq -0x78(%rbp), %rdi
callq 0xa0f0
movq %rax, -0x88(%rbp)
movb $0x1, -0x89(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0xbf80
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
callq 0xbfb0
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0xbfe0
testb $0x1, %al
jne 0xaea0
jmp 0xb11b
leaq -0xa0(%rbp), %rdi
callq 0xc010
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0xc040
testb $0x1, 0x90(%rax)
jne 0xaecd
jmp 0xb10a
leaq -0xc0(%rbp), %rdi
callq 0x9570
movq -0xb0(%rbp), %rdi
callq 0xc040
movq %rax, %rsi
addq $0x28, %rsi
leaq -0xe0(%rbp), %rdi
callq 0xc450
jmp 0xaefa
leaq -0xe0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x97a0
movq %rax, -0xe8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x97d0
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xa070
testb $0x1, %al
jne 0xaf77
movl $0x8, -0x3c(%rbp)
jmp 0xb017
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x9820
leaq -0x60(%rbp), %rdi
callq 0x9860
jmp 0xb1bd
leaq -0xe8(%rbp), %rdi
callq 0xa0f0
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rdi
callq 0x8400
movl 0x8(%rax), %eax
movl %eax, -0x114(%rbp)
movq -0x88(%rbp), %rdi
callq 0x8400
movq %rax, %rcx
movl -0x114(%rbp), %eax
cmpl 0x8(%rcx), %eax
jne 0xb004
movq -0xf8(%rbp), %rdi
callq 0x8400
movl 0xc(%rax), %eax
movl %eax, -0x118(%rbp)
movq -0x88(%rbp), %rdi
callq 0x8400
movq %rax, %rcx
movl -0x118(%rbp), %eax
cmpl 0xc(%rcx), %eax
jne 0xb004
movq -0xf8(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0xc350
movl $0x8, -0x3c(%rbp)
jmp 0xb017
jmp 0xb006
leaq -0xe8(%rbp), %rdi
callq 0xa120
jmp 0xaf2e
leaq -0xe0(%rbp), %rdi
callq 0x9860
xorl %eax, %eax
testb $0x1, -0x89(%rbp)
movb %al, -0x119(%rbp)
je 0xb046
leaq -0xc0(%rbp), %rdi
callq 0x95b0
movb %al, -0x119(%rbp)
movb -0x119(%rbp), %al
andb $0x1, %al
movb %al, -0x89(%rbp)
testb $0x1, -0x89(%rbp)
je 0xb0ea
movq -0xb0(%rbp), %rdi
callq 0xc040
movq %rax, %rdi
addq $0x98, %rdi
leaq -0xc0(%rbp), %rsi
callq 0xc350
movl $0x0, -0x10c(%rbp)
movl $0x0, -0x110(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x10c(%rbp), %rsi
leaq -0x110(%rbp), %rdx
callq 0xc4e0
jmp 0xb0b3
movq -0xb0(%rbp), %rdi
callq 0xc040
movq %rax, %rdi
addq $0xa8, %rdi
leaq -0x108(%rbp), %rsi
callq 0xc400
leaq -0x108(%rbp), %rdi
callq 0xc430
movl $0x7, -0x3c(%rbp)
jmp 0xb0f1
movl $0x0, -0x3c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x9820
movl -0x3c(%rbp), %eax
testl %eax, %eax
je 0xb108
jmp 0xb106
jmp 0xb10a
jmp 0xb10a
leaq -0xa0(%rbp), %rdi
callq 0xc090
jmp 0xae84
testb $0x1, -0x89(%rbp)
je 0xb12d
movl $0x1, -0x3c(%rbp)
jmp 0xb13d
jmp 0xb12f
leaq -0x78(%rbp), %rdi
callq 0xa120
jmp 0xae0e
leaq -0x60(%rbp), %rdi
callq 0x9860
movl -0x3c(%rbp), %eax
subl $0x4, %eax
jne 0xb1ab
jmp 0xb150
movl $0x10, %edi
callq 0x7260
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x128(%rbp)
leaq 0x3518f(%rip), %rsi     # 0x402fd
callq 0x71c0
jmp 0xb175
movq -0x128(%rbp), %rdi
movq 0x52e6d(%rip), %rsi     # 0x5dff0
movq 0x52df6(%rip), %rdx     # 0x5df80
callq 0x78c0
jmp 0xb1cf
movq -0x128(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
callq 0x73c0
jmp 0xb1bd
leaq -0x18(%rbp), %rdi
callq 0xc330
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
leaq -0x18(%rbp), %rdi
callq 0xc330
movq -0x68(%rbp), %rdi
callq 0x7930
nop