// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %eax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x32c0a5
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x60(%rbp)
jmp 0x32c0db
movq -0x38(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rcx
subl (%rcx), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x20(%rbp), %rcx
subl (%rcx), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x32c0e7
jmp 0x32c106
leaq 0x6b33a(%rip), %rdi     # 0x397428
leaq 0x6a0c4(%rip), %rsi     # 0x3961b9
movl $0x1397, %edx           # imm = 0x1397
leaq 0x6b335(%rip), %rcx     # 0x397436
callq 0x2130
movq -0x50(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x32c112
jmp 0x32c131
leaq 0x6b3c2(%rip), %rdi     # 0x3974db
leaq 0x6a099(%rip), %rsi     # 0x3961b9
movl $0x1397, %edx           # imm = 0x1397
leaq 0x6b30a(%rip), %rcx     # 0x397436
callq 0x2130
movq $0x0, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
cmpq -0x58(%rbp), %rax
jne 0x32c157
movq $0x1, -0x68(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
cmpq -0x60(%rbp), %rax
jne 0x32c16d
movq $0x1, -0x70(%rbp)
movq -0x68(%rbp), %rax
addq -0x70(%rbp), %rax
cmpq $0x1, %rax
je 0x32c188
movq $0x0, -0x8(%rbp)
jmp 0x32c51c
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movl %eax, -0xa0(%rbp)
movq -0x48(%rbp), %rax
movl %eax, -0x9c(%rbp)
movq $0x0, -0x78(%rbp)
xorl %eax, %eax
cmpl $-0x1, -0xa0(%rbp)
movb %al, -0xa1(%rbp)
je 0x32c22a
movl -0xa0(%rbp), %eax
cmpl -0x9c(%rbp), %eax
setne %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x32c236
jmp 0x32c288
movq 0x10(%rbp), %rax
movl -0xa0(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
cmpl $-0x1, 0x14(%rax)
je 0x32c269
movq 0x10(%rbp), %rax
movl -0xa0(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x10(%rbp), %rax
movl -0xa0(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, -0xa0(%rbp)
jmp 0x32c204
movl -0xa0(%rbp), %eax
cmpl -0x9c(%rbp), %eax
je 0x32c2a3
movq $0x0, -0x8(%rbp)
jmp 0x32c51c
movq -0x60(%rbp), %rax
movl %eax, -0xa0(%rbp)
movq -0x50(%rbp), %rax
movl %eax, -0x9c(%rbp)
movq $0x0, -0x80(%rbp)
xorl %eax, %eax
cmpl $-0x1, -0xa0(%rbp)
movb %al, -0xa2(%rbp)
je 0x32c2e5
movl -0xa0(%rbp), %eax
cmpl -0x9c(%rbp), %eax
setne %al
movb %al, -0xa2(%rbp)
movb -0xa2(%rbp), %al
testb $0x1, %al
jne 0x32c2f1
jmp 0x32c343
movq 0x18(%rbp), %rax
movl -0xa0(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
cmpl $-0x1, 0x14(%rax)
je 0x32c324
movq 0x18(%rbp), %rax
movl -0xa0(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x18(%rbp), %rax
movl -0xa0(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, -0xa0(%rbp)
jmp 0x32c2bf
movl -0xa0(%rbp), %eax
cmpl -0x9c(%rbp), %eax
je 0x32c35e
movq $0x0, -0x8(%rbp)
jmp 0x32c51c
cmpq $0x0, -0x28(%rbp)
je 0x32c3c2
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq 0x18(%rbp), %rcx
imulq $0x24, -0x50(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq 0x18(%rbp), %rcx
imulq $0x24, -0x60(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
movq -0x80(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jge 0x32c3d1
movq $0x0, -0x80(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x80(%rbp), %rax
jg 0x32c3f3
movq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x32c409
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
subq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
cvtsi2sdq -0x98(%rbp), %xmm0
cvtsi2sdq -0x88(%rbp), %xmm1
mulsd -0x40(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x32c514
movq -0x10(%rbp), %rdi
movq 0x10(%rbp), %rsi
imulq $0x24, -0x58(%rbp), %rax
addq %rax, %rsi
movq 0x10(%rbp), %rdx
imulq $0x24, -0x48(%rbp), %rax
addq %rax, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x328b90
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
movq 0x18(%rbp), %rsi
imulq $0x24, -0x60(%rbp), %rax
addq %rax, %rsi
movq 0x18(%rbp), %rdx
imulq $0x24, -0x50(%rbp), %rax
addq %rax, %rdx
movq -0x28(%rbp), %rcx
callq 0x328b90
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x80(%rbp), %rax
jg 0x32c4b3
movq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x32c4c9
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
subq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
cvtsi2sdq -0x98(%rbp), %xmm0
cvtsi2sdq -0x88(%rbp), %xmm1
mulsd -0x40(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x32c508
movq -0x98(%rbp), %rax
cmpq 0x30(%rbp), %rax
jge 0x32c512
movq $0x0, -0x8(%rbp)
jmp 0x32c51c
jmp 0x32c514
movq $0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)