// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_sub_ov_adv_cigar(ul_idx_t const*, overlap_region*, char*, UC_Read*, bit_extz_t*, long, double, double, utg_ct_t*, unsigned long, unsigned long, unsigned long, kv_rtrace_t*, kv_ul_ov_t*)
uint64_t gen_sub_ov_adv_cigar(const ul_idx_t *udb, overlap_region* o, char* qstr, UC_Read *tu, 
bit_extz_t *exz, int64_t ql, double o_rate, double e_rate, utg_ct_t *ct_a, uint64_t ct_n, 
uint64_t sid, uint64_t oid, kv_rtrace_t *trace, kv_ul_ov_t *res)
{
	uint64_t ts, te, i, l, rn = res->n, rev = o->y_pos_strand, s, e, rid, t[2]; 
	ma_utg_t *u = &(udb->ug->u.a[o->y_id]); ul_ov_t z; rtrace_t tz; int64_t k;
    if(!rev){
        ts = o->y_pos_s; te = o->y_pos_e + 1; k = 0;
    } else {
        ts = u->len - (o->y_pos_e+1); te = u->len - o->y_pos_s; 
        k = ((int64_t)o->w_list.n)-1; if(k < 0) k = 0;
    }

    if(!ct_a) {    
        for (i = l = 0; i < u->n; i++) {
            rid = u->a[i]>>33; 
            s = l; e = l + Get_READ_LENGTH(R_INF, rid);///note: [s, e) are pos of t
            l += (uint32_t)u->a[i];
            if(e <= ts) continue; 
            if(s >= te) break;
            t[0] = (rev?(u->len-e):(s)); t[1] = (rev?(u->len-s):(e));
            if(extract_subov_cigar(udb, qstr, tu, exz, t[0], t[1], o, o_rate, &k, ql, e_rate, sid, rid, &tz, &z)) {
                tz.oid = oid; 
                z.ts -= t[0]; z.te -= t[0];
                z.rev = ((o->y_pos_strand == ((u->a[i]>>32)&1))?0:1);
                if(z.rev) {
                    t[0] = z.ts; t[1] = z.te;
                    z.ts = Get_READ_LENGTH(R_INF, rid) - t[1];
                    z.te = Get_READ_LENGTH(R_INF, rid) - t[0];
                }
                ///non-contained read at the unitg
                z.el = 1; 
                ///rid
                z.tn = rid;
                ///i-th read at the unitig
                z.qn = trace->n;
                kv_push(ul_ov_t, *res, z);
                kv_push(rtrace_t, *trace, tz);
            //     fprintf(stderr, "+[M::%s::rid->%lu::rev->%lu] utg_t::[%ld, %ld), ql::%ld\n", 
            // __func__, rid, rev, t[0], t[1], ql);
                // fprintf(stderr, "+[M::%s::%.*s::%c] q::[%u, %u), ql::%ld, t::[%u, %u), tl::%lu, err::%u\n", 
                // __func__, (int)Get_NAME_LENGTH(R_INF, z.tn), Get_NAME(R_INF, z.tn), 
                // "+-"[z.rev], z.qs, z.qe, ql, z.ts, z.te, Get_READ_LENGTH(R_INF, z.tn), z.sec);
            }
        }
    } else {
        for (i = 0; i < ct_n; i++) {
            rid = ct_a[i].x>>1; s = ct_a[i].s; e = ct_a[i].e;
            if(e <= ts) continue; 
            if(s >= te) break;
            t[0] = (rev?(u->len-e):(s)); t[1] = (rev?(u->len-s):(e));
            if(extract_subov_cigar(udb, qstr, tu, exz, t[0], t[1], o, o_rate, &k, ql, e_rate, sid, rid, &tz, &z)) {
                tz.oid = oid;
                z.ts -= t[0]; z.te -= t[0];
                z.rev = ((o->y_pos_strand == (ct_a[i].x&1))?0:1);
                if(z.rev) {
                    t[0] = z.ts; t[1] = z.te;
                    z.ts = Get_READ_LENGTH(R_INF, rid) - t[1];
                    z.te = Get_READ_LENGTH(R_INF, rid) - t[0];
                }
                ///contained read at the unitg
                z.el = 0; 
                ///rid
                z.tn = rid;
                ///i-th read at the unitig
                z.qn = trace->n;
                kv_push(ul_ov_t, *res, z);
                kv_push(rtrace_t, *trace, tz);
            //     fprintf(stderr, "-[M::%s::rid->%lu::rev->%lu] utg_t::[%ld, %ld), ql::%ld\n", 
            // __func__, rid, rev, t[0], t[1], ql);
                // fprintf(stderr, "-[M::%s::%.*s::%c] q::[%u, %u), ql::%ld, t::[%u, %u), tl::%lu, err::%u\n", 
                // __func__, (int)Get_NAME_LENGTH(R_INF, z.tn), Get_NAME(R_INF, z.tn), 
                // "+-"[z.rev], z.qs, z.qe, ql, z.ts, z.te, Get_READ_LENGTH(R_INF, z.tn), z.sec);
            }
        }
    }
    return res->n-rn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movsd %xmm1, 0x98(%rsp)
movsd %xmm0, 0x90(%rsp)
movq %r9, 0x88(%rsp)
movq %r8, 0x80(%rsp)
movq %rcx, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq 0x158(%rsp), %r8
movq 0x130(%rsp), %r14
movl 0x10(%rsi), %eax
movl 0x1c(%rsi), %edx
movq %rdi, 0x68(%rsp)
movq (%rdi), %rcx
imulq $0x28, %rax, %r12
addq 0x10(%rcx), %r12
movl 0x18(%rsi), %eax
movl %edx, 0x64(%rsp)
testl %edx, %edx
je 0x4e7c6
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl (%r12), %edi
notl %eax
movl %eax, %ecx
addl %edi, %ecx
subl 0x14(%rsi), %edi
movq 0x48(%rsi), %rax
cmpq $0x2, %rax
pushq $0x1
popq %rdx
cmovgeq %rax, %rdx
decq %rdx
movl %edi, %eax
jmp 0x4e7cd
movl 0x14(%rsi), %ecx
incl %eax
xorl %edx, %edx
movq 0x8(%r8), %rdi
movq %rdi, 0xb8(%rsp)
movl %ecx, %ecx
movq %rcx, 0xa8(%rsp)
movl %eax, %eax
movq %rax, 0xa0(%rsp)
movq %rdx, 0xb0(%rsp)
testq %r14, %r14
movq %rsi, 0x58(%rsp)
je 0x4ea29
movq 0x138(%rsp), %r13
leaq 0x3c(%rsp), %rbp
subq $0x1, %r13
jb 0x4ec7c
movl 0x8(%r14), %r9d
cmpl 0xa8(%rsp), %r9d
jbe 0x4ea20
movl 0x4(%r14), %ebx
cmpl 0xa0(%rsp), %ebx
jae 0x4ec7c
cmpl $0x0, 0x64(%rsp)
je 0x4e85c
movl (%r12), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, %rcx
subq %r9, %rcx
subq %rbx, %rax
movq %rcx, %rbx
movq %rax, %r9
movl (%r14), %r15d
movq %rbp, 0x30(%rsp)
leaq 0xc4(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rsi, (%rsp)
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rcx
movq %rbx, %r8
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
callq 0x4e3a6
movq 0x58(%rsp), %rsi
testq %rax, %rax
je 0x4ea20
shrl %r15d
movq 0x148(%rsp), %rax
movl %eax, 0xf4(%rsp)
movl 0x4c(%rsp), %eax
movl 0x50(%rsp), %edx
subl %ebx, %eax
movl %eax, 0x4c(%rsp)
subl %ebx, %edx
movl %edx, 0x50(%rsp)
movl 0x1c(%rsi), %esi
movl (%r14), %edi
andl $0x1, %edi
xorl %ecx, %ecx
cmpl %edi, %esi
setne %sil
je 0x4e927
leaq 0x205922(%rip), %rdi    # 0x254230
movq 0x18(%rdi), %rdi
movl (%rdi,%r15,8), %edi
movl %edi, %r8d
subl %edx, %r8d
movl %r8d, 0x4c(%rsp)
subl %eax, %edi
movl %edi, 0x50(%rsp)
movb %sil, %cl
shll $0x1f, %ecx
movl 0x54(%rsp), %eax
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl %edx, %eax
orl %ecx, %eax
movl %eax, 0x54(%rsp)
movl %r15d, 0x48(%rsp)
movq 0x150(%rsp), %r15
movl 0x8(%r15), %eax
movl %eax, 0x3c(%rsp)
movq 0x158(%rsp), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x4e98d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups 0x3c(%rsp), %xmm0
movups 0x48(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movq (%r15), %rax
movq 0x8(%r15), %rcx
cmpq 0x10(%r15), %rcx
jne 0x4e9de
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r15)
imulq $0x34, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r15)
movq 0x8(%r15), %rcx
movq 0x58(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r15)
imulq $0x34, %rcx, %rcx
movl 0xf4(%rsp), %edx
movl %edx, 0x30(%rax,%rcx)
movups 0xc4(%rsp), %xmm0
movups 0xd4(%rsp), %xmm1
movups 0xe4(%rsp), %xmm2
movups %xmm2, 0x20(%rax,%rcx)
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
addq $0xc, %r14
jmp 0x4e810
xorl %r14d, %r14d
xorl %r13d, %r13d
movl 0x10(%r12), %eax
cmpq %rax, %r13
jae 0x4ec7c
movq 0x18(%r12), %rax
movq (%rax,%r13,8), %rbp
movq %rbp, %r15
shrq $0x21, %r15
leaq 0x2057dc(%rip), %rax    # 0x254230
movq 0x18(%rax), %rax
movq (%rax,%r15,8), %r9
addq %r14, %r9
cmpq 0xa8(%rsp), %r9
jbe 0x4ec6f
cmpq 0xa0(%rsp), %r14
jae 0x4ec7c
cmpl $0x0, 0x64(%rsp)
je 0x4ea99
movl (%r12), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, %rbx
subq %r9, %rbx
subq %r14, %rax
movq %rax, %r9
jmp 0x4ea9c
movq %r14, %rbx
leaq 0x3c(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0xc4(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rsi, (%rsp)
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rcx
movq %rbx, %r8
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
callq 0x4e3a6
movq 0x58(%rsp), %rsi
testq %rax, %rax
je 0x4ec6f
movq 0x148(%rsp), %rax
movl %eax, 0xf4(%rsp)
movl 0x4c(%rsp), %eax
movl 0x50(%rsp), %ecx
subl %ebx, %eax
movl %eax, 0x4c(%rsp)
subl %ebx, %ecx
movl %ecx, 0x50(%rsp)
movl 0x1c(%rsi), %esi
movq 0x18(%r12), %rdx
movq (%rdx,%r13,8), %rdi
shrq $0x20, %rdi
andl $0x1, %edi
xorl %edx, %edx
cmpq %rsi, %rdi
setne %sil
je 0x4eb71
leaq 0x2056d8(%rip), %rdi    # 0x254230
movq 0x18(%rdi), %rdi
movl (%rdi,%r15,8), %edi
movl %edi, %r8d
subl %ecx, %r8d
movl %r8d, 0x4c(%rsp)
subl %eax, %edi
movl %edi, 0x50(%rsp)
movb %sil, %dl
shll $0x1f, %edx
movl 0x54(%rsp), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
orl %edx, %eax
orl $0x40000000, %eax       # imm = 0x40000000
movl %eax, 0x54(%rsp)
movl %r15d, 0x48(%rsp)
movq 0x150(%rsp), %r15
movl 0x8(%r15), %eax
movl %eax, 0x3c(%rsp)
movq 0x158(%rsp), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x4ebdc
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups 0x3c(%rsp), %xmm0
movups 0x48(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movq (%r15), %rax
movq 0x8(%r15), %rcx
cmpq 0x10(%r15), %rcx
jne 0x4ec2d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r15)
imulq $0x34, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r15)
movq 0x8(%r15), %rcx
movq 0x58(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r15)
imulq $0x34, %rcx, %rcx
movl 0xf4(%rsp), %edx
movl %edx, 0x30(%rax,%rcx)
movups 0xc4(%rsp), %xmm0
movups 0xd4(%rsp), %xmm1
movups 0xe4(%rsp), %xmm2
movups %xmm2, 0x20(%rax,%rcx)
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movl %ebp, %eax
addq %rax, %r14
incq %r13
jmp 0x4ea2f
movq 0x158(%rsp), %rax
movq 0x8(%rax), %rax
subq 0xb8(%rsp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq