// File: /workspace/github_cpp_makefile_star2_O3/pgrouchy[P]NoiseWorld/mathNet.cpp
// Function: Genome::setNearestNeighbor(std::__cxx11::list<gridEntry*, std::allocator<gridEntry*>>*, bool&, Population*)
void Genome::setNearestNeighbor(list<gridEntry*> *fullGrid, bool &isWithinReproductionDistance, Population *myPop) {
    list<Genome*>::iterator curGenome, curGenome2; 
    list<gridEntry*> innerEntriesToCheck;
    list<gridEntry*> outerEntriesToCheck;
    list<gridEntry*>::iterator curGridEntry;
    double minDist, myDist, closestIndDist = DBL_MAX;
    int i, j, xGridBlock, yGridBlock;
    int counter = 1;
    bool neighbourFound = false;

    // first check the grid location that is also occupied by the agent
    for (curGridEntry = fullGrid[this->mainGridLocation[1]*(int)(ISLAND_SIZE/NN_BLOCK_SIZE) + this->mainGridLocation[0]].begin(); curGridEntry != fullGrid[this->mainGridLocation[1]*(int)(ISLAND_SIZE/NN_BLOCK_SIZE) + this->mainGridLocation[0]].end(); ++curGridEntry) {
        if (((*curGridEntry)->myGenome != this)) {
            (*curGridEntry)->myDist = sqrt(pow((this->myPosition[0] - (*curGridEntry)->myGenome->myPosition[0]),2.0) + pow((this->myPosition[1] - (*curGridEntry)->myGenome->myPosition[1]),2.0));
            innerEntriesToCheck.push_back((*curGridEntry));
        }
    }

    do { // check next group of grid sqaures
        for (i = (-1*counter); i <= counter; i++) {
            for (j = (-1*counter); j <= counter; j++) {
                if ((i == (-1*counter)) || (j == (-1*counter)) || (i == counter) || (j == counter)) {
                    xGridBlock = this->mainGridLocation[0] + i;
                    if (xGridBlock >= (int)(ISLAND_SIZE/NN_BLOCK_SIZE)) {
                        continue;
                    } else if (xGridBlock < 0) {
                        continue;
                    }
                    yGridBlock = this->mainGridLocation[1] + j;
                    if (yGridBlock >= (int)(ISLAND_SIZE/NN_BLOCK_SIZE)) {
                        continue;
                    } else if (yGridBlock < 0) {
                        continue;
                    }
                    for (curGridEntry = fullGrid[yGridBlock*(int)(ISLAND_SIZE/NN_BLOCK_SIZE) + xGridBlock].begin(); curGridEntry != fullGrid[yGridBlock*(int)(ISLAND_SIZE/NN_BLOCK_SIZE) + xGridBlock].end(); ++curGridEntry) {
                        (*curGridEntry)->myDist = sqrt(pow((this->myPosition[0] - (*curGridEntry)->myGenome->myPosition[0]),2.0) + pow((this->myPosition[1] - (*curGridEntry)->myGenome->myPosition[1]),2.0));
                        outerEntriesToCheck.push_back((*curGridEntry));
                    }
                }
            }
        }

        // first check the inner grid locations for a neighbor
        for (curGridEntry = innerEntriesToCheck.begin(); curGridEntry != innerEntriesToCheck.end(); ++curGridEntry) {
            if ((*curGridEntry)->myDist < closestIndDist) {
                neighbourFound = true;
                closestIndDist = (*curGridEntry)->myDist;
                this->closestInd = (*curGridEntry)->myGenome;
            }
        }

        if (neighbourFound) { // if we find a neighbor, we still need to search the next layer of grid locations as a closer neighbor may be located there
            for (curGridEntry = outerEntriesToCheck.begin(); curGridEntry != outerEntriesToCheck.end(); ++curGridEntry) {
                if ((*curGridEntry)->myDist < closestIndDist) {
                    closestIndDist = (*curGridEntry)->myDist;
                    this->closestInd = (*curGridEntry)->myGenome;
                }
            }
        } else { // get ready to check the next layer
            innerEntriesToCheck.clear();
            innerEntriesToCheck.splice(innerEntriesToCheck.begin(), outerEntriesToCheck);
            outerEntriesToCheck.clear();
            counter++;
        }
    } while ((!neighbourFound) && (counter <= 2));

    if (!neighbourFound) { // if after 5 layers no neighbor is found, do a search of the entire population
        minDist = DBL_MAX;
        for (curGenome = myPop->genomes.begin(); curGenome != myPop->genomes.end(); ++curGenome) {
            if ((*curGenome) != this) {
                myDist = sqrt(pow((this->myPosition[0] - (*curGenome)->myPosition[0]), 2.0) + pow((this->myPosition[1] - (*curGenome)->myPosition[1]), 2.0));
                if (myDist < minDist) {
                    minDist = myDist;
                    this->closestInd = (*curGenome);
                    closestIndDist = minDist;
                }
            }
        }
    }

    isWithinReproductionDistance = false;
    if (closestIndDist < REPRODUCTION_DIST) {
        isWithinReproductionDistance = true;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, 0x50(%rsp)
movq %rdx, %r15
movq %rdi, %r8
leaq 0x28(%rsp), %rdi
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
movq %rsp, %rcx
movq %rcx, 0x8(%rcx)
movq %rcx, (%rcx)
movq %rax, 0x10(%rcx)
movl 0x48(%r8), %eax
movl 0x4c(%r8), %ecx
leal (%rcx,%rcx,4), %edx
shll $0x5, %edx
addl %eax, %edx
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %rdx
movq %rsi, 0x40(%rsp)
leaq (%rsi,%rdx,8), %rdx
movq (%rdx), %rbx
cmpq %rdx, %rbx
je 0xa50f
leaq 0x28(%rsp), %r14
movq 0x10(%rbx), %rdx
movq (%rdx), %rsi
cmpq %r8, %rsi
je 0xa4eb
movupd 0x68(%r8), %xmm0
movupd 0x68(%rsi), %xmm1
subpd %xmm1, %xmm0
mulpd %xmm0, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rdx)
movl $0x18, %edi
movq %r8, %r12
callq 0x21e0
movq 0x10(%rbx), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x2060
incq 0x38(%rsp)
movl 0x48(%r12), %eax
movl 0x4c(%r12), %ecx
movq %r12, %r8
movq (%rbx), %rbx
leal (%rcx,%rcx,4), %edx
shll $0x5, %edx
addl %eax, %edx
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %rdx
movq 0x40(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
cmpq %rdx, %rbx
jne 0xa482
movq %r15, 0x18(%rsp)
movsd 0xc44(%rip), %xmm4      # 0xb160
movl $0x1, %r15d
movq %r8, 0x48(%rsp)
movsd %xmm4, 0x20(%rsp)
testl %r15d, %r15d
js 0xa61b
movl %r15d, %r14d
negl %r14d
movl %r14d, %ebp
movl %r14d, %ebx
cmpl %r15d, %ebx
je 0xa55a
cmpl %r15d, %ebp
je 0xa55a
cmpl %r14d, %ebp
je 0xa55a
cmpl %r14d, %ebx
jne 0xa5ff
movl 0x48(%r8), %eax
addl %ebp, %eax
cmpl $0x9f, %eax
ja 0xa5ff
movl 0x4c(%r8), %ecx
addl %ebx, %ecx
cmpl $0x9f, %ecx
ja 0xa5ff
leal (%rcx,%rcx,4), %ecx
shll $0x5, %ecx
addl %eax, %ecx
leaq (%rcx,%rcx,2), %rax
movq 0x40(%rsp), %rcx
leaq (%rcx,%rax,8), %r13
movq (%r13), %r12
cmpq %r13, %r12
je 0xa5ff
movq 0x10(%r12), %rax
movq (%rax), %rcx
movupd 0x68(%r8), %xmm0
movupd 0x68(%rcx), %xmm1
subpd %xmm1, %xmm0
mulpd %xmm0, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movl $0x18, %edi
callq 0x21e0
movq 0x10(%r12), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %rsp, %rsi
callq 0x2060
incq 0x10(%rsp)
movq (%r12), %r12
cmpq %r13, %r12
movq 0x48(%rsp), %r8
jne 0xa59b
leal 0x1(%rbx), %eax
cmpl %r15d, %ebx
movl %eax, %ebx
jne 0xa542
leal 0x1(%rbp), %eax
cmpl %r15d, %ebp
movl %eax, %ebp
jne 0xa53f
movq 0x28(%rsp), %rdi
leaq 0x28(%rsp), %r12
cmpq %r12, %rdi
je 0xa697
xorl %eax, %eax
movq %rdi, %rcx
movq %rsp, %r14
movsd 0x20(%rsp), %xmm4
movq 0x10(%rcx), %rdx
movsd 0x8(%rdx), %xmm0
ucomisd %xmm0, %xmm4
jbe 0xa654
movq (%rdx), %rax
movq %rax, 0x50(%r8)
movb $0x1, %al
movapd %xmm0, %xmm4
movq (%rcx), %rcx
cmpq %r12, %rcx
jne 0xa638
testb $0x1, %al
je 0xa69c
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0xa751
movq %rdi, %rcx
movq 0x10(%rcx), %rdx
movsd 0x8(%rdx), %xmm0
ucomisd %xmm0, %xmm4
jbe 0xa68a
movq (%rdx), %rdx
movq %rdx, 0x50(%r8)
movapd %xmm0, %xmm4
movq (%rcx), %rcx
cmpq %r14, %rcx
jne 0xa670
jmp 0xa73f
movq %rsp, %r14
jmp 0xa6bc
movsd %xmm4, 0x20(%rsp)
cmpq %r12, %rdi
je 0xa6bc
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x21f0
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0xa6a7
movq %r12, 0x30(%rsp)
movq %r12, 0x28(%rsp)
movq $0x0, 0x38(%rsp)
movq (%rsp), %rax
cmpq %r14, %rax
je 0xa6fd
movq %r12, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x20b0
movq (%rsp), %rax
movq 0x10(%rsp), %rcx
addq %rcx, 0x38(%rsp)
movq $0x0, 0x10(%rsp)
cmpq %r14, %rax
je 0xa71a
movq (%rax), %rbx
movl $0x18, %esi
movq %rax, %rdi
callq 0x21f0
movq %rbx, %rax
cmpq %r14, %rbx
jne 0xa702
movq %r14, 0x8(%rsp)
movq %r14, (%rsp)
movq $0x0, 0x10(%rsp)
incl %r15d
xorl %eax, %eax
movq %r14, %rdi
movq 0x48(%rsp), %r8
movsd 0x20(%rsp), %xmm4
testb $0x1, %al
jne 0xa74d
cmpl $0x3, %r15d
jl 0xa527
testb $0x1, %al
je 0xa758
movq 0x18(%rsp), %rdx
jmp 0xa7bc
movq 0x50(%rsp), %rsi
movq (%rsi), %rax
cmpq %rsi, %rax
movq 0x18(%rsp), %rdx
je 0xa7bc
movupd 0x68(%r8), %xmm0
movsd 0x9e8(%rip), %xmm1      # 0xb160
movq 0x10(%rax), %rcx
cmpq %r8, %rcx
je 0xa7b4
movupd 0x68(%rcx), %xmm2
movapd %xmm0, %xmm3
subpd %xmm2, %xmm3
mulpd %xmm3, %xmm3
movapd %xmm3, %xmm2
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
addsd %xmm3, %xmm2
sqrtsd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jbe 0xa7b4
movq %rcx, 0x50(%r8)
movapd %xmm2, %xmm4
movapd %xmm2, %xmm1
movq (%rax), %rax
cmpq %rsi, %rax
jne 0xa778
movsd 0xbcc(%rip), %xmm0      # 0xb390
ucomisd %xmm4, %xmm0
seta (%rdx)
movq %rsp, %rbx
cmpq %rbx, %rdi
je 0xa7e8
movq (%rdi), %r14
movl $0x18, %esi
callq 0x21f0
movq %r14, %rdi
cmpq %rbx, %r14
jne 0xa7d3
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0xa807
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x21f0
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0xa7f2
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa818
movq %rax, %r14
movq (%rsp), %rdi
movq %rsp, %r12
cmpq %r12, %rdi
leaq 0x28(%rsp), %r15
je 0xa841
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x21f0
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0xa82c
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0xa860
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x21f0
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0xa84b
movq %r14, %rdi
callq 0x2300