// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: set_utg_offset(unsigned int*, unsigned int, ma_ug_t*, asg_t*, unsigned long*, unsigned int, unsigned int)
uint32_t set_utg_offset(uint32_t *a, uint32_t a_n, ma_ug_t *ug, asg_t *read_sg, uint64_t* pos_idx, uint32_t is_clear,
uint32_t only_len)
{
    uint32_t ori, uid, v, nv, l, k;
    ///uint32_t *a = b->b.a, a_n = b->b.n;
    uint32_t u_i, r_i, len, p_v;
    asg_arc_t *av  = NULL;
    ma_utg_t* u = NULL;
    for (u_i = r_i = len = 0, p_v = (uint32_t)-1; u_i < a_n; u_i++)
    {
        uid = a[u_i] >> 1;
        ori = a[u_i] & 1;
        u = &(ug->u.a[uid]);
        if(u->n == 0) continue;

        for (r_i = 0; r_i < u->n; r_i++)
        {
            l = 0;
            v = (ori == 1?((uint64_t)((u->a[u->n - r_i - 1])^(uint64_t)(0x100000000)))>>32:((uint64_t)(u->a[r_i]))>>32);

            if(p_v != (uint32_t)-1 && is_clear == 0)
            {
                av = asg_arc_a(read_sg, p_v);
                nv = asg_arc_n(read_sg, p_v);
                
                for (k = 0; k < nv; k++)
                {
                    if(av[k].del) continue;
                    if(av[k].v == v) 
                    {
                        l = asg_arc_len(av[k]);
                        break;
                    }
                }
                if(k == nv) fprintf(stderr, "ERROR-set_utg_offset\n");
            }

            p_v = v; len += l;
            if(only_len) continue;

            if(is_clear == 1)
            {
                pos_idx[v>>1] = (uint64_t)-1;
            }
            else
            {
                pos_idx[v>>1] = len;
                pos_idx[v>>1] <<= 32;
                pos_idx[v>>1] |= (uint64_t)v;
            }
        }
    }

    if(p_v != (uint32_t)-1) len += read_sg->seq[p_v>>1].len;

    return len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %ebp
movq %rcx, %r9
movq %rdx, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movl 0x70(%rsp), %edi
movl %esi, %eax
movq %rax, 0x18(%rsp)
pushq $-0x1
popq %rax
xorl %esi, %esi
xorl %r14d, %r14d
movq %rcx, 0x30(%rsp)
movq %r8, 0x28(%rsp)
cmpq 0x18(%rsp), %rsi
je 0x81169
movq 0x8(%rsp), %rcx
movl (%rcx,%rsi,4), %r10d
movl %r10d, %ecx
shrl %ecx
movq 0x10(%rsp), %rdx
movq 0x10(%rdx), %r15
imulq $0x28, %rcx, %rdx
movl 0x10(%r15,%rdx), %ecx
testl %ecx, %ecx
je 0x81161
movq %rsi, 0x20(%rsp)
addq %rdx, %r15
xorl %r12d, %r12d
movl %r10d, 0x4(%rsp)
movl %ecx, %edx
cmpq %rdx, %r12
jae 0x8115c
movq 0x18(%r15), %rdx
testb $0x1, %r10b
jne 0x81096
movl 0x4(%rdx,%r12,8), %ebx
jmp 0x810a5
movl %r12d, %esi
notl %esi
addl %esi, %ecx
movl 0x4(%rdx,%rcx,8), %ebx
xorq $0x1, %rbx
xorl %r13d, %r13d
testl %ebp, %ebp
jne 0x810f5
cmpl $-0x1, %eax
je 0x810f5
movq 0x8(%r9), %rdx
movq 0x28(%r9), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rax
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %ecx
imulq $0x18, %rsi, %rsi
addq %rsi, %rdx
addq $0xc, %rdx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x81128
cmpl $0x0, (%rdx)
js 0x810e4
cmpl %ebx, -0x4(%rdx)
je 0x810ed
incq %rsi
addq $0x18, %rdx
jmp 0x810d5
movl -0xc(%rdx), %r13d
cmpl %esi, %eax
je 0x8112b
addl %r13d, %r14d
testl %edi, %edi
jne 0x8111a
movl %ebx, %eax
shrl %eax
cmpl $0x1, %ebp
jne 0x8110c
orq $-0x1, (%r8,%rax,8)
jmp 0x8111a
movq %r14, %rcx
shlq $0x20, %rcx
orq %rbx, %rcx
movq %rcx, (%r8,%rax,8)
incq %r12
movl 0x10(%r15), %ecx
movl %ebx, %eax
jmp 0x8107a
xorl %r13d, %r13d
movq 0x1d1eae(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x167764(%rip), %rdi    # 0x1e88a0
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movl 0x4(%rsp), %r10d
movl 0x70(%rsp), %edi
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
jmp 0x810f5
movq 0x20(%rsp), %rsi
incq %rsi
jmp 0x81037
cmpl $-0x1, %eax
je 0x81182
movq 0x20(%r9), %rcx
shrl %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl (%rcx,%rax,8), %edx
addl %r14d, %edx
movl %edx, %r14d
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq