// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, (%rsp)
movq 0x98(%rsp), %rax
movq 0x80(%rsp), %rsi
movq 0x78(%rsp), %rbp
cmpq $0x1, %rax
je 0x67349
testq %rax, %rax
jne 0x67351
cmpq $0x2, %rcx
je 0x67361
jmp 0x67379
cmpq $0x1, %rcx
je 0x67361
jmp 0x67379
xorq $0x2, %rax
movq %rcx, %rdx
xorq $0x1, %rdx
orq %rax, %rdx
jne 0x67379
movq %rsi, %rax
subq %rbp, %rax
subq $-0x80, %rax
cmpq 0xb8(%rsp), %rax
jl 0x6752d
movq 0x88(%rsp), %rbx
movl $0x32, %r8d
movl $0x1388, %r9d           # imm = 0x1388
movq %r15, %rdi
movq %rcx, %rsi
xorl %edx, %edx
movq %r14, %rcx
pushq $0x10
pushq $0x200                  # imm = 0x200
callq 0x441c0
addq $0x10, %rsp
testq %rax, %rax
jle 0x6746f
movq 0x28(%r14), %rdx
xorl %r13d, %r13d
movq %rdx, 0x20(%rsp)
movq (%rdx,%r13,8), %rcx
shlq $0x4, %rcx
movl 0x4(%r15,%rcx), %r11d
movl 0x8(%r15,%rcx), %r14d
cmpq %r14, %rbp
jge 0x6745b
xorl %r10d, %r10d
cmpq $-0x1, %rbx
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 %rbx
pushq %r14
pushq %rbp
movq %r11, %rbx
callq 0x661e6
movq 0x90(%rsp), %rdx
movq %rbx, %r11
movq %r12, %rax
addq $0x70, %rsp
incq %r13
movq %r11, %rbx
movq %r14, %rbp
cmpq %r13, %rax
jne 0x673bd
jmp 0x67475
movq %rbp, %r14
movq %rbx, %r11
movq %r11, %rbx
movq 0x80(%rsp), %r11
cmpq %r11, %r14
jge 0x6752d
movq 0x90(%rsp), %rax
cmpq $-0x1, %rax
je 0x674a2
cmpq $-0x1, %rbx
je 0x674a2
xorl %r10d, %r10d
jmp 0x674cc
cmpq $-0x1, %rax
jne 0x674b4
movl $0x1, %r10d
cmpq $-0x1, %rbx
jne 0x674cc
cmpq $-0x1, %rax
setne %cl
cmpq $-0x1, %rbx
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 %rbx
pushq %r11
pushq %r14
callq 0x661e6
addq $0x70, %rsp
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq