// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rsi, %r14
cmpb $0x1, 0xf9(%rsi)
jne 0x9cb6
movq 0x100(%r14), %rax
movq %rax, (%rdi)
movq 0x108(%r14), %rax
jmp 0xa00b
movq (%rdx), %rax
testq %rax, %rax
je 0xa02a
movq 0x20(%rax), %rax
testq %rax, %rax
je 0xa049
movswq 0x62(%rax), %r12
movq 0x98(%r14), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r12, %rdx
jbe 0xa068
movq %rdi, 0x28(%rsp)
movq 0xa0(%r14), %rcx
movq (%rcx), %r15
movq 0x8(%rcx), %rcx
subq %r15, %rcx
sarq $0x4, %rcx
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rdx
cmpq %r12, %rdx
jbe 0xa068
leaq (%r12,%r12,2), %rcx
leaq (%rax,%rcx,8), %rbx
leaq (%r12,%r12,4), %r13
shlq $0x4, %r13
movq 0x8(%rbx), %rbp
subq (%rbx), %rbp
shrq $0x4, %rbp
movq 0x8(%r14), %rax
cmpb $0x1, 0x48(%rax)
jne 0x9db2
movq 0x16293(%rip), %rdi     # 0x1fff0
leaq 0xeeaa(%rip), %rsi      # 0x18c0e
movl $0x2e, %edx
callq 0x32a0
movq 0x1627b(%rip), %rdi     # 0x1fff0
movl %r12d, %esi
callq 0x33e0
movq %rax, %r12
leaq 0xeeb6(%rip), %rsi      # 0x18c3d
movl $0x13, %edx
movq %rax, %rdi
callq 0x32a0
movl %ebp, %esi
movq %r12, %rdi
callq 0x31a0
leaq 0xfae9(%rip), %rsi      # 0x1988e
movl $0x1, %edx
movq %rax, %rdi
callq 0x32a0
addq %r13, %r15
leaq 0xc(%rsp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl %ebp, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq (%r14), %rax
movq (%rax), %rdi
callq 0x10e1e
mulsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xc(%rsp)
movslq %eax, %rdx
movq (%rbx), %rsi
shlq $0x4, %rdx
movq (%rsi,%rdx), %rcx
movq %rcx, 0x18(%rsp)
movq 0x8(%rsi,%rdx), %rdx
movq %rdx, 0x20(%rsp)
testq %rdx, %rdx
je 0x9e14
lock
incl 0x8(%rdx)
testq %rcx, %rcx
je 0xa02a
cmpb $0x1, 0x10(%rcx)
jne 0x9e4a
cmpb $0x0, 0x11(%rcx)
jne 0x9eaf
movq 0x30(%r15), %rcx
movq 0x40(%r15), %rdx
addq $-0x4, %rdx
cmpq %rdx, %rcx
je 0x9e90
movl %eax, (%rcx)
addq $0x4, %rcx
movq %rcx, 0x30(%r15)
jmp 0x9e9d
movq 0x20(%rcx), %rax
testq %rax, %rax
je 0xa049
movsd 0x10(%rsp), %xmm0
ucomisd 0x70(%rax), %xmm0
jbe 0x9e83
movq 0x18(%rcx), %rax
testq %rax, %rax
je 0xa049
movsd 0x70(%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0x9e83
movb $0x1, %r13b
jmp 0x9eb9
movq 0x8(%r14), %rax
movzbl 0x48(%rax), %eax
addl %eax, %r12d
jmp 0x9eb9
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
callq 0x110d2
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0xa02a
movb $0x1, 0x11(%rax)
movq 0x8(%r14), %rax
movzbl 0x48(%rax), %eax
addl %eax, %ebp
leaq 0x20(%rsp), %rdi
callq 0x10dec
testb $0x1, %r13b
je 0x9dd5
movslq 0xc(%rsp), %r13
shlq $0x4, %r13
movq 0x8(%r14), %rax
addq (%rbx), %r13
cmpb $0x1, 0x48(%rax)
jne 0x9ffb
movq 0x16102(%rip), %r14     # 0x1fff0
leaq 0xed5c(%rip), %rsi      # 0x18c51
movl $0x1b, %edx
movq %r14, %rdi
callq 0x32a0
movq %r14, %rdi
movl %r12d, %esi
callq 0x33e0
movq %rax, %r15
leaq 0xed56(%rip), %rsi      # 0x18c6d
movl $0x16, %edx
movq %rax, %rdi
callq 0x32a0
movq %r15, %rdi
movl %ebp, %esi
callq 0x33e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
leaq 0xed25(%rip), %rsi      # 0x18c84
movl $0x12, %edx
movq %r14, %rdi
callq 0x32a0
movq (%r13), %rax
testq %rax, %rax
je 0xa02a
movq 0x20(%rax), %rax
testq %rax, %rax
je 0xa049
movsd 0x70(%rax), %xmm0
movq 0x1605e(%rip), %rdi     # 0x1fff0
callq 0x3360
movq %rax, %r14
leaq 0xe0ef(%rip), %rsi      # 0x18090
movl $0x4, %edx
movq %rax, %rdi
callq 0x32a0
movq (%r13), %rax
testq %rax, %rax
je 0xa02a
movq 0x18(%rax), %rax
testq %rax, %rax
je 0xa049
movsd 0x70(%rax), %xmm0
movq %r14, %rdi
callq 0x3360
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movq (%r13), %rax
movq 0x28(%rsp), %rdi
movq %rax, (%rdi)
movq 0x8(%r13), %rax
movq %rax, 0x8(%rdi)
testq %rax, %rax
je 0xa018
lock
incl 0x8(%rax)
movq %rdi, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xeac7(%rip), %rdi      # 0x18af8
leaq 0xf3ce(%rip), %rsi      # 0x19406
leaq 0xf3f3(%rip), %rcx      # 0x19432
movl $0x2d8, %edx            # imm = 0x2D8
callq 0x3170
leaq 0xeaa8(%rip), %rdi      # 0x18af8
leaq 0xeaa9(%rip), %rsi      # 0x18b00
leaq 0xf43d(%rip), %rcx      # 0x1949b
movl $0xc7, %edx
callq 0x3170
leaq 0xf469(%rip), %rdi      # 0x194d8
movq %r12, %rsi
xorl %eax, %eax
callq 0x3320
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x10dec
movq %rbx, %rdi
callq 0x33f0