// File: /workspace/github_cpp_makefile_star2_O3/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
leaq (%rax,%rax,4), %r12
shlq $0x3, %r12
addq 0x10(%rcx), %r12
movl 0x18(%rsi), %eax
movl %edx, 0x64(%rsp)
testl %edx, %edx
je 0x81c86
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
movl $0x1, %edx
cmovgeq %rax, %rdx
decq %rdx
movl %edi, %eax
jmp 0x81c8d
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 0x81f05
movq 0x138(%rsp), %r15
testq %r15, %r15
je 0x82172
leaq 0x3c(%rsp), %rbp
movl 0x8(%r14), %r9d
cmpl 0xa8(%rsp), %r9d
jbe 0x81ef3
movl 0x4(%r14), %ebx
cmpl 0xa0(%rsp), %ebx
jae 0x82172
cmpl $0x0, 0x64(%rsp)
je 0x81d1b
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), %r13d
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 0x8183c
movq 0x58(%rsp), %rsi
testq %rax, %rax
je 0x81ef3
shrl %r13d
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 0x81de6
leaq 0x214463(%rip), %rdi    # 0x296230
movq 0x18(%rdi), %rdi
movl (%rdi,%r13,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 %r13d, 0x48(%rsp)
movq 0x150(%rsp), %r13
movl 0x8(%r13), %eax
movl %eax, 0x3c(%rsp)
movq 0x158(%rsp), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x81e55
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movups 0x3c(%rsp), %xmm0
movups 0x48(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
movq (%r13), %rax
movq 0x8(%r13), %rcx
cmpq 0x10(%r13), %rcx
jne 0x81eb1
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r13)
imulq $0x34, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r13)
movq 0x8(%r13), %rcx
movq 0x58(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
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
decq %r15
jne 0x81cd9
jmp 0x82172
cmpl $0x0, 0x10(%r12)
je 0x82172
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x18(%r12), %rax
movq (%rax,%r14,8), %r13
movq %r13, %rbp
shrq $0x21, %rbp
leaq 0x214303(%rip), %rax    # 0x296230
movq 0x18(%rax), %rax
movq (%rax,%rbp,8), %r9
addq %rbx, %r9
cmpq 0xa8(%rsp), %r9
jbe 0x8215b
cmpq 0xa0(%rsp), %rbx
jae 0x82172
cmpl $0x0, 0x64(%rsp)
je 0x81f72
movl (%r12), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, %r15
subq %r9, %r15
subq %rbx, %rax
movq %rax, %r9
jmp 0x81f75
movq %rbx, %r15
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 %r15, %r8
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
callq 0x8183c
movq 0x58(%rsp), %rsi
testq %rax, %rax
je 0x8215b
movq 0x148(%rsp), %rax
movl %eax, 0xf4(%rsp)
movl 0x4c(%rsp), %eax
movl 0x50(%rsp), %ecx
subl %r15d, %eax
movl %eax, 0x4c(%rsp)
subl %r15d, %ecx
movl %ecx, 0x50(%rsp)
movl 0x1c(%rsi), %esi
movq 0x18(%r12), %rdx
movq (%rdx,%r14,8), %rdi
shrq $0x20, %rdi
andl $0x1, %edi
xorl %edx, %edx
cmpq %rsi, %rdi
setne %sil
je 0x8204b
leaq 0x2141fd(%rip), %rdi    # 0x296230
movq 0x18(%rdi), %rdi
movl (%rdi,%rbp,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 %ebp, 0x48(%rsp)
movq 0x150(%rsp), %rbp
movl 0x8(%rbp), %eax
movl %eax, 0x3c(%rsp)
movq 0x158(%rsp), %r15
movq (%r15), %rax
movq 0x8(%r15), %rcx
cmpq 0x10(%r15), %rcx
jne 0x820bd
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r15)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r15)
movq 0x8(%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r15)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movups 0x3c(%rsp), %xmm0
movups 0x48(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
cmpq 0x10(%rbp), %rcx
jne 0x82119
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbp)
imulq $0x34, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbp)
movq 0x8(%rbp), %rcx
movq 0x58(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbp)
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 %r13d, %eax
addq %rax, %rbx
incq %r14
movl 0x10(%r12), %eax
cmpq %rax, %r14
jb 0x81f16
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