// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x90(%rbp)
movq %rdi, %rax
movq %rax, -0x88(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
testb $0x1, 0xf9(%rax)
je 0x1513a
movq -0x90(%rbp), %rdi
movq -0x80(%rbp), %rsi
addq $0x100, %rsi            # imm = 0x100
callq 0x1df40
jmp 0x154a3
movq -0x18(%rbp), %rdi
callq 0x1de60
movq %rax, %rdi
callq 0x1deb0
movq %rax, %rdi
callq 0x1ded0
movq %rax, %rdi
callq 0x1e0b0
movw %ax, %cx
movq -0x80(%rbp), %rax
movswl %cx, %ecx
movl %ecx, -0x24(%rbp)
movq 0x98(%rax), %rdi
movslq -0x24(%rbp), %rsi
callq 0x1e0d0
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, -0x30(%rbp)
movq 0xa0(%rax), %rdi
movslq -0x24(%rbp), %rsi
callq 0x1e110
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1e150
movq %rax, %rcx
movq -0x80(%rbp), %rax
movl %ecx, -0x3c(%rbp)
movq 0x8(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x151fb
movq 0x33e35(%rip), %rdi     # 0x48ff0
leaq 0x1eae4(%rip), %rsi     # 0x33ca6
callq 0x42b0
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x44d0
movq %rax, %rdi
leaq 0x1eaf9(%rip), %rsi     # 0x33cd5
callq 0x42b0
movq %rax, %rdi
movl -0x3c(%rbp), %esi
callq 0x43e0
movq %rax, %rdi
leaq 0x1f80b(%rip), %rsi     # 0x34a01
callq 0x42b0
movb $0x0, -0x3d(%rbp)
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movb -0x3d(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x15222
jmp 0x1538d
movq -0x80(%rbp), %rax
movq (%rax), %rdi
callq 0x1e170
movl -0x3c(%rbp), %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x44(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x44(%rbp), %rsi
callq 0x1e190
movq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0x1df40
leaq -0x60(%rbp), %rdi
callq 0x1de60
testb $0x1, 0x10(%rax)
je 0x152cc
leaq -0x60(%rbp), %rdi
callq 0x1de60
testb $0x1, 0x11(%rax)
jne 0x152b0
movq -0x38(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0x1e1b0
jmp 0x15287
leaq -0x60(%rbp), %rdi
callq 0x1de60
movb $0x1, 0x11(%rax)
jmp 0x152b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x1e090
jmp 0x154b3
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x152c7
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1537f
leaq -0x60(%rbp), %rdi
callq 0x1de60
movq %rax, %rdi
callq 0x1deb0
movq %rax, -0x98(%rbp)
jmp 0x152e6
movq -0x98(%rbp), %rdi
callq 0x1ded0
movq %rax, %rdi
callq 0x1df20
movsd %xmm0, -0xa0(%rbp)
jmp 0x15304
movsd -0xa0(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x15364
leaq -0x60(%rbp), %rdi
callq 0x1de60
movq %rax, %rdi
callq 0x1e1e0
movq %rax, -0xa8(%rbp)
jmp 0x15331
movq -0xa8(%rbp), %rdi
callq 0x1ded0
movq %rax, %rdi
callq 0x1df20
movsd %xmm0, -0xb0(%rbp)
jmp 0x1534f
movsd -0xb0(%rbp), %xmm0
ucomisd -0x20(%rbp), %xmm0
jbe 0x15364
movb $0x1, -0x3d(%rbp)
jmp 0x1537d
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x1537b
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1537d
jmp 0x1537f
leaq -0x60(%rbp), %rdi
callq 0x1e090
jmp 0x15214
movq -0x30(%rbp), %rdi
movslq -0x44(%rbp), %rsi
callq 0x1e190
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, -0x78(%rbp)
movq 0x8(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x15493
movq 0x33c36(%rip), %rdi     # 0x48ff0
leaq 0x1e928(%rip), %rsi     # 0x33ce9
callq 0x42b0
movq %rax, %rdi
movl -0x48(%rbp), %esi
callq 0x44d0
movq %rax, %rdi
leaq 0x1e92a(%rip), %rsi     # 0x33d05
callq 0x42b0
movq %rax, %rdi
movl -0x4c(%rbp), %esi
callq 0x44d0
movq %rax, %rdi
movq 0x33bbb(%rip), %rsi     # 0x48fb0
callq 0x4300
movq 0x33bef(%rip), %rdi     # 0x48ff0
leaq 0x1e914(%rip), %rsi     # 0x33d1c
callq 0x42b0
movq %rax, -0xc0(%rbp)
movq -0x78(%rbp), %rdi
callq 0x1de60
movq %rax, %rdi
callq 0x1deb0
movq %rax, %rdi
callq 0x1ded0
movq %rax, %rdi
callq 0x1df20
movq -0xc0(%rbp), %rdi
callq 0x4550
movq %rax, %rdi
leaq 0x1dd1e(%rip), %rsi     # 0x33169
callq 0x42b0
movq %rax, -0xb8(%rbp)
movq -0x78(%rbp), %rdi
callq 0x1de60
movq %rax, %rdi
callq 0x1e1e0
movq %rax, %rdi
callq 0x1ded0
movq %rax, %rdi
callq 0x1df20
movq -0xb8(%rbp), %rdi
callq 0x4550
movq %rax, %rdi
movq 0x33b22(%rip), %rsi     # 0x48fb0
callq 0x4300
movq -0x90(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x1df40
movq -0x88(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x44e0
nopl (%rax)