// File: boczeratul[P]StarCraftGA/ga.cpp
// Function: RTR()
void RTR () {

    int i, j;

 
     int windowSize = nCurrent/20;
//    int windowSize = ( nCurrent < ell / 20) ? nCurrent : ell / 20;    // error  :  windowSize may be 0!!

    // Rick modified
    if ( windowSize == 0 )
    {
        fullReplace ();
        return;
    }
    // end Rick


    int *randArray = new int[windowSize];

    for (i = 0; i < nCurrent; ++i)
    {
        int index = 0 ;
        int distance;
        int minDistance = INT_MAX;                // max distance
        int maxFitness = 0;

        uniformArray (randArray, windowSize, 0, nCurrent - 1);

        for (j = 0; j < windowSize; j++)
        {
            //distance = getBBDistance (offspring[i], population[randArray[j]]);
            distance = getDistance (offspring[i], population[randArray[j]]);
            if (distance < minDistance)
            {
                index = randArray[j];
                minDistance = distance;
                maxFitness = fitness[index];
            }
            else if (distance == minDistance && maxFitness < fitness[randArray[j]] )
            {
                index = randArray[j];
                maxFitness = fitness[index];
            }
        }

        

        if ( offspringFitness[i] < fitness[index] ||
           (offspringFitness[i] == fitness[index] && offspring[i][0] < population[index][0] ) )
        {
            copyChromosome( population[index] , offspring[i] ) ;
            fitness[index] = offspringFitness[i];
            if ( SHOW_REPLACEMENT )
            {
                printf ("Replacing (%d) with ", index);
                //offspring[i].printOut ();
                printf ("\n");
            }
        }
        else if (SHOW_REPLACEMENT)
        {
            {
                printf ("Not replacing (%d) with offspring %d",  index, i);
                //offspring[i].printOut ();
                printf ("\n");
            }
        }
    }
    
    delete [] randArray;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl 0xc2ba(%rip), %eax      # 0xe0f8
movl $0x14, %ecx
cltd
idivl %ecx
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x1e59
callq 0x1de0
jmp 0x2058
movslq -0xc(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x18(%rbp)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl 0xc266(%rip), %eax      # 0xe0f8
jge 0x2041
movl $0x0, -0x1c(%rbp)
movl $0x7fffffff, -0x24(%rbp) # imm = 0x7FFFFFFF
movl $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %esi
movl 0xc23e(%rip), %ecx      # 0xe0f8
subl $0x1, %ecx
xorl %edx, %edx
callq 0x2ec0
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x1f8e
movq 0xc24a(%rip), %rdi      # 0xe128
movslq -0x4(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq 0xc230(%rip), %rsi      # 0xe120
movq -0x18(%rbp), %rax
movslq -0x8(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x2060
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1f3a
movq -0x18(%rbp), %rax
movslq -0x8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movq 0xc202(%rip), %rax      # 0xe130
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
jmp 0x1f7e
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x1f7c
movl -0x28(%rbp), %eax
movq 0xc1e4(%rip), %rcx      # 0xe130
movq -0x18(%rbp), %rdx
movslq -0x8(%rbp), %rsi
movslq (%rdx,%rsi,4), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x1f7c
movq -0x18(%rbp), %rax
movslq -0x8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movq 0xc1be(%rip), %rax      # 0xe130
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
jmp 0x1f7e
jmp 0x1f80
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x1ecb
movq 0xc1a3(%rip), %rax      # 0xe138
movslq -0x4(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq 0xc18d(%rip), %rcx      # 0xe130
movslq -0x1c(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jl 0x1fee
movq 0xc185(%rip), %rax      # 0xe138
movslq -0x4(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq 0xc16f(%rip), %rcx      # 0xe130
movslq -0x1c(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x202f
movq 0xc157(%rip), %rax      # 0xe128
movslq -0x4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
movq 0xc13e(%rip), %rcx      # 0xe120
movslq -0x1c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl (%rcx), %eax
jge 0x202f
movq 0xc12b(%rip), %rax      # 0xe120
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0xc124(%rip), %rax      # 0xe128
movslq -0x4(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1a30
movq 0xc120(%rip), %rax      # 0xe138
movslq -0x4(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq 0xc10a(%rip), %rax      # 0xe130
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x2031
jmp 0x2031
jmp 0x2033
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x1e89
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x2058
movq -0x30(%rbp), %rdi
callq 0x10e0
addq $0x30, %rsp
popq %rbp
retq
nop