// File: /workspace/github_cpp_makefile_star2_O1/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), %r15
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, 0x40(%rsp)
testl %edx, %edx
je 0x82415
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 0x8241c
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 %r15, %r15
movq %rsi, 0x60(%rsp)
je 0x826ad
movq 0x138(%rsp), %r14
testq %r14, %r14
je 0x8292e
leaq 0x44(%rsp), %rbp
movl 0x8(%r15), %r9d
cmpl 0xa8(%rsp), %r9d
jbe 0x8269b
movl 0x4(%r15), %eax
cmpl 0xa0(%rsp), %eax
jae 0x8292e
movq %rax, %rbx
cmpl $0x0, 0x40(%rsp)
je 0x824a2
movl (%r12), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
subq %r9, %rbx
cmpl $0x0, 0x40(%rsp)
je 0x824b7
movl (%r12), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
subq %rax, %r9
movl (%r15), %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 0x81f86
movq 0x60(%rsp), %rsi
testq %rax, %rax
je 0x8269b
shrl %r13d
movq 0x148(%rsp), %rax
movl %eax, 0xf4(%rsp)
movl 0x54(%rsp), %eax
movl 0x58(%rsp), %ecx
subl %ebx, %eax
movl %eax, 0x54(%rsp)
subl %ebx, %ecx
movl %ecx, 0x58(%rsp)
movl 0x1c(%rsi), %esi
movl (%r15), %edi
andl $0x1, %edi
xorl %r8d, %r8d
cmpl %edi, %esi
setne %r8b
shll $0x1f, %r8d
movl 0x5c(%rsp), %edx
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %edx
orl %r8d, %edx
movl %edx, 0x5c(%rsp)
cmpl %edi, %esi
je 0x8259a
leaq 0x208cac(%rip), %rsi    # 0x28b230
movq 0x18(%rsi), %rsi
movl (%rsi,%r13,8), %esi
movl %esi, %edi
subl %ecx, %edi
movl %edi, 0x54(%rsp)
subl %eax, %esi
movl %esi, 0x58(%rsp)
andl $0xbfffffff, %edx       # imm = 0xBFFFFFFF
movl %edx, 0x5c(%rsp)
movl %r13d, 0x50(%rsp)
movq 0x150(%rsp), %r13
movl 0x8(%r13), %eax
movl %eax, 0x44(%rsp)
movq 0x158(%rsp), %rbx
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x825f5
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbx)
movq (%rbx), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq %rax, (%rbx)
movq (%rbx), %rax
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 0x44(%rsp), %xmm0
movups 0x50(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
movq 0x10(%r13), %rax
cmpq %rax, 0x8(%r13)
jne 0x82651
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%r13)
movq (%r13), %rdi
imulq $0x34, %rcx, %rsi
callq 0x2370
movq %rax, (%r13)
movq (%r13), %rax
movq 0x8(%r13), %rcx
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)
movq 0x60(%rsp), %rsi
addq $0xc, %r15
decq %r14
jne 0x82468
jmp 0x8292e
cmpl $0x0, 0x10(%r12)
je 0x8292e
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x18(%r12), %rax
movq (%rax,%r15,8), %rbp
movq %rbp, %r13
shrq $0x21, %r13
leaq 0x208b5a(%rip), %rax    # 0x28b230
movq 0x18(%rax), %rax
movq (%rax,%r13,8), %r9
addq %r14, %r9
cmpq 0xa8(%rsp), %r9
jbe 0x82918
cmpq 0xa0(%rsp), %r14
jae 0x8292e
movq %r14, %rbx
cmpl $0x0, 0x40(%rsp)
je 0x82714
movl (%r12), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
subq %r9, %rbx
cmpl $0x0, 0x40(%rsp)
je 0x82729
movl (%r12), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
subq %r14, %r9
leaq 0x44(%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 0x81f86
movq 0x60(%rsp), %rsi
testq %rax, %rax
je 0x82918
movq 0x148(%rsp), %rax
movl %eax, 0xf4(%rsp)
movl 0x54(%rsp), %eax
movl 0x58(%rsp), %ecx
subl %ebx, %eax
movl %eax, 0x54(%rsp)
subl %ebx, %ecx
movl %ecx, 0x58(%rsp)
movl 0x1c(%rsi), %esi
movq 0x18(%r12), %rdx
movq (%rdx,%r15,8), %rdi
shrq $0x20, %rdi
andl $0x1, %edi
xorl %r8d, %r8d
cmpq %rsi, %rdi
setne %r8b
shll $0x1f, %r8d
movl 0x5c(%rsp), %edx
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %edx
orl %r8d, %edx
movl %edx, 0x5c(%rsp)
cmpq %rsi, %rdi
je 0x82817
leaq 0x208a2f(%rip), %rsi    # 0x28b230
movq 0x18(%rsi), %rsi
movl (%rsi,%r13,8), %esi
movl %esi, %edi
subl %ecx, %edi
movl %edi, 0x54(%rsp)
subl %eax, %esi
movl %esi, 0x58(%rsp)
orl $0x40000000, %edx       # imm = 0x40000000
movl %edx, 0x5c(%rsp)
movl %r13d, 0x50(%rsp)
movq 0x150(%rsp), %r13
movl 0x8(%r13), %eax
movl %eax, 0x44(%rsp)
movq 0x158(%rsp), %rbx
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x82872
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbx)
movq (%rbx), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq %rax, (%rbx)
movq (%rbx), %rax
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 0x44(%rsp), %xmm0
movups 0x50(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
movq 0x10(%r13), %rax
cmpq %rax, 0x8(%r13)
jne 0x828ce
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%r13)
movq (%r13), %rdi
imulq $0x34, %rcx, %rsi
callq 0x2370
movq %rax, (%r13)
movq (%r13), %rax
movq 0x8(%r13), %rcx
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)
movq 0x60(%rsp), %rsi
movl %ebp, %eax
addq %rax, %r14
incq %r15
movl 0x10(%r12), %eax
cmpq %rax, %r15
jb 0x826bf
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