// File: grmauri[P]pmm/pmm.cpp
// Function: algoritmo_genetico(int, int, double, double, double, tSolucao&, double&, double&)
void algoritmo_genetico(const int pop, const int cro, const double mut, const double eli,
                        const double tempo_max, Solucao &s, double &tempo_melhor, double &tempo_total)
{
    int filho, p1, p2;
    clock_t hI, hF;
    printf("\n\n>>> EXECUTANDO O AG...\n\n");
    hI = clock();
    for(int i = 0; i < pop; i++)
    {
        heuConAle(vet_populacao[i]);
        calcFO(vet_populacao[i]);
    }
    ordenar_populacao(pop);
    hF = clock();
    tempo_melhor = ((double)(hF - hI))/CLOCKS_PER_SEC;
    memcpy(&s, &vet_populacao[0], sizeof(vet_populacao[0]));
    #ifdef DBG
        printf("FO: %d\tTempo: %.2f\n", s.funObj, tempo_melhor);
    #endif
    tempo_total = tempo_melhor;
    while(tempo_total < tempo_max)
    {
       filho = pop;
       for(int i = 0; i < cro; i++)
       {
           //------------------
           // Selecao elite
           p1 = MAX(1,(eli/100.0)*pop);
           p1 = rand()%p1;
           do
               p2 = rand()%pop;
           while(p2 == p1);
           //------------------
           // Cruzamento
           crossover(p1,p2,filho,filho+1);
           if(vet_populacao[filho].funObj > s.funObj)
           {
                memcpy(&s, &vet_populacao[filho], sizeof(vet_populacao[filho]));
                hF = clock();
                tempo_melhor = ((double)(hF - hI))/CLOCKS_PER_SEC;
                #ifdef DBG
                    printf("FO: %d\tTempo: %.2f\n", s.funObj, tempo_melhor);
                #endif
           }
           if(vet_populacao[filho+1].funObj > s.funObj)
           {
                memcpy(&s, &vet_populacao[filho+1], sizeof(vet_populacao[filho+1]));
                hF = clock();
                tempo_melhor = ((double)(hF - hI))/CLOCKS_PER_SEC;
                #ifdef DBG
                    printf("FO: %d\tTempo: %.2f\n", s.funObj, tempo_melhor);
                #endif
           }
           //------------------
           // Mutacao
           if(rand()%100 < mut)
           {
               gerar_vizinha(vet_populacao[filho]);
               if(vet_populacao[filho].funObj > s.funObj)
               {
                    memcpy(&s, &vet_populacao[filho], sizeof(vet_populacao[filho]));
                    hF = clock();
                    tempo_melhor = ((double)(hF - hI))/CLOCKS_PER_SEC;
                    #ifdef DBG
                        printf("FO: %d\tTempo: %.2f\n", s.funObj, tempo_melhor);
                    #endif
               }
           }
           if(rand()%100 < mut)
           {
               gerar_vizinha(vet_populacao[filho+1]);
               if(vet_populacao[filho+1].funObj > s.funObj)
               {
                    memcpy(&s, &vet_populacao[filho+1], sizeof(vet_populacao[filho+1]));
                    hF = clock();
                    tempo_melhor = ((double)(hF - hI))/CLOCKS_PER_SEC;
                    #ifdef DBG
                        printf("FO: %d\tTempo: %.2f\n", s.funObj, tempo_melhor);
                    #endif
               }
           }
           filho += 2;
       }
       //------------------
       // Selecao de sobreviventes
       ordenar_populacao(pop + 2 * cro);
       hF = clock();
       tempo_total = ((double)(hF - hI))/CLOCKS_PER_SEC;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
leaq 0x32db(%rip), %rdi      # 0x711b
movb $0x0, %al
callq 0x2050
callq 0x2100
movq %rax, -0x50(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x3e9e
movslq -0x5c(%rbp), %rax
leaq 0x7b86(%rip), %rdi      # 0xb9f0
imulq $0x89c, %rax, %rax      # imm = 0x89C
addq %rax, %rdi
callq 0x4690
movslq -0x5c(%rbp), %rax
leaq 0x7b6c(%rip), %rdi      # 0xb9f0
imulq $0x89c, %rax, %rax      # imm = 0x89C
addq %rax, %rdi
callq 0x46f0
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x3e57
movl -0x4(%rbp), %edi
callq 0x4e40
callq 0x2100
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
subq -0x50(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x316c(%rip), %xmm1     # 0x7030
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rdi
leaq 0x7b15(%rip), %rsi      # 0xb9f0
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
movq -0x28(%rbp), %rax
movl 0x898(%rax), %esi
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
leaq 0x31d0(%rip), %rdi      # 0x70ce
movb $0x1, %al
callq 0x2050
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x38(%rbp), %rax
movsd (%rax), %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x430e
movl -0x4(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x42d0
movsd -0x18(%rbp), %xmm1
movsd 0x30ee(%rip), %xmm0     # 0x7040
divsd %xmm0, %xmm1
cvtsi2sdl -0x4(%rbp), %xmm0
mulsd %xmm0, %xmm1
movsd 0x30d1(%rip), %xmm0     # 0x7038
ucomisd %xmm1, %xmm0
jbe 0x3f7c
movsd 0x30c3(%rip), %xmm0     # 0x7038
movsd %xmm0, -0x68(%rbp)
jmp 0x3f9b
movsd -0x18(%rbp), %xmm0
movsd 0x30b7(%rip), %xmm1     # 0x7040
divsd %xmm1, %xmm0
cvtsi2sdl -0x4(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x40(%rbp)
callq 0x2070
cltd
idivl -0x40(%rbp)
movl %edx, -0x40(%rbp)
callq 0x2070
cltd
idivl -0x4(%rbp)
movl %edx, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x40(%rbp), %eax
je 0x3fb3
movl -0x40(%rbp), %edi
movl -0x44(%rbp), %esi
movl -0x3c(%rbp), %edx
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
callq 0x4f90
movslq -0x3c(%rbp), %rcx
leaq 0x7a0a(%rip), %rax      # 0xb9f0
imulq $0x89c, %rcx, %rcx      # imm = 0x89C
addq %rcx, %rax
movl 0x898(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x898(%rcx), %eax
jle 0x406f
movq -0x28(%rbp), %rdi
movslq -0x3c(%rbp), %rax
leaq 0x79df(%rip), %rsi      # 0xb9f0
imulq $0x89c, %rax, %rax      # imm = 0x89C
addq %rax, %rsi
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
callq 0x2100
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
subq -0x50(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x2fed(%rip), %xmm1     # 0x7030
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
movl 0x898(%rax), %esi
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
leaq 0x3066(%rip), %rdi      # 0x70ce
movb $0x1, %al
callq 0x2050
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x7971(%rip), %rax      # 0xb9f0
imulq $0x89c, %rcx, %rcx      # imm = 0x89C
addq %rcx, %rax
movl 0x898(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x898(%rcx), %eax
jle 0x410c
movq -0x28(%rbp), %rdi
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cltq
leaq 0x7942(%rip), %rsi      # 0xb9f0
imulq $0x89c, %rax, %rax      # imm = 0x89C
addq %rax, %rsi
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
callq 0x2100
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
subq -0x50(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x2f50(%rip), %xmm1     # 0x7030
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
movl 0x898(%rax), %esi
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
leaq 0x2fc9(%rip), %rdi      # 0x70ce
movb $0x1, %al
callq 0x2050
callq 0x2070
movl $0x64, %ecx
cltd
idivl %ecx
cvtsi2sd %edx, %xmm1
movsd -0x10(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x41dc
movslq -0x3c(%rbp), %rax
leaq 0x78b9(%rip), %rdi      # 0xb9f0
imulq $0x89c, %rax, %rax      # imm = 0x89C
addq %rax, %rdi
callq 0x4810
movslq -0x3c(%rbp), %rcx
leaq 0x789f(%rip), %rax      # 0xb9f0
imulq $0x89c, %rcx, %rcx      # imm = 0x89C
addq %rcx, %rax
movl 0x898(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x898(%rcx), %eax
jle 0x41da
movq -0x28(%rbp), %rdi
movslq -0x3c(%rbp), %rax
leaq 0x7874(%rip), %rsi      # 0xb9f0
imulq $0x89c, %rax, %rax      # imm = 0x89C
addq %rax, %rsi
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
callq 0x2100
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
subq -0x50(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x2e82(%rip), %xmm1     # 0x7030
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
movl 0x898(%rax), %esi
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
leaq 0x2efb(%rip), %rdi      # 0x70ce
movb $0x1, %al
callq 0x2050
jmp 0x41dc
callq 0x2070
movl $0x64, %ecx
cltd
idivl %ecx
cvtsi2sd %edx, %xmm1
movsd -0x10(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x42b9
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cltq
leaq 0x77e5(%rip), %rdi      # 0xb9f0
imulq $0x89c, %rax, %rax      # imm = 0x89C
addq %rax, %rdi
callq 0x4810
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x77c6(%rip), %rax      # 0xb9f0
imulq $0x89c, %rcx, %rcx      # imm = 0x89C
addq %rcx, %rax
movl 0x898(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x898(%rcx), %eax
jle 0x42b7
movq -0x28(%rbp), %rdi
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cltq
leaq 0x7797(%rip), %rsi      # 0xb9f0
imulq $0x89c, %rax, %rax      # imm = 0x89C
addq %rax, %rsi
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
callq 0x2100
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
subq -0x50(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x2da5(%rip), %xmm1     # 0x7030
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
movl 0x898(%rax), %esi
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
leaq 0x2e1e(%rip), %rdi      # 0x70ce
movb $0x1, %al
callq 0x2050
jmp 0x42b9
movl -0x3c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x3c(%rbp)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x3f39
movl -0x4(%rbp), %edi
movl -0x8(%rbp), %eax
shll %eax
addl %eax, %edi
callq 0x4e40
callq 0x2100
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
subq -0x50(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x2d33(%rip), %xmm1     # 0x7030
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0x3f15
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)