// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq 0x20(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq 0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x20(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movq 0x20(%rbp), %rax
movq $-0x1, 0x10(%rax)
movq 0x20(%rbp), %rax
movl $0xffffffff, 0x24(%rax) # imm = 0xFFFFFFFF
movq 0x20(%rbp), %rax
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movq 0x20(%rbp), %rax
movl $0xffffffff, 0x1c(%rax) # imm = 0xFFFFFFFF
movq 0x20(%rbp), %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
cmpq $0x0, -0x18(%rbp)
jg 0x3294a9
movl $0x0, -0x4(%rbp)
jmp 0x329b1e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
shrq $0x21, %rax
movl %eax, -0x94(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
shrq $0x20, %rax
andq $0x1, %rax
movl %eax, -0x98(%rbp)
movq $0x1, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x3295e2
movq -0x10(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
movq -0x38(%rbp), %rdx
subq $0x1, %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
ja 0x329530
jmp 0x3295e2
movq -0x10(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movq -0x38(%rbp), %rdx
subq $0x1, %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
cmpl %ecx, %eax
ja 0x329560
jmp 0x3295e2
cmpq $0x0, 0x10(%rbp)
je 0x3295cf
cmpq $0x0, 0x18(%rbp)
je 0x3295cf
movq 0x18(%rbp), %rdi
movq 0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rcx
imulq $0x18, -0x38(%rbp), %rax
addq %rax, %rcx
movq -0x10(%rbp), %r8
movq -0x38(%rbp), %rax
subq $0x1, %rax
imulq $0x18, %rax, %rax
addq %rax, %r8
movl -0x8(%rbp), %r9d
movl -0x94(%rbp), %r10d
movl -0x98(%rbp), %eax
movsd 0x58cde(%rip), %xmm0    # 0x382290
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movq $0x7d0, 0x10(%rsp)      # imm = 0x7D0
callq 0x328ff0
cmpl $0x0, %eax
jne 0x3295cf
jmp 0x3295e2
jmp 0x3295d1
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x3294e1
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jl 0x329645
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
shrq $0x20, %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
addq -0x18(%rbp), %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x58(%rbp), %rcx
movq 0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x329b1e
movq -0x28(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rax
cmpq -0x18(%rbp), %rax
jae 0x329736
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rcx
shrq %rcx
movq -0x28(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rcx
shrq $0x2, %rcx
movq -0x28(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rcx
shrq $0x4, %rcx
movq -0x28(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rcx
shrq $0x8, %rcx
movq -0x28(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rcx
shrq $0x10, %rcx
movq -0x28(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x70(%rax)
jmp 0x329738
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rax
cmpq -0x18(%rbp), %rax
jae 0x32981d
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rcx
shrq %rcx
movq -0x28(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x2, %rcx
movq -0x28(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x4, %rcx
movq -0x28(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x8, %rcx
movq -0x28(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rcx
shrq $0x10, %rcx
movq -0x28(%rbp), %rax
orq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x58(%rax)
jmp 0x32981f
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x70(%rbp)
movq $-0x1, -0x80(%rbp)
movq $-0x1, -0x78(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x3299d0
movq -0x10(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $-0x1, -0x50(%rbp)
movq -0x38(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jl 0x329983
movq -0x10(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movl (%rax), %eax
movq -0x88(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x3298df
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x88(%rbp), %rcx
movq 0x10(%rcx), %rcx
cmpl %ecx, %eax
jb 0x3298e4
jmp 0x329972
cmpq $0x0, 0x10(%rbp)
je 0x329942
cmpq $0x0, 0x18(%rbp)
je 0x329942
movq 0x18(%rbp), %rdi
movq 0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x88(%rbp), %rcx
movq -0x90(%rbp), %r8
movl -0x8(%rbp), %r9d
movl -0x94(%rbp), %r10d
movl -0x98(%rbp), %eax
movsd 0x5896b(%rip), %xmm0    # 0x382290
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movq $0x7d0, 0x10(%rsp)      # imm = 0x7D0
callq 0x328ff0
cmpl $0x0, %eax
jne 0x329942
jmp 0x329972
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
movq -0x40(%rbp), %rdx
addq (%rcx,%rdx,8), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
jle 0x329970
movq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x329972
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x329893
movq -0x48(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rdx
movq -0x68(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x78(%rbp), %rax
cmpq -0x48(%rbp), %rax
jge 0x3299bd
movq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x3299bf
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x329843
cmpq $0x0, -0x80(%rbp)
jge 0x3299e3
movl $0x0, -0x4(%rbp)
jmp 0x329b1e
movq -0x80(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
cmpq $0x0, -0x38(%rbp)
jl 0x329a27
movq -0x38(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x40(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x68(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
jmp 0x3299f3
cmpq $0x0, -0x40(%rbp)
jle 0x329a30
jmp 0x329a4f
leaq 0x6d529(%rip), %rdi     # 0x396f60
leaq 0x6c77b(%rip), %rsi     # 0x3961b9
movl $0x11d4, %edx           # imm = 0x11D4
leaq 0x6d51c(%rip), %rcx     # 0x396f66
callq 0x2130
movq $0x0, -0x58(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jl 0x329ad9
movq -0x10(%rbp), %rcx
movq -0x70(%rbp), %rax
movq -0x40(%rbp), %rdx
imulq $0x18, (%rax,%rdx,8), %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x329a6b
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
shrq $0x20, %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
addq -0x38(%rbp), %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x58(%rbp), %rcx
movq 0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)