// File: 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
subq $0x80, %rsp
movq %rdi, -0x58(%rbp)
movq %rdi, %rax
movq %rax, -0x60(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq 0xcbc1(%rip), %rdi      # 0xdfd8
leaq 0x7bed(%rip), %rsi      # 0x900b
callq 0x10f0
movq %rax, %rdi
movl -0xc(%rbp), %esi
callq 0x11b0
movq %rax, %rdi
leaq 0x7be3(%rip), %rsi      # 0x901b
callq 0x10f0
movq %rax, %rdi
movl -0x10(%rbp), %esi
callq 0x11b0
movq %rax, %rdi
leaq 0x7ec5(%rip), %rsi      # 0x9317
callq 0x10f0
movq %rax, %rdi
movq 0xcb67(%rip), %rsi      # 0xdfc8
callq 0x1130
movl -0xc(%rbp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax,2), %eax
movl %eax, -0x14(%rbp)
movb $0x0, -0x15(%rbp)
movslq -0x14(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
leaq -0x1d(%rbp), %rdi
movq %rdi, -0x48(%rbp)
callq 0x44f0
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rcx
leaq -0x1c(%rbp), %rdx
callq 0x4510
jmp 0x14a9
leaq -0x1d(%rbp), %rdi
callq 0x4590
movl $0x0, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x15f8
movq -0x58(%rbp), %rdi
movl $0x2, -0x30(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x44d0
movq -0x58(%rbp), %rdi
movl $0x0, (%rax)
movl $0x1, %esi
callq 0x44d0
movl $0x1, (%rax)
cmpl $0x0, -0x10(%rbp)
jle 0x15f3
callq 0x1060
movq -0x58(%rbp), %rdi
cltd
idivl -0x14(%rbp)
movl %edx, -0x34(%rbp)
movslq -0x34(%rbp), %rsi
callq 0x44d0
cmpl $-0x2, (%rax)
je 0x15ee
movq -0x58(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x44d0
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq 0xcaa1(%rip), %rdi      # 0xdfd8
leaq 0x7ae7(%rip), %rsi      # 0x9025
callq 0x10f0
movq %rax, -0x68(%rbp)
jmp 0x1549
movq -0x68(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x11b0
movq %rax, -0x70(%rbp)
jmp 0x155b
movq -0x70(%rbp), %rdi
movq 0xca62(%rip), %rsi      # 0xdfc8
callq 0x1130
jmp 0x156d
movl -0x10(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x1582
cmpl $0x1, -0x34(%rbp)
jne 0x15ec
movq 0xca4f(%rip), %rdi      # 0xdfd8
leaq 0x7aa2(%rip), %rsi      # 0x9032
callq 0x10f0
movq %rax, -0x78(%rbp)
jmp 0x159b
movq -0x78(%rbp), %rdi
movq 0xca22(%rip), %rsi      # 0xdfc8
callq 0x1130
jmp 0x15ad
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x15ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x1d(%rbp), %rdi
callq 0x4590
jmp 0x1691
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x45b0
jmp 0x1691
jmp 0x15ee
jmp 0x14f1
jmp 0x14b9
movl $0x2, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x1671
movq -0x58(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x44d0
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $-0x1, -0x3c(%rbp)
jne 0x164d
callq 0x1060
movq -0x58(%rbp), %rdi
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0x3c(%rbp)
movslq -0x3c(%rbp), %rsi
callq 0x44d0
cmpl $-0x2, (%rax)
jne 0x164b
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
jmp 0x161a
jmp 0x161a
movq -0x58(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x44d0
movl -0x7c(%rbp), %ecx
movl %ecx, (%rax)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x15ff
movb $0x1, -0x15(%rbp)
testb $0x1, -0x15(%rbp)
jne 0x1684
movq -0x58(%rbp), %rdi
callq 0x45b0
movq -0x60(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x11c0
nopw (%rax,%rax)