// File: /workspace/github_cpp_makefile_star2_O2/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 0xc3e27
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0xad7bd
movslq (%rbx), %rax
leal -0x1(%rax), %ecx
movq %rax, %rdx
shlq $0x4, %rdx
movq 0x10(%rbx), %rdi
movl -0x10(%rdx,%rdi), %edx
movl %ecx, %r9d
testl %r9d, %r9d
js 0xc3dc4
movl %r9d, %esi
movl %esi, %r9d
subl $0x1, %r9d
jb 0xc3d02
leal -0x1(%rsi), %r8d
movl %r8d, %r10d
shlq $0x4, %r10
movl (%rdi,%r10), %r8d
movq %rsi, %r11
shlq $0x4, %r11
cmpl (%rdi,%r11), %r8d
jne 0xc3d56
addq %rdi, %r10
leaq (%rdi,%r11), %rsi
movl 0x4(%r10), %r8d
incl %r8d
cmpl 0x4(%rsi), %r8d
jne 0xc3d02
movl 0xc(%rsi), %esi
addl %esi, 0xc(%r10)
orl $-0x1, 0x8(%rdi,%r11)
jmp 0xc3d02
decl %edx
leal -0x1(%rsi), %r9d
cmpl %esi, %ecx
jl 0xc3db9
movq %rsi, %r10
shlq $0x4, %r10
cmpl $-0x1, 0x8(%rdi,%r10)
je 0xc3db4
leaq (%rdi,%r10), %r11
addq $0x8, %r11
movl %r9d, %ebp
testl %ebp, %ebp
js 0xc3db4
movl %ebp, %r14d
shlq $0x4, %r14
cmpl %edx, (%rdi,%r14)
jne 0xc3db4
leaq (%rdi,%r14), %r15
movl 0x4(%r15), %r15d
movl -0x4(%r11), %r12d
cmpl %r12d, %r15d
je 0xc3da6
decl %r12d
decl %ebp
cmpl %r12d, %r15d
jne 0xc3d7a
movl 0xc(%rdi,%r10), %r10d
addl %r10d, 0xc(%rdi,%r14)
orl $-0x1, (%r11)
incq %rsi
jmp 0xc3d5c
movl %r8d, %edx
movl %r9d, %ecx
jmp 0xc3cff
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
shlq $0x4, %rdx
xorl %eax, %eax
movslq %eax, %rsi
cmpq %rcx, %rdx
je 0xc3e10
movl 0x8(%rdi,%rcx), %r8d
cmpl $-0x1, %r8d
je 0xc3e0a
movl 0xc(%rdi,%rcx), %eax
movq %rsi, %r9
shlq $0x4, %r9
movl %eax, 0xc(%rdi,%r9)
movl %r8d, 0x8(%rdi,%r9)
movq (%rdi,%rcx), %rax
movq %rax, (%rdi,%r9)
incl %esi
movl %esi, %eax
addq $0x10, %rcx
jmp 0xc3dd6
movq %rsi, (%rbx)
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0xada2d
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
xorl %eax, %eax
retq