// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: determin_hap_alignment_boundary_single_side(unsigned long*, long long, long long, long long, long long, long long, long long, long long, float, int, unsigned long*, ma_hit_t_alloc*, asg_t*, R_to_U*, unsigned int*, unsigned int*, unsigned int*)
void determin_hap_alignment_boundary_single_side(uint64_t* readIDs, long long queryLen, long long targetBeg, 
long long targetEnd, long long targetID, long long eMatch, long long eTotal, long long dir, 
float H_rate, int is_local, uint64_t* position_index, ma_hit_t_alloc* reverse_sources, asg_t *read_g, 
R_to_U* ruIndex, uint32_t* n_matchLen, uint32_t* n_max_count, uint32_t* n_min_count)
{
    if(queryLen == 0)
    {
        (*n_matchLen) = (*n_min_count) = (*n_max_count) = 0;
        return;
    }
    long long i, maxId, min_count = eTotal, max_count = eMatch, matchLen = 0;
    long long rLen, score = 0, max_score = 0;
    uint32_t is_found, is_match;
    if(dir == 0)
    {
        for (i = 0, maxId = 0; i < queryLen; i++)
        {
            
            check_hap_match(readIDs[i]>>33, targetBeg, targetEnd, targetID, position_index, reverse_sources, 
            read_g, ruIndex, &is_found, &is_match);

            min_count += is_found;
            max_count += is_match;
            if(max_count > min_count*H_rate) maxId = i;

            if(is_local && is_found)
            {
                rLen = read_g->seq[readIDs[i]>>33].len;
                score += (is_match? rLen : (rLen*(-1)));
                if(score >= max_score) max_score = score, maxId = i;
            }
        }

        for (i = maxId; i >= 0; i--)
        {
            check_hap_match(readIDs[i]>>33, targetBeg, targetEnd, targetID, position_index, reverse_sources, 
            read_g, ruIndex, &is_found, &is_match);

            ///if(is_found > 0 && is_match > 0 && is_match > is_found*Hap_rate)
            if(is_found == 1 && is_match == 1)
            {
                break;
            }
            min_count -= is_found;
            max_count -= is_match;
        }
        
        matchLen = i+1;
    }
    else
    {
        for (i = queryLen - 1, maxId = queryLen - 1; i >= 0; i--)
        {
            check_hap_match(readIDs[i]>>33, targetBeg, targetEnd, targetID, position_index, reverse_sources, 
            read_g, ruIndex, &is_found, &is_match);

            min_count += is_found;
            max_count += is_match;
            if(max_count > min_count*H_rate) maxId = i;

            if(is_local && is_found)
            {
                rLen = read_g->seq[readIDs[i]>>33].len;
                score += (is_match? rLen : (rLen*(-1)));
                if(score >= max_score) max_score = score, maxId = i;
            }
        }

        for (i = maxId; i < queryLen; i++)
        {
            check_hap_match(readIDs[i]>>33, targetBeg, targetEnd, targetID, position_index, reverse_sources, 
            read_g, ruIndex, &is_found, &is_match);

            ///if(is_found > 0 && is_match > 0 && is_match > is_found*Hap_rate)
            if(is_found == 1 && is_match == 1)
            {
                break;
            }
            min_count -= is_found;
            max_count -= is_match;
        }

        matchLen = queryLen - i;
    }

    ///need to check if min_count == 0
    if(min_count == 0)
    {
        (*n_matchLen) = (*n_min_count) = (*n_max_count) = 0;
        return;
    }

    (*n_matchLen) = matchLen;
    (*n_min_count) = min_count;
    (*n_max_count) = max_count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movss %xmm0, 0x34(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq 0xd8(%rsp), %rax
movq 0xd0(%rsp), %rcx
movq 0xc8(%rsp), %rdx
movq %rsi, 0x28(%rsp)
testq %rsi, %rsi
je 0xc8911
movq %rdi, %r12
movq %r9, 0x10(%rsp)
movq 0xb8(%rsp), %r10
movq 0x90(%rsp), %rbp
cmpq $0x0, 0x98(%rsp)
movq %rdi, 0x50(%rsp)
je 0xc891f
movq 0x28(%rsp), %rax
leaq -0x1(%rax), %r14
movq $0x0, 0x20(%rsp)
movq %r14, %r15
movq $0x0, 0x18(%rsp)
testq %r14, %r14
js 0xc88fd
cmpl $0x0, 0xa0(%rsp)
setne %r13b
movq (%r12,%r14,8), %rdi
shrq $0x21, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0xa8(%rsp), %r8
movq 0xb0(%rsp), %r9
leaq 0x8(%rsp), %rax
pushq %rax
leaq 0x14(%rsp), %rax
pushq %rax
pushq 0xd0(%rsp)
pushq %r10
movq %r10, %rbx
callq 0xd3c45
addq $0x20, %rsp
movl 0xc(%rsp), %ecx
movl %ecx, %edx
movl 0x8(%rsp), %eax
movq 0x10(%rsp), %rsi
addq %rax, %rsi
movq %rsi, 0x10(%rsp)
cvtsi2ss %rsi, %xmm0
addq %rdx, %rbp
cvtsi2ss %rbp, %xmm1
mulss 0x34(%rsp), %xmm1
ucomiss %xmm1, %xmm0
cmovaq %r14, %r15
testl %ecx, %ecx
setne %cl
andb %r13b, %cl
cmpb $0x1, %cl
jne 0xc887d
movq 0x20(%rbx), %rcx
movq (%r12,%r14,8), %rdx
shrq $0x21, %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rcx, %rdx
negq %rdx
testl %eax, %eax
cmovneq %rcx, %rdx
movq 0x20(%rsp), %rcx
addq %rdx, %rcx
movq 0x18(%rsp), %rdx
cmpq %rdx, %rcx
cmovgeq %r14, %r15
movq %rcx, 0x20(%rsp)
cmovgq %rcx, %rdx
movq %rdx, 0x18(%rsp)
movq %rbx, %r10
decq %r14
jmp 0xc879f
movq 0x50(%rsp), %rax
movq (%rax,%r15,8), %rdi
shrq $0x21, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0xa8(%rsp), %r8
movq 0xb0(%rsp), %r9
leaq 0x8(%rsp), %rax
pushq %rax
leaq 0x14(%rsp), %rax
pushq %rax
pushq 0xd0(%rsp)
pushq %r10
callq 0xd3c45
movq 0xd8(%rsp), %r10
addq $0x20, %rsp
movl 0xc(%rsp), %eax
movl %eax, %edx
xorl $0x1, %edx
movl 0x8(%rsp), %ecx
movl %ecx, %esi
xorl $0x1, %esi
orl %edx, %esi
je 0xc8907
movl %ecx, %ecx
subq %rax, %rbp
subq %rcx, 0x10(%rsp)
incq %r15
movq 0x28(%rsp), %r14
subq %r15, %r14
jg 0xc8888
movq 0x10(%rsp), %rbx
jmp 0xc8ab5
andl $0x0, (%rcx)
andl $0x0, (%rax)
andl $0x0, (%rdx)
jmp 0xc8afc
xorl %r15d, %r15d
movq 0x28(%rsp), %rax
testq %rax, %rax
cmovleq %r15, %rax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
cmpq %r15, 0x28(%rsp)
je 0xc8a33
cmpl $0x0, 0xa0(%rsp)
setne %r13b
movq (%r12,%r15,8), %rdi
shrq $0x21, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0xa8(%rsp), %r8
movq 0xb0(%rsp), %r9
leaq 0x8(%rsp), %rax
pushq %rax
leaq 0x14(%rsp), %rax
pushq %rax
pushq 0xd0(%rsp)
pushq %r10
movq %r10, %rbx
callq 0xd3c45
addq $0x20, %rsp
movl 0xc(%rsp), %ecx
movl %ecx, %edx
movl 0x8(%rsp), %eax
movq 0x10(%rsp), %rsi
addq %rax, %rsi
movq %rsi, 0x10(%rsp)
cvtsi2ss %rsi, %xmm0
addq %rdx, %rbp
cvtsi2ss %rbp, %xmm1
mulss 0x34(%rsp), %xmm1
ucomiss %xmm1, %xmm0
cmovaq %r15, %r14
testl %ecx, %ecx
setne %cl
andb %r13b, %cl
cmpb $0x1, %cl
jne 0xc8a28
movq 0x20(%rbx), %rcx
movq (%r12,%r15,8), %rdx
shrq $0x21, %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rcx, %rdx
negq %rdx
testl %eax, %eax
cmovneq %rcx, %rdx
movq 0x20(%rsp), %rcx
addq %rdx, %rcx
movq 0x18(%rsp), %rdx
cmpq %rdx, %rcx
cmovgeq %r15, %r14
movq %rcx, 0x20(%rsp)
cmovgq %rcx, %rdx
movq %rdx, 0x18(%rsp)
movq %rbx, %r10
incq %r15
jmp 0xc8948
movq 0x10(%rsp), %rbx
testq %r14, %r14
js 0xc8ab2
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rdi
shrq $0x21, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0xa8(%rsp), %r8
movq 0xb0(%rsp), %r9
leaq 0x8(%rsp), %rax
pushq %rax
leaq 0x14(%rsp), %rax
pushq %rax
pushq 0xd0(%rsp)
pushq %r10
callq 0xd3c45
movq 0xd8(%rsp), %r10
addq $0x20, %rsp
movl 0xc(%rsp), %eax
movl %eax, %edx
xorl $0x1, %edx
movl 0x8(%rsp), %ecx
movl %ecx, %esi
xorl $0x1, %esi
orl %edx, %esi
je 0xc8ab2
movl %ecx, %ecx
subq %rax, %rbp
subq %rcx, %rbx
decq %r14
jmp 0xc8a38
incq %r14
testq %rbp, %rbp
je 0xc8adb
movq 0xc8(%rsp), %rax
movl %r14d, (%rax)
movq 0xd8(%rsp), %rax
movl %ebp, (%rax)
movq 0xd0(%rsp), %rax
movl %ebx, (%rax)
jmp 0xc8afc
movq 0xd0(%rsp), %rax
andl $0x0, (%rax)
movq 0xd8(%rsp), %rax
andl $0x0, (%rax)
movq 0xc8(%rsp), %rax
andl $0x0, (%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq