// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%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 0x1f89a9
movq %r9, %r12
movq %rcx, %r13
movq $0x0, 0xa8(%r8)
cmpq $0x1, %rcx
setne %bl
je 0x1f8995
movq %r8, 0x8(%rsp)
movl 0x28(%rdx), %eax
cmpl 0x10(%rdx), %eax
movq %rdx, 0x10(%rsp)
jbe 0x1f8721
leaq 0x18(%rdx), %r15
movq $-0x1, %rsi
movl $0x2, %r14d
movl -0x18(%r15), %ecx
movl (%r15), %edx
cmpl %ecx, %edx
je 0x1f8721
cmpl %ecx, %edx
jae 0x1f8701
leaq -0x1(%r14), %rbp
xorl %r8d, %r8d
cmpq %rsi, %rbp
setne %r8b
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %r9
callq 0x1f83ce
movq %rbp, %rsi
testq %rax, %rax
jne 0x1f8721
cmpq %r14, %r13
je 0x1f8995
setg %bl
movl 0x28(%r15), %eax
leaq 0x18(%r15), %rcx
incq %r14
cmpl 0x10(%r15), %eax
movq %rcx, %r15
ja 0x1f86ca
testb $0x1, %bl
je 0x1f8995
xorl %eax, %eax
movq 0x8(%rsp), %r14
movq %rax, 0x48(%r14)
movq %rax, 0x60(%r14)
cmpq %r13, 0x68(%r14)
jae 0x1f8791
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(%r14)
movq 0x70(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x70(%r14)
movq %r12, 0x20(%rsp)
movq 0x70(%r14), %r15
cmpq %r13, 0x50(%r14)
movq 0x10(%rsp), %rbx
jae 0x1f87f7
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(%r14)
movq 0x58(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%r14)
movq 0x58(%r14), %rdi
movq $0x0, 0xa8(%r14)
cmpq $0x2, %r13
movl $0x1, %eax
cmovgeq %r13, %rax
movq %rax, 0x28(%rsp)
leaq -0x8(%rbx), %r14
movq $-0x1, %rbp
xorl %r9d, %r9d
movq $-0x1, %rsi
movq $-0x1, %rax
movq $-0x1, %rcx
movq %r15, 0x30(%rsp)
movq %rdi, 0x58(%rsp)
movq %rax, 0x38(%rsp)
movq %rcx, 0x50(%rsp)
leaq (%r9,%r9,2), %rax
movl 0x8(%rbx,%rax,8), %r12d
testq %r9, %r9
movq %r14, 0x48(%rsp)
movq %rbp, 0x40(%rsp)
je 0x1f88f4
leaq (%rbx,%rax,8), %r15
movq $-0x1, %r8
movq %r12, %r13
movl (%r14), %eax
cmpl 0x10(%r15), %eax
jae 0x1f88de
movl -0x10(%r14), %ecx
movl (%r15), %edx
cmpl %edx, %ecx
je 0x1f88de
cmpl %edx, %ecx
jbe 0x1f88cc
movq %r8, 0x60(%rsp)
xorl %r8d, %r8d
cmpq %r9, %rsi
setne %r8b
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r9, %rbx
callq 0x1f83ce
movq %rbx, %r9
movq 0x60(%rsp), %r8
movq 0x58(%rsp), %rdi
movq %rbx, %rsi
testq %rax, %rax
je 0x1f88cc
movq %r9, %rsi
jmp 0x1f88de
movq (%rdi,%rbp,8), %rax
addq %r12, %rax
cmpq %r13, %rax
cmovgq %rax, %r13
cmovgq %rbp, %r8
leaq -0x1(%rbp), %rax
incq %rbp
addq $-0x18, %r14
cmpq $0x1, %rbp
movq %rax, %rbp
jg 0x1f8879
jmp 0x1f88fe
movq %r12, %r13
movq $-0x1, %r8
movq %r13, (%rdi,%r9,8)
movq 0x30(%rsp), %r15
movq %r8, (%r15,%r9,8)
movq 0x38(%rsp), %rax
cmpq %r13, %rax
movq 0x50(%rsp), %rcx
cmovlq %r9, %rcx
cmovleq %r13, %rax
incq %r9
movq 0x40(%rsp), %rbp
incq %rbp
movq 0x48(%rsp), %r14
addq $0x18, %r14
cmpq 0x28(%rsp), %r9
movq 0x10(%rsp), %rbx
jne 0x1f8845
testq %rcx, %rcx
js 0x1f89a9
movl $0x1, %eax
movq %rcx, -0x8(%rdi,%rax,8)
movq (%r15,%rcx,8), %rcx
incq %rax
testq %rcx, %rcx
jns 0x1f894e
xorl %r13d, %r13d
movq %rbx, %rcx
movq 0x20(%rsp), %r12
movq -0x10(%rdi,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movq 0x10(%rbx,%rdx,8), %rsi
movq %rsi, 0x10(%rcx)
movdqu (%rbx,%rdx,8), %xmm0
movdqu %xmm0, (%rcx)
incq %r13
addq $0x18, %rcx
decq %rax
cmpq $0x1, %rax
jg 0x1f896a
movl $0x0, (%r12)
movl %r13d, 0x4(%r12)
movl $0x1, %eax
jmp 0x1f89ab
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq