// File: 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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x170, %rsp            # imm = 0x170
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq 0x30(%rbp), %rax
movl $0x0, (%rax)
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jg 0x3330b3
jmp 0x333932
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x3330fe
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movl 0x18(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
xorl %ecx, %ecx
subq -0x40(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
jmp 0x333118
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x40(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rax
movq $0x0, 0xb0(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x98(%rbp), %rax
jge 0x333295
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
movq -0x90(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl 0x4(%rax,%rcx,8), %eax
shlq $0x20, %rax
movq %rax, -0xa8(%rbp)
movq -0x90(%rbp), %rax
addq -0xa8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movabsq $0x80000000, %rax       # imm = 0x80000000
addq -0xa8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0xb8(%rcx), %rax
jne 0x33324d
movq -0x18(%rbp), %rax
cmpq $0x0, 0xb8(%rax)
je 0x333200
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rax
shlq %rax
movq %rax, -0xc0(%rbp)
jmp 0x33320e
movl $0x2, %eax
movq %rax, -0xc0(%rbp)
jmp 0x33320e
movq -0xc0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xc0(%rax)
movq -0xa8(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rax
movq -0x18(%rbp), %rsi
movq 0xb0(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0xb0(%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x33327e
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x33315d
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jge 0x33341f
movq -0xa0(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x3332e4
movq -0xb0(%rbp), %rax
subq -0x90(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x3332f2
movq -0x90(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0xa8(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x333326
movq -0xa8(%rbp), %rax
xorq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0xa8(%rbp)
movq -0x90(%rbp), %rax
addq -0xa8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0xb8(%rcx), %rax
jne 0x3333d7
movq -0x18(%rbp), %rax
cmpq $0x0, 0xb8(%rax)
je 0x33338a
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rax
shlq %rax
movq %rax, -0xd8(%rbp)
jmp 0x333398
movl $0x2, %eax
movq %rax, -0xd8(%rbp)
jmp 0x333398
movq -0xd8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0xc0(%rax)
movq -0xa8(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rax
movq -0x18(%rbp), %rsi
movq 0xb0(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0xb0(%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x333408
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x3332a0
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
movq %rax, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movq 0x18(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x90(%rbp), %rax
cmpq -0x98(%rbp), %rax
jge 0x333732
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rcx
movq -0x90(%rbp), %rdx
movabsq $0x80000000, %rax       # imm = 0x80000000
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
je 0x3334b4
jmp 0x33371b
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rcx
xorl %eax, %eax
cmpq -0x98(%rbp), %rcx
movb %al, -0xd9(%rbp)
jge 0x33351e
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rax
movq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq -0x18(%rbp), %rcx
movq 0xc0(%rcx), %rcx
movq -0x88(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
sete %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x33352d
jmp 0x333719
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rcx
movq -0x88(%rbp), %rdx
movabsq $0x80000000, %rax       # imm = 0x80000000
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
jne 0x333558
jmp 0x333702
jmp 0x33355a
movq 0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jne 0x3335c9
movq 0x18(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x33358b
movq 0x18(%rbp), %rax
movq 0x38(%rax), %rax
shlq %rax
movq %rax, -0xe8(%rbp)
jmp 0x333599
movl $0x2, %eax
movq %rax, -0xe8(%rbp)
jmp 0x333599
movq -0xe8(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq 0x18(%rbp), %rax
movq 0x40(%rax), %rdi
movq 0x18(%rbp), %rax
imulq $0x18, 0x38(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq 0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x18(%rbp), %rdx
movq 0x30(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x30(%rdx)
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rax
movq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rax
movq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movl %eax, %ecx
movq -0x58(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0xc0(%rax), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shlq $0x21, %rax
shrq $0x21, %rax
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x333696
movq -0xb0(%rbp), %rax
movq -0x58(%rbp), %rcx
subq 0x10(%rcx), %rax
subq $0x1, %rax
movq %rax, -0x100(%rbp)
jmp 0x3336a5
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x100(%rbp)
movq -0xf8(%rbp), %rax
movq -0xf0(%rbp), %rcx
movq -0x100(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %edi
movq -0x50(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %esi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x50(%rbp), %rax
movl (%rax), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdx
callq 0x327090
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x3334c6
jmp 0x33371b
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x333475
movq 0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x98(%rbp)
movq 0x18(%rbp), %rax
movq 0x40(%rax), %rdi
movq 0x18(%rbp), %rax
movq 0x40(%rax), %rsi
movq 0x18(%rbp), %rax
imulq $0x18, 0x30(%rax), %rax
addq %rax, %rsi
callq 0x310150
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq 0x18(%rbp), %rax
movq 0x40(%rax), %rdx
movq 0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq 0x18(%rbp), %r8
leaq -0x80(%rbp), %r9
callq 0x332980
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x33379a
jmp 0x3337b9
leaq 0x672b0(%rip), %rdi     # 0x39aa51
leaq 0x62a11(%rip), %rsi     # 0x3961b9
movl $0x17b3, %edx           # imm = 0x17B3
leaq 0x6459f(%rip), %rcx     # 0x397d53
callq 0x2130
cmpq $0x0, -0x88(%rbp)
jne 0x3337c8
jmp 0x333932
movl -0x7c(%rbp), %eax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq 0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x3337e2
jmp 0x333801
leaq 0x645f1(%rip), %rdi     # 0x397dda
leaq 0x629c9(%rip), %rsi     # 0x3961b9
movl $0x17b4, %edx           # imm = 0x17B4
leaq 0x64557(%rip), %rcx     # 0x397d53
callq 0x2130
movq -0x20(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x138(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x128(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0x120(%rbp)
movq 0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x118(%rbp)
movq 0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x110(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x3338a2
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x40(%rbp), %rax
movq %rax, -0x158(%rbp)
jmp 0x3338ad
movq -0x38(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x108(%rbp), %r10
movq -0x110(%rbp), %r11
movq -0x118(%rbp), %rbx
movq -0x120(%rbp), %r14
movq -0x128(%rbp), %r9
movq -0x130(%rbp), %r8
movq -0x138(%rbp), %rcx
movq -0x140(%rbp), %rdx
movq -0x148(%rbp), %rsi
movq -0x150(%rbp), %rdi
movq -0x158(%rbp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x330cf0
movq -0x18(%rbp), %rdi
movq 0x28(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rdx
callq 0x3320a0
movl %eax, %ecx
movq 0x30(%rbp), %rax
movl %ecx, (%rax)
addq $0x170, %rsp            # imm = 0x170
popq %rbx
popq %r14
popq %rbp
retq
nop