// File: /workspace/github_cpp_makefile_star2_O3/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
xorl %r12d, %r12d
movq %r12, 0x10(%rsp)
movq 0x10120(%rip), %r13     # 0x1fff0
leaq 0x94bb(%rip), %rsi      # 0x19392
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, 0x38(%rsp)
movq 0x8(%rbx), %rax
cmpb $0x1, 0x4b(%rax)
jne 0xff3c
movq 0x88(%rax), %rcx
movq (%rcx), %rcx
movq %rcx, 0x38(%rsp)
movl 0x38(%rax), %eax
cvtsi2sd %rax, %xmm1
movsd 0x8c24(%rip), %xmm0     # 0x18b70
addsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x31e0
movsd %xmm0, 0x60(%rsp)
leaq 0x40(%rbx), %rax
movq %rax, 0x68(%rsp)
movl 0xd0(%rbx), %eax
leaq 0x9490(%rip), %r12      # 0x19405
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %r14d, %r14d
movl $0x0, 0x20(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x24(%rsp)
testl %eax, %eax
je 0xffcf
movb $0x0, 0xf8(%rbx)
cmpb $0x0, 0xf9(%rbx)
je 0x100cf
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0xa976
incl 0x20(%rsp)
jmp 0x101e8
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 0x9395(%rip), %rsi      # 0x1939e
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, 0x40(%rsp)
movq %rax, 0x28(%rsp)
callq 0x3190
movq %rax, %rbp
movq %rbx, %rdi
xorl %esi, %esi
leaq 0x40(%rsp), %rdx
leaq 0x28(%rsp), %rcx
callq 0xb5c0
movl $0x1b, %edx
movq %r13, %rdi
leaq 0x9345(%rip), %rsi      # 0x193b2
callq 0x32a0
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x3190
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subsd 0x8(%rsp), %xmm0
movq %r13, %rdi
callq 0x3360
movl $0x9, %edx
movq %rax, %rdi
leaq 0x9324(%rip), %rsi      # 0x193ce
callq 0x32a0
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x100c5
decq 0x8(%rdi)
jne 0x100c5
movq (%rdi), %rax
callq *0x8(%rax)
movl %r14d, 0x8(%rsp)
jmp 0x10272
movq (%rbx), %rax
movq (%rax), %rdi
callq 0x10d90
movq 0x8(%rbx), %rax
movsd 0x18(%rax), %xmm1
movapd %xmm1, %xmm2
movsd 0x6f19(%rip), %xmm3     # 0x17008
addsd %xmm3, %xmm2
divsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
seta 0xf8(%rbx)
jbe 0x101d7
movq (%rbx), %rax
movq (%rax), %rdi
callq 0x10d90
callq 0x31e0
divsd 0x60(%rsp), %xmm0
addsd 0x6ee2(%rip), %xmm0     # 0x17008
movq 0x8(%rbx), %rax
divsd 0x20(%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
cmpb $0x1, 0x48(%rax)
jne 0x10187
movl $0x18, %edx
movq %r13, %rdi
leaq 0x928e(%rip), %rsi      # 0x193d8
callq 0x32a0
movq %r13, %rdi
movsd 0x8(%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 %r14d, %r15d
movl $0x20, %edi
callq 0x3260
movq %rax, %rbp
movsd 0x8(%rsp), %xmm0
addsd (%rsp), %xmm0
movq %rax, %rdi
callq 0x11de4
movq %rbp, 0x30(%rsp)
movq 0xf0(%rbx), %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
callq 0x11c7e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa976
incl 0x18(%rsp)
movl %r15d, %r14d
jmp 0x101e8
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0xa976
incl 0x1c(%rsp)
movq %rbx, %rdi
callq 0xee2e
cmpb $0x0, 0xe8(%rbx)
je 0x101fe
incl %r14d
jmp 0x10214
movsd (%rsp), %xmm0
ucomisd 0xe0(%rbx), %xmm0
jbe 0x10214
movb $0x1, 0xe8(%rbx)
movl $0xd, %edx
movq %r13, %rdi
leaq 0x91ce(%rip), %rsi      # 0x193f1
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, 0x8(%rsp)
js 0x10272
movq %rbx, %rdi
movl %r14d, %esi
callq 0xef72
movq %rbx, %rdi
callq 0x140f4
movl $0x5, %edx
movq %r13, %rdi
leaq 0x9176(%rip), %rsi      # 0x193ff
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
movq %rbp, %rdi
movsd (%rsp), %xmm0
callq 0x3360
movq %rax, %rbp
movl $0x5, %edx
movq %rax, %rdi
leaq 0x913d(%rip), %r15      # 0x1940b
movq %r15, %rsi
callq 0x32a0
movsd 0x30(%rbx), %xmm0
movq %rbp, %rdi
callq 0x3360
movq %rax, %rbp
movl $0x7, %edx
movq %rax, %rdi
leaq 0x911c(%rip), %r14      # 0x19411
movq %r14, %rsi
callq 0x32a0
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x106cf
movsd 0x70(%rax), %xmm0
movq %rbp, %rdi
callq 0x3360
movq %rax, %rbp
movl $0x5, %edx
movq %rax, %rdi
leaq 0x90f0(%rip), %rsi      # 0x19419
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 0x106cf
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 0x10(%rsp), %r14
leaq 0x38(%rsp), %r15
jne 0x103d8
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xf874
testb %al, %al
jne 0x103b6
movsd 0x10(%rsp), %xmm1
movsd 0x6c32(%rip), %xmm0     # 0x17008
jmp 0x10428
movq (%rbx), %rax
movsd 0x30(%rbx), %xmm0
mulsd 0xc8(%rbx), %xmm0
movsd %xmm0, 0x70(%rsp)
movq (%rax), %rdi
callq 0x10d90
movsd 0x6c0a(%rip), %xmm1     # 0x17008
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x31e0
divsd 0x70(%rsp), %xmm0
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x6be6(%rip), %xmm0     # 0x17008
movsd %xmm1, 0x10(%rsp)
ucomisd %xmm0, %xmm1
jbe 0x1043d
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xfa70
movb %al, 0xf9(%rbx)
movq 0x8(%rbx), %rax
cmpb $0x1, 0x4c(%rax)
jne 0x1057f
movq %r13, %r15
movl 0x24(%rsp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd 0x10(%rsp), %xmm1
mulsd 0x20(%rax), %xmm1
subsd %xmm0, %xmm1
movq 0x68(%rsp), %r14
cvttsd2si %xmm1, %r13
movq 0xff8d(%rip), %rsi      # 0x20418
movq 0xff8e(%rip), %rdx      # 0x20420
movq 0xfb37(%rip), %rdi      # 0x1ffd0
callq 0x32a0
movq %rax, %rbp
movl $0x2, %edx
movq %rax, %rdi
leaq 0x8f6f(%rip), %rsi      # 0x1941f
callq 0x32a0
movl %r13d, %esi
movq %rbp, %rdi
callq 0x31a0
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
leaq 0x9064(%rip), %rsi      # 0x19536
callq 0x32a0
movq (%r14), %rax
testq %rax, %rax
je 0x106cf
movsd 0x70(%rax), %xmm0
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1494e
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %rbp, %rdi
callq 0x32a0
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
leaq 0x8f06(%rip), %rsi      # 0x19422
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 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
movl 0x8(%rsp), %r14d
je 0x1056a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3270
addl %r13d, 0x24(%rsp)
movq 0x8(%rbx), %rax
movq %r15, %r13
leaq 0x8e88(%rip), %r12      # 0x19405
jmp 0x10584
movl 0x8(%rsp), %r14d
movsd (%rax), %xmm0
ucomisd 0x6a80(%rip), %xmm0     # 0x17010
movsd 0x10(%rsp), %xmm1
jbe 0x105b4
movq %rbx, %rdi
movsd (%rsp), %xmm0
movsd %xmm1, (%rsp)
movsd (%rsp), %xmm1
callq 0xf4a2
movsd (%rsp), %xmm1
movl 0xd0(%rbx), %eax
incl %eax
movl %eax, 0xd0(%rbx)
movsd 0x6a3e(%rip), %xmm0     # 0x17008
ucomisd %xmm1, %xmm0
movsd %xmm1, (%rsp)
ja 0xffa1
movq 0xfa10(%rip), %rbx      # 0x1fff0
leaq 0x8e3d(%rip), %rsi      # 0x19424
movl $0x25, %edx
movq %rbx, %rdi
movsd %xmm1, (%rsp)
callq 0x32a0
movq %rbx, %rdi
movsd (%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 0x8e13(%rip), %rsi      # 0x1944a
movl $0x9, %edx
movq %rbx, %rdi
callq 0x32a0
movq %rbx, %rdi
movl 0x18(%rsp), %esi
callq 0x33e0
movq %rax, %rbx
leaq 0x8dfa(%rip), %rsi      # 0x19454
movl $0x8, %edx
movq %rax, %rdi
callq 0x32a0
movq %rbx, %rdi
movl 0x20(%rsp), %esi
callq 0x33e0
movq %rax, %rbx
leaq 0x8de0(%rip), %rsi      # 0x1945d
movl $0x8, %edx
movq %rax, %rdi
callq 0x32a0
movq %rbx, %rdi
movl 0x1c(%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 $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8422(%rip), %rdi      # 0x18af8
leaq 0x8423(%rip), %rsi      # 0x18b00
leaq 0x8e17(%rip), %rcx      # 0x194fb
movl $0xc7, %edx
callq 0x3170
movq %rax, %rbx
movl $0x20, %esi
movq %rbp, %rdi
jmp 0x10729
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x1072e
callq 0x3515
jmp 0x1072e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x1072e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3270
movq %rbx, %rdi
callq 0x33f0