// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: poa_chain_0(poa_g_t*, ma_ug_t*, uc_block_t*, ul_str_t*, long, long, long, integer_t*, long, long, unsigned int*)
void poa_chain_0(poa_g_t *g, ma_ug_t *ug, uc_block_t *raw, ul_str_t *str, int64_t s, int64_t e, int64_t is_rev, integer_t *buf, int64_t str_id, int64_t debug_qid, uint32_t *is_circle)
{
    (*is_circle) = 0;
    if(e <= s) return;
    uint32_t *g_idx = g->srt_b.res.a; uc_block_t *z; integer_aln_t *b; ul_chain_t rr; int64_t i, k, n;
    uint64_t *pat = (is_rev?(str->a + str->cn - e):(str->a + s)), pp; int64_t pat_n = e - s;
    // fprintf(stderr, "[M::%s::] ts::%ld, te::%ld, is_rev::%ld, g->seq.n::%u, pat_n::%lu\n", 
    // __func__, s, e, is_rev, (uint32_t)g->seq.n, pat_n);

    g->srt_b.aln.n = 0; n = g->seq.n;
    for (k = 0; k < n; k++) {///graph
        pp = (((uint64_t)(g->seq.a[g_idx[k]].nid))<<32); pp += ((uint64_t)(k)); pp += ((uint64_t)(0x80000000));
        kv_push(uint64_t, g->srt_b.aln, pp);
    }
    for (k = 0; k < pat_n; k++) {
        pp = (uint32_t)pat[poa_str_idx(k, pat_n, is_rev)]; if(is_rev) pp ^= 1; pp <<= 32; pp += ((uint64_t)(k));
        kv_push(uint64_t, g->srt_b.aln, pp);
    }

    radix_sort_srt64(g->srt_b.aln.a, g->srt_b.aln.a + g->srt_b.aln.n); n = g->srt_b.aln.n;
    for (k = 0, buf->b.n = 0; k < n; k++) {
        if(g->srt_b.aln.a[k]&((uint64_t)(0x80000000))) continue;///skip nodes in the graph
        for (i = k+1; (i < n) && ((g->srt_b.aln.a[k]>>32) == (g->srt_b.aln.a[i]>>32)); i++) {
            if((g->srt_b.aln.a[i]&((uint64_t)(0x80000000))) == 0) continue;///skip nodes in the read
            ///a[k] is read (q); a[i] is graph (t)
            kv_pushp(integer_aln_t, buf->b, &b);
            b->vq = g->srt_b.aln.a[k]>>32;
            b->tn_rev_qk = (uint32_t)g->srt_b.aln.a[k];
            b->tk = ((g->srt_b.aln.a[i]<<33)>>33);
            z = &(raw[pat[poa_str_idx(b->tn_rev_qk, pat_n, is_rev)]>>32]);
            b->sc = ug_occ_w(z->ts, z->te, &(ug->u.a[z->hid]));
        }
    }

    n = buf->b.n;
    radix_sort_integer_aln_t_srt(buf->b.a, buf->b.a + buf->b.n); ///sorted by qk
    // fprintf(stderr, "[M::%s::] # align pairs::%ld\n", __func__, n);

    // for (i = 0; i < n; i++) {///sort score
    //     b = &(buf->b.a[i]); z = &(raw[pat[poa_str_idx(b->tn_rev_qk, pat_n, is_rev)]>>32]);
    //     pp = ug_occ_w(z->ts, z->te, &(ug->u.a[z->hid]));
    //     b->tn_rev_qk += (pp<<32);
    // }
    

    i = integer_g_chain(g, ug, buf->b.a, buf->b.n, buf, &rr); assert(i);
    if(!i) return; buf->b.n = rr.e; assert(buf->b.n);
    append_aligned_integer_seq_by_aln_pair(ug, raw, g, g->seq.n, pat, pat_n, is_rev, buf->b.a, buf->b.n, str_id, (is_rev?(str->cn - e):(s)));
    (*is_circle) = update_poa_dp(g, debug_qid, ug);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, 0x18(%rsp)
movq 0xd0(%rsp), %rax
andl $0x0, (%rax)
movq %rsi, 0x10(%rsp)
movq %r9, %rax
subq %r8, %rax
movq %rax, 0x8(%rsp)
jle 0x1b85ba
movq %rdi, %rbx
movq 0x10(%rcx), %rax
cmpq $0x0, 0xb0(%rsp)
movq %r8, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
je 0x1b8230
movl 0x18(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%rax,%rcx,8), %rcx
leaq (,%r9,8), %rax
subq %rax, %rcx
movq %rcx, (%rsp)
jmp 0x1b8238
leaq (%rax,%r8,8), %rax
movq %rax, (%rsp)
movq 0x90(%rbx), %r14
andq $0x0, 0xb0(%rbx)
movq (%rbx), %rax
xorl %r15d, %r15d
testq %rax, %rax
cmovgq %rax, %r15
negq %r15
movl $0x80000000, %r12d      # imm = 0x80000000
movl $0x80000000, %r13d      # imm = 0x80000000
leaq (%r15,%r13), %rax
cmpq %r12, %rax
je 0x1b82dc
movl (%r14), %eax
movq 0x10(%rbx), %rdx
movq 0xb0(%rbx), %rcx
movl 0x4(%rdx,%rax,8), %ebp
movq 0xc0(%rbx), %rax
cmpq 0xb8(%rbx), %rcx
jne 0x1b82bd
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0xb8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0xc0(%rbx)
movq 0xb0(%rbx), %rcx
shlq $0x20, %rbp
addq %r13, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, 0xb0(%rbx)
movq %rbp, (%rax,%rcx,8)
incq %r13
addq $0x4, %r14
jmp 0x1b8263
xorl %r14d, %r14d
cmpq $0x0, 0xb0(%rsp)
setne %r14b
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
testq %rax, %rax
movl $0x0, %r12d
cmovgq %rax, %r12
leaq -0x1(%rax), %r13
cmpq %r15, %r12
je 0x1b838f
cmpq $0x0, 0xb0(%rsp)
movq %r15, %rax
cmovneq %r13, %rax
movq (%rsp), %rcx
movl (%rcx,%rax,8), %ebp
xorl %r14d, %ebp
movq 0xb0(%rbx), %rcx
movq 0xc0(%rbx), %rax
cmpq 0xb8(%rbx), %rcx
jne 0x1b836e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0xb8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0xc0(%rbx)
movq 0xb0(%rbx), %rcx
shlq $0x20, %rbp
addq %r15, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, 0xb0(%rbx)
movq %rbp, (%rax,%rcx,8)
incq %r15
decq %r13
jmp 0x1b8305
movq 0xb0(%rbx), %rax
movq 0xc0(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq 0xb0(%rbx), %r13
movq 0xb8(%rsp), %rax
andq $0x0, 0x30(%rax)
xorl %eax, %eax
testq %r13, %r13
movl $0x0, %esi
cmovgq %r13, %rsi
xorl %r14d, %r14d
movq %rsi, 0x38(%rsp)
cmpq %rsi, %r14
je 0x1b84fc
movq 0xc0(%rbx), %r12
testb $-0x80, 0x3(%r12,%r14,8)
jne 0x1b84f4
movq %r14, %r15
incq %r15
cmpq %r13, %r15
jge 0x1b84f4
movq (%r12,%r15,8), %rcx
movq (%r12,%r14,8), %rdx
xorq %rcx, %rdx
shrq $0x20, %rdx
jne 0x1b84f4
incq %r15
testl %ecx, %ecx
jns 0x1b83f2
movq 0xb8(%rsp), %rbp
movq 0x40(%rbp), %rdi
cmpq 0x38(%rbp), %rax
jne 0x1b8456
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x38(%rbp)
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x40(%rbp)
movq 0x30(%rbp), %rax
movq 0xc0(%rbx), %r12
leaq -0x1(%r15), %rcx
movq %rcx, 0x48(%rsp)
leaq 0x1(%rax), %rcx
movq %rcx, 0x40(%rsp)
movq %rcx, 0x30(%rbp)
imulq $0x18, %rax, %rbp
movq (%r12,%r14,8), %rax
movl %eax, %ecx
shrq $0x20, %rax
movl %eax, 0x4(%rdi,%rbp)
movq %rcx, 0x10(%rdi,%rbp)
movl -0x8(%r12,%r15,8), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
movq %rcx, %rdx
notq %rdx
addq 0x8(%rsp), %rdx
cmpq $0x0, 0xb0(%rsp)
movl %eax, (%rdi,%rbp)
cmoveq %rcx, %rdx
movq (%rsp), %rax
movl 0x4(%rax,%rdx,8), %eax
imulq $0x24, %rax, %rax
movq 0x18(%rsp), %rdx
movl (%rdx,%rax), %ecx
movq %rdi, %r15
movl 0xc(%rdx,%rax), %edi
movl 0x10(%rdx,%rax), %esi
imulq $0x28, %rcx, %rdx
movq 0x10(%rsp), %rax
addq 0x10(%rax), %rdx
callq 0x1b2312
movl %eax, 0x8(%r15,%rbp)
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %r15
movq 0x38(%rsp), %rsi
jmp 0x1b83ef
incq %r14
jmp 0x1b83d0
movq 0xb8(%rsp), %r14
movq 0x40(%r14), %rdi
imulq $0x18, %rax, %rsi
addq %rdi, %rsi
callq 0x1a47a8
movq 0x30(%r14), %rcx
movq 0x40(%r14), %rdx
leaq 0x50(%rsp), %r9
movq %rbx, %rdi
movq %r14, %r8
callq 0x1b7e68
testq %rax, %rax
je 0x1b85c9
movl 0x54(%rsp), %eax
movq %rax, 0x30(%r14)
testq %rax, %rax
movq 0x30(%rsp), %r10
movq 0x28(%rsp), %rdx
je 0x1b85e8
movq 0x40(%r14), %rcx
cmpq $0x0, 0xb0(%rsp)
je 0x1b8572
movq 0x20(%rsp), %rsi
movl 0x18(%rsi), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
subq %rdx, %r10
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
pushq %r10
pushq 0xd0(%rsp)
pushq %rax
pushq %rcx
pushq 0xd8(%rsp)
callq 0x1b7268
addq $0x30, %rsp
movq %rbx, %rdi
callq 0x1b7a9b
movq 0xd0(%rsp), %rcx
movl %eax, (%rcx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x43750(%rip), %rdi     # 0x1fbd20
leaq 0x3ef02(%rip), %rsi     # 0x1f74d9
leaq 0x40a44(%rip), %rcx     # 0x1f9022
movl $0x17b3, %edx           # imm = 0x17B3
callq 0x2110
leaq 0x40aba(%rip), %rdi     # 0x1f90a9
leaq 0x3eee3(%rip), %rsi     # 0x1f74d9
leaq 0x40a25(%rip), %rcx     # 0x1f9022
movl $0x17b4, %edx           # imm = 0x17B4
callq 0x2110