// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/ecovlp.cpp
// Function: check_well_cal(asg16_v*, asg64_v*, unsigned char*, unsigned char*, long, long, ma_hit_t_alloc*)
void check_well_cal(asg16_v *sc, asg64_v *idx, uint8_t *f_ec, uint8_t *abnormal, int64_t len, int64_t min_dp, ma_hit_t_alloc *in)
{
    uint64_t k, s, e; int64_t dp, old_dp, st = 0, ed; ma_hit_t *z; 

    (*f_ec) = 1; (*abnormal) = 0; idx->n = 0;
    for (k = 0; k < in->length; k++) {
        z = &(in->buffer[k]);
        s = ((uint32_t)(z->qns)); e = z->qe;
        kv_push(uint64_t, (*idx), (s<<1));
        kv_push(uint64_t, (*idx), (e<<1)|1);
    }

    radix_sort_ec64(idx->a, idx->a + idx->n);
    for (k = 0, dp = 0, st = ed = 0; k < idx->n; ++k) {
        old_dp = dp;
        ///if a[j] is qe
        if (idx->a[k]&1) --dp;
        else ++dp;

        ed = idx->a[k]>>1;
        if(ed > st) {
            if(old_dp < min_dp) (*f_ec) = 0;
            if(old_dp == 0) {
                if(st > 0 && ed < len) {
                    (*abnormal) = 1;
                }else if((*abnormal)==0){
                    (*abnormal) = 2;
                }
            }
        }

        st = ed;
    }


    ed = len; old_dp = dp;
    if(ed > st) {
        if(old_dp < min_dp) (*f_ec) = 0;
        if(old_dp == 0) {
            if(st > 0 && ed < len) {
                (*abnormal) = 1;
            }else if((*abnormal) == 0){
                (*abnormal) = 2;
            }
        }
    }

    if((*f_ec)) {
        for (k = 0; (k < sc->n) && ((sc->a[k]>>14) == 0); k++);
        if(k < sc->n) (*f_ec) = 0;
    }    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r12
movq %r8, %r13
movq %rcx, %r15
movq %rsi, %rbp
movq %rdi, %rbx
movq 0x60(%rsp), %r14
movq %rdx, 0x20(%rsp)
movb $0x1, (%rdx)
movb $0x0, (%rcx)
movq $0x0, (%rsi)
cmpl $0x0, 0xc(%r14)
je 0x188081
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq 0x10(%rbp), %rdi
movl $0x8, %r13d
xorl %r15d, %r15d
movq (%r14), %rax
movq -0x8(%rax,%r13), %rbx
movl (%rax,%r13), %r12d
movq (%rbp), %rax
cmpq 0x8(%rbp), %rax
jne 0x187ffd
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movl %ebx, %ecx
addq %rcx, %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
movq %rcx, (%rdi,%rax,8)
movq (%rbp), %rax
cmpq 0x8(%rbp), %rax
jne 0x188040
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
leaq 0x1(,%r12,2), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
movq %rcx, (%rdi,%rax,8)
incq %r15
movl 0xc(%r14), %eax
addq $0x28, %r13
cmpq %rax, %r15
jb 0x187fbf
movq (%rbp), %rax
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %r12
movq (%rsp), %r13
jmp 0x188087
movq 0x10(%rbp), %rdi
xorl %eax, %eax
leaq (%rdi,%rax,8), %rsi
callq 0x17fc92
cmpq $0x0, (%rbp)
movq 0x20(%rsp), %r10
je 0x188100
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq %rdx, %rdi
movq 0x10(%rbp), %rdx
movq (%rdx,%rcx,8), %rsi
movq %rsi, %rdx
shrq %rdx
cmpq %rdi, %rdx
jbe 0x1880e8
cmpq %r12, %rax
jge 0x1880c1
movb $0x0, (%r10)
testq %rax, %rax
jne 0x1880e8
testq %rdi, %rdi
setne %r8b
cmpq %r13, %rdx
setl %r9b
movb $0x1, %dil
testb %r9b, %r8b
jne 0x1880e5
cmpb $0x0, (%r15)
jne 0x1880e8
movb $0x2, %dil
movb %dil, (%r15)
andl $0x1, %esi
negq %rsi
orq $0x1, %rsi
addq %rsi, %rax
incq %rcx
cmpq (%rbp), %rcx
jb 0x1880a2
jmp 0x188104
xorl %eax, %eax
xorl %edx, %edx
cmpq %r13, %rdx
jge 0x188121
cmpq %r12, %rax
jge 0x188112
movb $0x0, (%r10)
testq %rax, %rax
jne 0x188121
cmpb $0x0, (%r15)
jne 0x188121
movb $0x2, (%r15)
cmpb $0x0, (%r10)
je 0x18814b
movq (%rbx), %rax
testq %rax, %rax
je 0x18814b
movq 0x10(%rbx), %rcx
xorl %edx, %edx
cmpw $0x4000, (%rcx,%rdx,2)  # imm = 0x4000
jae 0x188147
incq %rdx
cmpq %rdx, %rax
jne 0x188135
jmp 0x18814b
movb $0x0, (%r10)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq