// File: /workspace/github_cpp_makefile_star2_O2/gchen98[P]macs/algorithm.cpp
// Function: GraphBuilder::getRandomEdgeToCoalesce(boost::shared_ptr<Edge>&, double)
EdgePtr GraphBuilder::getRandomEdgeToCoalesce(EdgePtr & coalescingEdge,
double dHeight){

    if (this->bEndGeneConversion){
        return this->gcOldEdge;
    }
    int iPopulation = coalescingEdge->getBottomNodeRef()->getPopulation();
    EdgePtrVector & pEdgeVector = this->pEdgeVectorByPop->at(iPopulation);
    
    EdgeIndexQueue & pVectorIndicesToRecycle = this->pVectorIndicesToRecycle->at(iPopulation);
    unsigned int edgeVectorSize = pEdgeVector.size();
    if (pConfig->bDebug){
      cerr<<"getRandomEdgeToCoalesce: selecting population "<<iPopulation<<" with avail edges: "<< edgeVectorSize<<"\n";
    }
    bool found = false;
    int iUnifPick =-1;
    int out_of_range = 0,already_deleted=0;
    while(!found){
        // guessing by randomly selecting elements from the vector of graph
        // edges appears to work pretty well here
        iUnifPick = static_cast<int>(pRandNumGenerator->unifRV()*
        edgeVectorSize);
        EdgePtr edge = pEdgeVector[iUnifPick];
        if (edge->bDeleted){
            // if this is marked for deletion, take the opportunity
            // to store the vector index in a queue so this element can
            // recycled when a new edge is to be added
            if (!edge->bInQueue){
                pVectorIndicesToRecycle.push(iUnifPick);
                edge->bInQueue = true;
            }
            if (pConfig->bDebug){
              //cerr<<"getRandomEdgeToCoalesce: edge already deleted \n";
              ++already_deleted;
            }
        // if the proposed coalescing time is within the endpoints of this
        // randomly picked edge, select this edge
        }else if (edge->getBottomNodeRef()->getHeight()<dHeight
         && edge->getTopNodeRef()->getHeight()>dHeight) {
          found = true;
        }else{
          if (pConfig->bDebug){
            //cerr<<"getRandomEdgeToCoalesce: Edge out of range\n";
            ++out_of_range;
          }
        }
    }
    EdgePtr & edge = pEdgeVector[iUnifPick];
    if (pConfig->bDebug){
        cerr<<"Stats: out of range edges: "<<out_of_range<<" and already deleted: "<<already_deleted<<endl;
        cerr<<"Edge to coalesce: "<<edge->getBottomNodeRef()->getHeight()<<" to "<<
        edge->getTopNodeRef()->getHeight()<<endl;
    }

    return edge;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm0, 0x8(%rsp)
movq %rsi, %r14
cmpb $0x1, 0xf9(%rsi)
jne 0x88c2
movq 0x100(%r14), %rax
movq %rax, (%rdi)
movq 0x108(%r14), %rax
jmp 0x8b31
movq %rdi, 0x20(%rsp)
movq %rdx, %rdi
callq 0xda24
leaq 0x20(%rax), %rdi
callq 0xda4e
movswq 0x62(%rax), %rbx
movq 0x98(%r14), %rdi
movq %rbx, %rsi
callq 0xdab2
movq %rax, %r15
movq 0xa0(%r14), %rdi
movq %rbx, %rsi
callq 0xdad0
movq %rax, 0x28(%rsp)
movq 0x8(%r15), %r12
subq (%r15), %r12
shrq $0x4, %r12
movq 0x8(%r14), %rax
cmpb $0x1, 0x48(%rax)
jne 0x895e
movq 0x166d1(%rip), %rdi     # 0x1eff0
leaq 0xd2f8(%rip), %rsi      # 0x15c1e
callq 0x3250
movq %rax, %rdi
movl %ebx, %esi
callq 0x33f0
leaq 0xd311(%rip), %rsi      # 0x15c4d
movq %rax, %rdi
callq 0x3250
movl %r12d, %esi
movq %rax, %rdi
callq 0x31a0
leaq 0xdf48(%rip), %rsi      # 0x1689e
movq %rax, %rdi
callq 0x3250
leaq 0x4(%rsp), %rax
orl $-0x1, (%rax)
movl %r12d, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
xorl %r13d, %r13d
xorl %ebx, %ebx
testb $0x1, %bl
jne 0x8a5e
movq (%r14), %rdi
callq 0xdaee
mulsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x4(%rsp)
cltq
movq (%r15), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rdx
movq %rdx, 0x10(%rsp)
movq 0x8(%rcx,%rax), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x89c4
lock
incl 0x8(%rax)
movq %r12, %rdi
callq 0xda24
movb 0x10(%rax), %bl
movq %r12, %rdi
callq 0xda24
cmpb $0x1, %bl
jne 0x8a09
cmpb $0x0, 0x11(%rax)
jne 0x89fd
movq 0x28(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0xe37c
movq %r12, %rdi
callq 0xda24
movb $0x1, 0x11(%rax)
movq 0x8(%r14), %rax
movzbl 0x48(%rax), %eax
addl %eax, %ebp
jmp 0x8a4d
addq $0x20, %rax
movq %rax, %rdi
callq 0xda4e
movsd 0x8(%rsp), %xmm0
ucomisd 0x70(%rax), %xmm0
jbe 0x8a42
movq %r12, %rdi
callq 0xda24
leaq 0x18(%rax), %rdi
callq 0xda4e
movsd 0x70(%rax), %xmm0
movb $0x1, %bl
ucomisd 0x8(%rsp), %xmm0
ja 0x8a4f
movq 0x8(%r14), %rax
movzbl 0x48(%rax), %eax
addl %eax, %r13d
xorl %ebx, %ebx
leaq 0x18(%rsp), %rdi
callq 0xdff8
jmp 0x8980
movslq 0x4(%rsp), %rbx
shlq $0x4, %rbx
movq 0x8(%r14), %rax
addq (%r15), %rbx
cmpb $0x1, 0x48(%rax)
jne 0x8b22
movq 0x16571(%rip), %r14     # 0x1eff0
leaq 0xd1db(%rip), %rsi      # 0x15c61
movq %r14, %rdi
callq 0x3250
movq %rax, %rdi
movl %r13d, %esi
callq 0x33f0
leaq 0xd1dd(%rip), %rsi      # 0x15c7d
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
movl %ebp, %esi
callq 0x33f0
movq %rax, %rdi
callq 0x3100
leaq 0xd1d3(%rip), %rsi      # 0x15c94
movq %r14, %rdi
callq 0x3250
movq %rax, %r14
movq %rbx, %rdi
callq 0xda24
leaq 0x20(%rax), %rdi
callq 0xda4e
movsd 0x70(%rax), %xmm0
movq %r14, %rdi
callq 0x3370
leaq 0xc5a8(%rip), %rsi      # 0x15099
movq %rax, %rdi
callq 0x3250
movq %rax, %r14
movq %rbx, %rdi
callq 0xda24
leaq 0x18(%rax), %rdi
callq 0xda4e
movsd 0x70(%rax), %xmm0
movq %r14, %rdi
callq 0x3370
movq %rax, %rdi
callq 0x3100
movq (%rbx), %rax
movq 0x20(%rsp), %rdi
movq %rax, (%rdi)
movq 0x8(%rbx), %rax
movq %rax, 0x8(%rdi)
testq %rax, %rax
je 0x8b3e
lock
incl 0x8(%rax)
movq %rdi, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0xdff8
movq %r14, %rdi
callq 0x3400
nop