// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %rdi, %rbx
xorl %r12d, %r12d
movq %r12, 0x8(%rsp)
movq 0x10081(%rip), %r13     # 0x1fff0
leaq 0x93bc(%rip), %rsi      # 0x19332
movl $0xb, %edx
movq %r13, %rdi
callq 0x32a0
movq 0x8(%rbx), %rax
movzbl 0x48(%rax), %esi
movq %r13, %rdi
callq 0x33a0
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
movq %r12, 0x40(%rsp)
movq 0x8(%rbx), %rax
cmpb $0x1, 0x4b(%rax)
jne 0xffdb
movq 0x88(%rax), %rcx
movq (%rcx), %rcx
movq %rcx, 0x40(%rsp)
movl 0x38(%rax), %eax
cvtsi2sd %rax, %xmm1
movsd 0x8b85(%rip), %xmm0     # 0x18b70
addsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x31e0
movsd %xmm0, 0x68(%rsp)
leaq 0x40(%rbx), %rax
movq %rax, 0x70(%rsp)
leaq 0x9397(%rip), %r12      # 0x193a5
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x24(%rsp)
cmpl $0x0, 0xd0(%rbx)
je 0x1017b
movb $0x0, 0xf8(%rbx)
movl $0x2, %ebp
cmpb $0x0, 0xf9(%rbx)
jne 0x10153
movq (%rbx), %rax
movq (%rax), %rdi
callq 0x10e1e
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm1
movapd %xmm1, %xmm2
addsd 0x6f87(%rip), %xmm2     # 0x17008
divsd %xmm2, %xmm1
xorl %ebp, %ebp
ucomisd %xmm0, %xmm1
seta 0xf8(%rbx)
jbe 0x10153
movq (%rbx), %rax
movq (%rax), %rdi
callq 0x10e1e
callq 0x31e0
divsd 0x68(%rsp), %xmm0
addsd 0x6f52(%rip), %xmm0     # 0x17008
movq 0x8(%rbx), %rax
divsd 0x20(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
cmpb $0x1, 0x48(%rax)
jne 0x10117
movl $0x18, %edx
movq %r13, %rdi
leaq 0x929e(%rip), %rsi      # 0x19378
callq 0x32a0
movq %r13, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3360
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movl $0x20, %edi
callq 0x3260
movq %rax, %rbp
movsd 0x10(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movq %rax, %rdi
callq 0x11e58
movq %rbp, 0x38(%rsp)
movq 0xf0(%rbx), %rdi
leaq 0x38(%rsp), %rsi
callq 0x11cea
movl $0x1, %ebp
movq %rbx, %rdi
leaq 0x38(%rsp), %rsi
callq 0xaa68
cmpl $0x2, %ebp
je 0x1027b
cmpl $0x1, %ebp
jne 0x10281
incl 0x18(%rsp)
jmp 0x10285
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movq %rax, %rbp
movl $0x13, %edx
movq %rax, %rdi
leaq 0x9189(%rip), %rsi      # 0x1933e
callq 0x32a0
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
xorl %eax, %eax
movq %rax, 0x48(%rsp)
movq %rax, 0x30(%rsp)
callq 0x3190
movq %rax, %rbp
movq %rbx, %rdi
xorl %esi, %esi
leaq 0x48(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0xb6bc
movl $0x1b, %edx
movq %r13, %rdi
leaq 0x9139(%rip), %rsi      # 0x19352
callq 0x32a0
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x3190
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subsd 0x10(%rsp), %xmm0
movq %r13, %rdi
callq 0x3360
movl $0x9, %edx
movq %rax, %rdi
leaq 0x9118(%rip), %rsi      # 0x1936e
callq 0x32a0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x10271
decq 0x8(%rdi)
jne 0x10271
movq (%rdi), %rax
callq *0x8(%rax)
movl %r14d, 0x10(%rsp)
jmp 0x10310
incl 0x1c(%rsp)
jmp 0x10285
incl 0x20(%rsp)
movq %rbx, %rdi
callq 0xedf2
cmpb $0x0, 0xe8(%rbx)
je 0x1029b
incl %r14d
jmp 0x102b2
movsd 0x8(%rsp), %xmm0
ucomisd 0xe0(%rbx), %xmm0
jbe 0x102b2
movb $0x1, 0xe8(%rbx)
movl $0xd, %edx
movq %r13, %rdi
leaq 0x90d0(%rip), %rsi      # 0x19391
callq 0x32a0
movq %r13, %rdi
movl %r14d, %esi
callq 0x33e0
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
testl %r14d, %r14d
movl %r14d, 0x10(%rsp)
js 0x10310
movq %rbx, %rdi
movl %r14d, %esi
callq 0xef6c
movq %rbx, %rdi
callq 0x141aa
movl $0x5, %edx
movq %r13, %rdi
leaq 0x9078(%rip), %rsi      # 0x1939f
callq 0x32a0
movl 0xd0(%rbx), %esi
movq %r13, %rdi
callq 0x33e0
movq %rax, %rbp
movl $0x5, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x32a0
movsd 0x8(%rsp), %xmm0
movq %rbp, %rdi
movsd %xmm0, 0x80(%rsp)
callq 0x3360
movq %rax, %rbp
movl $0x5, %edx
movq %rax, %rdi
leaq 0x9035(%rip), %r15      # 0x193ab
movq %r15, %rsi
callq 0x32a0
movsd 0x30(%rbx), %xmm0
movq %rbp, %rdi
callq 0x3360
movq %rax, %rbp
movl $0x7, %edx
movq %rax, %rdi
leaq 0x9014(%rip), %r14      # 0x193b1
movq %r14, %rsi
callq 0x32a0
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x10759
movsd 0x70(%rax), %xmm0
movq %rbp, %rdi
callq 0x3360
movq %rax, %rbp
movl $0x5, %edx
movq %rax, %rdi
leaq 0x8fe8(%rip), %rsi      # 0x193b9
callq 0x32a0
movl $0x5, %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x32a0
movsd 0x28(%rbx), %xmm0
movq %rbp, %rdi
callq 0x3360
movq %rax, %rbp
movl $0x7, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x32a0
movq 0x38(%rbx), %rax
testq %rax, %rax
je 0x10759
movsd 0x70(%rax), %xmm0
movq %rbp, %rdi
callq 0x3360
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movq 0x8(%rbx), %rax
cmpb $0x1, 0x4b(%rax)
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %r15
jne 0x10472
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xf8ca
testb %al, %al
jne 0x1045e
jmp 0x104be
movq (%rbx), %rax
movsd 0x30(%rbx), %xmm0
mulsd 0xc8(%rbx), %xmm0
movsd %xmm0, 0x78(%rsp)
movq (%rax), %rdi
callq 0x10e1e
movsd 0x6b70(%rip), %xmm1     # 0x17008
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x31e0
divsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
ucomisd 0x6b3c(%rip), %xmm0     # 0x17008
jbe 0x104dd
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfac6
movb %al, 0xf9(%rbx)
movq 0x8(%rbx), %rax
cmpb $0x1, 0x4c(%rax)
jne 0x10615
movl 0x24(%rsp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd 0x8(%rsp), %xmm1
mulsd 0x20(%rax), %xmm1
subsd %xmm0, %xmm1
movq 0x70(%rsp), %r14
cvttsd2si %xmm1, %r12
movq 0xfef0(%rip), %rsi      # 0x20418
movq 0xfef1(%rip), %rdx      # 0x20420
movq 0xfa9a(%rip), %rdi      # 0x1ffd0
callq 0x32a0
movq %rax, %rbp
movl $0x2, %edx
movq %rax, %rdi
leaq 0x8e72(%rip), %rsi      # 0x193bf
callq 0x32a0
movl %r12d, %esi
movq %rbp, %rdi
callq 0x31a0
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
leaq 0x8f67(%rip), %rsi      # 0x194d6
callq 0x32a0
movq (%r14), %rax
testq %rax, %rax
je 0x10759
movsd 0x70(%rax), %xmm0
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x14a38
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %rbp, %rdi
callq 0x32a0
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
leaq 0x8e09(%rip), %rsi      # 0x193c2
callq 0x32a0
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
movl 0x10(%rsp), %r14d
je 0x10607
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3270
addl %r12d, 0x24(%rsp)
leaq 0x8d92(%rip), %r12      # 0x193a5
jmp 0x1061a
movl 0x10(%rsp), %r14d
movq 0x8(%rbx), %rax
movsd (%rax), %xmm0
ucomisd 0x69e6(%rip), %xmm0     # 0x17010
jbe 0x10640
movsd 0x8(%rsp), %xmm1
movq %rbx, %rdi
movsd 0x28(%rsp), %xmm0
callq 0xf51e
movsd 0x8(%rsp), %xmm1
incl 0xd0(%rbx)
movsd 0x69b4(%rip), %xmm0     # 0x17008
movsd %xmm1, 0x28(%rsp)
ucomisd %xmm1, %xmm0
ja 0x1003b
movq 0xf985(%rip), %rbx      # 0x1fff0
leaq 0x8d52(%rip), %rsi      # 0x193c4
movl $0x25, %edx
movq %rbx, %rdi
callq 0x32a0
movq %rbx, %rdi
movsd 0x28(%rsp), %xmm0
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
leaq 0x8d2c(%rip), %rsi      # 0x193ea
movl $0x9, %edx
movq %rbx, %rdi
callq 0x32a0
movq %rbx, %rdi
movl 0x18(%rsp), %esi
callq 0x33e0
movq %rax, %rbx
leaq 0x8d13(%rip), %rsi      # 0x193f4
movl $0x8, %edx
movq %rax, %rdi
callq 0x32a0
movq %rbx, %rdi
movl 0x1c(%rsp), %esi
callq 0x33e0
movq %rax, %rbx
leaq 0x8cf9(%rip), %rsi      # 0x193fd
movl $0x8, %edx
movq %rax, %rdi
callq 0x32a0
movq %rbx, %rdi
movl 0x20(%rsp), %esi
callq 0x33e0
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
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8398(%rip), %rdi      # 0x18af8
leaq 0x8399(%rip), %rsi      # 0x18b00
leaq 0x8d2d(%rip), %rcx      # 0x1949b
movl $0xc7, %edx
callq 0x3170
movq %rax, %rbx
movl $0x20, %esi
movq %rbp, %rdi
jmp 0x107b3
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x107b8
callq 0x3500
jmp 0x107b8
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x107b8
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3270
movq %rbx, %rdi
callq 0x33f0