// File: chhylp123[P]hifiasm/hic.cpp
// Function: get_hic_distance(pe_hit*, hc_links*, ha_ug_index const*, unsigned int*)
uint64_t get_hic_distance(pe_hit* hit, hc_links* link, const ha_ug_index* idx, uint32_t *is_cc)
{
    uint64_t s_uid, s_dir, e_uid, e_dir, u_dis, k;
    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)
    {
        if(is_cc) (*is_cc) = 1;
        return MAX(s_pos, e_pos) - MIN(s_pos, e_pos);
    } 
    hc_linkeage* t = &(link->a.a[s_uid]);
    for (k = 0; k < t->e.n; k++)
    {
        if(t->e.a[k].del || t->e.a[k].uID != e_uid) continue;
        if(is_cc) (*is_cc) = t->e.a[k].is_cc;
        s_dir = (!!(t->e.a[k].dis&(uint64_t)2));
        e_dir = (!!(t->e.a[k].dis&(uint64_t)1));
        u_dis = (t->e.a[k].dis ==(uint64_t)-1? (uint64_t)-1 : t->e.a[k].dis>>3);
        // if(s_uid == 24684 && s_pos == 124953 && e_uid == 16950 && e_pos == 93039)
        // {
        //     fprintf(stderr, "*****************s_dir: %lu, e_dir: %lu, u_dis: %lu\n", s_dir, e_dir, u_dis);
        // }
        if(u_dis == (uint64_t)-1) return (uint64_t)-1;
        if(s_dir == 1) s_pos = (long long)idx->ug->g->seq[s_uid].len - s_pos - 1;
        if(e_dir == 1) e_pos = (long long)idx->ug->g->seq[e_uid].len - e_pos - 1;
        e_pos = e_pos + u_dis - (long long)idx->ug->g->seq[e_uid].len;
        return MAX(s_pos, e_pos) - MIN(s_pos, e_pos);
    }

    return (uint64_t)-1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x28, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
shlq %rax
movq -0x20(%rbp), %rdx
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shrq %cl, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
andq 0x30(%rcx), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq -0x20(%rbp), %rdx
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shrq %cl, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x20(%rbp), %rcx
andq 0x30(%rcx), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x40(%rbp), %rax
jne 0x1db824
cmpq $0x0, -0x28(%rbp)
je 0x1db7c7
movq -0x28(%rbp), %rax
movl $0x1, (%rax)
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jl 0x1db7db
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x1db7e3
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jg 0x1db802
movq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x1db80d
movq -0x68(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rax
movq -0x88(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x1dba7a
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x30, -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x70(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1dba72
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x58(%rbp), %rcx
addq %rcx, %rax
movb 0x18(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
jne 0x1db884
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x58(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x40(%rbp), %rax
je 0x1db889
jmp 0x1dba61
cmpq $0x0, -0x28(%rbp)
je 0x1db8ae
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x58(%rbp), %rcx
addq %rcx, %rax
movb 0x18(%rax), %al
andb $0x7f, %al
movzbl %al, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x58(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
andq $0x2, %rax
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movq %rax, -0x38(%rbp)
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x58(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
andq $0x1, %rax
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movq %rax, -0x48(%rbp)
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x58(%rbp), %rcx
addq %rcx, %rax
cmpq $-0x1, 0x10(%rax)
jne 0x1db92d
movq $-0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x1db94c
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x58(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
shrq $0x3, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $-0x1, -0x50(%rbp)
jne 0x1db96b
movq $-0x1, -0x8(%rbp)
jmp 0x1dba7a
cmpq $0x1, -0x38(%rbp)
jne 0x1db99b
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x30(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x60(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x60(%rbp)
cmpq $0x1, -0x48(%rbp)
jne 0x1db9cb
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x40(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x68(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
addq -0x50(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movq -0x40(%rbp), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
subq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jl 0x1dba0f
movq -0x60(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x1dba1a
movq -0x68(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jg 0x1dba3f
movq -0x60(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x1dba4a
movq -0x68(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x1dba7a
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1db840
movq $-0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x28, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)