// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: integer_chain(unsigned int, integer_aln_t*, long, long, integer_t*, ma_ug_t*, ul_str_idx_t*, all_ul_t*, ul_chain_t*)
uint32_t integer_chain(uint32_t qid, integer_aln_t *a, int64_t a_n, int64_t offset, integer_t *buf, 
ma_ug_t *ug, ul_str_idx_t *str_idx, all_ul_t *ul_idx, 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;
    ///already sorted by qe
    int64_t i, k, max_f, max_k, sc, csc, *p, *f, tf, ti/**, is_circle**/;
    integer_aln_t *li, *lk; uint32_t tid = a[0].tn_rev_qk>>33; uint32_t is_rev = (a[0].tn_rev_qk>>32)&1;
    for (i = 1, sc = 0; i < a_n; ++i) {
        sc += a[i].sc;
        if(a[i].tk <= a[i-1].tk) break;///== means there is a circle
        if(((uint32_t)a[i].tn_rev_qk) <= ((uint32_t)a[i-1].tn_rev_qk)) break;
        if(str_idx && ul_idx && (!dis_check_integer_aln_t(ul_idx, str_idx, 
                                    ug, &(a[i]), &(a[i-1]), qid, tid, is_rev, 0.08, 2000))) {
            break;
        }
    }
    // if(tid == 269 || tid == 276 || tid == 277 || tid == 278) fprintf(stderr, "tid->%u, i->%ld, an->%ld\n", tid, i, a_n);
    if(i >= a_n) {//the whole chain is co-inear
        sc += a[0].sc; 
        res->v = a[0].tn_rev_qk>>32; res->s = offset; res->e = offset + a_n; res->sc = sc; 
        return 1;
    }
    // is_circle = 1;
    // if(str_idx->str.a[qid].is_cir == 0 && str_idx->str.a[tid].is_cir == 0) is_circle = 0;

    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;
    for (i = 0; i < a_n; ++i) {
        li = &(a[i]); csc = a[i].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(lk->tk >= li->tk || ((uint32_t)lk->tn_rev_qk) >= ((uint32_t)li->tn_rev_qk)) continue;
            // if(is_circle && (!dis_check_integer_aln_t(ul_idx, str_idx, ug, li, lk, qid, tid, is_rev, 0.08))) continue;
            if(str_idx && ul_idx && (!dis_check_integer_aln_t(ul_idx, str_idx, 
                                                    ug, li, lk, qid, tid, is_rev, 0.08, 2000))) {
                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;
        }
    }

    if(ti < 0) return 0;
    
    for (i = ti, k = 0; i >= 0; i = p[i]) f[k++] = i;
    assert(k > 0);
    for (i = sc = 0, k--; k >= 0; k--, i++) {
        a[i] = a[f[k]]; sc += a[i].sc;
        // if(tid == 269) {
        //     fprintf(stderr, "[%ld] qk->%u, tk->%u\n", i, (uint32_t)a[i].tn_rev_qk, a[i].tk);
        // }
    }
    res->v = a[0].tn_rev_qk>>32; res->s = offset; res->e = offset + i; res->sc = sc;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x58(%rsp)
movq 0xc0(%rsp), %r12
orq $-0x1, (%r12)
orl $-0x1, 0x8(%r12)
orq $-0x1, 0x20(%r12)
movl %edi, 0x1c(%rsp)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x10(%r12)
testq %rdx, %rdx
jle 0x1b3ad5
movq %rcx, 0x40(%rsp)
movq %r8, %rbp
movq 0x10(%rsi), %rax
movq %rax, %rcx
shrq $0x21, %rcx
movq %rcx, 0x48(%rsp)
shrq $0x20, %rax
andl $0x1, %eax
movq %rax, 0x50(%rsp)
cmpq $0x0, 0xb0(%rsp)
setne %al
cmpq $0x0, 0xb8(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0xf(%rsp)
movq %rsi, 0x20(%rsp)
leaq 0x18(%rsi), %rbx
pushq $0x1
popq %r14
xorl %r13d, %r13d
movq %rdx, 0x28(%rsp)
cmpq %r14, %rdx
je 0x1b3b3d
movl (%rbx), %eax
cmpl -0x18(%rbx), %eax
jbe 0x1b38b3
movl 0x10(%rbx), %eax
cmpl -0x8(%rbx), %eax
jbe 0x1b38b3
movl 0x8(%rbx), %r15d
cmpb $0x0, 0xf(%rsp)
je 0x1b38a2
leaq -0x18(%rbx), %r8
subq $0x8, %rsp
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq %rbx, %rcx
movl 0x24(%rsp), %r9d
movq 0x2fcd0(%rip), %xmm0    # 0x1e3558
pushq $0x7d0                  # imm = 0x7D0
pushq 0x60(%rsp)
pushq 0x60(%rsp)
callq 0x1b3580
addq $0x20, %rsp
testl %eax, %eax
je 0x1b38b3
addq %r15, %r13
incq %r14
addq $0x18, %rbx
movq 0x28(%rsp), %rdx
jmp 0x1b3838
andq $0x0, 0x48(%rbp)
andq $0x0, 0x60(%rbp)
movq 0x28(%rsp), %rbx
cmpq %rbx, 0x68(%rbp)
jae 0x1b3921
leaq -0x1(%rbx), %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, 0x30(%rsp)
movq %rax, 0x70(%rbp)
jmp 0x1b392a
movq 0x70(%rbp), %rax
movq %rax, 0x30(%rsp)
movq 0x20(%rsp), %r14
cmpq %rbx, 0x50(%rbp)
jae 0x1b398e
leaq -0x1(%rbx), %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, 0x10(%rsp)
movq %rax, 0x58(%rbp)
jmp 0x1b3997
movq 0x58(%rbp), %rax
movq %rax, 0x10(%rsp)
leaq -0x18(%r14), %rax
movq %rax, 0x68(%rsp)
pushq $-0x1
popq %rbp
xorl %r12d, %r12d
movq %rbp, %rcx
cmpq %rbx, %r12
je 0x1b3ab2
movq %rcx, 0x70(%rsp)
imulq $0x18, %r12, %rax
leaq (%r14,%rax), %rbx
movl 0x8(%r14,%rax), %r14d
pushq $-0x1
popq %rax
movq %rax, 0x38(%rsp)
movq %r14, 0x60(%rsp)
movq %r12, %r15
imulq $0x18, %r15, %r13
addq 0x68(%rsp), %r13
testq %r15, %r15
jle 0x1b3a79
movl (%r13), %eax
cmpl (%rbx), %eax
jae 0x1b3a44
movl 0x10(%r13), %eax
cmpl 0x10(%rbx), %eax
jae 0x1b3a44
cmpb $0x0, 0xf(%rsp)
je 0x1b3a4d
subq $0x8, %rsp
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq %rbx, %rcx
movq %r13, %r8
movl 0x24(%rsp), %r9d
movq 0x2fb2e(%rip), %xmm0    # 0x1e3558
pushq $0x7d0                  # imm = 0x7D0
pushq 0x60(%rsp)
pushq 0x60(%rsp)
callq 0x1b3580
addq $0x20, %rsp
testl %eax, %eax
jne 0x1b3a4d
addq $-0x18, %r13
decq %r15
jmp 0x1b39dd
movq 0x10(%rsp), %rax
movq -0x8(%rax,%r15,8), %rax
addq 0x60(%rsp), %rax
decq %r15
cmpq %r14, %rax
movq 0x38(%rsp), %rcx
cmovgq %r15, %rcx
movq %rcx, 0x38(%rsp)
cmovgq %rax, %r14
jmp 0x1b39d4
movq 0x10(%rsp), %rax
movq %r14, (%rax,%r12,8)
movq 0x70(%rsp), %rcx
cmpq %r14, %rcx
cmovleq %r14, %rcx
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdx
movq %rdx, (%rax,%r12,8)
cmovlq %r12, %rbp
incq %r12
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r14
jmp 0x1b39a9
testq %rbp, %rbp
js 0x1b3ad5
xorl %edx, %edx
testq %rbp, %rbp
js 0x1b3adc
movq 0x10(%rsp), %rax
movq %rbp, (%rax,%rdx,8)
incq %rdx
movq 0x30(%rsp), %rax
movq (%rax,%rbp,8), %rbp
jmp 0x1b3ab9
xorl %eax, %eax
jmp 0x1b3b69
testq %rdx, %rdx
je 0x1b3b78
leaq 0x8(%r14), %rax
xorl %r13d, %r13d
movq %rdx, %rcx
movq 0xc0(%rsp), %r12
movq 0x40(%rsp), %rdi
subq $0x1, %rcx
jb 0x1b3b2b
movq 0x10(%rsp), %rsi
imulq $0x18, (%rsi,%rcx,8), %rsi
movdqu (%r14,%rsi), %xmm0
movdqu %xmm0, -0x8(%rax)
movq 0x10(%r14,%rsi), %rsi
movq %rsi, 0x8(%rax)
movl (%rax), %esi
addq %rsi, %r13
addq $0x18, %rax
jmp 0x1b3afc
movl 0x14(%r14), %eax
movl %eax, 0x8(%r12)
movl %edi, (%r12)
addq %rdi, %rdx
jmp 0x1b3b5c
movq 0x20(%rsp), %rcx
movl 0x8(%rcx), %eax
movl 0x14(%rcx), %ecx
addq %rax, %r13
movl %ecx, 0x8(%r12)
movq 0x40(%rsp), %rax
movl %eax, (%r12)
addq %rax, %rdx
movl %edx, 0x4(%r12)
movq %r13, 0x10(%r12)
pushq $0x1
popq %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x44701(%rip), %rdi     # 0x1f8280
leaq 0x43953(%rip), %rsi     # 0x1f74d9
leaq 0x446f9(%rip), %rcx     # 0x1f8286
movl $0x11d4, %edx           # imm = 0x11D4
callq 0x2110