// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
leaq 0x40(%rsp), %r15
movq %r15, 0x8(%r15)
movq %r15, (%r15)
andq $0x0, 0x10(%r15)
movq %rdx, 0x30(%rsp)
movq %rsi, %r12
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movq %rdi, %r14
movl 0x48(%rdi), %eax
movl 0x4c(%rdi), %ecx
imull $0xa0, %ecx, %edx
addl %eax, %edx
movslq %edx, %rdx
imulq $0x18, %rdx, %rbx
addq %rsi, %rbx
imull $0xa0, %ecx, %ecx
addl %eax, %ecx
movslq %ecx, %rax
imulq $0x18, %rax, %rcx
addq %r12, %rcx
movq (%rbx), %rbx
cmpq %rcx, %rbx
je 0x857e
movq 0x10(%rbx), %rax
movq (%rax), %rdx
cmpq %r14, %rdx
je 0x852c
leaq 0x10(%rbx), %rsi
movupd 0x68(%r14), %xmm0
movupd 0x68(%rdx), %xmm1
subpd %xmm1, %xmm0
mulpd %xmm0, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
sqrtsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movq %r15, %rdi
callq 0x8d18
movl 0x48(%r14), %eax
movl 0x4c(%r14), %ecx
jmp 0x851a
movsd 0x1aa2(%rip), %xmm0     # 0xa028
movsd %xmm0, 0x8(%rsp)
pushq $0x1
popq %rbp
pushq $0x2
popq %rax
movl %eax, (%rsp)
movq %r12, 0x38(%rsp)
movl %ebp, %ebx
negl %ebx
movl %ebx, 0x4(%rsp)
cmpl (%rsp), %ebx
je 0x865b
movl 0x4(%rsp), %r12d
cmpl (%rsp), %r12d
je 0x8654
movl %ebp, %r13d
cmpl %ebp, %r12d
je 0x85d4
cmpl %r13d, %ebx
je 0x85d4
cmpl 0x4(%rsp), %ebx
je 0x85d4
cmpl 0x4(%rsp), %r12d
jne 0x85f0
movl 0x48(%r14), %eax
addl %ebx, %eax
cmpl $0x9f, %eax
ja 0x85f0
movl 0x4c(%r14), %ecx
addl %r12d, %ecx
cmpl $0x9f, %ecx
jbe 0x85f8
incl %r12d
movl %r13d, %ebp
jmp 0x85b0
imull $0xa0, %ecx, %ecx
addl %eax, %ecx
imulq $0x18, %rcx, %r15
addq 0x38(%rsp), %r15
movq %r15, %rbp
movq (%rbp), %rbp
cmpq %r15, %rbp
je 0x85f0
leaq 0x10(%rbp), %rsi
movq 0x10(%rbp), %rax
movq (%rax), %rcx
movupd 0x68(%r14), %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
sqrtsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
leaq 0x18(%rsp), %rdi
callq 0x8d18
jmp 0x860c
incl %ebx
jmp 0x85a2
xorl %eax, %eax
leaq 0x40(%rsp), %rbx
movq %rbx, %rcx
movq (%rcx), %rcx
cmpq %rbx, %rcx
je 0x8693
movq 0x10(%rcx), %rdx
movsd 0x8(%rdx), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x8665
movq (%rdx), %rax
movq %rax, 0x50(%r14)
movb $0x1, %al
movsd %xmm0, 0x8(%rsp)
jmp 0x8665
testb $0x1, %al
jne 0x8730
movq %rbx, %rdi
callq 0x8d24
movq 0x40(%rsp), %rsi
movq %rbx, %rdi
leaq 0x18(%rsp), %rbx
movq %rbx, %rdx
callq 0x8ffc
movq %rbx, %rdi
callq 0x8d24
incl %ebp
movl (%rsp), %eax
incl %eax
movl %eax, (%rsp)
cmpl $0x4, %eax
jne 0x859a
movupd 0x68(%r14), %xmm0
movsd 0x1947(%rip), %xmm1     # 0xa028
movq 0x10(%rsp), %rax
movq (%rax), %rax
cmpq 0x10(%rsp), %rax
je 0x8766
movq 0x10(%rax), %rcx
cmpq %r14, %rcx
je 0x86e6
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 0x86e6
movq %rcx, 0x50(%r14)
movsd %xmm2, 0x8(%rsp)
movapd %xmm2, %xmm1
jmp 0x86e6
leaq 0x18(%rsp), %rax
movq (%rax), %rax
leaq 0x18(%rsp), %rcx
cmpq %rcx, %rax
je 0x8766
movq 0x10(%rax), %rcx
movsd 0x8(%rcx), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x8735
movq (%rcx), %rcx
movq %rcx, 0x50(%r14)
movsd %xmm0, 0x8(%rsp)
jmp 0x8735
movsd 0x1bc2(%rip), %xmm0     # 0xa330
ucomisd 0x8(%rsp), %xmm0
movq 0x30(%rsp), %rax
seta (%rax)
leaq 0x18(%rsp), %rdi
callq 0x8d74
leaq 0x40(%rsp), %rdi
callq 0x8d74
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x87a1
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x8d74
leaq 0x40(%rsp), %rdi
callq 0x8d74
movq %rbx, %rdi
callq 0x2310