// File: /workspace/github_cpp_makefile_star2_O2/NeoResearch[P]dbft3-spork-detect/main.cpp
// Function: makeTest(int, int)
vector<int> makeTest(int f, int failed = 0)
{
    cout << "making test (f=" << f << "; failed=" << failed << ")" << endl;
    int N = 3 * f + 1;

    vector<int> choices(N, -1);

    int count_primaries = 0;
    // must have at least one primary alive (non-faulty)
    while (count_primaries == 0)
    {
        count_primaries = 2; // two primaries (must have at least one alive)
        // double proposal
        choices[0] = 0;
        choices[1] = 1;
        // set failed nodes
        while (failed > 0)
        {
            int i = rand() % N; // failed
            if (choices[i] != -2)
            {
                choices[i] = -2;
                cout << " --> failed " << i << endl;
                failed--;
                if ((i == 0) || (i == 1))
                {
                    cout << "-----> lost primary!" << endl;
                    count_primaries--;
                }
            }
        }
    }

    // set rest of nodes first responses
    for (unsigned i = 2; i < N; i++)
    {
        int c = choices[i];
        while (c == -1) // not selected yet
        {
            c = rand() % 2;       // double proposal
            if (choices[c] == -2) // re-propose because it's faulty
            {
                c = -1;
                continue;
            }
        }
        choices[i] = c;
    }

    return choices;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %r14d
movl %esi, %r12d
movq %rdi, %rbx
movq 0x5bf7(%rip), %rdi      # 0x6fd8
leaq 0x2c4f(%rip), %rsi      # 0x4037
callq 0x1100
movq %rax, %rdi
movl %r12d, %esi
callq 0x11d0
leaq 0x2c48(%rip), %rsi      # 0x4047
movq %rax, %rdi
callq 0x1100
movq %rax, %rdi
movl %r14d, %esi
callq 0x11d0
leaq 0x2f28(%rip), %rsi      # 0x4341
movq %rax, %rdi
callq 0x1100
movq %rax, %rdi
callq 0x1090
leal (%r12,%r12,2), %r15d
incl %r15d
movslq %r15d, %rsi
leaq -0x30(%rbp), %rdx
orl $-0x1, (%rdx)
leaq -0x29(%rbp), %rcx
movq %rbx, %rdi
callq 0x2ebe
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0x14d9
movq (%rbx), %rax
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, (%rax)
pushq $0x2
popq %r12
testl %r14d, %r14d
jle 0x1449
callq 0x1050
cltd
idivl %r15d
movslq %edx, %rax
movq (%rbx), %rcx
cmpl $-0x2, (%rcx,%rax,4)
je 0x1466
movl %edx, %r13d
movl $0xfffffffe, (%rcx,%rax,4) # imm = 0xFFFFFFFE
movq 0x5b47(%rip), %rdi      # 0x6fd8
leaq 0x2bb9(%rip), %rsi      # 0x4051
callq 0x1100
movq %rax, %rdi
movl %r13d, %esi
callq 0x11d0
movq %rax, %rdi
callq 0x1090
decl %r14d
cmpl $0x1, %r13d
ja 0x1466
movq 0x5b18(%rip), %rdi      # 0x6fd8
leaq 0x2b97(%rip), %rsi      # 0x405e
callq 0x1100
movq %rax, %rdi
callq 0x1090
decl %r12d
jmp 0x1466
movl %r15d, %r14d
pushq $0x2
popq %r15
pushq $0x2
popq %r12
pushq $-0x1
popq %r13
cmpq %r14, %r15
jae 0x151b
movq (%rbx), %rcx
movl (%rcx,%r15,4), %eax
cmpl $-0x1, %eax
jne 0x1512
callq 0x1050
cltd
idivl %r12d
movslq %edx, %rax
movq (%rbx), %rcx
cmpl $-0x2, (%rcx,%rax,4)
cmovel %r13d, %eax
jmp 0x14f4
movl %eax, (%rcx,%r15,4)
incq %r15
jmp 0x14e8
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x152f
movq %rax, %r14
movq %rbx, %rdi
callq 0x322c
movq %r14, %rdi
callq 0x11e0