// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: gen_updated_trace(asg16_v*, asg16_v*, asg16_v*, char*, unsigned long, char*, unsigned long, asg64_v*, bit_extz_t*, unsigned long)
void gen_updated_trace(asg16_v *qcc, asg16_v *tcc, asg16_v *tcc_res, char *qstr, uint64_t ql, char *tstr, uint64_t tl, asg64_v *srt, bit_extz_t *exz, uint64_t rid)
{
    uint64_t k, ck, qk, tk, wq[2], wt[2], old_dp, dp, s, e, srt_n, si, ei, so, os, oe, *qd, *td, qs, qe, ts, te, q0, t0; 
    asg16_v *cc; uint32_t len; uint16_t c, bq, bt;

    srt->n = 0;

    cc = qcc;
    ck = qk = tk = wq[0] = wq[1] = wt[0] = wt[1] = 0;
    while (ck < cc->n) {
        wq[0] = qk; wt[0] = tk;
        ck = pop_trace_bp_f(cc, ck, &c, &bq, &bt, &len);
        if(c != 2) qk += len;
        if(c != 3) tk += len;
        wq[1] = qk; wt[1] = tk;
        if(c != 0) continue;

        kv_push(uint64_t, (*srt), (wq[0]<<1));
        kv_push(uint64_t, (*srt), ((wq[1]<<1)|1));
    }

    cc = tcc;
    ck = qk = tk = wq[0] = wq[1] = wt[0] = wt[1] = 0;
    while (ck < cc->n) {
        wq[0] = qk; wt[0] = tk;
        ck = pop_trace_bp_f(cc, ck, &c, &bq, &bt, &len);
        if(c != 2) qk += len;
        if(c != 3) tk += len;
        wq[1] = qk; wt[1] = tk;
        if(c != 0) continue;

        kv_push(uint64_t, (*srt), (wt[0]<<1));
        kv_push(uint64_t, (*srt), ((wt[1]<<1)|1));
    }

    // fprintf(stderr, "[M::%s] rid::%lu, ql::%lu, tl::%lu\n", __func__, rid, ql, tl);

    radix_sort_ec64(srt->a, srt->a + srt->n);
    for (k = 0, dp = e = srt_n = 0, s = (uint64_t)-1; k < srt->n; k++) {
        old_dp = dp;
        //if a[k] is qe
        if (srt->a[k]&1) --dp;
        else ++dp;

        if(old_dp >= 2 && s != (uint64_t)-1) {
            e = srt->a[k]>>1;
            if(e > s) srt->a[srt_n++] = ((s<<32)|(e));
        }

        s = (uint64_t)-1;
        if(dp >= 2) s = srt->a[k]>>1;
        // if (old_dp < 2 && dp >= 2) {///old_dp < dp, a[k] is qs
        //     s = srt->a[k]>>1;
        // } else if (old_dp >= 2 && dp < 2) {///old_dp > dp, a[k] is qe
        //     e = srt->a[k]>>1;
        //     if(e > s) {
        //         srt->a[srt_n++] = ((s<<32)|(e));
        //         fprintf(stderr, "[M::%s] x::[%lu,\t%lu)\n", __func__, s, e);
        //     }
        // }
    }

    
    // for (k = 0; k < srt_n; k++) {
    //     fprintf(stderr, "[M::%s] x[%lu]::[%lu,\t%u)\n", __func__, k, srt->a[k]>>32, (uint32_t)srt->a[k]);
    // }
    

    if(srt_n > 0) {
        srt->n = srt_n;

        cc = qcc;
        k = ck = qk = tk = wq[0] = wq[1] = wt[0] = wt[1] = 0;
        while (ck < cc->n) {
            wq[0] = qk; wt[0] = tk;
            ck = pop_trace_bp_f(cc, ck, &c, &bq, &bt, &len);
            if(c != 2) qk += len;
            if(c != 3) tk += len;
            wq[1] = qk; wt[1] = tk;
            if(c != 0) continue;
            si = wq[0]; ei = wq[1];
            so = wt[0]; ///eo = wt[1];

            for (; (k > 0) && ((k >= srt_n) || (((uint32_t)(srt->a[k])) > si)); k--);
            for (; k < srt_n; k++) {
                s = srt->a[k]>>32; e = (uint32_t)(srt->a[k]);
                if(s >= ei) break;
                if(s >= si && e <= ei) {
                    os = so + s - si;
                    oe = so + e - si; 
                    kv_push(uint64_t, (*srt), ((os<<32)|(oe)));
                }
            }
        }
        assert(srt->n == (srt_n<<1));

        cc = tcc;
        k = ck = qk = tk = wq[0] = wq[1] = wt[0] = wt[1] = 0;
        while (ck < cc->n) {
            wq[0] = qk; wt[0] = tk;
            ck = pop_trace_bp_f(cc, ck, &c, &bq, &bt, &len);
            if(c != 2) qk += len;
            if(c != 3) tk += len;
            wq[1] = qk; wt[1] = tk;
            if(c != 0) continue;
            si = wt[0]; ei = wt[1];
            so = wq[0]; ///eo = wq[1];

            for (; (k > 0) && ((k >= srt_n) || (((uint32_t)(srt->a[k])) > si)); k--);
            for (; k < srt_n; k++) {
                s = srt->a[k]>>32; e = (uint32_t)(srt->a[k]);
                // fprintf(stderr, "######[M::%s] t[%lu]::[%lu,\t%lu) i::[%lu,\t%lu)\n", __func__, k, s, e, si, ei);
                if(s >= ei) break;
                if(s >= si && e <= ei) {
                    os = so + s - si;
                    oe = so + e - si; 
                    kv_push(uint64_t, (*srt), ((os<<32)|(oe)));
                    // fprintf(stderr, "[M::%s] ******\n", __func__);
                }
            }
        }
        // if(!(srt->n == (srt_n*3))) {
        //     fprintf(stderr, "[M::%s] rid::%lu, srt_n::%lu, srt->n::%lu\n", __func__, rid, srt_n, (uint64_t)srt->n);
        // }
        assert(srt->n == (srt_n*3));

        
        tcc_res->n = 0; ///reset tcc
        qd = srt->a + srt_n; td = srt->a + srt_n + srt_n; uint64_t nl = 0;///, dbg_ql, dbg_tl;
        for (k = q0 = t0 = 0; k < srt_n; k++) {
            qs = qd[k]>>32; qe = (uint32_t)qd[k];
            ts = td[k]>>32; te = (uint32_t)td[k];
            nl += qs - qe;
            // assert((qe - qs) == (te - ts));
            // assert(!memcmp(qstr + qs, tstr + ts, sizeof((*qstr))*(qe - qs)));

            // if(t0 > ts || q0 > qs) {
            //     fprintf(stderr, "[M::%s] rid::%lu, ql::%lu, tl::%lu\n", __func__, rid, ql, tl);
            // }
            // fprintf(stderr, "[M::%s] qseq::[%lu,%lu), ql::%lu, tseq::[%lu,%lu), tl::%lu\n", __func__, t0, ts, tl, q0, qs, ql);

            gen_cc_fly(tcc_res, tstr + t0, ts - t0, qstr + q0, qs - q0, exz, 0.25, MAX_SIN_L, MAX_SIN_E);

            // cal_updated_trace_len(tcc_res, &dbg_ql, &dbg_tl);
            // assert(dbg_ql == ts && dbg_tl == qs);

            // fprintf(stderr, "******\n");
            
            push_trace_bp_f(tcc_res, 0, (uint16_t)-1, (uint16_t)-1,  qe - qs, 1);

            // cal_updated_trace_len(tcc_res, &dbg_ql, &dbg_tl);
            // assert(dbg_ql == te && dbg_tl == qe);

            q0 = qe; t0 = te;
        }

        qs = ql; ts = tl;
        // fprintf(stderr, "[M::%s] qseq::[%lu,%lu), ql::%lu, tseq::[%lu,%lu), tl::%lu\n", __func__, t0, ts, tl, q0, qs, ql);
        gen_cc_fly(tcc_res, tstr + t0, ts - t0, qstr + q0, qs - q0, exz, 0.25, MAX_SIN_L, MAX_SIN_E);
        // if(!(dbg_ql == ts && dbg_tl == qs)) {
        //     fprintf(stderr, "[M::%s] rid::%lu, qseq::[%lu,%lu), ql::%lu, tseq::[%lu,%lu), tl::%lu\n", __func__, rid, t0, ts, tl, q0, qs, ql);
        // }
        // cal_updated_trace_len(tcc_res, &dbg_ql, &dbg_tl);
        // assert(dbg_ql == ts && dbg_tl == qs);

        // fprintf(stderr, "[M::%s] srt_n::%lu, nl::%lu, ql::%lu, tl::%lu\n", __func__, srt_n, nl, ql, tl);
    } else {
        gen_cc_fly(tcc_res, tstr, tl, qstr, ql, exz, 0.25, MAX_SIN_L, MAX_SIN_E);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x38(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r12
movq 0x98(%rsp), %rax
andq $0x0, (%rax)
xorl %ebx, %ebx
leaq 0x8(%rsp), %r13
xorl %r14d, %r14d
movq %rdi, 0x18(%rsp)
movl %r14d, %eax
cmpq %rax, (%r12)
jbe 0x14afed
movq %rbx, %rbp
movq %r12, %rdi
movl %r14d, %esi
leaq 0x4(%rsp), %rdx
leaq 0x6(%rsp), %rcx
leaq 0xe(%rsp), %r8
movq %r13, %r9
callq 0xeeaa9
movl %eax, %r14d
movzwl 0x4(%rsp), %eax
movl 0x8(%rsp), %r15d
addq %rbx, %r15
cmpw $0x2, %ax
movq %r15, %rbx
cmoveq %rbp, %rbx
testw %ax, %ax
jne 0x14af00
movq 0x98(%rsp), %r12
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
jne 0x14af8d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
addq %rbp, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movq %rbp, (%rax,%rcx,8)
movq (%r12), %rcx
cmpq 0x8(%r12), %rcx
jne 0x14afcf
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
leaq 0x1(,%r15,2), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r12)
movq %rdx, (%rax,%rcx,8)
movq 0x18(%rsp), %r12
jmp 0x14af00
xorl %ebx, %ebx
leaq 0x4(%rsp), %r12
leaq 0x6(%rsp), %r13
xorl %r14d, %r14d
movq 0x10(%rsp), %rcx
movl %r14d, %eax
cmpq %rax, (%rcx)
jbe 0x14b0fc
movq %rbx, %rbp
movq %rcx, %rbx
movq %rcx, %rdi
movl %r14d, %esi
movq %r12, %rdx
movq %r13, %rcx
leaq 0xe(%rsp), %r8
leaq 0x8(%rsp), %r9
callq 0xeeaa9
movl %eax, %r14d
movzwl 0x4(%rsp), %eax
movl 0x8(%rsp), %r15d
addq %rbp, %r15
cmpw $0x3, %ax
movq %rbx, %rcx
movq %r15, %rbx
cmoveq %rbp, %rbx
testw %ax, %ax
jne 0x14b001
movq %r12, %r13
movq 0x98(%rsp), %r12
movq (%r12), %rcx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rcx
jne 0x14b094
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
addq %rbp, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movq %rbp, (%rax,%rcx,8)
movq (%r12), %rcx
cmpq 0x8(%r12), %rcx
jne 0x14b0d6
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rcx
leaq 0x1(,%r15,2), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r12)
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rsp), %rcx
movq %r13, %r12
leaq 0x6(%rsp), %r13
jmp 0x14b001
movq 0x98(%rsp), %rbp
movq (%rbp), %rax
movq 0x10(%rbp), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x13d044
pushq $-0x1
popq %rax
xorl %ecx, %ecx
movq %rax, %rdi
xorl %r10d, %r10d
xorl %edx, %edx
cmpq (%rbp), %rdx
jae 0x14b178
movq 0x10(%rbp), %rsi
movq (%rsi,%rdx,8), %r8
cmpq $0x2, %rcx
jb 0x14b155
cmpq $-0x1, %rdi
je 0x14b155
movq %r8, %r9
shrq %r9
cmpq %rdi, %r9
jbe 0x14b155
shlq $0x20, %rdi
orq %rdi, %r9
movq %r9, (%rsi,%r10,8)
incq %r10
andl $0x1, %r8d
negq %r8
orq $0x1, %r8
addq %r8, %rcx
movq %rax, %rdi
cmpq $0x2, %rcx
jb 0x14b173
movq (%rsi,%rdx,8), %rdi
shrq %rdi
incq %rdx
jmp 0x14b122
testq %r10, %r10
je 0x14b54f
movq %r10, (%rbp)
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %esi, %esi
xorl %r15d, %r15d
movq %r10, 0x20(%rsp)
movq 0x18(%rsp), %r14
movl %esi, %eax
cmpq %rax, (%r14)
jbe 0x14b2d7
movq %r13, %r12
movq %r14, %rdi
leaq 0x4(%rsp), %rdx
leaq 0x6(%rsp), %rcx
leaq 0xe(%rsp), %r8
leaq 0x8(%rsp), %r9
callq 0xeeaa9
movq %rbx, %r8
movq 0x20(%rsp), %r10
movl %eax, %esi
movzwl 0x4(%rsp), %eax
cmpw $0x2, %ax
movl 0x8(%rsp), %ebx
movq %rbx, %r13
movl $0x0, %ecx
cmoveq %rcx, %r13
addq %r12, %r13
cmpw $0x3, %ax
cmoveq %rcx, %rbx
addq %r8, %rbx
testw %ax, %ax
jne 0x14b199
movq %r15, %r14
subq $0x1, %r15
jb 0x14b214
cmpq %r10, %r14
jae 0x14b1f9
movq 0x10(%rbp), %rax
movl (%rax,%r14,8), %eax
cmpq %r12, %rax
ja 0x14b1f9
subq %r12, %r8
movl %esi, 0x30(%rsp)
movq %r8, 0x50(%rsp)
cmpq %r10, %r14
jae 0x14b2bf
movq 0x10(%rbp), %rax
movq (%rax,%r14,8), %r15
movl %r15d, %ebp
shrq $0x20, %r15
cmpq %r13, %r15
jae 0x14b2c7
cmpq %r12, %r15
jb 0x14b2af
cmpq %r13, %rbp
ja 0x14b2af
movq 0x98(%rsp), %rdi
movq (%rdi), %rcx
cmpq 0x8(%rdi), %rcx
jne 0x14b297
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x98(%rsp), %rdi
movq 0x50(%rsp), %r8
movl 0x30(%rsp), %esi
movq 0x20(%rsp), %r10
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
addq %r8, %r15
addq %r8, %rbp
shlq $0x20, %r15
orq %rbp, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movq %r15, (%rax,%rcx,8)
incq %r14
movq 0x98(%rsp), %rbp
jmp 0x14b220
movq %r14, %r15
jmp 0x14b194
movq %r14, %r15
movq 0x98(%rsp), %rbp
jmp 0x14b194
leaq (%r10,%r10), %rax
cmpq %rax, (%rbp)
jne 0x14b59b
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %esi, %esi
xorl %r15d, %r15d
movq 0x10(%rsp), %rbx
movl %esi, %eax
cmpq %rax, (%rbx)
jbe 0x14b440
movq %rbp, %r12
movq %rbx, %rdi
leaq 0x4(%rsp), %rdx
leaq 0x6(%rsp), %rcx
leaq 0xe(%rsp), %r8
leaq 0x8(%rsp), %r9
callq 0xeeaa9
movq %r14, %rdi
movq 0x20(%rsp), %r10
movl %eax, %esi
movzwl 0x4(%rsp), %eax
cmpw $0x2, %ax
movl 0x8(%rsp), %ebp
movq %rbp, %r14
cmoveq %r13, %r14
addq %rdi, %r14
cmpw $0x3, %ax
cmoveq %r13, %rbp
addq %r12, %rbp
testw %ax, %ax
jne 0x14b2f7
movq %r15, %r13
subq $0x1, %r15
jb 0x14b375
cmpq %r10, %r13
jae 0x14b352
movq 0x98(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%r13,8), %eax
cmpq %r12, %rax
ja 0x14b352
subq %r12, %rdi
movl %esi, 0x18(%rsp)
movq %rdi, 0x30(%rsp)
cmpq %r10, %r13
jae 0x14b425
movq 0x98(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r13,8), %r15
movl %r15d, %ebx
shrq $0x20, %r15
cmpq %rbp, %r15
jae 0x14b430
cmpq %r12, %r15
jb 0x14b418
cmpq %rbp, %rbx
ja 0x14b418
movq 0x98(%rsp), %r8
movq (%r8), %rcx
cmpq 0x8(%r8), %rcx
jne 0x14b400
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x98(%rsp), %r8
movq 0x30(%rsp), %rdi
movl 0x18(%rsp), %esi
movq 0x20(%rsp), %r10
movq %rax, 0x10(%r8)
movq (%r8), %rcx
addq %rdi, %r15
addq %rdi, %rbx
shlq $0x20, %r15
orq %rbx, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %r15, (%rax,%rcx,8)
incq %r13
movq 0x10(%rsp), %rbx
jmp 0x14b381
movq %r13, %r15
xorl %r13d, %r13d
jmp 0x14b2f7
movq %r13, %r15
movq 0x10(%rsp), %rbx
xorl %r13d, %r13d
jmp 0x14b2f7
leaq (%r10,%r10,2), %rax
movq 0x98(%rsp), %rcx
cmpq %rax, (%rcx)
jne 0x14b5ba
movq 0x28(%rsp), %rax
andq $0x0, (%rax)
leaq (,%r10,8), %rax
addq 0x10(%rcx), %rax
movq %rax, 0x10(%rsp)
leaq (%rax,%r10,8), %rax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x28(%rsp), %r15
cmpq %r13, %r10
je 0x14b51d
movq 0x10(%rsp), %rdx
movq (%rdx,%r13,8), %r14
movq %r14, %r12
shrq $0x20, %r12
movl %r14d, %ebp
movq 0x18(%rsp), %rdx
movq (%rdx,%r13,8), %rdx
movl %edx, %ebx
shrq $0x20, %rdx
movq 0x38(%rsp), %rsi
addq %rcx, %rsi
subq %rcx, %rdx
movq 0x40(%rsp), %rcx
addq %rax, %rcx
movq %r12, %r8
subq %rax, %r8
movq %r15, %rdi
movq 0xa0(%rsp), %r9
movsd 0x98045(%rip), %xmm0    # 0x1e3520
pushq $0x7ff                  # imm = 0x7FF
pushq $0x2710                 # imm = 0x2710
callq 0x14a8df
popq %rax
popq %rcx
subl %r12d, %r14d
movq %r15, %rdi
xorl %esi, %esi
movl $0xffff, %edx           # imm = 0xFFFF
movl $0xffff, %ecx           # imm = 0xFFFF
movl %r14d, %r8d
pushq $0x1
popq %r9
callq 0x14f542
movq 0x20(%rsp), %r10
incq %r13
movq %rbp, %rax
movq %rbx, %rcx
jmp 0x14b484
movq 0x38(%rsp), %rsi
addq %rcx, %rsi
movq 0x90(%rsp), %rdx
subq %rcx, %rdx
movq 0x40(%rsp), %rcx
addq %rax, %rcx
movq 0x48(%rsp), %r8
subq %rax, %r8
movsd 0x97fd8(%rip), %xmm0    # 0x1e3520
movq 0x28(%rsp), %rdi
jmp 0x14b573
movsd 0x97fc9(%rip), %xmm0    # 0x1e3520
movq 0x28(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %r8
movq 0xa0(%rsp), %r9
pushq $0x7ff                  # imm = 0x7FF
pushq $0x2710                 # imm = 0x2710
callq 0x14a8df
popq %rax
popq %rcx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa5cc1(%rip), %rdi     # 0x1f1263
leaq 0xa4952(%rip), %rsi     # 0x1efefb
leaq 0xa5cc8(%rip), %rcx     # 0x1f1278
movl $0x15e4, %edx           # imm = 0x15E4
callq 0x2110
leaq 0xa5d36(%rip), %rdi     # 0x1f12f7
leaq 0xa4933(%rip), %rsi     # 0x1efefb
leaq 0xa5ca9(%rip), %rcx     # 0x1f1278
movl $0x1602, %edx           # imm = 0x1602
callq 0x2110