// File: /workspace/github_cpp_makefile_star2_O1/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
testl %esi, %esi
je 0xbb897
movl %esi, %eax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq %rcx, 0x20(%rsp)
movl %r9d, 0x4(%rsp)
movq %r8, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movq %rax, 0x30(%rsp)
movl (%rdi,%rax,4), %r12d
movl %r12d, %esi
shrl %esi
movq 0x10(%rdx), %rax
leaq (%rsi,%rsi,4), %rsi
cmpl $0x0, 0x10(%rax,%rsi,8)
je 0xbb882
leaq (%rax,%rsi,8), %r13
movl 0x10(%r13), %r11d
testl %r11d, %r11d
je 0xbb882
xorl %ebx, %ebx
movl %r15d, %eax
testb $0x1, %r12b
jne 0xbb7ad
movq 0x18(%r13), %rsi
movl 0x4(%rsi,%rbx,8), %r15d
jmp 0xbb7c1
movq 0x18(%r13), %r10
movl %ebx, %esi
notl %esi
addl %esi, %r11d
movl 0x4(%r10,%r11,8), %r15d
xorq $0x1, %r15
xorl %ebp, %ebp
testl %r9d, %r9d
jne 0xbb823
cmpl $-0x1, %eax
je 0xbb823
movq 0x28(%rcx), %rsi
movl %eax, %eax
movq (%rsi,%rax,8), %rax
xorl %ebp, %ebp
testl %eax, %eax
je 0xbb816
movq 0x8(%rcx), %r11
movq %rax, %rsi
shrq $0x20, %rsi
movl %eax, %r10d
leaq (%rsi,%rsi,2), %rsi
leaq (%r11,%rsi,8), %rsi
addq $0xc, %rsi
xorl %r11d, %r11d
cmpl $0x0, (%rsi)
js 0xbb805
cmpl %r15d, -0x4(%rsi)
je 0xbb81b
incq %r11
addq $0x18, %rsi
cmpq %r11, %r10
jne 0xbb7fa
movl %eax, %r11d
jmp 0xbb81e
xorl %r11d, %r11d
jmp 0xbb81e
movl -0xc(%rsi), %ebp
cmpl %eax, %r11d
je 0xbb862
addl %ebp, %r14d
cmpl $0x0, 0x70(%rsp)
jne 0xbb850
movl %r15d, %eax
shrl %eax
cmpl $0x1, %r9d
jne 0xbb842
movq $-0x1, (%r8,%rax,8)
jmp 0xbb850
movq %r14, %rsi
shlq $0x20, %rsi
orq %r15, %rsi
movq %rsi, (%r8,%rax,8)
incq %rbx
movl 0x10(%r13), %r11d
cmpq %r11, %rbx
jb 0xbb799
jmp 0xbb882
callq 0x10636
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %r8
movl 0x4(%rsp), %r9d
movq 0x20(%rsp), %rcx
jmp 0xbb823
movq 0x30(%rsp), %rax
incq %rax
cmpq 0x28(%rsp), %rax
jne 0xbb765
jmp 0xbb8a0
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r14d, %r14d
cmpl $-0x1, %r15d
je 0xbb8bc
movq 0x20(%rcx), %rax
shrl %r15d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl (%rax,%r15,8), %ecx
addl %r14d, %ecx
movl %ecx, %r14d
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq