// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movq %rcx, 0x68(%rsp)
movq %rdx, %r15
movq %rdi, 0x70(%rsp)
callq 0x1040
cltq
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x2, %rsi
xorl %r14d, %r14d
xorl %edx, %edx
divq %rsi
movq %rdx, %r13
movslq %r13d, %rbx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
movq %rsi, %rax
mulq %rcx
movq %rdx, %r12
shrq %r12
addq %rbx, %r12
leaq -0x1(%rsi), %rax
cmpq %r12, %rax
cmovbq %rax, %r12
leaq 0x38(%rsp), %rdx
movl %r14d, (%rdx)
leaq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rcx
callq 0x1ab4
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x2, %rsi
movl %r14d, (%rbp)
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x1ab4
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x2, %rsi
leaq 0x8(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x90(%rsp), %rdi
leaq 0x4(%rsp), %rcx
callq 0x1ab4
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1888
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x25d0
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x2, %rsi
leaq 0x4(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x78(%rsp), %rdi
leaq 0x3(%rsp), %rcx
callq 0x1ab4
leaq 0x8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x1888
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x261e
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
movq 0x20(%rsp), %rax
cmpl %r12d, %r13d
movq 0x68(%rsp), %r14
jg 0x2671
movq (%r15), %rcx
movq (%r14), %rdx
movq 0x8(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %r8
movslq %r12d, %r9
movl (%rcx,%rbx,4), %r10d
movl %r10d, (%rax,%rbx,4)
movl (%rdx,%rbx,4), %r10d
movl %r10d, (%rsi,%rbx,4)
movslq (%rax,%rbx,4), %r10
incl (%rdi,%r10,4)
movslq (%rsi,%rbx,4), %r10
incl (%r8,%r10,4)
leaq 0x1(%rbx), %r10
cmpq %r9, %rbx
movq %r10, %rbx
jl 0x2645
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq (%r14), %rdx
movq 0x50(%rsp), %rsi
movq (%r15), %rdi
xorl %r9d, %r9d
xorl %r8d, %r8d
movq 0x70(%rsp), %rbx
movl %r8d, %r8d
cmpq %r8, %rcx
jbe 0x26c1
cmpl $-0x1, (%rax,%r8,4)
jne 0x26bc
movslq (%rdx,%r8,4), %r10
cmpl $0x0, (%rsi,%r10,4)
je 0x26bc
movslq (%rdi,%r8,4), %r9
movl %r9d, (%rax,%r8,4)
incl (%rsi,%r9,4)
movb $0x1, %r9b
incl %r8d
jmp 0x2693
xorl %r8d, %r8d
testb $0x1, %r9b
movl $0x0, %r9d
jne 0x2693
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
movq (%r15), %rsi
movq 0x38(%rsp), %rdi
movq (%r14), %r8
xorl %r10d, %r10d
xorl %r9d, %r9d
movl %r9d, %r9d
cmpq %r9, %rdx
jbe 0x2720
cmpl $-0x1, (%rcx,%r9,4)
jne 0x271b
movslq (%rsi,%r9,4), %r11
cmpl $0x0, (%rdi,%r11,4)
je 0x271b
movslq (%r8,%r9,4), %r10
movl %r10d, (%rcx,%r9,4)
incl (%rdi,%r10,4)
movb $0x1, %r10b
incl %r9d
jmp 0x26f2
xorl %r9d, %r9d
testb $0x1, %r10b
movl $0x0, %r10d
jne 0x26f2
movq (%r15), %rdx
movq 0x8(%r15), %rsi
subq %rdx, %rsi
je 0x2773
sarq $0x2, %rsi
movq (%r14), %rdi
cmpq $0x1, %rsi
adcq $0x0, %rsi
xorl %r8d, %r8d
cmpl $-0x1, (%rax,%r8,4)
jne 0x275c
movl (%rdi,%r8,4), %r9d
movl %r9d, (%rax,%r8,4)
cmpl $-0x1, (%rcx,%r8,4)
jne 0x276b
movl (%rdx,%r8,4), %r9d
movl %r9d, (%rcx,%r8,4)
incq %r8
cmpq %r8, %rsi
jne 0x274d
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x2f56
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x279c
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x27b3
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x27ca
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x27e1
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x282c
movq 0x18(%rsp), %rsi
jmp 0x281f
movq %rax, %rbx
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x282c
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
jmp 0x282c
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2862
movq 0x30(%rsp), %rsi
jmp 0x2855
movq %rax, %rbx
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x2862
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
jmp 0x2862
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x287e
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
jmp 0x287e
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2895
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x1130
movq %rbx, %rdi
callq 0x11c0
nop