// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: gap_chain_check(ma_ug_t*, integer_aln_t*, integer_aln_t*, long, ul_str_t*, ul_str_t*, uc_block_t*, uc_block_t*, long, long, double, long)
int64_t gap_chain_check(ma_ug_t *ug, integer_aln_t *i0, integer_aln_t *i1, int64_t is_rev, ul_str_t *q_str, ul_str_t *t_str, 
uc_block_t *q_block, uc_block_t *t_block, int64_t qid, int64_t tid, double diff_rate, int64_t hard_thres)
{
    int64_t i0_qk, i0_tk, i1_qk, i1_tk, q_near, t_near, lq, lt, min, max, dif; uint32_t e, k;
    i0_qk = (uint32_t)(i0->tn_rev_qk); i1_qk = (uint32_t)(i1->tn_rev_qk); 
    if(is_rev == 0) {
        i0_tk = i0->tk; i1_tk = i1->tk;
    } else {
        i1_tk = t_str->cn - i0->tk - 1; i0_tk = t_str->cn - i1->tk - 1;
    }
    assert(i0_qk < i1_qk); assert(i0_tk < i1_tk);
    q_near = t_near = 0;
    if((i0_qk + 1) == i1_qk) q_near = 1;
    if((i0_tk + 1) == i1_tk) t_near = 1;
    if((q_near + t_near) != 1) return 0;

    i0_qk = q_str->a[i0_qk]>>32; i1_qk = q_str->a[i1_qk]>>32; 
    i0_tk = t_str->a[i0_tk]>>32; i1_tk = t_str->a[i1_tk]>>32; 

    k = i1_qk; e = i0_qk; lq = 0;
    while ((k != (uint32_t)-1) && (k != e)) {
        if(q_block[k].pidx != (uint32_t)-1) lq += q_block[k].pdis;
        k = q_block[k].pidx;
    }
    if(k != e) return 0;
    
    k = i1_tk; e = i0_tk; lt = 0;
    while ((k != (uint32_t)-1) && (k != e)) {
        if(t_block[k].pidx != (uint32_t)-1) lt += t_block[k].pdis;
        k = t_block[k].pidx;
    }
    if(k != e) return 0;
    if(is_rev) {
        lt += (int64_t)ug->g->seq[t_block[i0_tk].hid].len;
        lt -= (int64_t)ug->g->seq[t_block[i1_tk].hid].len;
    }
    if(lt < 0) lt = 0;
    if(lq <= lt) {
        min = lq; max = lt;
    } else {
        min = lt; max = lq;
    }

    dif = max - min;
    if(dif >= (min*diff_rate)) {
        // fprintf(stderr, "+[M::%s] qid::%ld, tid::%ld, i0_qk::%ld, i1_qk::%ld, lq::%ld, lt::%ld\n", 
        // __func__, qid, tid, i0_qk, i1_qk, lq, lt);
        lq = normlize_gdis(ug, &(q_block[i1_qk]), &(q_block[i0_qk]), 0);
        lt = normlize_gdis(ug, &(t_block[i1_tk]), &(t_block[i0_tk]), is_rev);
        // fprintf(stderr, "-[M::%s] qid::%ld, tid::%ld, i0_qk::%ld, i1_qk::%ld, lq::%ld, lt::%ld\n", 
        // __func__, qid, tid, i0_qk, i1_qk, lq, lt);
        // fprintf(stderr, "[M::%s] q_block[i0_qk].qs::%u, q_block[i0_qk].qe::%u, q_block[i1_qk].qs::%u, q_block[i1_qk].qe::%u\n", 
        // __func__, q_block[i0_qk].qs, q_block[i0_qk].qe, q_block[i1_qk].qs, q_block[i1_qk].qe);

        // fprintf(stderr, "[M::%s] t_block[i0_tk].qs::%u, t_block[i0_tk].qe::%u, t_block[i1_tk].qs::%u, t_block[i1_tk].qe::%u\n", 
        // __func__, t_block[i0_tk].qs, t_block[i0_tk].qe, t_block[i1_tk].qs, t_block[i1_tk].qe);
        if(lq <= lt) {
            min = lq; max = lt;
        } else {
            min = lt; max = lq;
        }

        dif = max - min;
        if(dif >= (min*diff_rate) || dif < hard_thres) return 0;
    }
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdi, %r12
movl 0x10(%rsi), %ecx
movl 0x10(%rdx), %edi
testq %rbx, %rbx
je 0x200b62
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl 0x18(%r9), %r11d
movl (%rsi), %eax
notl %eax
addl %r11d, %eax
movl (%rdx), %r10d
notl %r10d
addl %r11d, %r10d
jmp 0x200b67
movl (%rsi), %r10d
movl (%rdx), %eax
cmpl %edi, %ecx
jae 0x200d8b
movl %eax, %esi
movl %r10d, %r10d
cmpl %esi, %r10d
jae 0x200d94
leaq 0x1(%rcx), %rax
cmpq %rdi, %rax
sete %al
leaq 0x1(%r10), %rdx
cmpq %rsi, %rdx
sete %dl
cmpb %dl, %al
je 0x200c5b
movq 0x40(%rsp), %rax
movq 0x10(%r8), %rdx
movq (%rdx,%rcx,8), %rcx
shrq $0x20, %rcx
movq (%rdx,%rdi,8), %rdx
shrq $0x20, %rdx
movq 0x10(%r9), %rdi
movl 0x4(%rdi,%r10,8), %r13d
movl 0x4(%rdi,%rsi,8), %ebp
cmpq %rcx, %rdx
setne %dil
xorl %esi, %esi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
cmpq %r8, %rdx
je 0x200c01
cmpl %ecx, %edx
je 0x200c01
xorl %esi, %esi
movl %edx, %r8d
movl %r8d, %edi
leaq (%rdi,%rdi,8), %rdi
movl 0x14(%rax,%rdi,4), %r8d
cmpl $-0x1, %r8d
je 0x200c08
leaq (%rax,%rdi,4), %rdi
movl 0x18(%rdi), %edi
addq %rdi, %rsi
cmpl %ecx, %r8d
setne %dil
jne 0x200bdc
testb %dil, %dil
je 0x200c0d
jmp 0x200c5b
cmpl %ecx, %r8d
jne 0x200c5b
movq 0x48(%rsp), %r14
cmpq %r13, %rbp
setne %r8b
xorl %edi, %edi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
cmpq %r9, %rbp
je 0x200c56
cmpl %r13d, %ebp
je 0x200c56
xorl %edi, %edi
movl %ebp, %r9d
movl %r9d, %r8d
leaq (%r8,%r8,8), %r8
movl 0x14(%r14,%r8,4), %r9d
cmpl $-0x1, %r9d
je 0x200c70
leaq (%r14,%r8,4), %r8
movl 0x18(%r8), %r8d
addq %r8, %rdi
cmpl %r13d, %r9d
setne %r8b
jne 0x200c30
testb %r8b, %r8b
je 0x200c75
xorl %r15d, %r15d
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl %r13d, %r9d
jne 0x200c5b
testq %rbx, %rbx
je 0x200cbb
movq 0x18(%r12), %r8
movq 0x20(%r8), %r8
leaq (,%r13,8), %r9
addq %r13, %r9
movl (%r14,%r9,4), %r9d
movl (%r8,%r9,8), %r9d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andq %r10, %r9
addq %r9, %rdi
leaq (,%rbp,8), %r9
addq %rbp, %r9
movl (%r14,%r9,4), %r9d
movl (%r8,%r9,8), %r8d
andq %r10, %r8
subq %r8, %rdi
xorl %r8d, %r8d
testq %rdi, %rdi
cmovgq %rdi, %r8
movq %rsi, %rdi
subq %r8, %rdi
negq %rdi
movq %rsi, %r9
subq %r8, %r9
cmovlq %rsi, %r8
cmovleq %rdi, %r9
cvtsi2sd %r9, %xmm2
cvtsi2sd %r8, %xmm1
mulsd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jb 0x200d80
movsd %xmm0, (%rsp)
leaq (%rdx,%rdx,8), %rdx
leaq (%rax,%rdx,4), %rsi
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rdx
xorl %r15d, %r15d
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x1ff427
movq %r12, %rdi
movq %rax, %r12
leaq (,%rbp,8), %rax
addq %rbp, %rax
leaq (%r14,%rax,4), %rsi
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%r14,%rax,4), %rdx
movq %rbx, %rcx
callq 0x1ff427
movq %r12, %rdx
subq %rax, %rdx
negq %rdx
movq %r12, %rcx
subq %rax, %rcx
cmovlq %r12, %rax
cmovleq %rdx, %rcx
cmpq 0x60(%rsp), %rcx
jl 0x200c5e
cvtsi2sd %rcx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x200c5e
movl $0x1, %r15d
jmp 0x200c5e
leaq 0x45a5a(%rip), %rdi     # 0x2467ec
jmp 0x200d9b
leaq 0x45b04(%rip), %rdi     # 0x24689f
leaq 0x44867(%rip), %rsi     # 0x245609
leaq 0x45a51(%rip), %rcx     # 0x2467fa
movl $0x1397, %edx           # imm = 0x1397
callq 0x2110