// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movss %xmm0, 0x2c(%rsp)
movq %r8, %r14
movq %rcx, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq 0xe8(%rsp), %rcx
movq 0xe0(%rsp), %rdx
movq 0xd8(%rsp), %r8
movq %rsi, 0x38(%rsp)
testq %rsi, %rsi
je 0x106aaa
movq %r9, %r15
movq 0xc0(%rsp), %rsi
movq 0xa0(%rsp), %r8
cmpq $0x0, 0xa8(%rsp)
movq %rdi, 0x30(%rsp)
je 0x106ab8
movq 0x38(%rsp), %rax
leaq -0x1(%rax), %r9
testq %rax, %rax
jle 0x10692c
movq $0x0, 0x40(%rsp)
leaq 0x8(%rsp), %rbx
movq $0x0, 0x60(%rsp)
movq %r9, %r12
movq (%rdi,%r12,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movl 0xc(%rsi,%rax,8), %ecx
movb $0x1, %dl
testl %ecx, %ecx
je 0x106871
movl %ecx, 0x48(%rsp)
movq %r9, 0x10(%rsp)
movq %r8, 0x20(%rsp)
movq %r15, 0x18(%rsp)
leaq (%rsi,%rax,8), %rbp
movl $0xc, %r13d
xorl %r15d, %r15d
movq (%rbp), %rax
movl (%rax,%r13), %esi
movl %esi, 0x4(%rsp)
movq 0xc8(%rsp), %rax
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%rsi,8)
js 0x1067fc
movl 0x4(%rsp), %esi
movq 0xd0(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rbx, %rcx
callq 0xa306f
movl 0xc(%rsp), %eax
cmpl %r14d, %eax
jne 0x10683c
cmpl $-0x1, %eax
je 0x10683c
cmpl $0x1, 0x8(%rsp)
jne 0x10683c
movl 0x4(%rsp), %eax
movq 0xb8(%rsp), %rcx
movl (%rcx,%rax,8), %eax
cmpl 0x50(%rsp), %eax
setae %cl
cmpl 0x58(%rsp), %eax
setbe %al
testb %al, %cl
je 0x10683c
jmp 0x106875
movq 0xd0(%rsp), %rdi
leaq 0x4(%rsp), %rdx
movq %rbx, %rcx
callq 0xa306f
movl 0x4(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x10683c
cmpl $0x1, 0x8(%rsp)
je 0x10683c
movq 0xc8(%rsp), %rcx
movq 0x20(%rcx), %rcx
cmpl $0x0, (%rcx,%rax,8)
jns 0x1067ab
incq %r15
movl 0xc(%rbp), %eax
addq $0x28, %r13
cmpq %rax, %r15
jb 0x10678d
xorl %eax, %eax
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r8
movq 0x30(%rsp), %rdi
movq 0xc0(%rsp), %rsi
movq 0x10(%rsp), %r9
movb $0x1, %dl
jmp 0x106898
xorl %eax, %eax
jmp 0x10689c
movl $0x1, %eax
xorl %edx, %edx
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r8
movq 0x30(%rsp), %rdi
movq 0xc0(%rsp), %rsi
movq 0x10(%rsp), %r9
movl 0x48(%rsp), %ecx
cmpl $0x1, %ecx
sbbq $-0x1, %r8
addq %rax, %r15
xorps %xmm0, %xmm0
cvtsi2ss %r15, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
mulss 0x2c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
cmovaq %r12, %r9
cmpl $0x0, 0xb0(%rsp)
je 0x10691b
testl %ecx, %ecx
je 0x10691b
movq 0xc8(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rdi,%r12,8), %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, %rcx
negq %rcx
testb %dl, %dl
cmoveq %rax, %rcx
movq 0x60(%rsp), %rdx
addq %rcx, %rdx
movq 0x40(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %r12, %r9
movq %rdx, 0x60(%rsp)
cmovgq %rdx, %rax
movq %rax, 0x40(%rsp)
leaq -0x1(%r12), %rax
testq %r12, %r12
movq %rax, %r12
jg 0x106753
cmpq 0x38(%rsp), %r9
jge 0x106a9a
leaq 0xc(%rsp), %rbx
leaq 0x8(%rsp), %r13
movq (%rdi,%r9,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movl 0xc(%rsi,%rax,8), %r10d
testl %r10d, %r10d
je 0x106a42
movl %r10d, 0x2c(%rsp)
movq %r9, 0x10(%rsp)
movq %r8, 0x20(%rsp)
movq %r15, 0x18(%rsp)
leaq (%rsi,%rax,8), %rbp
movl $0xc, %r12d
xorl %r15d, %r15d
movq (%rbp), %rax
movl (%rax,%r12), %esi
movl %esi, 0x4(%rsp)
movq 0xc8(%rsp), %rax
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%rsi,8)
js 0x1069e9
movl 0x4(%rsp), %esi
movq 0xd0(%rsp), %rdi
movq %rbx, %rdx
movq %r13, %rcx
callq 0xa306f
movl 0xc(%rsp), %eax
cmpl %r14d, %eax
jne 0x106a29
cmpl $-0x1, %eax
je 0x106a29
cmpl $0x1, 0x8(%rsp)
jne 0x106a29
movl 0x4(%rsp), %eax
movq 0xb8(%rsp), %rcx
movl (%rcx,%rax,8), %eax
cmpl 0x50(%rsp), %eax
setae %cl
cmpl 0x58(%rsp), %eax
setbe %al
testb %al, %cl
je 0x106a29
jmp 0x106a48
movq 0xd0(%rsp), %rdi
leaq 0x4(%rsp), %rdx
movq %r13, %rcx
callq 0xa306f
movl 0x4(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x106a29
cmpl $0x1, 0x8(%rsp)
je 0x106a29
movq 0xc8(%rsp), %rcx
movq 0x20(%rcx), %rcx
cmpl $0x0, (%rcx,%rax,8)
jns 0x10699a
incq %r15
movl 0xc(%rbp), %eax
addq $0x28, %r12
cmpq %rax, %r15
jb 0x10697c
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x106a51
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x106a72
movq $-0x1, %rcx
movb $0x1, %al
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r8
movq 0x30(%rsp), %rdi
movq 0xc0(%rsp), %rsi
movq 0x10(%rsp), %r9
movl 0x2c(%rsp), %r10d
testl %r10d, %r10d
setne %dl
testb %al, %dl
jne 0x106a9a
cmpl $0x1, %r10d
adcq $-0x1, %r8
addq %rcx, %r15
incq %r9
cmpq 0x38(%rsp), %r9
jne 0x106941
movq 0x38(%rsp), %r9
movq 0x38(%rsp), %rax
subq %r9, %rax
movq %rax, %r9
jmp 0x106e10
xorl %eax, %eax
movl %eax, (%rdx)
movl %eax, (%rcx)
movl %eax, (%r8)
jmp 0x106e40
cmpq $0x0, 0x38(%rsp)
jle 0x106ca8
movq $0x0, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
xorl %r9d, %r9d
xorl %r13d, %r13d
movq (%rdi,%r13,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movl 0xc(%rsi,%rax,8), %ecx
movb $0x1, %dl
testl %ecx, %ecx
je 0x106bf3
movl %ecx, 0x10(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x20(%rsp)
leaq (%rsi,%rax,8), %r12
movl $0xc, %ebp
xorl %ebx, %ebx
movq (%r12), %rax
movl (%rax,%rbp), %esi
movl %esi, 0x4(%rsp)
movq 0xc8(%rsp), %rax
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%rsi,8)
js 0x106b7f
movl 0x4(%rsp), %esi
movq 0xd0(%rsp), %rdi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0xa306f
movl 0xc(%rsp), %eax
cmpl %r14d, %eax
jne 0x106bc1
cmpl $-0x1, %eax
je 0x106bc1
cmpl $0x1, 0x8(%rsp)
jne 0x106bc1
movl 0x4(%rsp), %eax
movq 0xb8(%rsp), %rcx
movl (%rcx,%rax,8), %eax
cmpl 0x50(%rsp), %eax
setae %cl
cmpl 0x58(%rsp), %eax
setbe %al
testb %al, %cl
je 0x106bc1
jmp 0x106bf7
movq 0xd0(%rsp), %rdi
leaq 0x4(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0xa306f
movl 0x4(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x106bc1
cmpl $0x1, 0x8(%rsp)
je 0x106bc1
movq 0xc8(%rsp), %rcx
movq 0x20(%rcx), %rcx
cmpl $0x0, (%rcx,%rax,8)
jns 0x106b2c
incq %rbx
movl 0xc(%r12), %eax
addq $0x28, %rbp
cmpq %rax, %rbx
jb 0x106b0f
xorl %eax, %eax
movq 0x20(%rsp), %r8
movq 0x30(%rsp), %rdi
movq 0xc0(%rsp), %rsi
movq 0x18(%rsp), %r9
movb $0x1, %dl
jmp 0x106c15
xorl %eax, %eax
jmp 0x106c19
movl $0x1, %eax
xorl %edx, %edx
movq 0x20(%rsp), %r8
movq 0x30(%rsp), %rdi
movq 0xc0(%rsp), %rsi
movq 0x18(%rsp), %r9
movl 0x10(%rsp), %ecx
cmpl $0x1, %ecx
sbbq $-0x1, %r8
addq %rax, %r15
xorps %xmm0, %xmm0
cvtsi2ss %r15, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
mulss 0x2c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
cmovaq %r13, %r9
cmpl $0x0, 0xb0(%rsp)
je 0x106c98
testl %ecx, %ecx
je 0x106c98
movq 0xc8(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rdi,%r13,8), %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, %rcx
negq %rcx
testb %dl, %dl
cmoveq %rax, %rcx
movq 0x40(%rsp), %rdx
addq %rcx, %rdx
movq 0x48(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %r13, %r9
movq %rdx, 0x40(%rsp)
cmovgq %rdx, %rax
movq %rax, 0x48(%rsp)
incq %r13
cmpq 0x38(%rsp), %r13
jne 0x106adc
jmp 0x106cab
xorl %r9d, %r9d
testq %r9, %r9
js 0x106e0d
leaq 0x8(%rsp), %r13
movq (%rdi,%r9,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movl 0xc(%rsi,%rax,8), %r10d
testl %r10d, %r10d
je 0x106db6
movl %r10d, 0x10(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x20(%rsp)
leaq (%rsi,%rax,8), %r12
movl $0xc, %ebp
xorl %ebx, %ebx
movq (%r12), %rax
movl (%rax,%rbp), %esi
movl %esi, 0x4(%rsp)
movq 0xc8(%rsp), %rax
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%rsi,8)
js 0x106d5b
movl 0x4(%rsp), %esi
movq 0xd0(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %r13, %rcx
callq 0xa306f
movl 0xc(%rsp), %eax
cmpl %r14d, %eax
jne 0x106d9b
cmpl $-0x1, %eax
je 0x106d9b
cmpl $0x1, 0x8(%rsp)
jne 0x106d9b
movl 0x4(%rsp), %eax
movq 0xb8(%rsp), %rcx
movl (%rcx,%rax,8), %eax
cmpl 0x50(%rsp), %eax
setae %cl
cmpl 0x58(%rsp), %eax
setbe %al
testb %al, %cl
je 0x106d9b
jmp 0x106dbc
movq 0xd0(%rsp), %rdi
leaq 0x4(%rsp), %rdx
movq %r13, %rcx
callq 0xa306f
movl 0x4(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x106d9b
cmpl $0x1, 0x8(%rsp)
je 0x106d9b
movq 0xc8(%rsp), %rcx
movq 0x20(%rcx), %rcx
cmpl $0x0, (%rcx,%rax,8)
jns 0x106d0a
incq %rbx
movl 0xc(%r12), %eax
addq $0x28, %rbp
cmpq %rax, %rbx
jb 0x106ced
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x106dc5
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x106de1
movq $-0x1, %rcx
movb $0x1, %al
movq 0x20(%rsp), %r8
movq 0x30(%rsp), %rdi
movq 0xc0(%rsp), %rsi
movq 0x18(%rsp), %r9
movl 0x10(%rsp), %r10d
testl %r10d, %r10d
setne %dl
testb %al, %dl
jne 0x106e0d
cmpl $0x1, %r10d
adcq $-0x1, %r8
addq %rcx, %r15
leaq -0x1(%r9), %rax
testq %r9, %r9
movq %rax, %r9
jg 0x106cb9
movq $-0x1, %r9
incq %r9
movq 0xe8(%rsp), %rcx
movq 0xe0(%rsp), %rdx
movq 0xd8(%rsp), %rsi
testq %r8, %r8
je 0x106e38
movl %r9d, (%rsi)
movl %r8d, (%rcx)
movl %r15d, (%rdx)
jmp 0x106e40
xorl %eax, %eax
movl %eax, (%rdx)
movl %eax, (%rcx)
movl %eax, (%rsi)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq