// File: chhylp123[P]hifiasm/Correct.cpp
// Function: cal_exz_infi_adv(overlap_region*, ul_idx_t const*, hpc_t*, All_reads*, bit_extz_t*, char*, UC_Read*, long, long, long, long, long*, long*, long, long*, long, long)
int64_t cal_exz_infi_adv(overlap_region *z, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, 
bit_extz_t *exz, char* qstr, UC_Read *tu, int64_t qs, int64_t qe, int64_t ts, int64_t te, 
int64_t *pts, int64_t *pte, int64_t thre, int64_t *pthre, int64_t q_tot_l, int64_t mode)
{
    clear_align(*exz);
    int64_t aux_beg = 0, ql, tl, t_tot_l = -1, dd; 
    char *q_string, *t_string; int32_t rev = z->y_pos_strand, id = z->y_id; 
    ql = qe - qs; tl = te - ts; dd = MAX(ql, tl); 
    if(hpc_g) t_tot_l = hpc_len(*hpc_g, id);
    else if(uref) t_tot_l = uref->ug->u.a[id].len;
    else t_tot_l = Get_READ_LENGTH((*rref), id);
   
    if(mode == 3) {
        update_semi_coord(uref, hpc_g, rref, z, qs, qe, ((thre>dd)?dd:thre), &ts, &te, &aux_beg);
    } else if(mode == 1 || mode == 2) {
        adjust_ext_offset(&qs, &qe, &ts, &te, q_tot_l, t_tot_l, ((thre>dd)?dd:thre), mode);
    }
    
    if((qe > qs) && (te > ts) && (ts != -1) && (te != -1)) {
        ql = qe - qs; tl = te - ts; 
        dd = MAX(ql, tl); 
        if(thre > dd) thre = dd; 
        if(thre <= (*pthre)) return 0;
        (*pthre) = thre;
        
        q_string = qstr + qs; 
        t_string = retrieve_str_seq_exz(tu, ts, tl, (*pts), (*pte)-(*pts), rev, uref, hpc_g, rref, id);
        (*pts) = ts; (*pte) = te;

        if(mode == 0) { //global
            cal_exz_global(t_string, tl, q_string, ql, thre, exz);
        } else if(mode == 1) {///forward extension
            cal_exz_extension_0(t_string, tl, q_string, ql, thre, exz);
        } else if(mode == 2) {///backward extension
            cal_exz_extension_1(t_string, tl, q_string, ql, thre, exz);
        } else if(mode == 3) {//semi-global
            cal_exz_semi(t_string, tl, q_string, ql, thre, aux_beg, exz);
        }

        if(is_align(*exz)) {
            // cigar_check(t_string, q_string, exz); 
            // if(mode == 1) {
            //     fprintf(stderr, "\n[M::%s::ql::%ld] qs::%ld, qe::%ld, ts::%ld, te::%ld, mode::%ld, err::%d, thre::%d, exz_q[%d, %d], exz_t[%d, %d]\n", 
            //                      __func__, ql, qs, qe, ts, te, mode, exz->err, exz->thre, exz->ts, exz->te, exz->ps, exz->pe);
            //     fprintf(stderr, "[M::%s::] pstr::%.*s\n", __func__, (int32_t)tl, t_string);
            //     fprintf(stderr, "[M::%s::] tstr::%.*s\n", __func__, (int32_t)ql, q_string);
            //     fprintf(stderr, "[M::%s::] exz->cigar.n::%d\n", __func__, (int32_t)exz->cigar.n);
            // }
            exz->ps += ts; exz->pe += ts;
            exz->ts += qs; exz->te += qs;
            return 1;
        }
        return 0;
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xf8, %rsp
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, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x38(%rbp), %rax
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
movq $0x0, -0x48(%rbp)
movq $-0x1, -0x60(%rbp)
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x7c(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x80(%rbp)
movq 0x20(%rbp), %rax
subq 0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x30(%rbp), %rax
subq 0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jl 0x5f09e
movq -0x50(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x5f0a9
movq -0x58(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5f0e2
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movslq -0x80(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
sarl %eax
cltq
movq %rax, -0x60(%rbp)
jmp 0x5f124
cmpq $0x0, -0x20(%rbp)
je 0x5f10e
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movslq -0x80(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x60(%rbp)
jmp 0x5f122
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
jmp 0x5f124
cmpq $0x3, 0x60(%rbp)
jne 0x5f1ea
movq -0x20(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x98(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0x90(%rbp)
movq 0x48(%rbp), %rax
cmpq -0x68(%rbp), %rax
jle 0x5f188
movq -0x68(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x5f193
movq 0x48(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x90(%rbp), %r9
movq -0x98(%rbp), %r8
movq -0xa0(%rbp), %rcx
movq -0xa8(%rbp), %rdx
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rbx
leaq 0x28(%rbp), %r11
leaq 0x30(%rbp), %r10
leaq -0x48(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x57f40
jmp 0x5f269
cmpq $0x1, 0x60(%rbp)
je 0x5f1f8
cmpq $0x2, 0x60(%rbp)
jne 0x5f267
movq 0x58(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq 0x48(%rbp), %rax
cmpq -0x68(%rbp), %rax
jle 0x5f225
movq -0x68(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x5f230
movq 0x48(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xc8(%rbp), %r9
movq -0xd0(%rbp), %r8
movq -0xd8(%rbp), %r10
movq 0x60(%rbp), %rax
leaq 0x18(%rbp), %rdi
leaq 0x20(%rbp), %rsi
leaq 0x28(%rbp), %rdx
leaq 0x30(%rbp), %rcx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x581e0
jmp 0x5f269
movq 0x20(%rbp), %rax
cmpq 0x18(%rbp), %rax
jle 0x5f4c2
movq 0x30(%rbp), %rax
cmpq 0x28(%rbp), %rax
jle 0x5f4c2
cmpq $-0x1, 0x28(%rbp)
je 0x5f4c2
cmpq $-0x1, 0x30(%rbp)
je 0x5f4c2
movq 0x20(%rbp), %rax
subq 0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x30(%rbp), %rax
subq 0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jl 0x5f2ca
movq -0x50(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x5f2d5
movq -0x58(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x48(%rbp), %rax
cmpq -0x68(%rbp), %rax
jle 0x5f2f2
movq -0x68(%rbp), %rax
movq %rax, 0x48(%rbp)
movq 0x48(%rbp), %rax
movq 0x50(%rbp), %rcx
cmpq (%rcx), %rax
jg 0x5f30c
movq $0x0, -0x10(%rbp)
jmp 0x5f4ca
movq 0x48(%rbp), %rcx
movq 0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
addq 0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x10(%rbp), %rdi
movq 0x28(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x38(%rbp), %rax
movq (%rax), %rcx
movq 0x40(%rbp), %rax
movq (%rax), %r8
movq 0x38(%rbp), %rax
subq (%rax), %r8
movl -0x7c(%rbp), %eax
movb %al, %r9b
movq -0x20(%rbp), %rbx
movq -0x28(%rbp), %r11
movq -0x30(%rbp), %r10
movslq -0x80(%rbp), %rax
movzbl %r9b, %r9d
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0xc0c80
movq %rax, -0x78(%rbp)
movq 0x28(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
movq 0x30(%rbp), %rcx
movq 0x40(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x60(%rbp)
jne 0x5f3c0
movq -0x78(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x70(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x48(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %r9
callq 0x5e760
jmp 0x5f456
cmpq $0x1, 0x60(%rbp)
jne 0x5f3ed
movq -0x78(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x70(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x48(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %r9
callq 0x5e8e0
jmp 0x5f454
cmpq $0x2, 0x60(%rbp)
jne 0x5f41a
movq -0x78(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x70(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x48(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %r9
callq 0x5ea60
jmp 0x5f452
cmpq $0x3, 0x60(%rbp)
jne 0x5f450
movq -0x78(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x70(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x48(%rbp), %rax
movl %eax, %r8d
movq -0x48(%rbp), %rax
movl %eax, %r9d
movq -0x38(%rbp), %rax
movq %rax, (%rsp)
callq 0x5ebe0
jmp 0x5f452
jmp 0x5f454
jmp 0x5f456
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x38(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jg 0x5f4b8
movq 0x28(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq 0x10(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x10(%rax)
movq 0x28(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq 0x14(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x14(%rax)
movq 0x18(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq 0x1c(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x1c(%rax)
movq 0x18(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq 0x20(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x20(%rax)
movq $0x1, -0x10(%rbp)
jmp 0x5f4ca
movq $0x0, -0x10(%rbp)
jmp 0x5f4ca
movq $0x0, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0xf8, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax,%rax)