// File: /workspace/github_cpp_makefile_star2_O1/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 0x102073
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 0xeacb5
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
testl %eax, %eax
jle 0x10200b
leal -0x1(%rax), %ecx
movslq %eax, %rdx
shlq $0x4, %rdx
movl -0x10(%rdx,%rdi), %edx
movl %ecx, %esi
movq %rcx, %r8
subq $0x1, %rcx
jb 0x102002
movq %rcx, %r10
shlq $0x4, %r10
movl (%rdi,%r10), %r9d
movq %r8, %r14
shlq $0x4, %r14
movl (%rdi,%r14), %r11d
cmpl %r11d, %r9d
jne 0x101f7b
addq %rdi, %r10
leaq (%rdi,%r14), %r15
movl 0x4(%r10), %ebp
incl %ebp
cmpl 0x4(%r15), %ebp
jne 0x101f72
movl 0xc(%r15), %ebp
addl %ebp, 0xc(%r10)
movl $0xffffffff, 0x8(%rdi,%r14) # imm = 0xFFFFFFFF
cmpl %r11d, %r9d
je 0x102002
movslq %esi, %r10
cmpq %r10, %r8
jg 0x101ffd
decl %edx
movq %r8, %r10
testq %r8, %r8
setle %bpl
movq %r10, %r11
shlq $0x4, %r11
cmpl $-0x1, 0x8(%rdi,%r11)
sete %r14b
orb %bpl, %r14b
jne 0x101ff1
leaq (%rdi,%r11), %r14
addq $0x8, %r14
movl %r8d, %r15d
decl %r15d
movq %r15, %r12
shlq $0x4, %r12
cmpl %edx, (%rdi,%r12)
jne 0x101ff1
leaq (%rdi,%r12), %r13
movl 0x4(%r13), %ebp
movl -0x4(%r14), %r13d
cmpl %r13d, %ebp
je 0x101fe0
decl %r13d
cmpl %r13d, %ebp
je 0x101fe0
testl %r15d, %r15d
jg 0x101fb0
jmp 0x101ff1
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 0x101f88
movl %ecx, %esi
movl %r9d, %edx
testq %r8, %r8
jg 0x101f26
xorl %esi, %esi
testl %eax, %eax
jle 0x102056
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq 0xc(%rdi), %rcx
cmpl $-0x1, -0x4(%rcx)
je 0x10204a
movl (%rcx), %edx
movslq %esi, %rsi
movq %rsi, %r8
shlq $0x4, %r8
movl %edx, 0xc(%rdi,%r8)
movl -0x4(%rcx), %edx
movl %edx, 0x8(%rdi,%r8)
movl -0xc(%rcx), %edx
movl %edx, (%rdi,%r8)
movl -0x8(%rcx), %edx
movl %edx, 0x4(%rdi,%r8)
incl %esi
addq $0x10, %rcx
decq %rax
jne 0x10201a
movslq %esi, %rsi
movq %rsi, (%rbx)
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0xeaf30
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
xorl %eax, %eax
retq