// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: get_hic_distance_bench(pe_hit*, hc_links*, bench_idx*, ma_ug_t*, unsigned long*)
uint64_t get_hic_distance_bench(pe_hit* 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 = ((hit->s<<1)>>(64 - idx->uID_bits)); s_pos = hit->s & idx->pos_mode;
    e_uid = ((hit->e<<1)>>(64 - idx->uID_bits)); e_pos = hit->e & 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 %rbp
pushq %r14
pushq %rbx
movq %rcx, %rax
movq $-0x1, (%r8)
movq (%rdi), %r9
movq 0x8(%rdi), %rdi
leaq (%r9,%r9), %r11
xorl %ecx, %ecx
subb 0x18(%rdx), %cl
shrq %cl, %r11
leaq (%rdi,%rdi), %r10
shrq %cl, %r10
movq 0x20(%rdx), %rcx
andq %rcx, %r9
andq %rcx, %rdi
cmpq %r10, %r11
jne 0x165057
movq $0x0, (%r8)
subq %rdi, %r9
movq %r9, %rax
negq %rax
cmovsq %r9, %rax
jmp 0x165117
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x165110
movq 0x10(%rdx), %r14
addq $0x8, %r14
xorl %ebx, %ebx
cmpq -0x8(%r14), %r11
jb 0x165078
cmpq (%r14), %r11
jb 0x165089
incq %rbx
addq $0x28, %r14
cmpq %rbx, %rcx
jne 0x16506d
movq %rcx, %rbx
jmp 0x16508e
testq %rcx, %rcx
je 0x1650b1
movq 0x10(%rdx), %r14
addq $0x8, %r14
xorl %edx, %edx
cmpq -0x8(%r14), %r10
jb 0x1650a3
cmpq (%r14), %r10
jb 0x1650b3
incq %rdx
addq $0x28, %r14
cmpq %rdx, %rcx
jne 0x165098
jmp 0x165110
xorl %edx, %edx
cmpq %rcx, %rbx
sete %bpl
cmpq %rcx, %rdx
sete %cl
orb %bpl, %cl
jne 0x165110
cmpq %rdx, %rbx
jne 0x1650d3
movq $0x0, (%r8)
jmp 0x165110
movq $0x1, (%r8)
movq 0x10(%rsi), %rdx
leaq (%r11,%r11,2), %rsi
shlq $0x4, %rsi
movq (%rdx,%rsi), %rcx
testq %rcx, %rcx
je 0x165110
addq %rsi, %rdx
movq 0x10(%rdx), %rdx
addq $0x18, %rdx
cmpb $0x0, (%rdx)
js 0x165107
movl -0x10(%rdx), %esi
cmpq %rsi, %r10
je 0x16511c
addq $0x28, %rdx
decq %rcx
jne 0x1650fa
movq $-0x1, %rax
popq %rbx
popq %r14
popq %rbp
retq
movq -0x8(%rdx), %rdx
movq %rdx, %rcx
shrq $0x2, %rcx
movl %edx, %esi
andl $0x1, %esi
leaq 0xd60ed(%rip), %r8      # 0x23b220
imulq (%r8,%rsi,8), %rcx
testb $0x2, %dl
je 0x165153
movq 0x10(%rax), %rax
leaq (%r10,%r10,4), %rdx
movl (%rax,%rdx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
notq %rdi
addq %rax, %rdi
addq %rdi, %rcx
subq %rcx, %r9
jmp 0x165048