// File: NeoResearch[P]dbft3-spork-detect/main.cpp
// Function: selections(std::vector<int, std::allocator<int>>, int)
vector<vector<pair<int, int>>> selections(vector<int> choices, int f)
{
    int N = 3 * f + 1;
    vector<vector<pair<int, int>>> sel(N, vector<pair<int, int>>(2 * f, make_pair(-2, -2)));
    // for each i in N, select possible 2f responses, in order
    for (int i = 0; i < N; i++)
    {
        if (choices[i] == -2) // faulty node
            continue;
        vector<int> choicesi = make2f(choices, f, i); // 2f responses for replica i
        for (unsigned j = 0; j < choicesi.size(); j++)
            sel[i][j] = make_pair(choicesi[j], choices[choicesi[j]]);
        // may have bug here
        // R_3 | 1(1) |    1(3)    0(0)    | Cancel 1
        // we must prefer response over request, if double response being made
        // I think this only applies to non-prepare nodes
        if (i > 0) // only backups (or primary 1) ... not primary zero
        {
            int prepZero = -1;
            int respZero = -1;
            int prepOne = -1;
            int respOne = -1;
            for (unsigned k = 0; k < sel[i].size(); k++)
            {
                if ((sel[i][k].second == 0) && (sel[i][k].first == 0)) // prepareZero
                    prepZero = k;
                if ((sel[i][k].second == 1) && (sel[i][k].first == 1)) // prepareOne
                    prepOne = k;
                if ((sel[i][k].second == 0) && (sel[i][k].first == i)) // respZero
                    respZero = k;
                if ((sel[i][k].second == 1) && (sel[i][k].first == i)) // respOne
                    respOne = k;
            }
            cout << "*** i=" << i << " | choice=" << choices[i] << " | ";
            for (unsigned k = 0; k < sel[i].size(); k++)
                cout << sel[i][k].second << "(" << sel[i][k].first << ") ";
            cout << endl;

            // disabling multiple response
            /*
            if (choices[i] == 0) // assert that my prepare and response is going out
            {
                //assert(prepZero>-1); // this is my choice, not in this vector (TODO: improve)
                assert(respZero > -1);
                // if re-proposing, must prioritize reply
                if ((prepOne > -1) && (respOne == -1))
                {
                    // propose but no reply, let's change
                    //sel[i][prepOne].first = i; // now I'm responding, not sending proposal only
                    sel[i].push_back(make_pair(i, 1)); // My response
                    //assert(respOne > -1); // THIS must be valid
                }
            }
            if (choices[i] == 1) // assert that my prepare and response is going out
            {
                //assert(prepOne>-1); // this is my choice, not in this vector (TODO: improve)
                if(i != 1) // 'one' does not respond to itself
                    assert(respOne > -1);
                // if re-proposing, must prioritize reply
                if ((prepZero > -1) && (respZero == -1))
                {
                    // propose but no reply, let's change
                    //sel[i][prepZero].first = i; // now I'm responding, not sending proposal only
                    sel[i].push_back(make_pair(i, 0)); // My response
                    //assert(respZero > -1); // THIS must be valid
                }
            }
            */
            // DISABLED: this generates forks

        } // backups
    }

    return sel;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rsi, -0xf0(%rbp)
movq %rdi, -0xe8(%rbp)
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax,2), %eax
movl %eax, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movslq -0x18(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0x14(%rbp), %eax
addl %eax, %eax
cltq
movq %rax, -0xd0(%rbp)
movl $0xfffffffe, -0x44(%rbp) # imm = 0xFFFFFFFE
movl $0xfffffffe, -0x48(%rbp) # imm = 0xFFFFFFFE
leaq -0x44(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x4a00
movq %rax, -0x40(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x4a30
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rcx
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x4a50
jmp 0x18c3
leaq -0x5d(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x4ad0
movq -0xe8(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0xf8(%rbp), %rcx
leaq -0x38(%rbp), %rdx
callq 0x4af0
jmp 0x18f3
leaq -0x5d(%rbp), %rdi
callq 0x4b70
leaq -0x38(%rbp), %rdi
callq 0x4b90
leaq -0x49(%rbp), %rdi
callq 0x4bf0
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x1e70
movq -0xf0(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x44d0
cmpl $-0x2, (%rax)
jne 0x1975
jmp 0x1e62
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x1967
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x5d(%rbp), %rdi
callq 0x4b70
leaq -0x38(%rbp), %rdi
callq 0x4b90
leaq -0x49(%rbp), %rdi
callq 0x4bf0
jmp 0x1ea2
movq -0xf0(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x4c10
jmp 0x198a
movl -0x14(%rbp), %edx
movl -0x64(%rbp), %ecx
leaq -0x80(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x16a0
jmp 0x19a2
leaq -0x98(%rbp), %rdi
callq 0x45b0
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq %rax, -0x100(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x44b0
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0x1ad4
movl -0x9c(%rbp), %eax
movl %eax, %esi
leaq -0x80(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x44d0
movq -0x118(%rbp), %rdi
movq %rax, -0x110(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, %esi
callq 0x44d0
movq -0xf0(%rbp), %rdi
movslq (%rax), %rsi
callq 0x44d0
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x4d00
movq %rax, -0x108(%rbp)
jmp 0x1a3b
movq -0xe8(%rbp), %rdi
movq -0x108(%rbp), %rax
movq %rax, -0xa4(%rbp)
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0x9c(%rbp), %eax
movl %eax, %esi
callq 0x4d50
movq %rax, %rdi
leaq -0xa4(%rbp), %rsi
callq 0x4d70
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x19b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x1e96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x45b0
jmp 0x1e96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x45b0
jmp 0x1e96
cmpl $0x0, -0x64(%rbp)
jle 0x1e59
movl $0xffffffff, -0xa8(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xac(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xb0(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xb4(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0xb8(%rbp)
movq -0xe8(%rbp), %rdi
movl -0xb8(%rbp), %eax
movq %rax, -0x120(%rbp)
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
callq 0x4da0
movq %rax, %rcx
movq -0x120(%rbp), %rax
cmpq %rcx, %rax
jae 0x1cbe
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xb8(%rbp), %eax
movl %eax, %esi
callq 0x4d50
cmpl $0x0, 0x4(%rax)
jne 0x1b9f
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xb8(%rbp), %eax
movl %eax, %esi
callq 0x4d50
cmpl $0x0, (%rax)
jne 0x1b9f
movl -0xb8(%rbp), %eax
movl %eax, -0xa8(%rbp)
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xb8(%rbp), %eax
movl %eax, %esi
callq 0x4d50
cmpl $0x1, 0x4(%rax)
jne 0x1bf6
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xb8(%rbp), %eax
movl %eax, %esi
callq 0x4d50
cmpl $0x1, (%rax)
jne 0x1bf6
movl -0xb8(%rbp), %eax
movl %eax, -0xb0(%rbp)
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xb8(%rbp), %eax
movl %eax, %esi
callq 0x4d50
cmpl $0x0, 0x4(%rax)
jne 0x1c4f
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xb8(%rbp), %eax
movl %eax, %esi
callq 0x4d50
movl (%rax), %eax
cmpl -0x64(%rbp), %eax
jne 0x1c4f
movl -0xb8(%rbp), %eax
movl %eax, -0xac(%rbp)
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xb8(%rbp), %eax
movl %eax, %esi
callq 0x4d50
cmpl $0x1, 0x4(%rax)
jne 0x1ca8
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xb8(%rbp), %eax
movl %eax, %esi
callq 0x4d50
movl (%rax), %eax
cmpl -0x64(%rbp), %eax
jne 0x1ca8
movl -0xb8(%rbp), %eax
movl %eax, -0xb4(%rbp)
jmp 0x1caa
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x1b10
movq 0xc313(%rip), %rdi      # 0xdfd8
leaq 0x737b(%rip), %rsi      # 0x9047
callq 0x10f0
movq %rax, -0x128(%rbp)
jmp 0x1cda
movq -0x128(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x11b0
movq %rax, -0x130(%rbp)
jmp 0x1cf2
movq -0x130(%rbp), %rdi
leaq 0x734e(%rip), %rsi      # 0x904e
callq 0x10f0
movq %rax, -0x138(%rbp)
jmp 0x1d0e
movq -0xf0(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x44d0
movq -0x138(%rbp), %rdi
movl (%rax), %esi
callq 0x11b0
movq %rax, -0x140(%rbp)
jmp 0x1d35
movq -0x140(%rbp), %rdi
leaq 0x7316(%rip), %rsi      # 0x9059
callq 0x10f0
jmp 0x1d4a
movl $0x0, -0xbc(%rbp)
movq -0xe8(%rbp), %rdi
movl -0xbc(%rbp), %eax
movq %rax, -0x148(%rbp)
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
callq 0x4da0
movq %rax, %rcx
movq -0x148(%rbp), %rax
cmpq %rcx, %rax
jae 0x1e42
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xbc(%rbp), %eax
movl %eax, %esi
callq 0x4d50
movl 0x4(%rax), %esi
movq 0xc222(%rip), %rdi      # 0xdfd8
callq 0x11b0
movq %rax, -0x150(%rbp)
jmp 0x1dc4
movq -0x150(%rbp), %rdi
leaq 0x7233(%rip), %rsi      # 0x9005
callq 0x10f0
movq %rax, -0x158(%rbp)
jmp 0x1de0
movq -0xe8(%rbp), %rdi
movslq -0x64(%rbp), %rsi
callq 0x4d30
movq %rax, %rdi
movl -0xbc(%rbp), %eax
movl %eax, %esi
callq 0x4d50
movq -0x158(%rbp), %rdi
movl (%rax), %esi
callq 0x11b0
movq %rax, -0x160(%rbp)
jmp 0x1e17
movq -0x160(%rbp), %rdi
leaq 0x7466(%rip), %rsi      # 0x928b
callq 0x10f0
jmp 0x1e2c
jmp 0x1e2e
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x1d54
movq 0xc18f(%rip), %rdi      # 0xdfd8
movq 0xc178(%rip), %rsi      # 0xdfc8
callq 0x1130
jmp 0x1e57
jmp 0x1e59
leaq -0x80(%rbp), %rdi
callq 0x45b0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1915
movb $0x1, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
jne 0x1e86
movq -0xe8(%rbp), %rdi
callq 0x4dc0
movq -0xe0(%rbp), %rax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0xe8(%rbp), %rdi
callq 0x4dc0
movq -0x58(%rbp), %rdi
callq 0x11c0
nopl (%rax,%rax)