// File: /workspace/github_cpp_makefile_star2_O1/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), %r11d
testq %rbx, %rbx
je 0x1f5587
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl 0x18(%r9), %edi
movl (%rsi), %eax
notl %eax
addl %edi, %eax
movl (%rdx), %esi
notl %esi
addl %edi, %esi
jmp 0x1f558b
movl (%rsi), %esi
movl (%rdx), %eax
cmpl %r11d, %ecx
jae 0x1f57c1
movl %eax, %edi
movl %esi, %r10d
cmpl %edi, %r10d
jae 0x1f57ca
leaq 0x1(%rcx), %rax
cmpq %r11, %rax
sete %al
leaq 0x1(%r10), %rdx
cmpq %rdi, %rdx
sete %dl
cmpb %dl, %al
je 0x1f5694
movq 0x40(%rsp), %rax
movq 0x10(%r8), %rdx
movq (%rdx,%rcx,8), %rcx
shrq $0x20, %rcx
movq (%rdx,%r11,8), %rdx
shrq $0x20, %rdx
cmpq %rcx, %rdx
setne %r8b
xorl %esi, %esi
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
cmpq %r11, %rdx
je 0x1f5623
cmpl %ecx, %edx
je 0x1f5623
xorl %esi, %esi
movl %edx, %r11d
movl %r11d, %r8d
leaq (%r8,%r8,8), %r8
movl 0x14(%rax,%r8,4), %r11d
cmpl $-0x1, %r11d
je 0x1f5611
leaq (%rax,%r8,4), %r8
movl 0x18(%r8), %r8d
addq %r8, %rsi
cmpl %ecx, %r11d
setne %r8b
cmpl $-0x1, %r11d
je 0x1f5623
cmpl %ecx, %r11d
jne 0x1f55f4
testb %r8b, %r8b
jne 0x1f5694
movq 0x10(%r9), %r8
movl 0x4(%r8,%r10,8), %r13d
movl 0x4(%r8,%rdi,8), %r14d
movq 0x48(%rsp), %rbp
cmpq %r13, %r14
setne %r8b
xorl %edi, %edi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
cmpq %r9, %r14
je 0x1f568f
cmpl %r13d, %r14d
je 0x1f568f
xorl %edi, %edi
movl %r14d, %r9d
movl %r9d, %r8d
leaq (%r8,%r8,8), %r8
movl 0x14(%rbp,%r8,4), %r9d
cmpl $-0x1, %r9d
je 0x1f567d
leaq (,%r8,4), %r8
addq %rbp, %r8
movl 0x18(%r8), %r8d
addq %r8, %rdi
cmpl %r13d, %r9d
setne %r8b
cmpl $-0x1, %r9d
je 0x1f568f
cmpl %r13d, %r9d
jne 0x1f5659
testb %r8b, %r8b
je 0x1f56a9
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 0x1f56ea
movq 0x18(%r12), %r8
movq 0x20(%r8), %r8
leaq (,%r13,8), %r9
addq %r13, %r9
movl (%rbp,%r9,4), %r9d
movl (%r8,%r9,8), %r9d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andq %r10, %r9
addq %r9, %rdi
leaq (%r14,%r14,8), %r9
movl (%rbp,%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 0x1f57b6
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 0x1f3f03
movq %r12, %rdi
movq %rax, %r12
leaq (%r14,%r14,8), %rax
leaq (,%rax,4), %rsi
addq %rbp, %rsi
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (,%rax,4), %rdx
addq %rbp, %rdx
movq %rbx, %rcx
callq 0x1f3f03
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 0x1f5697
cvtsi2sd %rcx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x1f5697
movl $0x1, %r15d
jmp 0x1f5697
leaq 0x44d40(%rip), %rdi     # 0x23a508
jmp 0x1f57d1
leaq 0x44dea(%rip), %rdi     # 0x23a5bb
leaq 0x43b4d(%rip), %rsi     # 0x239325
leaq 0x44d37(%rip), %rcx     # 0x23a516
movl $0x1397, %edx           # imm = 0x1397
callq 0x2110