// File: alex-courtis[P]xlayoutdisplay/src/calculations.cpp
// Function: activateOutputs(std::__cxx11::list<std::shared_ptr<Output>, std::allocator<std::shared_ptr<Output>>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Monitors const&)
const shared_ptr<Output> activateOutputs(const list<shared_ptr<Output>> &outputs, const string &desiredPrimary,
                                         const Monitors &monitors) {
    if (outputs.empty()) throw invalid_argument("activateOutputs received empty outputs");

    shared_ptr<Output> primary;

    for (const auto &output : outputs) {

        // don't activate any monitors that shouldn't
        if (monitors.shouldDisableOutput(output->name))
            continue;

        // only activate currently active or connected ouputs
        if (output->state != Output::active && output->state != Output::connected)
            continue;

        // mark active
        output->desiredActive = true;

        // default first to primary
        if (!primary)
            primary = output;

        // user selected primary
        if (!desiredPrimary.empty() && strcasecmp(desiredPrimary.c_str(), output->name.c_str()) == 0)
            primary = output;
    }

    if (!primary) throw runtime_error("no active or connected outputs found");

    return primary;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x60(%rbp)
movq %rdi, %rax
movq %rax, -0x58(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0xc2a0
testb $0x1, %al
jne 0xa9f5
jmp 0xaa48
movl $0x10, %edi
callq 0x7260
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x68(%rbp)
leaq 0x358a1(%rip), %rsi     # 0x402b1
callq 0x79d0
jmp 0xaa17
movq -0x68(%rbp), %rdi
movq 0x535a6(%rip), %rsi     # 0x5dfc8
movq 0x53587(%rip), %rdx     # 0x5dfb0
callq 0x78c0
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x73c0
jmp 0xac12
movq -0x60(%rbp), %rdi
movb $0x0, -0x2d(%rbp)
callq 0xc2c0
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0xbf80
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0xbfb0
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0xbfe0
testb $0x1, %al
jne 0xaa8d
jmp 0xab81
leaq -0x40(%rbp), %rdi
callq 0xc010
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rdi
callq 0xc040
movq -0x78(%rbp), %rdi
movq %rax, %rsi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
movb %al, -0x69(%rbp)
jmp 0xaabf
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0xaac8
jmp 0xaade
jmp 0xab73
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0xac09
movq -0x50(%rbp), %rdi
callq 0xc040
cmpl $0x0, 0x20(%rax)
je 0xaafe
movq -0x50(%rbp), %rdi
callq 0xc040
cmpl $0x1, 0x20(%rax)
je 0xaafe
jmp 0xab73
movq -0x50(%rbp), %rdi
callq 0xc040
movq -0x60(%rbp), %rdi
movb $0x1, 0x90(%rax)
callq 0xc2e0
testb $0x1, %al
jne 0xab28
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xc300
movq -0x18(%rbp), %rdi
callq 0x7890
testb $0x1, %al
jne 0xab71
movq -0x18(%rbp), %rdi
callq 0x71e0
movq %rax, -0x80(%rbp)
movq -0x50(%rbp), %rdi
callq 0xc040
movq %rax, %rdi
callq 0x71e0
movq -0x80(%rbp), %rdi
movq %rax, %rsi
callq 0x7570
cmpl $0x0, %eax
jne 0xab71
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xc300
jmp 0xab73
leaq -0x40(%rbp), %rdi
callq 0xc090
jmp 0xaa77
movq -0x60(%rbp), %rdi
callq 0xc2e0
testb $0x1, %al
jne 0xabe9
movl $0x10, %edi
callq 0x7260
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x88(%rbp)
leaq 0x3572c(%rip), %rsi     # 0x402d8
callq 0x71c0
jmp 0xabb3
movq -0x88(%rbp), %rdi
movq 0x5342f(%rip), %rsi     # 0x5dff0
movq 0x533b8(%rip), %rdx     # 0x5df80
callq 0x78c0
jmp 0xac1b
movq -0x88(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x73c0
jmp 0xac09
movb $0x1, -0x2d(%rbp)
testb $0x1, -0x2d(%rbp)
jne 0xabfc
movq -0x60(%rbp), %rdi
callq 0xc330
movq -0x58(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0xc330
movq -0x28(%rbp), %rdi
callq 0x7930
nopl (%rax,%rax)