// File: /workspace/github_cpp_makefile_star2_O2/gchen98[P]macs/algorithm.cpp
// Function: GraphBuilder::build()
void GraphBuilder::build(){
    double curPos = 0.0,lastPos = 0.0,dMaxPos = 1.0;
    unsigned int iLastCumulativePos = 0;
    cerr<<"Debugging: "<<pConfig->bDebug<<endl;

    HotSpotBinPtrList::iterator hotSpotIt;
    if (pConfig->bVariableRecomb){
      hotSpotIt=pConfig->pHotSpotBinPtrList->begin();
    }
    // gene conversion stuff
    GeneConversionPtr newGC;
    double dLogTractRatio = log((pConfig->iGeneConvTract-1.)/pConfig->iGeneConvTract);
    const int XOVER=0,GCSTART=1,GCEND=2;
    int xovers=0,gcstarts=0,gcends=0;
    int lastRE;
    int iHistoryMax = 0;
    do{
        if (iGraphIteration==0){
            cerr<<endl<<"Graph Builder begin"<<endl;
            NodePtr dummy1;
            EventPtr dummy2;
            double current=clock();
            this->traverseEvents(false,dummy1,dummy2);
            cerr<<"Time for build prior tree: "<<(clock()-current)<<" seconds\n";
        }else{
            // at this point decide whether we invoke a plain x-over
            // or a new gene conversion event
            this->bBeginGeneConversion = false;
            lastRE=XOVER;
            if (this->bEndGeneConversion){
              lastRE=GCEND;
            }else{
              this->bBeginGeneConversion = pRandNumGenerator->unifRV()<
              (pConfig->dGeneConvRatio/(pConfig->dGeneConvRatio+1))?true:false;
              if (bBeginGeneConversion){
                lastRE=GCSTART;
                double dTractLen = (1.+log(pRandNumGenerator->unifRV())/
                dLogTractRatio)/pConfig->dSeqLength;
                if (pConfig->bDebug) cerr<<"Proposing tract length: "
                <<dTractLen<<endl;
                    newGC = GeneConversionPtr(new GeneConversion(
                    curPos+dTractLen));
                    pGeneConversionPtrSet->insert(newGC);
              }
            }
            invokeRecombination(newGC);
            switch(lastRE){
            case XOVER:
              ++xovers;
              break;
            case GCSTART:
              ++gcstarts;
              break;
            case GCEND:
              ++gcends;
              break;
            }
            // mark the graph edges as the local tree
            markCurrentTree();
            if (!bIncrementHistory){
                double dBoundary = curPos - dTrailingGap;
                if (dBoundary>0.){
                  bIncrementHistory = true;
                }
            }else{
                ++iHistoryMax;
            }
            cerr<<"iHistoryMax: "<<iHistoryMax<<endl;
            if (iHistoryMax>=0){
                pruneARG(iHistoryMax);
            }
        }

        initializeCurrentTree();

        cerr<<"Tree:"<<iGraphIteration<<
        ",pos:"<<curPos<<
        ",len:"<<dLastTreeLength<<
        ",TMRCA:"<<localMRCA->getHeight()<<
        ",ARG:"<<
        ",len:"<<dArgLength<<
        ",TMRCA:"<<grandMRCA->getHeight()<<
        endl;
        if (pConfig->bVariableRecomb){
            bool bBinCrossed;
            do{
                bBinCrossed = getNextPos(curPos,hotSpotIt);
            }while(bBinCrossed);
        }else{
            curPos+=pRandNumGenerator->expRV(getRate());
        }
        // check if we reached the end of the region
        if (curPos>dMaxPos) curPos=dMaxPos;
        // check if there was an existing gene conversion event that needs
        // to be closed. backtrack if necessary.
        this->bEndGeneConversion  = checkPendingGeneConversions(curPos);
        if (pConfig->bNewickFormat){
            // Chen
            //uint iSegLength = (curPos-lastPos)*pConfig->dSeqLength;
            // Schiffels following two lines fixes
            uint iSegLength = curPos*pConfig->dSeqLength-iLastCumulativePos;
            iLastCumulativePos += iSegLength; 
            cout<<NEWICKTREE<<"\t["<<iSegLength<<"]"<<
            getNewickTree(localMRCA->getHeight(),localMRCA)<<";"<<endl;
        }
        if (pConfig->dTheta>0.0){
            addMutations(lastPos,curPos);
        }
        lastPos = curPos;
        ++iGraphIteration;
    }while(curPos<dMaxPos);
    cerr<<"Completed the chromosome at position "<<curPos<<endl;
    //if (pMutationPtrVector->size()>0){
    //        cout<<HAPLOEND<<endl;
    //}
    cerr<<"gcstarts:"<<gcstarts<<" gcends:"<<gcends<<" xovers:"<<xovers<<endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %rbx
andq $0x0, 0x8(%rsp)
movq 0x11be6(%rip), %rdi     # 0x1eff0
leaq 0x8f31(%rip), %rsi      # 0x16342
callq 0x3250
movq 0x8(%rbx), %rcx
movzbl 0x48(%rcx), %esi
movq %rax, %rdi
callq 0x33b0
movq %rax, %rdi
callq 0x3100
andq $0x0, 0x40(%rsp)
movq 0x8(%rbx), %rax
cmpb $0x1, 0x4b(%rax)
jne 0xd44d
movq 0x88(%rax), %rcx
movq (%rcx), %rcx
movq %rcx, 0x40(%rsp)
movl 0x38(%rax), %eax
cvtsi2sd %rax, %xmm1
movsd 0x871b(%rip), %xmm0     # 0x15b78
addsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x31e0
movsd %xmm0, 0x48(%rsp)
leaq 0x40(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x38(%rbx), %rax
movq %rax, 0x50(%rsp)
movl 0xd0(%rbx), %eax
xorl %ebp, %ebp
movq 0x11b5f(%rip), %r15     # 0x1eff0
xorpd %xmm0, %xmm0
movl $0x0, 0x20(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x24(%rsp)
movsd %xmm0, (%rsp)
testl %eax, %eax
je 0xd4e8
movb $0x0, 0xf8(%rbx)
cmpb $0x0, 0xf9(%rbx)
je 0xd58c
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0x914a
incl 0x20(%rsp)
jmp 0xd670
movq %r15, %rdi
callq 0x3100
movq %rax, %rdi
leaq 0x8e54(%rip), %rsi      # 0x1634e
callq 0x3250
movq %rax, %rdi
callq 0x3100
andq $0x0, 0x58(%rsp)
andq $0x0, 0x28(%rsp)
callq 0x3190
movq %rax, %r13
movq %rbx, %rdi
xorl %esi, %esi
leaq 0x58(%rsp), %rdx
leaq 0x28(%rsp), %rcx
callq 0x9a62
movq %r15, %rdi
leaq 0x8e29(%rip), %rsi      # 0x16362
callq 0x3250
movq %rax, %r14
cvtsi2sd %r13, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x3190
cvtsi2sd %rax, %xmm0
subsd 0x10(%rsp), %xmm0
movq %r14, %rdi
callq 0x3370
movq %rax, %rdi
leaq 0x8e10(%rip), %rsi      # 0x1637e
callq 0x3250
leaq 0x28(%rsp), %rdi
callq 0x6ce8
leaq 0x58(%rsp), %rdi
callq 0xdb46
jmp 0xd6ca
movq (%rbx), %rdi
callq 0xdaee
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm1
movapd %xmm1, %xmm2
movsd 0x6a5f(%rip), %xmm3     # 0x14008
addsd %xmm3, %xmm2
divsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
seta 0xf8(%rbx)
jbe 0xd65f
movq (%rbx), %rdi
callq 0xdaee
callq 0x31e0
divsd 0x48(%rsp), %xmm0
addsd 0x6a2b(%rip), %xmm0     # 0x14008
movq 0x8(%rbx), %rax
divsd 0x20(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
cmpb $0x1, 0x48(%rax)
jne 0xd617
movq %r15, %rdi
leaq 0x8d8c(%rip), %rsi      # 0x16388
callq 0x3250
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3370
movq %rax, %rdi
callq 0x3100
pushq $0x20
popq %rdi
callq 0x3260
movq %rax, %r13
movsd 0x10(%rsp), %xmm0
addsd (%rsp), %xmm0
movq %rax, %rdi
callq 0xf68a
movq %r13, 0x30(%rsp)
movq 0xf0(%rbx), %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
callq 0xf4e2
movq %rbx, %rdi
movq %r14, %rsi
callq 0x914a
incl 0x18(%rsp)
jmp 0xd670
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0x914a
incl 0x1c(%rsp)
movq %rbx, %rdi
callq 0xc860
cmpb $0x0, 0xe8(%rbx)
je 0xd685
incl %ebp
jmp 0xd69b
movsd (%rsp), %xmm0
ucomisd 0xe0(%rbx), %xmm0
jbe 0xd69b
movb $0x1, 0xe8(%rbx)
movq %r15, %rdi
leaq 0x8cfc(%rip), %rsi      # 0x163a1
callq 0x3250
movq %rax, %rdi
movl %ebp, %esi
callq 0x33f0
movq %rax, %rdi
callq 0x3100
testl %ebp, %ebp
js 0xd6ca
movq %rbx, %rdi
movl %ebp, %esi
callq 0xc92a
movq %rbx, %rdi
callq 0x11178
movq %r15, %rdi
leaq 0x8cd3(%rip), %rsi      # 0x163af
callq 0x3250
movl 0xd0(%rbx), %esi
movq %rax, %rdi
callq 0x33f0
movq %rax, %rdi
leaq 0x8cbc(%rip), %rsi      # 0x163b5
callq 0x3250
movq %rax, %rdi
movsd (%rsp), %xmm0
callq 0x3370
movq %rax, %rdi
leaq 0x8ca6(%rip), %r13      # 0x163bb
movq %r13, %rsi
callq 0x3250
movsd 0x30(%rbx), %xmm0
movq %rax, %rdi
callq 0x3370
movq %rax, %rdi
leaq 0x8c8d(%rip), %r12      # 0x163c1
movq %r12, %rsi
callq 0x3250
movq %rax, %r14
movq 0x38(%rsp), %rdi
callq 0xda4e
movsd 0x70(%rax), %xmm0
movq %r14, %rdi
callq 0x3370
movq %rax, %rdi
leaq 0x8c69(%rip), %rsi      # 0x163c9
callq 0x3250
movq %rax, %rdi
movq %r13, %rsi
callq 0x3250
movsd 0x28(%rbx), %xmm0
movq %rax, %rdi
callq 0x3370
movq %rax, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, %r14
movq 0x50(%rsp), %rdi
callq 0xda4e
movsd 0x70(%rax), %xmm0
movq %r14, %rdi
callq 0x3370
movq %rax, %rdi
callq 0x3100
movq 0x8(%rbx), %rax
cmpb $0x1, 0x4b(%rax)
jne 0xd7d8
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xcfc0
testb %al, %al
jne 0xd7be
movsd 0x8(%rsp), %xmm0
jmp 0xd7fd
movq (%rbx), %rdi
movsd 0x30(%rbx), %xmm0
mulsd 0xc8(%rbx), %xmm0
callq 0xddac
addsd (%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x8(%rsp), %r14
ucomisd 0x6803(%rip), %xmm0     # 0x14008
jbe 0xd816
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd136
movb %al, 0xf9(%rbx)
movq 0x8(%rbx), %rax
cmpb $0x1, 0x4c(%rax)
jne 0xd8fa
movl %ebp, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
movl 0x24(%rsp), %ecx
cvtsi2sd %rcx, %xmm1
mulsd 0x20(%rax), %xmm0
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %r13
movq 0x11773(%rip), %rdi     # 0x1efd0
leaq 0x11bb4(%rip), %rsi     # 0x1f418
callq 0x3210
movq %rax, %rdi
leaq 0x8b5c(%rip), %rsi      # 0x163cf
callq 0x3250
movl %r13d, %esi
movq %rax, %rdi
callq 0x31a0
movq %rax, %rdi
leaq 0x8c59(%rip), %rsi      # 0x164e6
callq 0x3250
movq %rax, %r14
movq 0x38(%rsp), %r12
movq %r12, %rdi
callq 0xda4e
movsd 0x70(%rax), %xmm0
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x117b4
movq %r14, %rdi
movq %r15, %rsi
callq 0x3210
movq %rax, %rdi
leaq 0x8b03(%rip), %rsi      # 0x163d2
callq 0x3250
movq %rax, %rdi
callq 0x3100
addl %r13d, 0x24(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x3468
movq 0x8(%rbx), %rax
movl 0x10(%rsp), %ebp
movq 0x116f6(%rip), %r15     # 0x1eff0
movsd (%rax), %xmm0
ucomisd 0x670a(%rip), %xmm0     # 0x14010
movsd 0x8(%rsp), %xmm1
jbe 0xd92a
movq %rbx, %rdi
movsd (%rsp), %xmm0
movsd %xmm1, (%rsp)
movsd (%rsp), %xmm1
callq 0xcca2
movsd (%rsp), %xmm1
movl 0xd0(%rbx), %eax
incl %eax
movl %eax, 0xd0(%rbx)
movsd 0x66c8(%rip), %xmm0     # 0x14008
ucomisd %xmm1, %xmm0
movapd %xmm1, %xmm0
ja 0xd4b5
movq 0x1169b(%rip), %rbx     # 0x1eff0
leaq 0x8a78(%rip), %rsi      # 0x163d4
movq %rbx, %rdi
movsd %xmm1, (%rsp)
callq 0x3250
movq %rax, %rdi
movsd (%rsp), %xmm0
callq 0x3370
movq %rax, %rdi
callq 0x3100
leaq 0x8a75(%rip), %rsi      # 0x163fa
movq %rbx, %rdi
callq 0x3250
movq %rax, %rdi
movl 0x18(%rsp), %esi
callq 0x33f0
leaq 0x8a64(%rip), %rsi      # 0x16404
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
movl 0x20(%rsp), %esi
callq 0x33f0
leaq 0x8a52(%rip), %rsi      # 0x1640d
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
movl 0x1c(%rsp), %esi
callq 0x33f0
movq %rax, %rdi
callq 0x3100
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r13, %rdi
callq 0x3270
jmp 0xda1c
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x6ce8
leaq 0x58(%rsp), %rdi
callq 0xdb46
jmp 0xda1c
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x3468
movq %rbx, %rdi
callq 0x3400