// File: chhylp123[P]hifiasm/Correct.cpp
// Function: cal_exz_infi_simi_adv(ul_idx_t const*, hpc_t*, All_reads*, bit_extz_t*, char*, UC_Read*, long, long, long, long, long*, long*, long, long*, long, long, long, long, int, int, long, overlap_region*, long)
int64_t cal_exz_infi_simi_adv(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 qmin, int64_t qmax, 
int64_t tmin, int64_t tmax, int32_t rev, int32_t id, int64_t mode, overlap_region *z,
int64_t gen_trace)
{   ///mode cannot be 3
    clear_align(*exz);
    int64_t aux_beg = 0, ql, tl, dd; char *q_string, *t_string; 
    ql = qe - qs; tl = te - ts; dd = MAX(ql, tl); 
    if(mode == 3 && z) {
        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_fixed_t(&qs, &qe, &ts, &te, qmin, qmax, tmin, tmax, ((thre>dd)?dd:thre), mode);
    }

    // if(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
    //         fprintf(stderr, "[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), thre::%ld, pthre::%ld\n", 
    //         __func__, mode, qs, qe, ts, te, thre, *pthre);
    // }
    
    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(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
        //     fprintf(stderr, "[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), thre::%ld, pthre::%ld\n", 
        //     __func__, mode, qs, qe, ts, te, thre, *pthre);
        // }
        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(!gen_trace) {
            if(mode == 0) { //global
                cal_exz_global_simi(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 1) {///forward extension
                cal_exz_extension_0_simi(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 2) {///backward extension
                cal_exz_extension_1_simi(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 3) {//semi-global; mode cannot be 3
                cal_exz_semi_simi(t_string, tl, q_string, ql, thre, aux_beg, exz);
            }
        } else {
            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)) {
            exz->ps += ts; exz->pe += ts;
            exz->ts += qs; exz->te += qs;
            // if(exz->ps == 18327 && exz->pe + 1 == 18601 && exz->ts == 145990 && exz->te + 1 == 146191) {
            //     fprintf(stderr, "[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld)\n", 
            //     __func__, mode, qs, qe, ts, te);
            //     fprintf(stderr, "[M::%s::] q_string::%.*s\n", __func__, (int32_t)ql, q_string);
            //     fprintf(stderr, "[M::%s::] t_string::%.*s\n", __func__, (int32_t)tl, t_string);
            // }
            return 1;
        }
        // if(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
        //     fprintf(stderr, "[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld)\n", 
        //     __func__, mode, qs, qe, ts, te);
        //     fprintf(stderr, "[M::%s::] q_string::%.*s\n", __func__, (int32_t)ql, q_string);
        //     fprintf(stderr, "[M::%s::] t_string::%.*s\n", __func__, (int32_t)tl, t_string);
        // }
        return 0;
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xf8, %rsp
movq 0x90(%rbp), %rax
movq 0x88(%rbp), %rax
movq 0x80(%rbp), %rax
movl 0x78(%rbp), %eax
movl 0x70(%rbp), %eax
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, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x30(%rbp), %rax
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
movq $0x0, -0x48(%rbp)
movq 0x18(%rbp), %rax
subq 0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x28(%rbp), %rax
subq 0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jl 0x6004e
movq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x60056
movq -0x58(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x3, 0x80(%rbp)
jne 0x60135
cmpq $0x0, 0x88(%rbp)
je 0x60135
movq -0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x98(%rbp)
movq 0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jle 0x600d3
movq -0x60(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x600de
movq 0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x80(%rbp), %r9
movq -0x88(%rbp), %r8
movq -0x90(%rbp), %rcx
movq -0x98(%rbp), %rdx
movq -0xa0(%rbp), %rsi
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rbx
leaq 0x20(%rbp), %r11
leaq 0x28(%rbp), %r10
leaq -0x48(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x57f40
jmp 0x601ef
cmpq $0x1, 0x80(%rbp)
je 0x6014d
cmpq $0x2, 0x80(%rbp)
jne 0x601ed
movq 0x50(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq 0x58(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq 0x60(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq 0x68(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq 0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jle 0x60190
movq -0x60(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x6019b
movq 0x40(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xb8(%rbp), %r11
movq -0xc0(%rbp), %rbx
movq -0xc8(%rbp), %r9
movq -0xd0(%rbp), %r8
movq -0xd8(%rbp), %r10
movq 0x80(%rbp), %rax
leaq 0x10(%rbp), %rdi
leaq 0x18(%rbp), %rsi
leaq 0x20(%rbp), %rdx
leaq 0x28(%rbp), %rcx
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x5fe20
jmp 0x601ef
movq 0x18(%rbp), %rax
cmpq 0x10(%rbp), %rax
jle 0x6053b
movq 0x28(%rbp), %rax
cmpq 0x20(%rbp), %rax
jle 0x6053b
cmpq $-0x1, 0x20(%rbp)
je 0x6053b
cmpq $-0x1, 0x28(%rbp)
je 0x6053b
movq 0x18(%rbp), %rax
subq 0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x28(%rbp), %rax
subq 0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jl 0x60250
movq -0x50(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x6025b
movq -0x58(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jle 0x60278
movq -0x60(%rbp), %rax
movq %rax, 0x40(%rbp)
movq 0x40(%rbp), %rax
movq 0x48(%rbp), %rcx
cmpq (%rcx), %rax
jg 0x60292
movq $0x0, -0x10(%rbp)
jmp 0x60543
movq 0x40(%rbp), %rcx
movq 0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
addq 0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rdi
movq 0x20(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x30(%rbp), %rax
movq (%rax), %rcx
movq 0x38(%rbp), %rax
movq (%rax), %r8
movq 0x30(%rbp), %rax
subq (%rax), %r8
movl 0x70(%rbp), %eax
movb %al, %r9b
movq -0x18(%rbp), %rbx
movq -0x20(%rbp), %r11
movq -0x28(%rbp), %r10
movslq 0x78(%rbp), %rax
movzbl %r9b, %r9d
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0xc0c80
movq %rax, -0x70(%rbp)
movq 0x20(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, (%rax)
movq 0x28(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x90(%rbp)
jne 0x603fb
cmpq $0x0, 0x80(%rbp)
jne 0x60357
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %eax, %r8d
movq -0x30(%rbp), %r9
callq 0x5e820
jmp 0x603f6
cmpq $0x1, 0x80(%rbp)
jne 0x60387
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %eax, %r8d
movq -0x30(%rbp), %r9
callq 0x5e9a0
jmp 0x603f4
cmpq $0x2, 0x80(%rbp)
jne 0x603b7
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %eax, %r8d
movq -0x30(%rbp), %r9
callq 0x5eb20
jmp 0x603f2
cmpq $0x3, 0x80(%rbp)
jne 0x603f0
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %eax, %r8d
movq -0x48(%rbp), %rax
movl %eax, %r9d
movq -0x30(%rbp), %rax
movq %rax, (%rsp)
callq 0x5ecc0
jmp 0x603f2
jmp 0x603f4
jmp 0x603f6
jmp 0x604cf
cmpq $0x0, 0x80(%rbp)
jne 0x6042e
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %eax, %r8d
movq -0x30(%rbp), %r9
callq 0x5e760
jmp 0x604cd
cmpq $0x1, 0x80(%rbp)
jne 0x6045e
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %eax, %r8d
movq -0x30(%rbp), %r9
callq 0x5e8e0
jmp 0x604cb
cmpq $0x2, 0x80(%rbp)
jne 0x6048e
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %eax, %r8d
movq -0x30(%rbp), %r9
callq 0x5ea60
jmp 0x604c9
cmpq $0x3, 0x80(%rbp)
jne 0x604c7
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rdx
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %eax, %r8d
movq -0x48(%rbp), %rax
movl %eax, %r9d
movq -0x30(%rbp), %rax
movq %rax, (%rsp)
callq 0x5ebe0
jmp 0x604c9
jmp 0x604cb
jmp 0x604cd
jmp 0x604cf
movq -0x30(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x30(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jg 0x60531
movq 0x20(%rbp), %rdx
movq -0x30(%rbp), %rax
movslq 0x10(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x10(%rax)
movq 0x20(%rbp), %rdx
movq -0x30(%rbp), %rax
movslq 0x14(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x14(%rax)
movq 0x10(%rbp), %rdx
movq -0x30(%rbp), %rax
movslq 0x1c(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x1c(%rax)
movq 0x10(%rbp), %rdx
movq -0x30(%rbp), %rax
movslq 0x20(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x20(%rax)
movq $0x1, -0x10(%rbp)
jmp 0x60543
movq $0x0, -0x10(%rbp)
jmp 0x60543
movq $0x0, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0xf8, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)