// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
movq 0x60(%rsp), %r14
movq %rdx, 0x18(%rsp)
movb $0x1, (%rdx)
movq %rcx, 0x10(%rsp)
movb $0x0, (%rcx)
movq $0x0, (%rsi)
cmpl $0x0, 0xc(%r14)
je 0x180b20
movl $0x8, %r13d
xorl %r15d, %r15d
movq (%r14), %rax
movq -0x8(%rax,%r13), %rbx
movl (%rax,%r13), %r12d
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0x180aac
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
movl %ebx, %eax
addq %rax, %rax
movq (%rbp), %rcx
movq 0x10(%rbp), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
movq %rax, (%rdi,%rcx,8)
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0x180af0
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
leaq 0x1(,%r12,2), %rax
movq (%rbp), %rcx
movq 0x10(%rbp), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbp)
movq %rax, (%rdx,%rcx,8)
incq %r15
movl 0xc(%r14), %eax
addq $0x28, %r13
cmpq %rax, %r15
jb 0x180a71
movq (%rbp), %rax
movq 0x10(%rbp), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x178fc2
cmpq $0x0, (%rbp)
je 0x180baf
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r11
movq 0x8(%rsp), %rbx
movq (%rsp), %r14
movq %rdx, %rdi
movq 0x10(%rbp), %rdx
movq (%rdx,%rcx,8), %rsi
movq %rsi, %rdx
shrq %rdx
cmpq %rdi, %rdx
jbe 0x180b97
cmpq %rbx, %rax
jge 0x180b70
movb $0x0, (%r10)
testq %rax, %rax
jne 0x180b97
testq %rdi, %rdi
setne %r8b
cmpq %r14, %rdx
setl %r9b
movb $0x1, %dil
testb %r9b, %r8b
jne 0x180b94
cmpb $0x0, (%r11)
jne 0x180b97
movb $0x2, %dil
movb %dil, (%r11)
andl $0x1, %esi
negq %rsi
orq $0x1, %rsi
addq %rsi, %rax
incq %rcx
cmpq (%rbp), %rcx
jb 0x180b51
jmp 0x180bc6
xorl %eax, %eax
xorl %edx, %edx
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r11
movq 0x8(%rsp), %rbx
movq (%rsp), %r14
cmpq %r14, %rdx
movq 0x20(%rsp), %rcx
jge 0x180be8
cmpq %rbx, %rax
jge 0x180bd9
movb $0x0, (%r10)
testq %rax, %rax
jne 0x180be8
cmpb $0x0, (%r11)
jne 0x180be8
movb $0x2, (%r11)
cmpb $0x0, (%r10)
je 0x180c2d
movq (%rcx), %rax
testq %rax, %rax
setne %dl
je 0x180c2d
movq 0x10(%rcx), %rcx
cmpw $0x3fff, (%rcx)         # imm = 0x3FFF
ja 0x180c25
leaq -0x1(%rax), %rdx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x180c2d
leaq 0x1(%rsi), %rdi
cmpw $0x4000, 0x2(%rcx,%rsi,2) # imm = 0x4000
movq %rdi, %rsi
jb 0x180c0a
cmpq %rax, %rdi
setb %dl
testb %dl, %dl
je 0x180c2d
movb $0x0, (%r10)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq