// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x10(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, 0x8(%rsp)
movq 0xb8(%rsp), %rax
movq 0x98(%rsp), %rdx
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %r10
cmpq $0x1, %rdx
je 0x35e81
testq %rdx, %rdx
jne 0x35e89
cmpq $0x2, %rcx
je 0x35e99
jmp 0x35eac
cmpq $0x1, %rcx
je 0x35e99
jmp 0x35eac
xorq $0x2, %rdx
movq %rcx, %rsi
xorq $0x1, %rsi
orq %rdx, %rsi
jne 0x35eac
movq %rdi, %rdx
subq %r10, %rdx
subq $-0x80, %rdx
cmpq %rax, %rdx
jl 0x3604e
movq 0x88(%rsp), %r15
xorl %r14d, %r14d
pushq $0x32
popq %r8
movl $0x1388, %r9d           # imm = 0x1388
movq %r12, %rdi
movq %rcx, %rsi
xorl %edx, %edx
movq %r13, %rcx
pushq $0x10
pushq $0x200                  # imm = 0x200
movq %r10, %rbx
callq 0x1751e
movq %r15, %r10
movq %rbx, %r11
addq $0x10, %rsp
movq %rax, %rbp
movq 0x28(%r13), %rcx
testq %rax, %rax
cmovleq %r14, %rbp
cmpq %r14, %rbp
je 0x35f99
movq (%rcx,%r14,8), %rax
shlq $0x4, %rax
movl 0x4(%r12,%rax), %ebx
movl 0x8(%r12,%rax), %r13d
cmpq %r13, %r11
jge 0x35f8b
xorl %eax, %eax
cmpq $-0x1, %r10
sete %al
addl %eax, %eax
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r12, %r15
movq %rcx, %r12
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %r8
movq 0x78(%rsp), %r9
movsd 0x28(%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 %rax
pushq %rbx
pushq %r10
pushq %r13
pushq %r11
callq 0x34db7
movq %r12, %rcx
movq %r15, %r12
addq $0x70, %rsp
incq %r14
movq %r13, %r11
movq %rbx, %r10
jmp 0x35ef3
movq %r10, %r14
movq 0x80(%rsp), %r10
cmpq %r10, %r11
jge 0x3604e
movq 0x90(%rsp), %rbx
cmpq $-0x1, %rbx
je 0x35fc5
cmpq $-0x1, %r14
je 0x35fc5
xorl %eax, %eax
jmp 0x35fed
cmpq $-0x1, %rbx
jne 0x35fd6
cmpq $-0x1, %r14
je 0x35fd6
pushq $0x1
popq %rax
jmp 0x35fed
cmpq $-0x1, %rbx
setne %al
cmpq $-0x1, %r14
sete %cl
andb %al, %cl
movzbl %cl, %eax
xorq $0x3, %rax
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %r8
movq 0x78(%rsp), %r9
movsd 0x28(%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 %rax
pushq %rbx
pushq %r14
pushq %r10
pushq %r11
callq 0x34db7
addq $0x70, %rsp
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq