// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq 0xc0(%rsp), %rax
movl $0x0, (%rax)
movq %r9, %rax
subq %r8, %rax
movq %rax, 0x10(%rsp)
jle 0x1f8dbf
movq %rdi, %rbx
movq 0x10(%rcx), %rax
cmpq $0x0, 0xa0(%rsp)
movq %r8, 0x20(%rsp)
movq %r9, 0x18(%rsp)
movq %rcx, 0x38(%rsp)
je 0x1f8a31
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 0x1f8a3a
leaq (%rax,%r8,8), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rbx), %r14
movq $0x0, 0xb0(%rbx)
movq (%rbx), %r12
testq %r12, %r12
jle 0x1f8ae7
negq %r12
movl $0x80000000, %r13d      # imm = 0x80000000
movl $0x80000000, %r15d      # imm = 0x80000000
movl (%r14), %eax
movq 0x10(%rbx), %rcx
movl 0x4(%rcx,%rax,8), %ebp
movq 0xb8(%rbx), %rax
cmpq %rax, 0xb0(%rbx)
jne 0x1f8ab0
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0xb8(%rbx)
movq 0xc0(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0xc0(%rbx)
shlq $0x20, %rbp
addq %r15, %rbp
movq 0xb0(%rbx), %rax
movq 0xc0(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xb0(%rbx)
movq %rbp, (%rcx,%rax,8)
leaq (%r12,%r15), %rax
incq %rax
incq %r15
addq $0x4, %r14
cmpq %r13, %rax
jne 0x1f8a67
cmpq $0x0, 0x10(%rsp)
jle 0x1f8b9e
xorl %r12d, %r12d
cmpq $0x0, 0xa0(%rsp)
setne %r12b
movq 0x20(%rsp), %r13
notq %r13
addq 0x18(%rsp), %r13
xorl %ebp, %ebp
movl $0x2, %r15d
cmpq $0x0, 0xa0(%rsp)
movq %rbp, %rax
cmovneq %r13, %rax
movq 0x8(%rsp), %rcx
movl (%rcx,%rax,8), %r14d
xorl %r12d, %r14d
movq 0xb8(%rbx), %rax
cmpq %rax, 0xb0(%rbx)
jne 0x1f8b6d
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r15, %rsi
movq %rsi, 0xb8(%rbx)
movq 0xc0(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0xc0(%rbx)
shlq $0x20, %r14
addq %rbp, %r14
movq 0xb0(%rbx), %rax
movq 0xc0(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xb0(%rbx)
movq %r14, (%rcx,%rax,8)
incq %rbp
addq $-0x1, %r13
jb 0x1f8b18
movq 0xb0(%rbx), %rax
movq 0xc0(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
movq 0xb0(%rbx), %r12
movq 0xa8(%rsp), %r14
movq $0x0, 0x30(%r14)
testq %r12, %r12
jle 0x1f8d01
xorl %r13d, %r13d
movq 0xc0(%rbx), %rax
testb $-0x80, 0x3(%rax,%r13,8)
sete %al
leaq 0x1(%r13), %rbp
cmpq %r12, %rbp
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1f8cf5
movq 0xc0(%rbx), %rcx
movq (%rcx,%rbp,8), %rax
movq (%rcx,%r13,8), %rcx
xorq %rax, %rcx
shrq $0x20, %rcx
jne 0x1f8cf5
testl %eax, %eax
jns 0x1f8ce9
movq 0x38(%r14), %rax
cmpq %rax, 0x30(%r14)
jne 0x1f8c54
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x38(%r14)
movq 0x40(%r14), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x40(%r14)
movq 0x30(%r14), %rax
movq 0x40(%r14), %r15
leaq 0x1(%rax), %rcx
movq %rcx, 0x30(%r14)
leaq (%rax,%rax,2), %r14
movq 0xc0(%rbx), %rax
movq (%rax,%r13,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
movl %ecx, 0x4(%r15,%r14,8)
movq %rdx, 0x10(%r15,%r14,8)
movl (%rax,%rbp,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq %rdx, %rcx
notq %rcx
addq 0x10(%rsp), %rcx
cmpq $0x0, 0xa0(%rsp)
movl %eax, (%r15,%r14,8)
cmoveq %rdx, %rcx
movq 0x8(%rsp), %rax
movl 0x4(%rax,%rcx,8), %eax
leaq (%rax,%rax,8), %rax
movq 0x30(%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 0x28(%rsp), %rax
addq 0x10(%rax), %rdx
callq 0x1f2d50
movl %eax, 0x8(%r15,%r14,8)
movq 0xa8(%rsp), %r14
incq %rbp
cmpq %r12, %rbp
jl 0x1f8bfd
incq %r13
cmpq %r12, %r13
jne 0x1f8bd8
movq 0x30(%r14), %rax
movq 0x40(%r14), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e4585
movq 0x30(%r14), %rcx
movq 0x40(%r14), %rdx
leaq 0x40(%rsp), %r9
movq %rbx, %rdi
movq %r14, %r8
callq 0x1f864d
testq %rax, %rax
je 0x1f8dce
movl 0x44(%rsp), %eax
movq %rax, 0x30(%r14)
testq %rax, %rax
movq 0x20(%rsp), %r11
movq 0x18(%rsp), %rdx
je 0x1f8ded
movq 0xb0(%rsp), %r10
movq 0x40(%r14), %rcx
cmpq $0x0, 0xa0(%rsp)
je 0x1f8d7c
movq 0x38(%rsp), %rsi
movl 0x18(%rsi), %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
subq %rdx, %r11
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r9
pushq %r11
pushq %r10
pushq %rax
pushq %rcx
pushq 0xc8(%rsp)
callq 0x1f7986
addq $0x30, %rsp
movq %rbx, %rdi
callq 0x1f822a
movq 0xc0(%rsp), %rcx
movl %eax, (%rcx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x44ca8(%rip), %rdi     # 0x23da7d
leaq 0x40549(%rip), %rsi     # 0x239325
leaq 0x41f9c(%rip), %rcx     # 0x23ad7f
movl $0x17b3, %edx           # imm = 0x17B3
callq 0x2110
leaq 0x42012(%rip), %rdi     # 0x23ae06
leaq 0x4052a(%rip), %rsi     # 0x239325
leaq 0x41f7d(%rip), %rcx     # 0x23ad7f
movl $0x17b4, %edx           # imm = 0x17B4
callq 0x2110