// File: gabrielfelipeg[P]TravellingSalesman-GeneticAlgo/src/population.cpp
// Function: Population::crossingOver(Individual&, Individual&)
std::pair < Individual, Individual > Population::crossingOver(Individual &a, Individual &b){
    int left = rand()%a.chromosome.size();
    int right = std::min(left + (a.chromosome.size() / 3), a.chromosome.size() - 1);
    std::vector <int> freq1(a.chromosome.size(), 0);
    std::vector <int> freq2(a.chromosome.size(), 0);
    Individual ret1(std::vector<int>(a.chromosome.size(), -1));
    Individual ret2(std::vector<int>(a.chromosome.size(), -1));

    for(int i = left; i <= right; i++){
        ret1.chromosome[i] = a.chromosome[i];
        ret2.chromosome[i] = b.chromosome[i];
        freq1[ret1.chromosome[i]]++;
        freq2[ret2.chromosome[i]]++;
    }

    bool conflito = true;
    while(conflito){
        conflito = false;
        for(int i = 0; i < ret1.chromosome.size(); i++){
            if(ret1.chromosome[i] == -1 && freq1[b.chromosome[i]]){
                conflito = true;
                ret1.chromosome[i] = a.chromosome[i];
                freq1[ret1.chromosome[i]]++;
            }
        }
    }
    conflito = true;
    while(conflito){
        conflito = false;
        for(int i = 0; i < ret2.chromosome.size(); i++){
            if(ret2.chromosome[i] == -1 && freq2[a.chromosome[i]]){
                conflito = true;
                ret2.chromosome[i] = b.chromosome[i];
                freq2[ret2.chromosome[i]]++;
            }
        }
    }

    for(int i = 0; i < a.chromosome.size(); i++){
        if(ret1.chromosome[i] == -1){
            ret1.chromosome[i] = b.chromosome[i];
        }
        if(ret2.chromosome[i] == -1){
            ret2.chromosome[i] = a.chromosome[i];
        }
    }
    return {ret1, ret2};
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x140(%rbp)
movq %rdi, %rax
movq %rax, -0x138(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
callq 0x1030
cltq
movq %rax, -0x130(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2d80
movq %rax, %rcx
movq -0x130(%rbp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2d80
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
movq -0x128(%rbp), %rax
shrq %rdx
addq %rdx, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2d80
decq %rax
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x2620
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2d80
movq %rax, -0x120(%rbp)
movl $0x0, -0x54(%rbp)
leaq -0x55(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x2df0
movq -0x120(%rbp), %rsi
movq -0x118(%rbp), %rcx
leaq -0x50(%rbp), %rdi
leaq -0x54(%rbp), %rdx
callq 0x2e10
jmp 0x487f
leaq -0x55(%rbp), %rdi
callq 0x1e30
movq -0x18(%rbp), %rdi
callq 0x2d80
movq %rax, -0x150(%rbp)
movl $0x0, -0x84(%rbp)
leaq -0x85(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x2df0
movq -0x150(%rbp), %rsi
movq -0x148(%rbp), %rcx
leaq -0x80(%rbp), %rdi
leaq -0x84(%rbp), %rdx
callq 0x2e10
jmp 0x48d5
leaq -0x85(%rbp), %rdi
callq 0x1e30
movq -0x18(%rbp), %rdi
callq 0x2d80
movq %rax, -0x160(%rbp)
movl $0xffffffff, -0xbc(%rbp) # imm = 0xFFFFFFFF
leaq -0xbd(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x2df0
movq -0x160(%rbp), %rsi
movq -0x158(%rbp), %rcx
leaq -0xb8(%rbp), %rdi
leaq -0xbc(%rbp), %rdx
callq 0x2e10
jmp 0x4931
leaq -0xa0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x28e0
jmp 0x4946
leaq -0xb8(%rbp), %rdi
callq 0x1c50
leaq -0xbd(%rbp), %rdi
callq 0x1e30
movq -0x18(%rbp), %rdi
callq 0x2d80
movq %rax, -0x170(%rbp)
movl $0xffffffff, -0xf4(%rbp) # imm = 0xFFFFFFFF
leaq -0xf5(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x2df0
movq -0x170(%rbp), %rsi
movq -0x168(%rbp), %rcx
leaq -0xf0(%rbp), %rdi
leaq -0xf4(%rbp), %rdx
callq 0x2e10
jmp 0x49ae
leaq -0xd8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x28e0
jmp 0x49c3
leaq -0xf0(%rbp), %rdi
callq 0x1c50
leaq -0xf5(%rbp), %rdi
callq 0x1e30
movl -0x24(%rbp), %eax
movl %eax, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0x4b5e
movq -0x18(%rbp), %rdi
movslq -0xfc(%rbp), %rsi
callq 0x2dd0
movl (%rax), %eax
movl %eax, -0x178(%rbp)
leaq -0xa0(%rbp), %rdi
movslq -0xfc(%rbp), %rsi
callq 0x2dd0
movl -0x178(%rbp), %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rdi
movslq -0xfc(%rbp), %rsi
callq 0x2dd0
movl (%rax), %eax
movl %eax, -0x174(%rbp)
leaq -0xd8(%rbp), %rdi
movslq -0xfc(%rbp), %rsi
callq 0x2dd0
movl -0x174(%rbp), %ecx
movl %ecx, (%rax)
leaq -0xa0(%rbp), %rdi
movslq -0xfc(%rbp), %rsi
callq 0x2dd0
movslq (%rax), %rsi
leaq -0x50(%rbp), %rdi
callq 0x2dd0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
leaq -0xd8(%rbp), %rdi
movslq -0xfc(%rbp), %rsi
callq 0x2dd0
movslq (%rax), %rsi
leaq -0x80(%rbp), %rdi
callq 0x2dd0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x49e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x55(%rbp), %rdi
callq 0x1e30
jmp 0x4ede
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x85(%rbp), %rdi
callq 0x1e30
jmp 0x4ed5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x4b16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x1c50
leaq -0xbd(%rbp), %rdi
callq 0x1e30
jmp 0x4ecc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x4b4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x1c50
leaq -0xf5(%rbp), %rdi
callq 0x1e30
jmp 0x4ec0
movb $0x1, -0xfd(%rbp)
testb $0x1, -0xfd(%rbp)
je 0x4c68
movb $0x0, -0xfd(%rbp)
movl $0x0, -0x104(%rbp)
movslq -0x104(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x2d80
movq %rax, %rcx
movq -0x180(%rbp), %rax
cmpq %rcx, %rax
jae 0x4c63
leaq -0xa0(%rbp), %rdi
movslq -0x104(%rbp), %rsi
callq 0x2dd0
cmpl $-0x1, (%rax)
jne 0x4c4d
movq -0x20(%rbp), %rdi
movslq -0x104(%rbp), %rsi
callq 0x2dd0
movslq (%rax), %rsi
leaq -0x50(%rbp), %rdi
callq 0x2dd0
cmpl $0x0, (%rax)
je 0x4c4d
movb $0x1, -0xfd(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x104(%rbp), %rsi
callq 0x2dd0
movl (%rax), %eax
movl %eax, -0x184(%rbp)
leaq -0xa0(%rbp), %rdi
movslq -0x104(%rbp), %rsi
callq 0x2dd0
movl -0x184(%rbp), %ecx
movl %ecx, (%rax)
leaq -0xa0(%rbp), %rdi
movslq -0x104(%rbp), %rsi
callq 0x2dd0
movslq (%rax), %rsi
leaq -0x50(%rbp), %rdi
callq 0x2dd0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x4c4f
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x4b83
jmp 0x4b65
movb $0x1, -0xfd(%rbp)
testb $0x1, -0xfd(%rbp)
je 0x4d72
movb $0x0, -0xfd(%rbp)
movl $0x0, -0x108(%rbp)
movslq -0x108(%rbp), %rax
movq %rax, -0x190(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x2d80
movq %rax, %rcx
movq -0x190(%rbp), %rax
cmpq %rcx, %rax
jae 0x4d6d
leaq -0xd8(%rbp), %rdi
movslq -0x108(%rbp), %rsi
callq 0x2dd0
cmpl $-0x1, (%rax)
jne 0x4d57
movq -0x18(%rbp), %rdi
movslq -0x108(%rbp), %rsi
callq 0x2dd0
movslq (%rax), %rsi
leaq -0x80(%rbp), %rdi
callq 0x2dd0
cmpl $0x0, (%rax)
je 0x4d57
movb $0x1, -0xfd(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x108(%rbp), %rsi
callq 0x2dd0
movl (%rax), %eax
movl %eax, -0x194(%rbp)
leaq -0xd8(%rbp), %rdi
movslq -0x108(%rbp), %rsi
callq 0x2dd0
movl -0x194(%rbp), %ecx
movl %ecx, (%rax)
leaq -0xd8(%rbp), %rdi
movslq -0x108(%rbp), %rsi
callq 0x2dd0
movslq (%rax), %rsi
leaq -0x80(%rbp), %rdi
callq 0x2dd0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x4d59
movl -0x108(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x108(%rbp)
jmp 0x4c8d
jmp 0x4c6f
movl $0x0, -0x10c(%rbp)
movslq -0x10c(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2d80
movq %rax, %rcx
movq -0x1a0(%rbp), %rax
cmpq %rcx, %rax
jae 0x4e52
leaq -0xa0(%rbp), %rdi
movslq -0x10c(%rbp), %rsi
callq 0x2dd0
cmpl $-0x1, (%rax)
jne 0x4df1
movq -0x20(%rbp), %rdi
movslq -0x10c(%rbp), %rsi
callq 0x2dd0
movl (%rax), %eax
movl %eax, -0x1a4(%rbp)
leaq -0xa0(%rbp), %rdi
movslq -0x10c(%rbp), %rsi
callq 0x2dd0
movl -0x1a4(%rbp), %ecx
movl %ecx, (%rax)
leaq -0xd8(%rbp), %rdi
movslq -0x10c(%rbp), %rsi
callq 0x2dd0
cmpl $-0x1, (%rax)
jne 0x4e3c
movq -0x18(%rbp), %rdi
movslq -0x10c(%rbp), %rsi
callq 0x2dd0
movl (%rax), %eax
movl %eax, -0x1a8(%rbp)
leaq -0xd8(%rbp), %rdi
movslq -0x10c(%rbp), %rsi
callq 0x2dd0
movl -0x1a8(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x4e3e
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x4d7c
movq -0x140(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0xd8(%rbp), %rdx
callq 0x5b90
jmp 0x4e6e
leaq -0xd8(%rbp), %rdi
callq 0x1c30
leaq -0xa0(%rbp), %rdi
callq 0x1c30
leaq -0x80(%rbp), %rdi
callq 0x1c50
leaq -0x50(%rbp), %rdi
callq 0x1c50
movq -0x138(%rbp), %rax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x1c30
leaq -0xa0(%rbp), %rdi
callq 0x1c30
leaq -0x80(%rbp), %rdi
callq 0x1c50
leaq -0x50(%rbp), %rdi
callq 0x1c50
movq -0x60(%rbp), %rdi
callq 0x1180
nopw (%rax,%rax)