// File: boczeratul[P]StarCraftGA/ga.cpp
// Function: tournament()
void tournament () {
    int i, j;

    // Adjusting population size
    int nNextGeneration = nCurrent;

//    if (nNextGeneration != nCurrent) {
//        delete[]selectionIndex;
//        delete[]offspring;
//        selectionIndex = new int[nNextGeneration];
//        offspring = new Chromosome[nNextGeneration];
//
//        for (i = 0; i < nNextGeneration; i++)
//            offspring[i].init (ell);
//    }

    int *randArray = (int*) malloc(selectionPressure * nNextGeneration * sizeof(int));

    int q = (selectionPressure * nNextGeneration) / nCurrent;
    int r = (selectionPressure * nNextGeneration) % nCurrent;

    for (i = 0; i < q; i++)
        uniformArray (randArray + (i * nCurrent), nCurrent, 0, nCurrent - 1);

    uniformArray (randArray + (q * nCurrent), r, 0, nCurrent - 1);


    for (i = 0; i < nNextGeneration; i++)
    {

        int winner = 0;
        double winnerFitness = DBL_MAX;

        for (j = 0; j < selectionPressure; j++)
        {
            int challenger = randArray[selectionPressure * i + j];
            int challengerFitness = fitness[challenger];

            if (challengerFitness < winnerFitness)
            {
                winner = challenger;
                winnerFitness = challengerFitness;
            }

        }
        selectionIndex[i] = winner;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl 0xc83a(%rip), %eax      # 0xe0f8
movl %eax, -0xc(%rbp)
movl 0xc7c9(%rip), %eax      # 0xe090
imull -0xc(%rbp), %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x10f0
movq %rax, -0x18(%rbp)
movl 0xc7af(%rip), %eax      # 0xe090
imull -0xc(%rbp), %eax
cltd
idivl 0xc80c(%rip)            # 0xe0f8
movl %eax, -0x1c(%rbp)
movl 0xc79b(%rip), %eax      # 0xe090
imull -0xc(%rbp), %eax
cltd
idivl 0xc7f8(%rip)            # 0xe0f8
movl %edx, -0x20(%rbp)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x194a
movq -0x18(%rbp), %rdi
movl -0x4(%rbp), %eax
imull 0xc7d8(%rip), %eax      # 0xe0f8
cltq
shlq $0x2, %rax
addq %rax, %rdi
movl 0xc7c9(%rip), %esi      # 0xe0f8
movl 0xc7c3(%rip), %ecx      # 0xe0f8
subl $0x1, %ecx
xorl %edx, %edx
callq 0x2ec0
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x190a
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %eax
imull 0xc7a0(%rip), %eax      # 0xe0f8
cltq
shlq $0x2, %rax
addq %rax, %rdi
movl -0x20(%rbp), %esi
movl 0xc78e(%rip), %ecx      # 0xe0f8
subl $0x1, %ecx
xorl %edx, %edx
callq 0x2ec0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x1a24
movl $0x0, -0x24(%rbp)
movsd 0x967a(%rip), %xmm0     # 0xb010
movsd %xmm0, -0x30(%rbp)
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl 0xc6e5(%rip), %eax      # 0xe090
jge 0x1a05
movq -0x18(%rbp), %rax
movl 0xc6d9(%rip), %ecx      # 0xe090
imull -0x4(%rbp), %ecx
addl -0x8(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
movq 0xc762(%rip), %rax      # 0xe130
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x38(%rbp)
cvtsi2sdl -0x38(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x19f8
movl -0x34(%rbp), %eax
movl %eax, -0x24(%rbp)
cvtsi2sdl -0x38(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x19fa
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x19a2
movl -0x24(%rbp), %edx
movq 0xc6f9(%rip), %rax      # 0xe108
movslq -0x4(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x197b
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)