// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x48(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movl -0x48(%rbp), %eax
cmpl -0xc(%rbp), %eax
jae 0xf75a7
movq -0x8(%rbp), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0x1, %eax
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xf73ce
jmp 0xf7599
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x68(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xf7597
movl $0x0, -0x40(%rbp)
cmpl $0x1, -0x30(%rbp)
jne 0xf7423
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x68(%rbp), %rax
movl 0x10(%rax), %eax
subl -0x4c(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
movabsq $0x100000000, %rax      # imm = 0x100000000
xorq (%rcx,%rdx,8), %rax
shrq $0x20, %rax
movq %rax, -0x70(%rbp)
jmp 0xf743a
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x4c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl %eax, -0x38(%rbp)
cmpl $-0x1, -0x54(%rbp)
je 0xf7512
cmpl $0x0, -0x2c(%rbp)
jne 0xf7512
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x54(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x54(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0xf74f0
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xf74b7
jmp 0xf74e5
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x38(%rbp), %eax
jne 0xf74e3
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x40(%rbp)
jmp 0xf74f0
jmp 0xf74e5
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xf7494
movl -0x44(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jne 0xf7510
movq 0x2ccae1(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x28fec5(%rip), %rsi    # 0x3873ce
movb $0x0, %al
callq 0x22a0
jmp 0xf7512
movl -0x38(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x40(%rbp), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, 0x10(%rbp)
je 0xf7529
jmp 0xf7589
cmpl $0x1, -0x2c(%rbp)
jne 0xf7544
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq $-0x1, (%rax,%rcx,8)
jmp 0xf7587
movl -0x50(%rbp), %eax
movl %eax, %edx
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
movl %eax, %edx
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0xf7589
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xf73d5
jmp 0xf7599
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xf737e
cmpl $-0x1, -0x54(%rbp)
je 0xf75ca
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)