// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: chain_aln(overlap_region*, Chain_Data*, k_mer_hit*, long, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, long, long, long, long, long, long, bit_extz_t*, long, double, long, unsigned long)
int64_t chain_aln(overlap_region *z, Chain_Data *dp, k_mer_hit *ch_a, int64_t ch_n, const ul_idx_t *uref, 
hpc_t *hpc_g, All_reads *rref, char* qstr, UC_Read *tu, int64_t qs, int64_t qe, int64_t ts, 
int64_t te, int64_t mode, int64_t wl, bit_extz_t *exz, int64_t q_tot, double e_rate, 
int64_t min_chain_aln, uint64_t rid)
{
    int64_t *m, mn = 0, k; int64_t q[2], t[2], is_chain_aln = 1; 
    if(mode == 0) {
        ///wrong
        // assert((ch_n >= 2) && (ch_a[0].self_offset == qs) && (ch_a[0].offset == ts) && 
        //                     (ch_a[ch_n-1].self_offset == qe) && (ch_a[ch_n-1].offset == te));
        //does not work with CNS alignment; it also could not work here
        if(ch_n == 2 && ((qe-qs)+128) < min_chain_aln) is_chain_aln = 0;
    } else if(mode == 1) {
        // assert((ch_n >= 1) && (ch_a[0].self_offset == qs) && (ch_a[0].offset == ts));
        //does not work with CNS alignment; it also could not work here
        if(ch_n == 1 && ((qe-qs)+128) < min_chain_aln) is_chain_aln = 0;
    } else if(mode == 2) {
        // assert((ch_n >= 1) && (ch_a[ch_n-1].self_offset == qe) && (ch_a[ch_n-1].offset == te));
        //does not work with CNS alignment; it also could not work here
        if(ch_n == 1 && ((qe-qs)+128) < min_chain_aln) is_chain_aln = 0;
    }

    if(is_chain_aln) {
        mn = lchain_refine(ch_a, ch_n, NULL, dp, 50, 5000, 512, 16); m = dp->tmp;
        q[0] = qs; t[0] = ts;
        for (k = 0; k < mn; k++) {
            q[1] = ch_a[m[k]].self_offset; t[1] = ch_a[m[k]].offset;
            if(q[1] > q[0]) {
                if((t[0] != -1) && (t[1] != -1)) {
                    mode = 0;//global
                } else if((t[0] != -1) && (t[1] == -1)) {
                    mode = 1;///forward extension
                } else if((t[0] == -1) && (t[1] != -1)) {
                    mode = 2;///backward extension
                } else {
                    mode = 3;//semi-global
                }
                if(!hc_aln_exz_adv(z, uref, hpc_g, rref, qstr, tu, q[0], q[1], t[0], t[1], mode, wl, exz, q_tot, e_rate, MAX_SIN_L, MAX_SIN_E, FORCE_SIN_L, -1, NULL)) {

                }
            }
            q[0] = q[1]; t[0] = t[1];
        }
        q[1] = qe; t[1] = te;
        if(q[1] > q[0]) {
            if((t[0] != -1) && (t[1] != -1)) {
                mode = 0;//global
            } else if((t[0] != -1) && (t[1] == -1)) {
                mode = 1;///forward extension
            } else if((t[0] == -1) && (t[1] != -1)) {
                mode = 2;///backward extension
            } else {
                mode = 3;//semi-global
            }
            if(!hc_aln_exz_adv(z, uref, hpc_g, rref, qstr, tu, q[0], q[1], t[0], t[1], mode, wl, exz, q_tot, e_rate, MAX_SIN_L, MAX_SIN_E, FORCE_SIN_L, -1, NULL)) {

            }
        }
        
    } else {

    } 

    // {
    //     q[0] = qs; t[0] = ts; q[1] = qe; t[1] = te;
    //     if(q[1] > q[0]) {
    //         hc_aln_exz_adv(z, uref, hpc_g, rref, qstr, tu, q[0], q[1], t[0], t[1], mode, wl, exz, q_tot, e_rate, MAX_SIN_L, MAX_SIN_E);
    //     }
    // }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movsd %xmm0, 0x18(%rsp)
movq %r9, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, (%rsp)
movq 0xb8(%rsp), %rax
movq 0x98(%rsp), %rdx
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %r13
cmpq $0x1, %rdx
je 0x67066
testq %rdx, %rdx
jne 0x6706e
cmpq $0x2, %rcx
je 0x6707e
jmp 0x67091
cmpq $0x1, %rcx
je 0x6707e
jmp 0x67091
xorq $0x2, %rdx
movq %rcx, %rsi
xorq $0x1, %rsi
orq %rdx, %rsi
jne 0x67091
movq %rdi, %rdx
subq %r13, %rdx
subq $-0x80, %rdx
cmpq %rax, %rdx
jl 0x67245
movq 0x88(%rsp), %r14
movl $0x32, %r8d
movl $0x1388, %r9d           # imm = 0x1388
movq %r15, %rdi
movq %rcx, %rsi
xorl %edx, %edx
movq %rbx, %rcx
pushq $0x10
pushq $0x200                  # imm = 0x200
callq 0x438d5
addq $0x10, %rsp
testq %rax, %rax
jle 0x67187
movq 0x28(%rbx), %rdx
xorl %ebp, %ebp
movq %rdx, 0x20(%rsp)
movq (%rdx,%rbp,8), %rcx
shlq $0x4, %rcx
movl 0x4(%r15,%rcx), %r11d
movl 0x8(%r15,%rcx), %ebx
cmpq %rbx, %r13
jge 0x67173
xorl %r10d, %r10d
cmpq $-0x1, %r14
sete %r10b
addl %r10d, %r10d
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rax, %r12
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %r8
movq 0x78(%rsp), %r9
movsd 0x20(%rsp), %xmm0
pushq $0x0
pushq $-0x1
pushq $0x200                  # imm = 0x200
pushq $0x7ff                  # imm = 0x7FF
pushq $0x2710                 # imm = 0x2710
pushq 0xe0(%rsp)
pushq 0xe0(%rsp)
pushq 0xe0(%rsp)
pushq %r10
pushq %r11
pushq %r14
pushq %rbx
pushq %r13
movq %r11, %r14
callq 0x65f06
movq 0x90(%rsp), %rdx
movq %r14, %r11
movq %r12, %rax
addq $0x70, %rsp
incq %rbp
movq %r11, %r14
movq %rbx, %r13
cmpq %rbp, %rax
jne 0x670d4
jmp 0x6718d
movq %r13, %rbx
movq %r14, %r11
movq %r11, %r14
movq 0x80(%rsp), %r11
cmpq %r11, %rbx
jge 0x67245
movq 0x90(%rsp), %rax
cmpq $-0x1, %rax
je 0x671ba
cmpq $-0x1, %r14
je 0x671ba
xorl %r10d, %r10d
jmp 0x671e4
cmpq $-0x1, %rax
jne 0x671cc
movl $0x1, %r10d
cmpq $-0x1, %r14
jne 0x671e4
cmpq $-0x1, %rax
setne %cl
cmpq $-0x1, %r14
sete %dl
andb %cl, %dl
movzbl %dl, %r10d
xorq $0x3, %r10
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %r8
movq 0x78(%rsp), %r9
movsd 0x20(%rsp), %xmm0
pushq $0x0
pushq $-0x1
pushq $0x200                  # imm = 0x200
pushq $0x7ff                  # imm = 0x7FF
pushq $0x2710                 # imm = 0x2710
pushq 0xe0(%rsp)
pushq 0xe0(%rsp)
pushq 0xe0(%rsp)
pushq %r10
pushq %rax
pushq %r14
pushq %r11
pushq %rbx
callq 0x65f06
addq $0x70, %rsp
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq