// File: /workspace/github_cpp_makefile_star2_O2/medvedevgroup[P]varmatch/src/wholegenome.cpp
// Function: WholeGenome::PathExtendOneStep(SequencePath&, std::multimap<int, int, std::less<int>, std::allocator<std::pair<int const, int>>>**, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<int, std::allocator<int>>&, int, int&)
int WholeGenome::PathExtendOneStep(SequencePath& sp,
                                   multimap<int, int> * choices_by_pos[],
                                   const string & reference_sequence,
                                   vector<int> & sync_points,
                                   int match_mode,
                                   int & variant_need_decision){
    //-1 operation fail, path deleted
    //0 operation succeed
    //1 operation fail, need to make decision first, then extend
    //2 path reached end, need to check if good

    if(sp.reached_sync_num >= sync_points.size()) return -1;

    int start_pos = sp.current_genome_pos + 1;
    int end_pos = sync_points[sp.reached_sync_num]; // the next sync point, end pos included

    for(int next_genome_pos = start_pos; next_genome_pos <= end_pos; next_genome_pos++){

        // before make decision, we need to check if the equal property still holds
        int variant_need_decision_ = PathNeedDecision(sp, choices_by_pos, next_genome_pos);
        if(variant_need_decision_ >= 0){

            // check equal property
            int statu = CheckPathEqualProperty(sp, match_mode);
            if(statu == -1) return -1;
            variant_need_decision = variant_need_decision_;
            return 1; // need decision on next position
        }

        // else extend one nt
        for(int i = 0; i < 4; i++){

            if(match_mode == 1){
                if(i%2 != 0) continue;
            }

            if(sp.string_sequences[i][next_genome_pos] == "."){
                sp.donor_sequences[i] += reference_sequence[next_genome_pos];
            }else{
                sp.donor_sequences[i] += sp.string_sequences[i][next_genome_pos];
            }
        }
        sp.current_genome_pos = next_genome_pos;
    }

    // reaches the end of end_pos
    sp.reached_sync_num ++;

    if(sp.reached_sync_num >= sync_points.size()){
        // last sync point is the end of ref genome sequence
        if(sp.donor_sequences[0] == sp.donor_sequences[2] &&
           sp.donor_sequences[1] == sp.donor_sequences[3]){
            return 2;
       }else{
            //dout << "delete this path at pos: " << sp.current_genome_pos << " for reach end but not equal";
            //PrintPath(sp);
            return -1;
       }
    }
    return CheckPathEqualProperty(sp, match_mode);
    // first try to converge, then extend

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, %r15
movslq 0x160(%rsi), %rsi
movq (%r8), %rcx
movq 0x8(%r8), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
pushq $-0x1
popq %rax
cmpq %rsi, %rdx
jbe 0x1105a
movl %r9d, %ebp
movq %r8, 0x18(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
movslq 0xc8(%r15), %r14
movslq (%rcx,%rsi,4), %rax
movq %rax, 0x30(%rsp)
leaq 0x8(%r15), %rax
movq %rax, 0x20(%rsp)
leaq 0xd0(%r15), %rax
movq %rax, 0x8(%rsp)
movq %r15, 0x28(%rsp)
cmpq 0x30(%rsp), %r14
jge 0x10fc7
incq %r14
movq %r15, %rsi
movq 0x38(%rsp), %rdx
movl %r14d, %ecx
callq 0x10c8a
testl %eax, %eax
jns 0x11005
movq %r14, %r15
shlq $0x5, %r15
movq 0x20(%rsp), %r12
movq 0x8(%rsp), %rbx
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x10fb6
cmpl $0x1, %ebp
sete %al
testb %r13b, %al
jne 0x10fa9
movq (%r12), %rdi
addq %r15, %rdi
leaq 0x11ac4(%rip), %rsi     # 0x22a3e
callq 0xa912
testb %al, %al
je 0x10f9a
movq 0x40(%rsp), %rax
movq (%rax), %rax
movsbl (%rax,%r14), %esi
movq %rbx, %rdi
callq 0x52a0
jmp 0x10fa9
movq (%r12), %rsi
addq %r15, %rsi
movq %rbx, %rdi
callq 0x55c0
incq %r13
addq $0x20, %rbx
addq $0x18, %r12
jmp 0x10f5b
movq 0x28(%rsp), %r15
movl %r14d, 0xc8(%r15)
jmp 0x10f21
movslq 0x160(%r15), %rax
incq %rax
movl %eax, 0x160(%r15)
movq 0x18(%rsp), %rdx
movq 0x8(%rdx), %rcx
subq (%rdx), %rcx
sarq $0x2, %rcx
cmpq %rax, %rcx
jbe 0x11024
movq %r15, %rsi
movl %ebp, %edx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10d0c
movl %eax, %ebx
movq %r15, %rsi
movl %ebp, %edx
callq 0x10d0c
cmpl $-0x1, %eax
pushq $-0x1
popq %rax
je 0x1105a
movq 0x10(%rsp), %rax
movl %ebx, (%rax)
pushq $0x1
jmp 0x11059
leaq 0x110(%r15), %rsi
movq 0x8(%rsp), %rdi
callq 0x93ce
testb %al, %al
je 0x11057
leaq 0xf0(%r15), %rdi
addq $0x130, %r15            # imm = 0x130
movq %r15, %rsi
callq 0x93ce
testb %al, %al
je 0x11057
pushq $0x2
jmp 0x11059
pushq $-0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop