// File: /workspace/github_cpp_makefile_star2_O3/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 0x9c74
movq 0x100(%r14), %rax
movq %rax, (%rdi)
movq 0x108(%r14), %rax
jmp 0x9fc2
movq (%rdx), %rax
testq %rax, %rax
je 0x9fe1
movq 0x20(%rax), %rax
testq %rax, %rax
je 0xa000
movswq 0x62(%rax), %rbx
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 %rbx, %rdx
jbe 0xa01f
movq %rdi, 0x18(%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 %rbx, %rdx
jbe 0xa01f
leaq (%rbx,%rbx,2), %rcx
leaq (%rax,%rcx,8), %r13
leaq (%rbx,%rbx,4), %r12
shlq $0x4, %r12
movq 0x8(%r13), %rbp
subq (%r13), %rbp
shrq $0x4, %rbp
movq 0x8(%r14), %rax
cmpb $0x1, 0x48(%rax)
jne 0x9d70
movq 0x162d4(%rip), %rdi     # 0x1fff0
leaq 0xef4b(%rip), %rsi      # 0x18c6e
movl $0x2e, %edx
callq 0x32a0
movq 0x162bc(%rip), %rdi     # 0x1fff0
movl %ebx, %esi
callq 0x33e0
movq %rax, %rbx
leaq 0xef58(%rip), %rsi      # 0x18c9d
movl $0x13, %edx
movq %rax, %rdi
callq 0x32a0
movl %ebp, %esi
movq %rbx, %rdi
callq 0x31a0
leaq 0xfb8b(%rip), %rsi      # 0x198ee
movl $0x1, %edx
movq %rax, %rdi
callq 0x32a0
addq %r12, %r15
leaq 0xc(%rsp), %rcx
movl %ebp, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0xffffffff, (%rcx)     # imm = 0xFFFFFFFF
xorl %r12d, %r12d
leaq 0x30(%rsp), %rbx
xorl %ebp, %ebp
movq (%r14), %rax
movq (%rax), %rdi
callq 0x10d90
mulsd 0x20(%rsp), %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0xc(%rsp)
movslq %ecx, %rdx
movq (%r13), %rsi
shlq $0x4, %rdx
movq (%rsi,%rdx), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%rsi,%rdx), %rdx
movq %rdx, 0x30(%rsp)
testq %rdx, %rdx
je 0x9dd5
lock
incl 0x8(%rdx)
testq %rax, %rax
je 0x9fe1
cmpb $0x1, 0x10(%rax)
jne 0x9e07
cmpb $0x0, 0x11(%rax)
jne 0x9e67
movq 0x30(%r15), %rdx
movq 0x40(%r15), %rsi
addq $-0x4, %rsi
cmpq %rsi, %rdx
je 0x9e48
movl %ecx, (%rdx)
addq $0x4, %rdx
movq %rdx, 0x30(%r15)
jmp 0x9e63
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0xa000
movsd 0x10(%rsp), %xmm0
ucomisd 0x70(%rcx), %xmm0
jbe 0x9e3b
movq 0x18(%rax), %rax
testq %rax, %rax
je 0xa000
movsd 0x70(%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
ja 0x9e7e
movq 0x8(%r14), %rax
movzbl 0x48(%rax), %eax
addl %eax, %r12d
jmp 0x9e71
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
callq 0x11052
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x9fe1
movb $0x1, 0x11(%rax)
movq 0x8(%r14), %rax
movzbl 0x48(%rax), %eax
addl %eax, %ebp
movq %rbx, %rdi
callq 0x10d5e
jmp 0x9d95
movq %rbx, %rdi
callq 0x10d5e
movslq 0xc(%rsp), %r15
shlq $0x4, %r15
movq 0x8(%r14), %rax
addq (%r13), %r15
cmpb $0x1, 0x48(%rax)
jne 0x9fb3
movq 0x16148(%rip), %rbx     # 0x1fff0
leaq 0xee02(%rip), %rsi      # 0x18cb1
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x32a0
movq %rbx, %rdi
movl %r12d, %esi
callq 0x33e0
movq %rax, %r14
leaq 0xedfc(%rip), %rsi      # 0x18ccd
movl $0x16, %edx
movq %rax, %rdi
callq 0x32a0
movq %r14, %rdi
movl %ebp, %esi
callq 0x33e0
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
leaq 0xedcb(%rip), %rsi      # 0x18ce4
movl $0x12, %edx
movq %rbx, %rdi
callq 0x32a0
movq (%r15), %rax
testq %rax, %rax
je 0x9fe1
movq 0x20(%rax), %rax
testq %rax, %rax
je 0xa000
movsd 0x70(%rax), %xmm0
movq 0x160a5(%rip), %rdi     # 0x1fff0
callq 0x3360
movq %rax, %rbx
leaq 0xe136(%rip), %rsi      # 0x18090
movl $0x4, %edx
movq %rax, %rdi
callq 0x32a0
movq (%r15), %rax
testq %rax, %rax
je 0x9fe1
movq 0x18(%rax), %rax
testq %rax, %rax
je 0xa000
movsd 0x70(%rax), %xmm0
movq %rbx, %rdi
callq 0x3360
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movq (%r15), %rax
movq 0x18(%rsp), %rdi
movq %rax, (%rdi)
movq 0x8(%r15), %rax
movq %rax, 0x8(%rdi)
testq %rax, %rax
je 0x9fcf
lock
incl 0x8(%rax)
movq %rdi, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xeb10(%rip), %rdi      # 0x18af8
leaq 0xf477(%rip), %rsi      # 0x19466
leaq 0xf49c(%rip), %rcx      # 0x19492
movl $0x2d8, %edx            # imm = 0x2D8
callq 0x3170
leaq 0xeaf1(%rip), %rdi      # 0x18af8
leaq 0xeaf2(%rip), %rsi      # 0x18b00
leaq 0xf4e6(%rip), %rcx      # 0x194fb
movl $0xc7, %edx
callq 0x3170
leaq 0xf512(%rip), %rdi      # 0x19538
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3320
movq %rax, %r14
movq %rbx, %rdi
callq 0x10d5e
movq %r14, %rdi
callq 0x33f0
nop