// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: get_hic_distance_bench_hap(pe_hit_hap*, hc_links*, bench_idx*, ma_ug_t*, unsigned long*)
uint64_t get_hic_distance_bench_hap(pe_hit_hap* hit, hc_links* link, bench_idx* idx, ma_ug_t *ug, uint64_t* is_trans)
{
    (*is_trans) = (uint64_t)-1;
    uint64_t s_uid, e_uid;
    long long s_pos, e_pos;
    s_uid = ((get_pe_s(*hit)<<1)>>(64 - idx->uID_bits)); s_pos = get_pe_s(*hit) & idx->pos_mode;
    e_uid = ((get_pe_e(*hit)<<1)>>(64 - idx->uID_bits)); e_pos = get_pe_e(*hit) & idx->pos_mode;
    if(s_uid == e_uid)
    {
        (*is_trans) = 0;
        return MAX(s_pos, e_pos) - MIN(s_pos, e_pos);
    }

    uint64_t s_i, e_i, k, ori;
    for (s_i = 0; s_i < idx->ug_idx.n; s_i++)
    {
        if(s_uid >= idx->ug_idx.a[s_i].uID_start && s_uid < idx->ug_idx.a[s_i].uID_end) break;
    }
    for (e_i = 0; e_i < idx->ug_idx.n; e_i++)
    {
        if(e_uid >= idx->ug_idx.a[e_i].uID_start && e_uid < idx->ug_idx.a[e_i].uID_end) break;
    }
    if(s_i == idx->ug_idx.n || e_i == idx->ug_idx.n) return (uint64_t)-1;
    if(s_i == e_i)
    {
        (*is_trans) = 0;
        return (uint64_t)-1;
    }

    (*is_trans) = 1;
    hc_linkeage* t = &(link->a.a[s_uid]);
    long long m_x[2] = {1, -1}, dis;
    for (k = 0; k < t->e.n; k++)
    {
        if(t->e.a[k].del || t->e.a[k].uID != e_uid) continue;
        ori = !!(t->e.a[k].dis & (uint64_t)2);
        dis = (long long)(t->e.a[k].dis>>2) * m_x[t->e.a[k].dis & (uint64_t)1];
        if(ori) e_pos = ug->u.a[e_uid].len - e_pos - 1;
        e_pos = e_pos + dis;
        return MAX(s_pos, e_pos) - MIN(s_pos, e_pos);
    }

    return (uint64_t)-1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rcx, %rax
orq $-0x1, (%r8)
movq (%rdi), %r10
movq (%r10), %r9
leaq (%r9,%r9), %r11
xorl %ecx, %ecx
subb 0x18(%rdx), %cl
shrq %cl, %r11
movzwl 0x10(%rdi), %edi
movq (%r10,%rdi,8), %rdi
leaq (%rdi,%rdi), %r10
shrq %cl, %r10
movq 0x20(%rdx), %rcx
andq %rcx, %r9
andq %rcx, %rdi
cmpq %r10, %r11
jne 0x11fb02
andq $0x0, (%r8)
subq %rdi, %r9
movq %r9, %rax
negq %rax
cmovsq %r9, %rax
jmp 0x11fb82
movq (%rdx), %rbx
movabsq $0x8, %r14
xorl %ecx, %ecx
cmpq %rcx, %rbx
je 0x11fb30
movq 0x10(%rdx), %r15
cmpq -0x8(%r15,%r14), %r11
jb 0x11fb27
cmpq (%r15,%r14), %r11
jb 0x11fb33
incq %rcx
addq $0x28, %r14
jmp 0x11fb11
movq %rbx, %rcx
movabsq $0x8, %r15
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x11fb78
movq 0x10(%rdx), %r12
cmpq -0x8(%r12,%r15), %r10
jb 0x11fb56
cmpq (%r12,%r15), %r10
jb 0x11fb5f
incq %r14
addq $0x28, %r15
jmp 0x11fb40
cmpq %rbx, %rcx
sete %dl
cmpq %r14, %rbx
sete %bl
orb %dl, %bl
jne 0x11fb78
cmpq %r14, %rcx
jne 0x11fb8a
andq $0x0, (%r8)
movabsq $-0x1, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq $0x1, (%r8)
movq 0x10(%rsi), %rdx
imulq $0x30, %r11, %rsi
leaq (%rdx,%rsi), %rcx
movq (%rdx,%rsi), %rsi
movabsq $0x18, %rdx
subq $0x1, %rsi
jb 0x11fb78
movq 0x10(%rcx), %r8
cmpb $0x0, (%r8,%rdx)
js 0x11fbc6
movl -0x10(%r8,%rdx), %r11d
cmpq %r11, %r10
je 0x11fbcc
addq $0x28, %rdx
jmp 0x11fbab
movq -0x8(%r8,%rdx), %rdx
movq %rdx, %rcx
shrq $0x2, %rcx
movl %edx, %esi
andl $0x1, %esi
leaq 0xcd52c(%rip), %r8      # 0x1ed110
imulq (%r8,%rsi,8), %rcx
testb $0x2, %dl
je 0x11fc04
movq 0x10(%rax), %rax
imulq $0x28, %r10, %rdx
movl (%rax,%rdx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
notq %rdi
addq %rax, %rdi
addq %rdi, %rcx
subq %rcx, %r9
jmp 0x11faf3