// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: integer_g_chain(poa_g_t*, ma_ug_t*, integer_aln_t*, long, integer_t*, ul_chain_t*)
int64_t integer_g_chain(poa_g_t *g, ma_ug_t *ug, integer_aln_t *a, int64_t a_n, integer_t *buf, ul_chain_t *res)
{
    res->v = res->s = res->e = (uint32_t)-1; res->sc = (uint64_t)-1; 
    res->q_sidx = res->q_eidx = res->t_sidx = res->t_eidx = (uint32_t)-1;
    if(a_n <= 0) return 0;
    int64_t i, k, *p, *f, tf, ti, csc, sc, max_f, max_k, vis_i, pas; integer_aln_t *li, *lk; 
    buf->vis.n = 0; vis_i = -1;
    for (i = 1; i < a_n; ++i) {//already sorted by qk
        if(((uint32_t)a[i].tn_rev_qk) <= ((uint32_t)a[i-1].tn_rev_qk)) break; ///== means there is a circle
        if(a[i].tk == a[i-1].tk) break;
        if(a[i].tk < a[i-1].tk) {
            pas = suffix_gorder_check(g, buf, a[i].tk, a[i-1].tk, vis_i==i?0:1, i); vis_i = i;
            if(pas) break;
        }
    }
    // fprintf(stderr, "[M::%s::] i::%ld, a_n::%ld\n", __func__, i, a_n);
    
    if(i >= a_n) {
        res->s = 0; res->e = a_n;
        return 1;
    }

    buf->p.n = buf->f.n = 0; 
    kv_resize(int64_t, buf->p, (uint64_t)a_n); p = buf->p.a;
    kv_resize(int64_t, buf->f, (uint64_t)a_n); f = buf->f.a;
    
    tf = ti = -1; buf->vis.n = 0; vis_i = -1;
    for (i = 0; i < a_n; ++i) {
        li = &(a[i]); csc = li->sc;
        max_f = csc; max_k = -1;
        
        for (k = i-1; k >= 0; --k) {
            lk = &(a[k]);
            ///qk of lk and li might be equal
            if(((uint32_t)lk->tn_rev_qk) >= ((uint32_t)li->tn_rev_qk)) continue;
            if(lk->tk == li->tk) continue;
            if(lk->tk > li->tk) {
                pas = suffix_gorder_check(g, buf, a[i].tk, a[k].tk, vis_i==i?0:1, i);
                // if(i == 17 && a[i].tk == 6 && k == 16 && a[k].tk == 60) {
                //     fprintf(stderr, "******i::%ld, a[i].tk::%u, k::%ld, a[k].tk::%u, vis_i::%ld, pas::%ld\n", 
                //         i, a[i].tk, k, a[k].tk, vis_i, pas);
                // }
                vis_i = i; if(pas) continue;
            }
            sc = csc + f[k];
            if(sc > max_f) {
                max_f = sc; max_k = k;
            }
        }
        f[i] = max_f; p[i] = max_k;
        if(tf < max_f) {
            tf = max_f; ti = i;
        }
        // fprintf(stderr, "[M::%s::i->%ld] qk::%u, tk::%u, max_k::%ld, max_f::%ld, ti::%ld\n", 
        // __func__, i, (uint32_t)li->tn_rev_qk, li->tk, max_k, max_f, ti);
    }

    if(ti < 0) return 0;
    for (i = ti, k = 0; i >= 0; i = p[i]) f[k++] = i;
    assert(k > 0);
    for (i = 0, k--; k >= 0; k--) {
        a[i] = a[f[k]]; i++;
    }
    res->s = 0; res->e = i;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
orq $-0x1, (%r9)
orl $-0x1, 0x8(%r9)
orq $-0x1, 0x20(%r9)
movq %rdi, 0x28(%rsp)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x10(%r9)
testq %rcx, %rcx
jle 0x1b814d
movq %r9, %r15
movq %r8, %r12
movq %rcx, %r8
andq $0x0, 0xa8(%r12)
leaq 0x28(%rdx), %r14
pushq $0x1
popq %rbx
pushq $-0x1
popq %rsi
movq %rcx, (%rsp)
movq %rdx, 0x8(%rsp)
cmpq %rbx, %r8
je 0x1b8184
movl (%r14), %eax
cmpl -0x18(%r14), %eax
jbe 0x1b7f12
movl -0x28(%r14), %ecx
movl -0x10(%r14), %edx
cmpl %ecx, %edx
je 0x1b7f12
cmpl %ecx, %edx
jae 0x1b7f09
xorl %r8d, %r8d
cmpq %rsi, %rbx
setne %r8b
movq 0x28(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %r9
callq 0x1b7c23
movq (%rsp), %r8
movq %rbx, %rsi
testq %rax, %rax
jne 0x1b7f12
incq %rbx
addq $0x18, %r14
jmp 0x1b7ec1
andq $0x0, 0x48(%r12)
andq $0x0, 0x60(%r12)
cmpq %r8, 0x68(%r12)
jae 0x1b7f83
leaq -0x1(%r8), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x68(%r12)
movq 0x70(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq (%rsp), %r8
movq %rax, %r9
movq %rax, 0x70(%r12)
jmp 0x1b7f88
movq 0x70(%r12), %r9
cmpq %r8, 0x50(%r12)
movq %r9, 0x20(%rsp)
movq %r15, 0x30(%rsp)
jae 0x1b7ffc
leaq -0x1(%r8), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x50(%r12)
movq 0x58(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x20(%rsp), %r9
movq (%rsp), %r8
movq %rax, %r10
movq %rax, 0x58(%r12)
jmp 0x1b8001
movq 0x58(%r12), %r10
movq 0x8(%rsp), %rdi
movq %r12, 0x40(%rsp)
andq $0x0, 0xa8(%r12)
leaq -0x8(%rdi), %r12
pushq $-0x1
popq %rbp
xorl %r15d, %r15d
movq %rbp, %r11
movq %rbp, %rax
movq %rbp, %rsi
movq %r12, 0x38(%rsp)
cmpq %r8, %r15
je 0x1b8133
movq %rax, 0x50(%rsp)
imulq $0x18, %r15, %rax
leaq (%rdi,%rax), %rbx
movl 0x8(%rdi,%rax), %eax
movq %r15, %r13
movq %rax, 0x48(%rsp)
movq %rax, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
imulq $0x18, %r13, %rax
addq %r12, %rax
movq %r13, %r14
testq %r14, %r14
jle 0x1b8109
movl (%rax), %ecx
cmpl 0x10(%rbx), %ecx
jae 0x1b807b
movl -0x10(%rax), %ecx
movl (%rbx), %edx
cmpl %edx, %ecx
jne 0x1b8084
addq $-0x18, %rax
decq %r14
jmp 0x1b8062
cmpl %edx, %ecx
jbe 0x1b80d5
leaq -0x1(%r14), %r13
xorl %r8d, %r8d
cmpq %r15, %rsi
setne %r8b
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq %r15, %r9
movq %r10, %r12
movq %r11, %rbp
callq 0x1b7c23
movq %rbp, %r11
pushq $-0x1
popq %rbp
movq %r12, %r10
movq 0x38(%rsp), %r12
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %rdi
movq (%rsp), %r8
movq %r15, %rsi
testq %rax, %rax
jne 0x1b8058
movq %r15, %rsi
movq -0x8(%r10,%r14,8), %rax
addq 0x48(%rsp), %rax
decq %r14
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
cmovgq %rax, %rcx
movq %rcx, 0x18(%rsp)
movq 0x10(%rsp), %rax
cmovgq %r14, %rax
movq %rax, 0x10(%rsp)
movq %r14, %r13
jmp 0x1b8058
movq 0x18(%rsp), %rcx
movq %rcx, (%r10,%r15,8)
movq 0x50(%rsp), %rax
cmpq %rcx, %rax
cmovlq %r15, %r11
movq 0x10(%rsp), %rdx
movq %rdx, (%r9,%r15,8)
cmovleq %rcx, %rax
incq %r15
jmp 0x1b802c
testq %r11, %r11
js 0x1b814d
xorl %r8d, %r8d
testq %r11, %r11
js 0x1b8151
movq %r11, (%r10,%r8,8)
incq %r8
movq (%r9,%r11,8), %r11
jmp 0x1b813b
xorl %eax, %eax
jmp 0x1b818f
testq %r8, %r8
je 0x1b819e
movq %rdi, %rax
movq %r8, %rcx
movq 0x30(%rsp), %r15
subq $0x1, %rcx
jb 0x1b8184
imulq $0x18, (%r10,%rcx,8), %rdx
movq 0x10(%rdi,%rdx), %rsi
movq %rsi, 0x10(%rax)
movdqu (%rdi,%rdx), %xmm0
movdqu %xmm0, (%rax)
addq $0x18, %rax
jmp 0x1b8161
andl $0x0, (%r15)
movl %r8d, 0x4(%r15)
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x400db(%rip), %rdi     # 0x1f8280
leaq 0x3f32d(%rip), %rsi     # 0x1f74d9
leaq 0x40e0c(%rip), %rcx     # 0x1f8fbf
movl $0x177d, %edx           # imm = 0x177D
callq 0x2110