// File: /workspace/github_cpp_makefile_star2_O3/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
movl 0x160(%rsi), %eax
movslq %eax, %rdx
movq (%r8), %rsi
movq 0x8(%r8), %rcx
subq %rsi, %rcx
sarq $0x2, %rcx
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
jbe 0x174a0
movl %r9d, %r14d
movslq 0xc8(%r15), %rbx
movslq (%rsi,%rdx,4), %rdx
movq %rdx, 0x30(%rsp)
cmpl %edx, %ebx
jge 0x173ee
movq %r8, 0x10(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x8(%r15), %rax
movq %rax, 0x20(%rsp)
leaq 0xd0(%r15), %rax
movq %rax, 0x18(%rsp)
movq %r15, 0x28(%rsp)
incq %rbx
movq %r15, %rsi
movq 0x38(%rsp), %rdx
movl %ebx, %ecx
callq 0x1702e
testl %eax, %eax
jns 0x1747b
movq %rbx, %rbp
shlq $0x5, %rbp
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
xorl %r15d, %r15d
cmpl $0x1, %r14d
sete %al
testb %r15b, %al
jne 0x173af
movq (%r12), %rdi
addq %rbp, %rdi
leaq 0x1a70b(%rip), %rsi     # 0x31a86
callq 0x50e0
testl %eax, %eax
je 0x1739b
movq (%r12), %rax
movq (%rax,%rbp), %rsi
movq 0x8(%rax,%rbp), %rdx
movq %r13, %rdi
callq 0x50f0
jmp 0x173af
movq 0x40(%rsp), %rax
movq (%rax), %rax
movsbl (%rax,%rbx), %esi
movq %r13, %rdi
callq 0x52d0
incq %r15
addq $0x20, %r13
addq $0x18, %r12
cmpq $0x4, %r15
jne 0x17361
movq 0x28(%rsp), %r15
movl %ebx, 0xc8(%r15)
cmpq 0x30(%rsp), %rbx
jne 0x17333
movl 0x160(%r15), %eax
movq 0x10(%rsp), %rdx
movq 0x8(%rdx), %rcx
subq (%rdx), %rcx
sarq $0x2, %rcx
incl %eax
movl %eax, 0x160(%r15)
cltq
cmpq %rax, %rcx
jbe 0x17417
movq %r15, %rsi
movl %r14d, %edx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x170dc
movq 0xd8(%r15), %rdx
cmpq 0x118(%r15), %rdx
jne 0x17474
testq %rdx, %rdx
je 0x17443
movq 0xd0(%r15), %rdi
movq 0x110(%r15), %rsi
callq 0x5350
testl %eax, %eax
jne 0x17474
movq 0xf8(%r15), %rdx
cmpq 0x138(%r15), %rdx
jne 0x17474
movl $0x2, %ebx
testq %rdx, %rdx
je 0x174a0
movq 0xf0(%r15), %rdi
movq 0x130(%r15), %rsi
callq 0x5350
testl %eax, %eax
je 0x174a0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x174a0
movl %eax, %r13d
movq %r15, %rsi
movl %r14d, %edx
callq 0x170dc
cmpl $-0x1, %eax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
je 0x174a0
movq 0x8(%rsp), %rax
movl %r13d, (%rax)
movl $0x1, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop