// File: /workspace/github_cpp_makefile_star2_O2/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, 0x8(%rsp)
movq %r8, (%rsp)
movq %rsi, %rbp
movq %rdi, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movb $0x1, (%rdx)
movq %rcx, 0x10(%rsp)
movb $0x0, (%rcx)
andq $0x0, (%rsi)
movq 0x60(%rsp), %r13
pushq $0x8
popq %r15
xorl %ebx, %ebx
movl 0xc(%r13), %eax
cmpq %rax, %rbx
jae 0x143968
movq (%r13), %rax
movq -0x8(%rax,%r15), %r14
movl (%rax,%r15), %r12d
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0x143906
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
movl %r14d, %edx
addq %rdx, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbp)
movq %rdx, (%rax,%rcx,8)
movq (%rbp), %rcx
cmpq 0x8(%rbp), %rcx
jne 0x143948
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(,%r12,2), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbp)
movq %rdx, (%rax,%rcx,8)
incq %rbx
addq $0x28, %r15
jmp 0x1438b8
movq (%rbp), %rax
movq 0x10(%rbp), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x13d044
xorl %eax, %eax
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %r11
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
movq (%rsp), %r15
movq %rdx, %rdi
cmpq (%rbp), %rcx
jae 0x1439f4
movq 0x10(%rbp), %rdx
movq (%rdx,%rcx,8), %rsi
movq %rsi, %rdx
shrq %rdx
cmpq %rdi, %rdx
jbe 0x1439e2
cmpq %r14, %rax
jge 0x1439bc
movb $0x0, (%r10)
testq %rax, %rax
jne 0x1439e2
testq %rdi, %rdi
setne %r8b
cmpq %r15, %rdx
setl %r9b
movb $0x1, %dil
testb %r9b, %r8b
jne 0x1439df
cmpb $0x0, (%rbx)
jne 0x1439e2
movb $0x2, %dil
movb %dil, (%rbx)
andl $0x1, %esi
negq %rsi
orq $0x1, %rsi
addq %rsi, %rax
incq %rcx
jmp 0x143997
cmpq %r15, %rdi
jge 0x143a0f
cmpq %r14, %rax
jge 0x143a02
movb $0x0, (%r10)
testq %rax, %rax
jne 0x143a0f
cmpb $0x0, (%rbx)
jne 0x143a0f
movb $0x2, (%rbx)
cmpb $0x0, (%r10)
je 0x143a33
movq (%r11), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x143a33
movq 0x10(%r11), %rdx
cmpw $0x4000, (%rdx,%rcx,2)  # imm = 0x4000
leaq 0x1(%rcx), %rcx
jb 0x143a1a
movb $0x0, (%r10)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq