// File: grmauri[P]pmm/pmm.cpp
// Function: simulated_annealing(double, int, double, double, double, tSolucao&, double&, double&)
void simulated_annealing(const double alfa, const int sa_max, const double temp_ini, const double temp_con,
                         const double tempo_max, Solucao &s, double &tempo_melhor, double &tempo_total)
{
    clock_t hI, hF;
    Solucao s_atual, s_vizinha;
    double temp, delta, x;
    printf("\n\n>>> EXECUTANDO O SA...\n\n");
    hI = clock();
    heuConAle(s);
    calcFO(s);
    hF = clock();
    tempo_melhor = ((double)(hF - hI))/CLOCKS_PER_SEC;
    #ifdef DBG
        printf("FO: %d\tTempo: %.2f\n", s.funObj, tempo_melhor);
    #endif // DBG
    tempo_total = tempo_melhor;
    memcpy(&s_atual, &s, sizeof(s));
    while(tempo_total < tempo_max)
    {
        temp = temp_ini;
        while(temp > temp_con)
        {
            for(int i = 0; i < sa_max; i++)
            {
                memcpy(&s_vizinha, &s_atual, sizeof(s_atual));
                gerar_vizinha(s_vizinha);
                delta = s_atual.funObj - s_vizinha.funObj;
                if(delta < 0)
                {
                    memcpy(&s_atual, &s_vizinha, sizeof(s_vizinha));
                    if(s_vizinha.funObj > s.funObj)
                    {
                        memcpy(&s, &s_vizinha, sizeof(s_vizinha));
                        hF = clock();
                        tempo_melhor = ((double)(hF - hI))/CLOCKS_PER_SEC;
                        #ifdef DBG
                            printf("FO: %d\tTempo: %.2f\n", s.funObj, tempo_melhor);
                        #endif // DBG
                    }
                }
                else
                {
                    x = rand()%1001;
                    x = x / 1000.0;
                    if(x < exp(-delta/temp))
                        memcpy(&s_atual, &s_vizinha, sizeof(s_vizinha));
                }
            }
            temp = temp * alfa;
            hF = clock();
            tempo_total = ((double)(hF - hI))/CLOCKS_PER_SEC;
            if(tempo_total >= tempo_max)
                break;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x11b0, %rsp           # imm = 0x11B0
movsd %xmm0, -0x8(%rbp)
movl %edi, -0xc(%rbp)
movsd %xmm1, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
movsd %xmm3, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
leaq 0x3b5e(%rip), %rdi      # 0x70b3
movb $0x0, %al
callq 0x2050
callq 0x2100
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
callq 0x4690
movq -0x30(%rbp), %rdi
callq 0x46f0
callq 0x2100
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x3a9b(%rip), %xmm1     # 0x7030
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x30(%rbp), %rax
movl 0x898(%rax), %esi
movq -0x38(%rbp), %rax
movsd (%rax), %xmm0
leaq 0x3b14(%rip), %rdi      # 0x70ce
movb $0x1, %al
callq 0x2050
movq -0x38(%rbp), %rax
movsd (%rax), %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x30(%rbp), %rsi
leaq -0x8ec(%rbp), %rdi
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
movq -0x40(%rbp), %rax
movsd (%rax), %xmm1
movsd -0x28(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3817
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x1190(%rbp)
movsd -0x1190(%rbp), %xmm0
ucomisd -0x20(%rbp), %xmm0
jbe 0x3812
movl $0x0, -0x11a4(%rbp)
movl -0x11a4(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x37bd
leaq -0x1188(%rbp), %rdi
leaq -0x8ec(%rbp), %rsi
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
leaq -0x1188(%rbp), %rdi
callq 0x4810
movl -0x54(%rbp), %eax
subl -0x8f0(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x1198(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x1198(%rbp), %xmm0
jbe 0x370e
leaq -0x8ec(%rbp), %rdi
leaq -0x1188(%rbp), %rsi
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
movl -0x8f0(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x898(%rcx), %eax
jle 0x3709
movq -0x30(%rbp), %rdi
leaq -0x1188(%rbp), %rsi
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
callq 0x2100
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x3953(%rip), %xmm1     # 0x7030
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x30(%rbp), %rax
movl 0x898(%rax), %esi
movq -0x38(%rbp), %rax
movsd (%rax), %xmm0
leaq 0x39cc(%rip), %rdi      # 0x70ce
movb $0x1, %al
callq 0x2050
jmp 0x37a7
callq 0x2070
movl $0x3e9, %ecx            # imm = 0x3E9
cltd
idivl %ecx
cvtsi2sd %edx, %xmm0
movsd %xmm0, -0x11a0(%rbp)
movsd -0x11a0(%rbp), %xmm0
movsd 0x38e9(%rip), %xmm1     # 0x7020
divsd %xmm1, %xmm0
movsd %xmm0, -0x11a0(%rbp)
movsd -0x11a0(%rbp), %xmm0
movsd %xmm0, -0x11b0(%rbp)
movsd -0x1198(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd -0x1190(%rbp), %xmm0
callq 0x22b0
movsd -0x11b0(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x37a5
leaq -0x8ec(%rbp), %rdi
leaq -0x1188(%rbp), %rsi
movl $0x89c, %edx            # imm = 0x89C
callq 0x2140
jmp 0x37a7
jmp 0x37a9
movl -0x11a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x11a4(%rbp)
jmp 0x3627
movsd -0x1190(%rbp), %xmm0
mulsd -0x8(%rbp), %xmm0
movsd %xmm0, -0x1190(%rbp)
callq 0x2100
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x3840(%rip), %xmm1     # 0x7030
divsd %xmm1, %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x40(%rbp), %rax
movsd (%rax), %xmm0
ucomisd -0x28(%rbp), %xmm0
jb 0x380d
jmp 0x3812
jmp 0x360a
jmp 0x35e6
addq $0x11b0, %rsp           # imm = 0x11B0
popq %rbp
retq