// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq 0xd0(%rsp), %rax
movl $0x0, (%rax)
movq %r9, %rax
subq %r8, %rax
movq %rax, 0x10(%rsp)
jle 0x2043ef
movq %rdi, %r10
movq 0x10(%rcx), %rax
cmpq $0x0, 0xb0(%rsp)
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x38(%rsp)
je 0x204028
movl 0x18(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%rax,%rcx,8), %rcx
leaq (,%r9,8), %rax
subq %rax, %rcx
movq %rcx, 0x8(%rsp)
jmp 0x204031
leaq (%rax,%r8,8), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%r10), %rbx
movq $0x0, 0xb0(%r10)
movq (%r10), %r14
testq %r14, %r14
movq %r10, (%rsp)
jle 0x2040e2
movq 0xc0(%r10), %rax
negq %r14
movl $0x80000000, %r15d      # imm = 0x80000000
movl $0x2, %r12d
movl $0x80000000, %r13d      # imm = 0x80000000
movl (%rbx), %edx
movq 0x10(%r10), %rsi
movq 0xb0(%r10), %rcx
movl 0x4(%rsi,%rdx,8), %ebp
cmpq 0xb8(%r10), %rcx
jne 0x2040b9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rsi, 0xb8(%r10)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %r10
movq %rax, 0xc0(%r10)
movq 0xb0(%r10), %rcx
shlq $0x20, %rbp
addq %r13, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, 0xb0(%r10)
movq %rbp, (%rax,%rcx,8)
leaq (%r14,%r13), %rcx
incq %rcx
incq %r13
addq $0x4, %rbx
cmpq %r15, %rcx
jne 0x20406f
cmpq $0x0, 0x10(%rsp)
jle 0x204192
xorl %ebx, %ebx
movq 0xb0(%rsp), %rbp
testq %rbp, %rbp
setne %bl
movq 0xc0(%r10), %rdi
movq 0x28(%rsp), %r14
notq %r14
addq 0x30(%rsp), %r14
xorl %r15d, %r15d
movl $0x2, %r12d
testq %rbp, %rbp
movq %r15, %rax
cmovneq %r14, %rax
movq 0x8(%rsp), %rcx
movl (%rcx,%rax,8), %r13d
xorl %ebx, %r13d
movq 0xb0(%r10), %rax
cmpq 0xb8(%r10), %rax
jne 0x204171
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0xb8(%r10)
shlq $0x3, %rsi
callq 0x2370
movq (%rsp), %r10
movq %rax, %rdi
movq %rax, 0xc0(%r10)
movq 0xb0(%r10), %rax
shlq $0x20, %r13
addq %r15, %r13
leaq 0x1(%rax), %rcx
movq %rcx, 0xb0(%r10)
movq %r13, (%rdi,%rax,8)
incq %r15
addq $-0x1, %r14
jb 0x20411b
jmp 0x204199
movq 0xc0(%r10), %rdi
movq 0xb0(%r10), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1ee586
movq (%rsp), %rax
movq 0xb0(%rax), %rsi
movq 0xb8(%rsp), %r14
movq $0x0, 0x30(%r14)
testq %rsi, %rsi
jle 0x20430f
movq 0xc0(%rax), %rbp
xorl %eax, %eax
xorl %ebx, %ebx
movq %rsi, 0x48(%rsp)
testb $-0x80, 0x3(%rbp,%rbx,8)
sete %cl
leaq 0x1(%rbx), %r15
cmpq %rsi, %r15
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
movq %r15, 0x40(%rsp)
jne 0x2042ff
movq (%rbp,%r15,8), %rcx
movq (%rbp,%rbx,8), %rdx
xorq %rcx, %rdx
shrq $0x20, %rdx
jne 0x2042ff
testl %ecx, %ecx
jns 0x2042f3
movq 0x40(%r14), %r12
cmpq 0x38(%r14), %rax
jne 0x204262
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x38(%r14)
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %r12, %rdi
callq 0x2370
movq %rax, %r12
movq %rax, 0x40(%r14)
movq 0x30(%r14), %rax
movq (%rsp), %rcx
movq 0xc0(%rcx), %rbp
leaq 0x1(%rax), %r13
movq %r13, 0x30(%r14)
leaq (%rax,%rax,2), %r14
movq (%rbp,%rbx,8), %rax
movl %eax, %ecx
shrq $0x20, %rax
movl %eax, 0x4(%r12,%r14,8)
movq %rcx, 0x10(%r12,%r14,8)
movl (%rbp,%r15,8), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
movq %rcx, %rdx
notq %rdx
addq 0x10(%rsp), %rdx
cmpq $0x0, 0xb0(%rsp)
movl %eax, (%r12,%r14,8)
cmoveq %rcx, %rdx
movq 0x8(%rsp), %rax
movl 0x4(%rax,%rdx,8), %eax
leaq (%rax,%rax,8), %rax
movq 0x20(%rsp), %rdx
movl (%rdx,%rax,4), %ecx
movl 0xc(%rdx,%rax,4), %edi
movl 0x10(%rdx,%rax,4), %esi
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
movq 0x18(%rsp), %rax
addq 0x10(%rax), %rdx
callq 0x1fe266
movl %eax, 0x8(%r12,%r14,8)
movq 0xb8(%rsp), %r14
movq %r13, %rax
movq 0x48(%rsp), %rsi
incq %r15
cmpq %rsi, %r15
jl 0x2041ff
movq 0x40(%rsp), %rbx
cmpq %rsi, %rbx
jne 0x2041dd
jmp 0x204311
xorl %eax, %eax
movq 0x40(%r14), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1eef97
movq 0x30(%r14), %rcx
movq 0x40(%r14), %rdx
leaq 0x50(%rsp), %r9
movq (%rsp), %rdi
movq %r14, %r8
callq 0x203c5d
testq %rax, %rax
movq %r14, %rcx
movq 0xb0(%rsp), %r14
je 0x2043fe
movl 0x54(%rsp), %eax
movq %rax, 0x30(%rcx)
testq %rax, %rax
je 0x20441d
movq 0xc0(%rsp), %r10
movq 0x40(%rcx), %rcx
testq %r14, %r14
movq (%rsp), %rbx
je 0x2043af
movq 0x38(%rsp), %rsi
movl 0x18(%rsi), %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
subq 0x30(%rsp), %r11
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r9
pushq %r11
pushq %r10
pushq %rax
pushq %rcx
pushq %r14
jmp 0x2043d4
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r9
pushq 0x30(%rsp)
pushq %r10
pushq %rax
pushq %rcx
pushq $0x0
callq 0x202f09
addq $0x30, %rsp
movq %rbx, %rdi
callq 0x203878
movq 0xd0(%rsp), %rcx
movl %eax, (%rcx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4595c(%rip), %rdi     # 0x249d61
leaq 0x411fd(%rip), %rsi     # 0x245609
leaq 0x42c50(%rip), %rcx     # 0x247063
movl $0x17b3, %edx           # imm = 0x17B3
callq 0x2110
leaq 0x42cc6(%rip), %rdi     # 0x2470ea
leaq 0x411de(%rip), %rsi     # 0x245609
leaq 0x42c31(%rip), %rcx     # 0x247063
movl $0x17b4, %edx           # imm = 0x17B4
callq 0x2110