// File: NeoResearch[P]dbft3-spork-detect/main.cpp
// Function: getCancels(std::vector<int, std::allocator<int>>, std::vector<std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>, std::allocator<std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>>>, int)
vector<int> getCancels(vector<int> choices, vector<vector<pair<int, int>>> selections, int f)
{
    int N = 3 * f + 1;
    vector<int> cancels(N, -2);
    // compute cancels for each replica
    // OLD // rule is: order by lowest to highest proposal, and take value 2*f
    // NEW RULE (simpler): if Prep0 has a non-faulty proposal, Cancel 0, otherwise Cancel 1
    for (unsigned i = 0; i < N; i++)
    {
        if (choices[i] == -2)
            continue;       // faulty node
        int count_zero = 0; // try to Cancel 0 (NEW RULE)
        //vector<int> responses;
        //responses.push_back(choices[i]); // own choice counts
        //for (unsigned j = 0; j < selections[i].size(); j++)
        //    responses.push_back(selections[i][j].second);
        ////cout << "RESPONSES FOR I=" << i << endl;
        ////printv(responses);
        //sort(responses.begin(), responses.end());
        //printv(responses);
        //int cancel = responses[2 * f - 1];
        for (unsigned j = 0; j < selections[i].size(); j++)
            if (selections[i][j].second == 0)
                count_zero++;
        if (choices[i] == 0)
            count_zero++;
        int cancel = 1;          // default one
        if (count_zero >= f + 1) // at least one trusted
            cancel = 0;

        ////cout << "cancel = " << cancel << endl;
        cancels[i] = cancel;
        // EXCEPTION: proposal j cannot Cancel more than j (otherwise it just assumes its "faultyness", what is absurd)
        if (i == 0)
            cancels[i] = 0; // FORCE. could do same for i=1 (but only two, so that's fine already)
    }

    return cancels;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdx, -0x80(%rbp)
movq %rsi, -0x78(%rbp)
movq %rdi, -0x68(%rbp)
movq %rdi, %rax
movq %rax, -0x70(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax,2), %eax
movl %eax, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
movslq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0xfffffffe, -0x28(%rbp) # imm = 0xFFFFFFFE
leaq -0x29(%rbp), %rdi
movq %rdi, -0x58(%rbp)
callq 0x44f0
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rcx
leaq -0x28(%rbp), %rdx
callq 0x4510
jmp 0x1f20
leaq -0x29(%rbp), %rdi
callq 0x4590
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x20(%rbp), %eax
jae 0x2062
movq -0x78(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, %esi
callq 0x44d0
cmpl $-0x2, (%rax)
jne 0x1f6e
jmp 0x2054
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x29(%rbp), %rdi
callq 0x4590
jmp 0x2082
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x80(%rbp), %rdi
movl -0x48(%rbp), %eax
movq %rax, -0x88(%rbp)
movl -0x40(%rbp), %eax
movl %eax, %esi
callq 0x4d30
movq %rax, %rdi
callq 0x4da0
movq %rax, %rcx
movq -0x88(%rbp), %rax
cmpq %rcx, %rax
jae 0x1fe2
movq -0x80(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, %esi
callq 0x4d30
movq %rax, %rdi
movl -0x48(%rbp), %eax
movl %eax, %esi
callq 0x4d50
cmpl $0x0, 0x4(%rax)
jne 0x1fd5
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1fd7
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1f7c
movq -0x78(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, %esi
callq 0x44d0
cmpl $0x0, (%rax)
jne 0x1ffe
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movl $0x1, -0x4c(%rbp)
movl -0x44(%rbp), %eax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jl 0x2019
movl $0x0, -0x4c(%rbp)
movq -0x68(%rbp), %rdi
movl -0x4c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x40(%rbp), %eax
movl %eax, %esi
callq 0x44d0
movl -0x8c(%rbp), %ecx
movl %ecx, (%rax)
cmpl $0x0, -0x40(%rbp)
jne 0x2052
movq -0x68(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, %esi
callq 0x44d0
movl $0x0, (%rax)
jmp 0x2054
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1f30
movb $0x1, -0x21(%rbp)
testb $0x1, -0x21(%rbp)
jne 0x2075
movq -0x68(%rbp), %rdi
callq 0x45b0
movq -0x70(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x11c0
nopl (%rax,%rax)