// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq 0x70(%rbp), %rax
movq 0x68(%rbp), %rax
movq 0x60(%rbp), %rax
movq 0x58(%rbp), %rax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x1, -0xa8(%rbp)
cmpq $0x0, 0x48(%rbp)
jne 0x62b27
cmpq $0x2, -0x48(%rbp)
jne 0x62b25
movq 0x30(%rbp), %rax
subq 0x28(%rbp), %rax
addq $0x80, %rax
cmpq 0x68(%rbp), %rax
jge 0x62b25
movq $0x0, -0xa8(%rbp)
jmp 0x62b89
cmpq $0x1, 0x48(%rbp)
jne 0x62b56
cmpq $0x1, -0x48(%rbp)
jne 0x62b54
movq 0x30(%rbp), %rax
subq 0x28(%rbp), %rax
addq $0x80, %rax
cmpq 0x68(%rbp), %rax
jge 0x62b54
movq $0x0, -0xa8(%rbp)
jmp 0x62b87
cmpq $0x2, 0x48(%rbp)
jne 0x62b85
cmpq $0x1, -0x48(%rbp)
jne 0x62b83
movq 0x30(%rbp), %rax
subq 0x28(%rbp), %rax
addq $0x80, %rax
cmpq 0x68(%rbp), %rax
jge 0x62b83
movq $0x0, -0xa8(%rbp)
jmp 0x62b85
jmp 0x62b87
jmp 0x62b89
cmpq $0x0, -0xa8(%rbp)
je 0x62ef8
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
movl $0x32, %r8d
movl $0x1388, %r9d           # imm = 0x1388
movq $0x200, (%rsp)          # imm = 0x200
movq $0x10, 0x8(%rsp)
callq 0x29e80
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x68(%rbp)
movq 0x28(%rbp), %rax
movq %rax, -0x90(%rbp)
movq 0x38(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
jge 0x62daa
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x88(%rbp)
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0x98(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x90(%rbp), %rax
jle 0x62d7d
cmpq $-0x1, -0xa0(%rbp)
je 0x62c79
cmpq $-0x1, -0x98(%rbp)
je 0x62c79
movq $0x0, 0x48(%rbp)
jmp 0x62cc1
cmpq $-0x1, -0xa0(%rbp)
je 0x62c97
cmpq $-0x1, -0x98(%rbp)
jne 0x62c97
movq $0x1, 0x48(%rbp)
jmp 0x62cbf
cmpq $-0x1, -0xa0(%rbp)
jne 0x62cb5
cmpq $-0x1, -0x98(%rbp)
je 0x62cb5
movq $0x2, 0x48(%rbp)
jmp 0x62cbd
movq $0x3, 0x48(%rbp)
jmp 0x62cbf
jmp 0x62cc1
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
movq -0x90(%rbp), %r13
movq -0x88(%rbp), %r12
movq -0xa0(%rbp), %r15
movq -0x98(%rbp), %r14
movq 0x48(%rbp), %rbx
movq 0x50(%rbp), %r11
movq 0x58(%rbp), %r10
movq 0x60(%rbp), %rax
movq %rax, -0xb0(%rbp)
movsd -0x60(%rbp), %xmm0
xorl %eax, %eax
movq -0xb0(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq $0x2710, 0x40(%rsp)     # imm = 0x2710
movq $0x7ff, 0x48(%rsp)      # imm = 0x7FF
movq $0x200, 0x50(%rsp)      # imm = 0x200
movq $-0x1, 0x58(%rsp)
movq $0x0, 0x60(%rsp)
callq 0x613a0
cmpq $0x0, %rax
jne 0x62d7b
jmp 0x62d7b
jmp 0x62d7d
movq -0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x62bf7
movq 0x30(%rbp), %rax
movq %rax, -0x88(%rbp)
movq 0x40(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x90(%rbp), %rax
jle 0x62ef6
cmpq $-0x1, -0xa0(%rbp)
je 0x62df2
cmpq $-0x1, -0x98(%rbp)
je 0x62df2
movq $0x0, 0x48(%rbp)
jmp 0x62e3a
cmpq $-0x1, -0xa0(%rbp)
je 0x62e10
cmpq $-0x1, -0x98(%rbp)
jne 0x62e10
movq $0x1, 0x48(%rbp)
jmp 0x62e38
cmpq $-0x1, -0xa0(%rbp)
jne 0x62e2e
cmpq $-0x1, -0x98(%rbp)
je 0x62e2e
movq $0x2, 0x48(%rbp)
jmp 0x62e36
movq $0x3, 0x48(%rbp)
jmp 0x62e38
jmp 0x62e3a
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
movq -0x90(%rbp), %r13
movq -0x88(%rbp), %r12
movq -0xa0(%rbp), %r15
movq -0x98(%rbp), %r14
movq 0x48(%rbp), %rbx
movq 0x50(%rbp), %r11
movq 0x58(%rbp), %r10
movq 0x60(%rbp), %rax
movq %rax, -0xb8(%rbp)
movsd -0x60(%rbp), %xmm0
xorl %eax, %eax
movq -0xb8(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq $0x2710, 0x40(%rsp)     # imm = 0x2710
movq $0x7ff, 0x48(%rsp)      # imm = 0x7FF
movq $0x200, 0x50(%rsp)      # imm = 0x200
movq $-0x1, 0x58(%rsp)
movq $0x0, 0x60(%rsp)
callq 0x613a0
cmpq $0x0, %rax
jne 0x62ef4
jmp 0x62ef4
jmp 0x62ef6
jmp 0x62efa
jmp 0x62efa
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop