// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0xb8, %rsp
movq 0x58(%rbp), %rax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movss %xmm0, -0x3c(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x17145a
movq 0x50(%rbp), %rax
movl $0x0, (%rax)
movq 0x58(%rbp), %rax
movl $0x0, (%rax)
movq 0x48(%rbp), %rax
movl $0x0, (%rax)
jmp 0x1718fb
movq 0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
cmpq $0x0, 0x18(%rbp)
jne 0x17169d
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x1715d4
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, %edi
movq -0x20(%rbp), %rax
movl %eax, %esi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq 0x28(%rbp), %r8
movq 0x30(%rbp), %r9
movq 0x38(%rbp), %rbx
movq 0x40(%rbp), %r11
leaq -0x84(%rbp), %r10
leaq -0x88(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x185820
movl -0x84(%rbp), %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x88(%rbp), %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
cvtsi2ssq -0x60(%rbp), %xmm0
cvtsi2ssq -0x58(%rbp), %xmm1
mulss -0x3c(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x17153f
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpl $0x0, 0x20(%rbp)
je 0x1715c1
cmpl $0x0, -0x84(%rbp)
je 0x1715c1
movq 0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x70(%rbp)
cmpl $0x0, -0x88(%rbp)
je 0x17158a
movq -0x70(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x171596
imulq $-0x1, -0x70(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x80(%rbp), %rax
jl 0x1715bf
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x1715c1
jmp 0x1715c3
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x17149d
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jl 0x17168c
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, %edi
movq -0x20(%rbp), %rax
movl %eax, %esi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq 0x28(%rbp), %r8
movq 0x30(%rbp), %r9
movq 0x38(%rbp), %rbx
movq 0x40(%rbp), %r11
leaq -0x84(%rbp), %r10
leaq -0x88(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x185820
cmpl $0x1, -0x84(%rbp)
jne 0x171655
cmpl $0x1, -0x88(%rbp)
jne 0x171655
jmp 0x17168c
movl -0x84(%rbp), %eax
movl %eax, %ecx
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x58(%rbp)
movl -0x88(%rbp), %eax
movl %eax, %ecx
movq -0x60(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1715dc
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x1718b0
movq -0x18(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x48(%rbp)
jl 0x1717e9
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, %edi
movq -0x20(%rbp), %rax
movl %eax, %esi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq 0x28(%rbp), %r8
movq 0x30(%rbp), %r9
movq 0x38(%rbp), %rbx
movq 0x40(%rbp), %r11
leaq -0x84(%rbp), %r10
leaq -0x88(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x185820
movl -0x84(%rbp), %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x88(%rbp), %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
cvtsi2ssq -0x60(%rbp), %xmm0
cvtsi2ssq -0x58(%rbp), %xmm1
mulss -0x3c(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x171754
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpl $0x0, 0x20(%rbp)
je 0x1717d6
cmpl $0x0, -0x84(%rbp)
je 0x1717d6
movq 0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x70(%rbp)
cmpl $0x0, -0x88(%rbp)
je 0x17179f
movq -0x70(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x1717ab
imulq $-0x1, -0x70(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x80(%rbp), %rax
jl 0x1717d4
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x1717d6
jmp 0x1717d8
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1716b5
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x1718a4
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, %edi
movq -0x20(%rbp), %rax
movl %eax, %esi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq 0x28(%rbp), %r8
movq 0x30(%rbp), %r9
movq 0x38(%rbp), %rbx
movq 0x40(%rbp), %r11
leaq -0x84(%rbp), %r10
leaq -0x88(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x185820
cmpl $0x1, -0x84(%rbp)
jne 0x17186d
cmpl $0x1, -0x88(%rbp)
jne 0x17186d
jmp 0x1718a4
movl -0x84(%rbp), %eax
movl %eax, %ecx
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x58(%rbp)
movl -0x88(%rbp), %eax
movl %eax, %ecx
movq -0x60(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1717f1
movq -0x18(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x1718d7
movq 0x50(%rbp), %rax
movl $0x0, (%rax)
movq 0x58(%rbp), %rax
movl $0x0, (%rax)
movq 0x48(%rbp), %rax
movl $0x0, (%rax)
jmp 0x1718fb
movq -0x68(%rbp), %rax
movl %eax, %ecx
movq 0x48(%rbp), %rax
movl %ecx, (%rax)
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x58(%rbp), %rax
movl %ecx, (%rax)
movq -0x60(%rbp), %rax
movl %eax, %ecx
movq 0x50(%rbp), %rax
movl %ecx, (%rax)
addq $0xb8, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)