// File: chhylp123[P]hifiasm/Correct.cpp
// Function: filter_adp_k_hits(k_mer_hit*, long, Chain_Data*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, long, long*, k_mer_hit*, double, long)
int64_t filter_adp_k_hits(k_mer_hit *ka, int64_t kn, Chain_Data *dp, uint64_t qs, uint64_t qe, uint64_t ts, uint64_t te, uint64_t readID, int64_t ci, int64_t *cmi, k_mer_hit *m,
double small_bw_rate, int64_t min_small_bw)
{
    int64_t k = ci, cmi0 = (*cmi); k_mer_hit *p = NULL, n;
    for (; k < kn && ka[k].readID == readID && ka[k].self_offset < qs; k++) {
        p = &(ka[k]);
        // fprintf(stderr, "[M::%s::] p->q::%ld, p->t::%ld\n", __func__, p->self_offset, p->offset);
        if(p->offset >= ts) continue;
        if((m) && ((m->offset >= p->offset) || (m->self_offset >= p->self_offset))) continue;
        ka[(*cmi)++] = *p;
    }
    n.self_offset = qs; n.offset = ts; p = &n;
    if(qs == (uint64_t)-1 || qe == (uint64_t)-1) p = NULL;
    // fprintf(stderr, "[M::%s::] cmi0::%ld, cmi::%ld\n", __func__, cmi0, (*cmi)); back = (*cmi);
    if((*cmi) > cmi0) {
        (*cmi) = cmi0 + filter_bad_khits(m, p, ka+cmi0, (*cmi)-cmi0, small_bw_rate, min_small_bw);
        if((*cmi) > cmi0) {
            (*cmi) = cmi0 + lchain_refine(ka+cmi0, (*cmi)-cmi0, ka+cmi0, dp, 50, 5000, 512, 16); 
        }
    }
    // if(back != (*cmi)) {
    //     fprintf(stderr, "sbsbsbsb[M::%s::] cmi0::%ld, cmi::%ld\n", __func__, cmi0, (*cmi));
    // }
    
    for (; k < kn && ka[k].readID == readID && ka[k].self_offset < qe; k++);
    return k;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq 0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x40(%rbp), %rcx
xorl %eax, %eax
cmpq -0x10(%rbp), %rcx
movb %al, -0x61(%rbp)
jge 0x5b3ab
movq -0x8(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq 0x18(%rbp), %rcx
movb %al, -0x61(%rbp)
jne 0x5b3ab
movq -0x8(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x20(%rbp), %rax
setb %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0x5b3b7
jmp 0x5b441
movq -0x8(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
cmpq -0x30(%rbp), %rax
jb 0x5b3d9
jmp 0x5b430
cmpq $0x0, 0x30(%rbp)
je 0x5b402
movq 0x30(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x50(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x5b400
movq 0x30(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x50(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jb 0x5b402
jmp 0x5b430
movq -0x50(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x28(%rbp), %rsi
movq (%rsi), %rdx
movq %rdx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x5b35b
movq -0x20(%rbp), %rax
movl %eax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movl %eax, -0x5c(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $-0x1, -0x20(%rbp)
je 0x5b465
cmpq $-0x1, -0x28(%rbp)
jne 0x5b46d
movq $0x0, -0x50(%rbp)
movq 0x28(%rbp), %rax
movq (%rax), %rax
cmpq -0x48(%rbp), %rax
jle 0x5b538
movq -0x48(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x48(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdx
movq 0x28(%rbp), %rax
movq (%rax), %rcx
subq -0x48(%rbp), %rcx
movsd -0x38(%rbp), %xmm0
movq 0x38(%rbp), %r8
callq 0x5b090
movq -0x70(%rbp), %rcx
addq %rax, %rcx
movq 0x28(%rbp), %rax
movq %rcx, (%rax)
movq 0x28(%rbp), %rax
movq (%rax), %rax
cmpq -0x48(%rbp), %rax
jle 0x5b536
movq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x28(%rbp), %rax
movq (%rax), %rsi
subq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x48(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdx
movq -0x18(%rbp), %rcx
movl $0x32, %r8d
movl $0x1388, %r9d           # imm = 0x1388
movq $0x200, (%rsp)          # imm = 0x200
movq $0x10, 0x8(%rsp)
callq 0x29e80
movq -0x78(%rbp), %rcx
addq %rax, %rcx
movq 0x28(%rbp), %rax
movq %rcx, (%rax)
jmp 0x5b538
jmp 0x5b53a
movq -0x40(%rbp), %rcx
xorl %eax, %eax
cmpq -0x10(%rbp), %rcx
movb %al, -0x79(%rbp)
jge 0x5b58a
movq -0x8(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq 0x18(%rbp), %rcx
movb %al, -0x79(%rbp)
jne 0x5b58a
movq -0x8(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x28(%rbp), %rax
setb %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x5b593
jmp 0x5b5a3
jmp 0x5b595
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x5b53a
movq -0x40(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq