// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, 0x8(%rsp)
movq $-0x1, %rax
movq %rax, (%r9)
movl $0xffffffff, 0x8(%r9)   # imm = 0xFFFFFFFF
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x10(%r9)
movq %rax, 0x20(%r9)
testq %rcx, %rcx
jle 0x203fa0
movq %r9, %r15
movq %r8, %rbp
movq %rcx, %r13
movq $0x0, 0xa8(%r8)
cmpq $0x1, %rcx
je 0x203f8e
movq %rdx, %r12
leaq 0x28(%rdx), %r14
movq $-0x1, %rsi
movl $0x1, %ebx
movl (%r14), %eax
cmpl -0x18(%r14), %eax
jbe 0x203d16
movl -0x28(%r14), %ecx
movl -0x10(%r14), %edx
cmpl %ecx, %edx
je 0x203d16
cmpl %ecx, %edx
jae 0x203d05
xorl %r8d, %r8d
cmpq %rsi, %rbx
setne %r8b
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %r9
callq 0x2039fe
movq %rbx, %rsi
testq %rax, %rax
jne 0x203d16
incq %rbx
addq $0x18, %r14
cmpq %rbx, %r13
jne 0x203cca
jmp 0x203f8e
xorl %eax, %eax
movq %rax, 0x48(%rbp)
movq %rax, 0x60(%rbp)
cmpq %r13, 0x68(%rbp)
jae 0x203d7d
leaq -0x1(%r13), %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(%rbp)
movq 0x70(%rbp), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %r14
movq %rax, 0x70(%rbp)
jmp 0x203d81
movq 0x70(%rbp), %r14
cmpq %r13, 0x50(%rbp)
movq %r15, 0x10(%rsp)
jae 0x203de3
leaq -0x1(%r13), %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(%rbp)
movq 0x58(%rbp), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x58(%rbp)
jmp 0x203de7
movq 0x58(%rbp), %rdi
movq %rbp, 0x58(%rsp)
movq $0x0, 0xa8(%rbp)
leaq -0x8(%r12), %rdx
movq $-0x1, %r8
xorl %ebp, %ebp
movq $-0x1, %rsi
movq $-0x1, %rax
movq $-0x1, %rcx
movq %r12, 0x28(%rsp)
movq %r13, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movq %rdi, 0x50(%rsp)
movq %rax, 0x30(%rsp)
movq %rcx, 0x48(%rsp)
leaq (,%rbp,2), %rax
addq %rbp, %rax
movl 0x8(%r12,%rax,8), %r13d
testq %rbp, %rbp
movq %rdx, 0x40(%rsp)
movq %r8, 0x38(%rsp)
je 0x203ee9
leaq (%r12,%rax,8), %r15
movq $-0x1, %rbx
movq %rdx, %r12
movq %r8, %r14
movq %r13, %r8
movl (%r12), %eax
cmpl 0x10(%r15), %eax
jae 0x203ed3
movl -0x10(%r12), %ecx
movl (%r15), %edx
cmpl %edx, %ecx
je 0x203ed3
cmpl %edx, %ecx
jbe 0x203ec1
movq %r8, 0x60(%rsp)
xorl %r8d, %r8d
cmpq %rbp, %rsi
setne %r8b
movq 0x8(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq %rbp, %r9
callq 0x2039fe
movq 0x60(%rsp), %r8
movq 0x50(%rsp), %rdi
movq %rbp, %rsi
testq %rax, %rax
je 0x203ec1
movq %rbp, %rsi
jmp 0x203ed3
movq (%rdi,%r14,8), %rax
addq %r13, %rax
cmpq %r8, %rax
cmovgq %rax, %r8
cmovgq %r14, %rbx
leaq -0x1(%r14), %rax
incq %r14
addq $-0x18, %r12
cmpq $0x1, %r14
movq %rax, %r14
jg 0x203e6f
jmp 0x203ef3
movq %r13, %r8
movq $-0x1, %rbx
movq %r8, (%rdi,%rbp,8)
movq 0x18(%rsp), %r14
movq %rbx, (%r14,%rbp,8)
movq 0x30(%rsp), %rax
cmpq %r8, %rax
movq 0x48(%rsp), %rcx
cmovlq %rbp, %rcx
cmovleq %r8, %rax
incq %rbp
movq 0x38(%rsp), %r8
incq %r8
movq 0x40(%rsp), %rdx
addq $0x18, %rdx
movq 0x20(%rsp), %r13
cmpq %r13, %rbp
movq 0x28(%rsp), %r12
jne 0x203e2e
testq %rcx, %rcx
js 0x203fa0
movl $0x1, %eax
movq %rcx, -0x8(%rdi,%rax,8)
movq (%r14,%rcx,8), %rcx
incq %rax
testq %rcx, %rcx
jns 0x203f46
xorl %r13d, %r13d
movq %r12, %rcx
movq 0x10(%rsp), %r15
movq -0x10(%rdi,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movq 0x10(%r12,%rdx,8), %rsi
movq %rsi, 0x10(%rcx)
movdqu (%r12,%rdx,8), %xmm0
movdqu %xmm0, (%rcx)
incq %r13
addq $0x18, %rcx
decq %rax
cmpq $0x1, %rax
jg 0x203f62
movl $0x0, (%r15)
movl %r13d, 0x4(%r15)
movl $0x1, %eax
jmp 0x203fa2
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq