// File: /workspace/github_cpp_makefile_star2_O3/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 %rbp
pushq %r14
pushq %rbx
movq %rcx, %rax
movq $-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 0x164f00
movq $0x0, (%r8)
subq %rdi, %r9
movq %r9, %rax
negq %rax
cmovsq %r9, %rax
jmp 0x164fc0
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x164fb9
movq 0x10(%rdx), %r14
addq $0x8, %r14
xorl %ebx, %ebx
cmpq -0x8(%r14), %r11
jb 0x164f21
cmpq (%r14), %r11
jb 0x164f32
incq %rbx
addq $0x28, %r14
cmpq %rbx, %rcx
jne 0x164f16
movq %rcx, %rbx
jmp 0x164f37
testq %rcx, %rcx
je 0x164f5a
movq 0x10(%rdx), %r14
addq $0x8, %r14
xorl %edx, %edx
cmpq -0x8(%r14), %r10
jb 0x164f4c
cmpq (%r14), %r10
jb 0x164f5c
incq %rdx
addq $0x28, %r14
cmpq %rdx, %rcx
jne 0x164f41
jmp 0x164fb9
xorl %edx, %edx
cmpq %rcx, %rbx
sete %bpl
cmpq %rcx, %rdx
sete %cl
orb %bpl, %cl
jne 0x164fb9
cmpq %rdx, %rbx
jne 0x164f7c
movq $0x0, (%r8)
jmp 0x164fb9
movq $0x1, (%r8)
movq 0x10(%rsi), %rdx
leaq (%r11,%r11,2), %rsi
shlq $0x4, %rsi
movq (%rdx,%rsi), %rcx
testq %rcx, %rcx
je 0x164fb9
addq %rsi, %rdx
movq 0x10(%rdx), %rdx
addq $0x18, %rdx
cmpb $0x0, (%rdx)
js 0x164fb0
movl -0x10(%rdx), %esi
cmpq %rsi, %r10
je 0x164fc5
addq $0x28, %rdx
decq %rcx
jne 0x164fa3
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 0xd6244(%rip), %r8      # 0x23b220
imulq (%r8,%rsi,8), %rcx
testb $0x2, %dl
je 0x164ffc
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 0x164ef1