// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdx, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
testl %esi, %esi
je 0xbc922
movl %r9d, %ebp
movl 0x70(%rsp), %r9d
movl %esi, %eax
movq %rax, 0x18(%rsp)
xorl %r10d, %r10d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq %rcx, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movq 0x20(%rsp), %rax
movl (%rax,%r10,4), %r11d
movl %r11d, %eax
shrl %eax
movq 0x28(%rsp), %rdx
movq 0x10(%rdx), %rsi
leaq (%rax,%rax,4), %rdx
movl 0x10(%rsi,%rdx,8), %eax
testl %eax, %eax
je 0xbc8f8
leaq (%rsi,%rdx,8), %r12
xorl %r14d, %r14d
movl %r15d, %edi
movq 0x18(%r12), %rdx
testb $0x1, %r11b
jne 0xbc822
movl 0x4(%rdx,%r14,8), %r15d
jmp 0xbc832
movl %r14d, %esi
notl %esi
addl %esi, %eax
movl 0x4(%rdx,%rax,8), %r15d
xorq $0x1, %r15
xorl %r13d, %r13d
testl %ebp, %ebp
jne 0xbc891
cmpl $-0x1, %edi
je 0xbc891
movq 0x28(%rcx), %rax
movl %edi, %edx
movq (%rax,%rdx,8), %rax
xorl %r13d, %r13d
testl %eax, %eax
je 0xbc885
movq 0x8(%rcx), %rdi
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %edx
leaq (%rsi,%rsi,2), %rsi
leaq (%rdi,%rsi,8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
cmpl $0x0, (%rsi)
js 0xbc875
cmpl %r15d, -0x4(%rsi)
je 0xbc889
incq %rdi
addq $0x18, %rsi
cmpq %rdi, %rdx
jne 0xbc86a
movl %eax, %edi
jmp 0xbc88d
xorl %edi, %edi
jmp 0xbc88d
movl -0xc(%rsi), %r13d
cmpl %eax, %edi
je 0xbc8ce
addl %r13d, %ebx
testl %r9d, %r9d
jne 0xbc8bb
movl %r15d, %eax
shrl %eax
cmpl $0x1, %ebp
jne 0xbc8ad
movq $-0x1, (%r8,%rax,8)
jmp 0xbc8bb
movq %rbx, %rdx
shlq $0x20, %rdx
orq %r15, %rdx
movq %rdx, (%r8,%rax,8)
incq %r14
movl 0x10(%r12), %eax
cmpq %rax, %r14
jb 0xbc80d
jmp 0xbc8f8
movq %r10, 0x30(%rsp)
movl %r11d, 0x4(%rsp)
callq 0x103e9
movl 0x4(%rsp), %r11d
movq 0x30(%rsp), %r10
movl 0x70(%rsp), %r9d
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rcx
jmp 0xbc891
incq %r10
cmpq 0x18(%rsp), %r10
jne 0xbc7df
cmpl $-0x1, %r15d
je 0xbc924
movq 0x20(%rcx), %rax
shrl %r15d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl (%rax,%r15,8), %ecx
addl %ebx, %ecx
movl %ecx, %ebx
jmp 0xbc924
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq