// File: /workspace/github_cpp_makefile_star2_O2/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 0x1b4f0d
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 0x1b4f12
movl (%rsi), %r10d
movl (%rdx), %eax
cmpl %edi, %ecx
jae 0x1b50da
movl %eax, %esi
movl %r10d, %r10d
cmpl %esi, %r10d
jae 0x1b50e3
leaq 0x1(%rcx), %rax
cmpq %rdi, %rax
sete %al
leaq 0x1(%r10), %rdx
cmpq %rsi, %rdx
sete %dl
cmpb %dl, %al
je 0x1b4fd6
movq 0x48(%rsp), %rbp
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
movq (%rdi,%r10,8), %r13
movq (%rdi,%rsi,8), %r14
xorl %esi, %esi
movl %edx, %edi
cmpl $-0x1, %edi
je 0x1b4f97
cmpl %ecx, %edi
je 0x1b4f97
movl %edi, %edi
imulq $0x24, %rdi, %r8
movl 0x14(%rax,%r8), %edi
cmpl $-0x1, %edi
je 0x1b4f72
addq %rax, %r8
movl 0x18(%r8), %r8d
addq %r8, %rsi
jmp 0x1b4f72
cmpl %ecx, %edi
jne 0x1b4fd6
shrq $0x20, %r13
shrq $0x20, %r14
xorl %edi, %edi
movl %r14d, %r8d
cmpl $-0x1, %r8d
je 0x1b4fd1
cmpl %r13d, %r8d
je 0x1b4fd1
movl %r8d, %r8d
imulq $0x24, %r8, %r9
movl 0x14(%rbp,%r9), %r8d
cmpl $-0x1, %r8d
je 0x1b4fa8
addq %rbp, %r9
movl 0x18(%r9), %r9d
addq %r9, %rdi
jmp 0x1b4fa8
cmpl %r13d, %r8d
je 0x1b4feb
xorl %r15d, %r15d
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rbx, %rbx
je 0x1b5025
movq 0x18(%r12), %r8
movq 0x20(%r8), %r8
imulq $0x24, %r13, %r9
movl (%rbp,%r9), %r9d
movl (%r8,%r9,8), %r9d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andq %r10, %r9
addq %r9, %rdi
imulq $0x24, %r14, %r9
movl (%rbp,%r9), %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 0x1b50d1
movsd %xmm0, (%rsp)
imulq $0x24, %rdx, %rsi
addq %rax, %rsi
imulq $0x24, %rcx, %rdx
addq %rax, %rdx
xorl %r15d, %r15d
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x1b3351
movq %r12, %rdi
movq %rax, %r12
imulq $0x24, %r14, %rsi
addq %rbp, %rsi
imulq $0x24, %r13, %rdx
addq %rbp, %rdx
movq %rbx, %rcx
callq 0x1b3351
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 0x1b4fd9
cvtsi2sd %rcx, %xmm0
cvtsi2sd %rax, %xmm1
mulsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x1b4fd9
pushq $0x1
popq %r15
jmp 0x1b4fd9
leaq 0x43667(%rip), %rdi     # 0x1f8748
jmp 0x1b50ea
leaq 0x43711(%rip), %rdi     # 0x1f87fb
leaq 0x423e8(%rip), %rsi     # 0x1f74d9
leaq 0x4365e(%rip), %rcx     # 0x1f8756
movl $0x1397, %edx           # imm = 0x1397
callq 0x2110