// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: merge_hap_hits(Hap_Align_warp*)
inline uint32_t merge_hap_hits(Hap_Align_warp* u_buffer)
{
    if(u_buffer->x.n == 0) return 0;

    radix_sort_Hap_Align_Pos_sort(u_buffer->x.a, u_buffer->x.a + u_buffer->x.n);
    uint32_t x_pos, x_pos_end, x_pos_beg;
    int k, i, j, m;

    /**
    for (i = 0; i < (int)u_buffer->x.n; i++)
    {
        fprintf(stderr, "****x: %u, y: %u, t_id: %u, is_color: %u\n", u_buffer->x.a[i].q_pos, u_buffer->x.a[i].t_pos,
        u_buffer->x.a[i].t_id, u_buffer->x.a[i].is_color);
    }
    **/
    
    i = u_buffer->x.n; i--;
    x_pos = u_buffer->x.a[i].q_pos; 
    x_pos_end = i;
    for (; i >= 0; i--)
    {
        k = i - 1;
        if(k < 0) continue;
        if(u_buffer->x.a[k].q_pos == u_buffer->x.a[i].q_pos &&
           u_buffer->x.a[k].t_pos+1 == u_buffer->x.a[i].t_pos)
        {
            u_buffer->x.a[k].is_color += u_buffer->x.a[i].is_color;
            u_buffer->x.a[i].t_id = (uint32_t)-1;
        }

        ///meet a new x_pos
        if(u_buffer->x.a[k].q_pos != u_buffer->x.a[i].q_pos)
        {
            x_pos_beg = i;
            for (m = x_pos_beg; m <= (int)x_pos_end; m++)
            {
                if(u_buffer->x.a[m].t_id == (uint32_t)-1) continue;
                for (j = k; j >= 0; j--)
                {
                    if(u_buffer->x.a[j].q_pos != x_pos - 1) break;
                    if(u_buffer->x.a[j].t_pos == u_buffer->x.a[m].t_pos ||
                       u_buffer->x.a[j].t_pos == u_buffer->x.a[m].t_pos - 1)
                    {
                        u_buffer->x.a[j].is_color += u_buffer->x.a[m].is_color;
                        u_buffer->x.a[m].t_id = (uint32_t)-1;
                        break;
                    }
                }
            }
            
            x_pos = u_buffer->x.a[k].q_pos;
            x_pos_end = k;
        }
    }


    

    for (i = 0, m = 0; i < (int)u_buffer->x.n; i++)
    {
        if(u_buffer->x.a[i].t_id!=(uint32_t)-1)
        {
            u_buffer->x.a[m].is_color = u_buffer->x.a[i].is_color;
            u_buffer->x.a[m].t_id = u_buffer->x.a[i].t_id;
            u_buffer->x.a[m].q_pos = u_buffer->x.a[i].q_pos;
            u_buffer->x.a[m].t_pos = u_buffer->x.a[i].t_pos;
            m++;
        }
    }

    u_buffer->x.n = m;

    /**
    for (i = 0; i < (int)u_buffer->x.n; i++)
    {
        fprintf(stderr, "####x: %u, y: %u, t_id: %u, is_color: %u\n", u_buffer->x.a[i].q_pos, u_buffer->x.a[i].t_pos,
        u_buffer->x.a[i].t_id, u_buffer->x.a[i].is_color);
    }
    **/
    radix_sort_Hap_Align_Weight_sort(u_buffer->x.a, u_buffer->x.a + u_buffer->x.n);
    return 0;
}

// Assembly:
movq (%rdi), %rsi
testq %rsi, %rsi
je 0x106396
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0xec6dc
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
testl %eax, %eax
jle 0x106377
leal -0x1(%rax), %ecx
movq %rcx, %rdx
shlq $0x4, %rdx
movl (%rdi,%rdx), %edx
movl %ecx, %esi
movq %rcx, %r8
subq $0x1, %rcx
jb 0x106331
movq %rcx, %r10
shlq $0x4, %r10
movl (%rdi,%r10), %r9d
movq %r8, %r11
shlq $0x4, %r11
cmpl (%rdi,%r11), %r9d
jne 0x1062af
addq %rdi, %r10
leaq (%rdi,%r11), %r9
movl 0x4(%r10), %ebp
incl %ebp
cmpl 0x4(%r9), %ebp
jne 0x106328
movl 0xc(%r9), %r9d
addl %r9d, 0xc(%r10)
movl $0xffffffff, 0x8(%rdi,%r11) # imm = 0xFFFFFFFF
jmp 0x106328
movslq %esi, %r10
cmpq %r10, %r8
jg 0x106323
decl %edx
movq %r8, %r10
movq %r10, %r11
shlq $0x4, %r11
cmpl $-0x1, 0x8(%rdi,%r11)
je 0x106317
leaq (%rdi,%r11), %r14
addq $0x8, %r14
movl %r8d, %r15d
decl %r15d
movq %r15, %r12
shlq $0x4, %r12
cmpl %edx, (%rdi,%r12)
jne 0x106317
leaq (%rdi,%r12), %r13
movl 0x4(%r13), %ebp
movl -0x4(%r14), %r13d
cmpl %r13d, %ebp
je 0x106306
decl %r13d
cmpl %r13d, %ebp
je 0x106306
testl %r15d, %r15d
jg 0x1062d6
jmp 0x106317
movl 0xc(%rdi,%r11), %r11d
addl %r11d, 0xc(%rdi,%r12)
movl $0xffffffff, (%r14)     # imm = 0xFFFFFFFF
leaq 0x1(%r10), %r11
cmpl %r10d, %esi
movq %r11, %r10
jg 0x1062bc
movl %ecx, %esi
movl %r9d, %edx
testq %r8, %r8
jg 0x106260
xorl %esi, %esi
testl %eax, %eax
jle 0x106379
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq 0xc(%rdi), %rcx
movl -0x4(%rcx), %edx
cmpl $-0x1, %edx
je 0x106369
movl (%rcx), %r8d
movslq %esi, %rsi
movq %rsi, %r9
shlq $0x4, %r9
movl %r8d, 0xc(%rdi,%r9)
movl %edx, 0x8(%rdi,%r9)
movq -0xc(%rcx), %rdx
movq %rdx, (%rdi,%r9)
incl %esi
addq $0x10, %rcx
decq %rax
jne 0x106340
movslq %esi, %rsi
jmp 0x106379
xorl %esi, %esi
movq %rsi, (%rbx)
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0xec952
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
xorl %eax, %eax
retq