// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, %rbx
callq 0x1030
cltq
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x2, %rsi
xorl %edx, %edx
divq %rsi
movslq %edx, %rbp
pushq $0x3
popq %rcx
movq %rsi, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r12
addq %rbp, %r12
leaq -0x1(%rsi), %rax
cmpq %r12, %rax
cmovbq %rax, %r12
leaq 0x38(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %r13
movq %r13, %rcx
callq 0x19b4
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x2, %rsi
andl $0x0, (%r13)
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x19b4
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x2, %rsi
leaq 0x8(%rsp), %rdx
orl $-0x1, (%rdx)
leaq 0x80(%rsp), %rdi
leaq 0x4(%rsp), %rcx
callq 0x19b4
leaq 0x20(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x17e0
leaq 0x80(%rsp), %rdi
callq 0x1612
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x2, %rsi
leaq 0x4(%rsp), %rdx
orl $-0x1, (%rdx)
leaq 0x68(%rsp), %rdi
leaq 0x3(%rsp), %rcx
callq 0x19b4
leaq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x17e0
leaq 0x68(%rsp), %rdi
callq 0x1612
movq (%r15), %rdx
movq 0x20(%rsp), %rcx
movq (%r14), %rsi
movq 0x8(%rsp), %rax
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %r8
movslq %r12d, %r9
cmpq %r9, %rbp
jg 0x2216
movl (%rdx,%rbp,4), %r10d
movl %r10d, (%rcx,%rbp,4)
movl (%rsi,%rbp,4), %r10d
movl %r10d, (%rax,%rbp,4)
movslq (%rcx,%rbp,4), %r10
incl (%rdi,%r10,4)
movslq (%rax,%rbp,4), %r10
incl (%r8,%r10,4)
incq %rbp
jmp 0x21ec
movq 0x28(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
movq (%r14), %rsi
movq 0x50(%rsp), %rdi
movq (%r15), %r8
xorl %r10d, %r10d
xorl %r9d, %r9d
movl %r9d, %r9d
cmpq %r9, %rdx
jbe 0x2261
cmpl $-0x1, (%rcx,%r9,4)
jne 0x225c
movslq (%rsi,%r9,4), %r11
cmpl $0x0, (%rdi,%r11,4)
je 0x225c
movslq (%r8,%r9,4), %r10
movl %r10d, (%rcx,%r9,4)
incl (%rdi,%r10,4)
movb $0x1, %r10b
incl %r9d
jmp 0x2233
xorl %r9d, %r9d
testb $0x1, %r10b
movl $0x0, %r10d
jne 0x2233
movq 0x10(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq (%r15), %rdx
movq 0x38(%rsp), %rsi
movq (%r14), %rdi
xorl %r9d, %r9d
xorl %r8d, %r8d
movl %r8d, %r8d
cmpq %r8, %rcx
jbe 0x22bb
cmpl $-0x1, (%rax,%r8,4)
jne 0x22b6
movslq (%rdx,%r8,4), %r10
cmpl $0x0, (%rsi,%r10,4)
je 0x22b6
movslq (%rdi,%r8,4), %r9
movl %r9d, (%rax,%r8,4)
incl (%rsi,%r9,4)
movb $0x1, %r9b
incl %r8d
jmp 0x228d
xorl %r8d, %r8d
testb $0x1, %r9b
movl $0x0, %r9d
jne 0x228d
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq 0x20(%rsp), %rdx
movq (%r14), %rsi
movq 0x8(%rsp), %rdi
xorl %r8d, %r8d
cmpq %r8, %rcx
je 0x2310
cmpl $-0x1, (%rdx,%r8,4)
jne 0x22fc
movl (%rsi,%r8,4), %r9d
movl %r9d, (%rdx,%r8,4)
cmpl $-0x1, (%rdi,%r8,4)
jne 0x230b
movl (%rax,%r8,4), %r9d
movl %r9d, (%rdi,%r8,4)
incq %r8
jmp 0x22e8
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x2886
leaq 0x8(%rsp), %rdi
callq 0x1612
leaq 0x20(%rsp), %rdi
callq 0x1612
leaq 0x38(%rsp), %rdi
callq 0x1612
leaq 0x50(%rsp), %rdi
callq 0x1612
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x2371
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x1612
jmp 0x237b
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
jmp 0x238d
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x1612
jmp 0x2397
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x1612
leaq 0x50(%rsp), %rdi
callq 0x1612
movq %rbx, %rdi
callq 0x1180
movq %rax, %rbx
jmp 0x23a1