// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %rcx, 0x50(%rsp)
movq %rdx, %r14
movslq 0x160(%rsi), %rax
movq (%r8), %rcx
movq 0x8(%r8), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpq %rax, %rdx
jbe 0x176dc
movl %r9d, %ebp
movq %rsi, %rbx
movq %r8, 0x8(%rsp)
movq 0x90(%rsp), %rdx
movq %rdx, 0x40(%rsp)
movslq 0xc8(%rsi), %r15
movslq (%rcx,%rax,4), %rax
leaq 0x8(%rsi), %rcx
movq %rcx, 0x28(%rsp)
leaq 0xd0(%rsi), %rcx
movq %rcx, 0x20(%rsp)
cmpl %eax, %r15d
movq %rax, 0x10(%rsp)
cmovgl %r15d, %eax
movslq %eax, %r12
movq %rsi, 0x38(%rsp)
movq %r14, 0x30(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, %r13
cmpq %r12, %r15
je 0x1762b
leaq 0x1(%r13), %r15
movq %rbx, %rsi
movq %r14, %rdx
movl %r15d, %ecx
callq 0x17212
movl %eax, (%rsp)
testl %eax, %eax
js 0x17583
movq %rbx, %rsi
movl %ebp, %edx
callq 0x172e0
cmpl $-0x1, %eax
je 0x17618
movq 0x40(%rsp), %rax
movl (%rsp), %ecx
movl %ecx, (%rax)
movl %ecx, %eax
movl $0x1, 0x4(%rsp)
jmp 0x17623
movq %r13, 0x48(%rsp)
movq %r15, %r14
shlq $0x5, %r14
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %rbx
xorl %r13d, %r13d
cmpl $0x1, %ebp
sete %al
testb %r13b, %al
jne 0x175ea
movq (%r12), %rdi
addq %r14, %rdi
leaq 0x1a4a1(%rip), %rsi     # 0x31a56
callq 0x50e0
testl %eax, %eax
je 0x175d5
movq (%r12), %rax
movq (%rax,%r14), %rsi
movq 0x8(%rax,%r14), %rdx
movq %rbx, %rdi
callq 0x50f0
jmp 0x175ea
movq 0x50(%rsp), %rax
movq (%rax), %rax
movsbl (%rax,%r15), %esi
movq %rbx, %rdi
callq 0x52d0
incq %r13
addq $0x20, %rbx
addq $0x18, %r12
cmpq $0x4, %r13
jne 0x1759c
movq 0x38(%rsp), %rbx
movl %r15d, 0xc8(%rbx)
movq 0x30(%rsp), %r14
movq 0x18(%rsp), %r12
movq 0x48(%rsp), %r13
jmp 0x17620
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movl (%rsp), %eax
testl %eax, %eax
js 0x17532
cmpq 0x10(%rsp), %r13
movl 0x4(%rsp), %r15d
jl 0x176dc
movslq 0x160(%rbx), %rax
incq %rax
movl %eax, 0x160(%rbx)
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %rcx
subq (%rdx), %rcx
sarq $0x2, %rcx
cmpq %rax, %rcx
jbe 0x17678
movq %rbx, %rsi
movl %ebp, %edx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x172e0
movq 0xd8(%rbx), %rdx
cmpq 0x118(%rbx), %rdx
jne 0x176d6
testq %rdx, %rdx
je 0x176a4
movq 0xd0(%rbx), %rdi
movq 0x110(%rbx), %rsi
callq 0x5350
testl %eax, %eax
jne 0x176d6
movq 0xf8(%rbx), %rdx
cmpq 0x138(%rbx), %rdx
jne 0x176d6
movl $0x2, %r15d
testq %rdx, %rdx
je 0x176dc
movq 0xf0(%rbx), %rdi
movq 0x130(%rbx), %rsi
callq 0x5350
testl %eax, %eax
je 0x176dc
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq