// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movl %edi, 0xc(%rsp)
movq 0xc0(%rsp), %r13
movq $-0x1, %rax
movq %rax, (%r13)
movl $0xffffffff, 0x8(%r13)  # imm = 0xFFFFFFFF
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x10(%r13)
movq %rax, 0x20(%r13)
testq %rdx, %rdx
jle 0x1ffc8d
movq 0x10(%rsi), %r11
movq %r11, %rbp
shrq $0x20, %rbp
cmpq $0x1, %rdx
jne 0x1ff8df
xorl %r14d, %r14d
jmp 0x1ff9b8
movq %rcx, 0x28(%rsp)
movq %r8, 0x20(%rsp)
shrq $0x21, %r11
andl $0x1, %ebp
cmpq $0x0, 0xb0(%rsp)
setne %al
cmpq $0x0, 0xb8(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0xb(%rsp)
movq %rsi, 0x10(%rsp)
leaq 0x18(%rsi), %rbx
movl $0x1, %r15d
xorl %r14d, %r14d
movq %rdx, 0x18(%rsp)
movl (%rbx), %eax
cmpl -0x18(%rbx), %eax
jbe 0x1ff9ce
movl 0x10(%rbx), %eax
cmpl -0x8(%rbx), %eax
jbe 0x1ff9ce
movl 0x8(%rbx), %r12d
cmpb $0x0, 0xb(%rsp)
je 0x1ff98f
leaq -0x18(%rbx), %r8
subq $0x8, %rsp
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rbx, %rcx
movl 0x14(%rsp), %r9d
movq 0x31c94(%rip), %xmm0    # 0x231608
pushq $0x7d0                  # imm = 0x7D0
pushq %rbp
pushq %r11
movq %r11, %r13
callq 0x1ff64b
movq %r13, %r11
addq $0x20, %rsp
testl %eax, %eax
je 0x1ff9ce
addq %r12, %r14
incq %r15
addq $0x18, %rbx
movq 0x18(%rsp), %rdx
cmpq %r15, %rdx
jne 0x1ff925
movq 0x10(%rsp), %rsi
movl 0x14(%rsi), %ebp
movq 0x28(%rsp), %rcx
movq 0xc0(%rsp), %r13
movl 0x8(%rsi), %eax
addq %r14, %rax
movl %ebp, 0x8(%r13)
movl %ecx, (%r13)
addq %rcx, %rdx
jmp 0x1ffc7e
xorl %eax, %eax
movq 0x20(%rsp), %r12
movq %rax, 0x48(%r12)
movq %rax, 0x60(%r12)
movq 0x18(%rsp), %rbx
cmpq %rbx, 0x68(%r12)
movq %rbp, 0x70(%rsp)
movq %r11, 0x68(%rsp)
jae 0x1ffa4f
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(%r12)
movq 0x70(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %r15
movq %rax, 0x70(%r12)
jmp 0x1ffa54
movq 0x70(%r12), %r15
movq 0x10(%rsp), %r14
cmpq %rbx, 0x50(%r12)
jae 0x1ffaba
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(%r12)
movq 0x58(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %rbx
movq %rax, 0x58(%r12)
jmp 0x1ffabf
movq 0x58(%r12), %rbx
leaq -0x18(%r14), %rcx
movq $-0x1, %r12
xorl %edx, %edx
movq $-0x1, %rax
movq $-0x1, %rsi
movq %r15, 0x38(%rsp)
movq %rsi, 0x40(%rsp)
movq %rax, 0x60(%rsp)
leaq (%rdx,%rdx,2), %rax
movl 0x8(%r14,%rax,8), %r15d
movq %rdx, 0x48(%rsp)
testq %rdx, %rdx
movq %rcx, 0x58(%rsp)
movq %r12, 0x50(%rsp)
je 0x1ffbb8
leaq (%r14,%rax,8), %r13
movq $-0x1, %rdi
movq %rcx, %r14
movq %r15, %rbp
movl (%r14), %eax
cmpl (%r13), %eax
jae 0x1ffb9d
movl 0x10(%r14), %eax
cmpl 0x10(%r13), %eax
jae 0x1ffb9d
cmpb $0x0, 0xb(%rsp)
je 0x1ffb8b
subq $0x8, %rsp
movq %rdi, 0x28(%rsp)
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r13, %rcx
movq %r14, %r8
movl 0x14(%rsp), %r9d
movq 0x31aa2(%rip), %xmm0    # 0x231608
pushq $0x7d0                  # imm = 0x7D0
pushq 0x80(%rsp)
pushq 0x80(%rsp)
callq 0x1ff64b
movq 0x40(%rsp), %rdi
addq $0x20, %rsp
testl %eax, %eax
je 0x1ffb9d
movq (%rbx,%r12,8), %rax
addq %r15, %rax
cmpq %rbp, %rax
cmovgq %r12, %rdi
cmovgq %rax, %rbp
leaq -0x1(%r12), %rax
incq %r12
addq $-0x18, %r14
cmpq $0x1, %r12
movq %rax, %r12
jg 0x1ffb1b
jmp 0x1ffbc2
movq $-0x1, %rdi
movq %r15, %rbp
movq 0x48(%rsp), %rdx
movq %rbp, (%rbx,%rdx,8)
movq 0x38(%rsp), %r15
movq %rdi, (%r15,%rdx,8)
movq 0x40(%rsp), %rsi
cmpq %rbp, %rsi
cmovleq %rbp, %rsi
movq 0x60(%rsp), %rax
cmovlq %rdx, %rax
incq %rdx
movq 0x50(%rsp), %r12
incq %r12
movq 0x58(%rsp), %rcx
addq $0x18, %rcx
cmpq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r14
jne 0x1ffadf
testq %rax, %rax
js 0x1ffc8d
movl $0x1, %ecx
movq %rax, -0x8(%rbx,%rcx,8)
movq (%r15,%rax,8), %rax
incq %rcx
testq %rax, %rax
jns 0x1ffc17
leaq 0x8(%r14), %r8
xorl %eax, %eax
movq 0x28(%rsp), %rdi
movq %rdi, %rdx
movq 0xc0(%rsp), %r13
movq -0x10(%rbx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
movdqu (%r14,%rsi,8), %xmm0
movdqu %xmm0, -0x8(%r8)
movq 0x10(%r14,%rsi,8), %rsi
movq %rsi, 0x8(%r8)
movl (%r8), %esi
addq %rsi, %rax
addq $0x18, %r8
incq %rdx
decq %rcx
cmpq $0x1, %rcx
jg 0x1ffc3e
movl 0x14(%r14), %ecx
movl %ecx, 0x8(%r13)
movl %edi, (%r13)
movl %edx, 0x4(%r13)
movq %rax, 0x10(%r13)
movl $0x1, %eax
jmp 0x1ffc8f
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq