// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]oatk/syncerr.c
// Function: find_error_syncmers
int64_t find_error_syncmers(scg_t *g, uint32_t err_mer_c, uint32_t max_err_c, uint32_t err_arc_c, double max_arc_f, int del_err)
{
    asmg_t *asmg;
    syncmer_t *scm;
    asmg_arc_t *a;
    size_t i, j, k, n_scm, n_arc;
    uint32_t nv, nw;
    int b[2];

    asmg = g->utg_asmg;
    n_scm = g->scm_db->n;
    scm = g->scm_db->a;

    for (i = 0; i < n_scm; ++i) {
        if (scm[i].del || scm[i].cov >= max_err_c)
            continue;
        if (scm[i].cov < err_mer_c) {
            scm[i].del = 1;
            continue;
        }
        nv = scm[i].cov;
        b[0] = b[1] = -1;
        for (k = 0; k < 2; ++k) {
            a = asmg_arc_a(asmg, i << 1 | k);
            n_arc = asmg_arc_n1(asmg, i << 1 | k);
            if (n_arc) b[k] = 0;
            else continue;
            n_arc = asmg_arc_n(asmg, i << 1 | k);
            for (j = 0; j < n_arc; ++j) {
                if (a[j].del) continue;
                nw = scm[a[j].w >> 1].cov;
                if (a[j].cov >= err_arc_c && a[j].cov >= MIN(nv, nw) * max_arc_f) {
                    // good arc found
                    b[k] = 1;
                    break;
                }
            }
        }
        if (!b[0] || !b[1]) scm[i].del = 1;
    }

    int64_t n_err = 0;
    uint32_t max_c = 0;
    for (i = 0; i < n_scm; ++i) {
        if (scm[i].del) {
            if (scm[i].cov > max_c)
                max_c = scm[i].cov;
            ++n_err;
#ifdef DEBUG_FIND_SYNCERR
            fprintf(stderr, "[DEBUG_FIND_SYNCERR::%s] error syncmer: %lu %u\n", __func__, i, scm[i].cov);
            if (scm[i].cov > err_mer_c) {
                for (k = 0; k < 2; ++k) {
                    a = asmg_arc_a(asmg, i << 1 | k);
                    n_arc = asmg_arc_n(asmg, i << 1 | k);
                    for (j = 0; j < n_arc; ++j) {
                        if (a[j].del || a[j].comp) continue;
                        fprintf(stderr, "[DEBUG_FIND_SYNCERR::%s] error syncmer arc list: "
                                "%lu %lu%c -> %lu%c v_cov = %u w_cov = %u arc_cov = %u\n", 
                                __func__, j, 
                                a[j].v>>1, "+-"[a[j].v&1], 
                                a[j].w>>1, "+-"[a[j].w&1], 
                                scm[a[j].v>>1].cov, scm[a[j].w>>1].cov, a[j].cov);
                    }
                }
            }
#endif
        }
    }
    
    if (del_err) {
        for (i = 0; i < n_scm; ++i)
            if (scm[i].del)
                asmg_vtx_del(asmg, i, 1);
    }

    fprintf(stderr, "[M::%s] error syncmer candidates: num = %ld, max_c = %u\n", __func__, n_err, max_c);

    return n_err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, 0x14(%rsp)
movl %edx, %r10d
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movq %rdx, 0x20(%rsp)
movq (%rax), %r8
movq 0x10(%rax), %r13
xorl %ebx, %ebx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq %r8, 0x8(%rsp)
movl %r10d, 0x1c(%rsp)
movl %esi, 0x18(%rsp)
cmpq %r8, %rbx
je 0xf663
movq %rbx, %r11
shlq $0x5, %r11
movl 0x10(%r13,%r11), %r9d
testl %r9d, %r9d
sets %al
cmpl %r10d, %r9d
setae %dl
orb %al, %dl
jne 0xf65b
cmpl %esi, %r9d
jae 0xf556
leaq (%r11,%r13), %rax
addq $0x10, %rax
orl $0x80000000, %r9d       # imm = 0x80000000
movl %r9d, (%rax)
jmp 0xf65b
movq %r11, 0x28(%rsp)
orq $-0x1, (%rsp)
movq 0x20(%rsp), %rax
movq 0x28(%rax), %r11
movq 0x30(%rax), %r14
movq %rbx, 0x30(%rsp)
leaq (%rbx,%rbx), %r15
movq 0x38(%rax), %rbp
addq $0x20, %r11
xorl %ebx, %ebx
cmpq $0x2, %rbx
je 0xf62e
movq %rbx, %rax
orq %r15, %rax
movq (%rbp,%rax,8), %rsi
imulq $0x30, (%r14,%rax,8), %rdx
addq %r11, %rdx
movq %rsi, %r8
movq %rdx, %rax
movq %rsi, %r12
subq $0x1, %r8
jb 0xf5c2
movl (%rax), %r10d
addl %r10d, %r10d
sarl $0x1f, %r10d
movslq %r10d, %r10
addq %r10, %r12
addq $0x30, %rax
jmp 0xf5a6
testq %r12, %r12
je 0xf626
andl $0x0, (%rsp,%rbx,4)
subq $0x1, %rsi
jb 0xf626
movl (%rdx), %r8d
btl $0x1e, %r8d
jb 0xf619
andl $0x3fffffff, %r8d       # imm = 0x3FFFFFFF
cmpl %ecx, %r8d
jb 0xf619
movq -0x18(%rdx), %rax
shlq $0x4, %rax
andq $-0x20, %rax
movl 0x10(%r13,%rax), %eax
andl %edi, %eax
movl %r9d, %r12d
cmpl %eax, %r9d
jb 0xf605
movl %eax, %r12d
cvtsi2sd %r8d, %xmm1
cvtsi2sd %r12d, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jae 0xf61f
addq $0x30, %rdx
jmp 0xf5cb
movl $0x1, (%rsp,%rbx,4)
incq %rbx
jmp 0xf580
cmpl $0x0, (%rsp)
movq 0x8(%rsp), %r8
movl 0x1c(%rsp), %r10d
movl 0x18(%rsp), %esi
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r11
je 0xf53f
cmpl $0x0, 0x4(%rsp)
je 0xf53f
incq %rbx
jmp 0xf511
leaq 0x10(%r13), %rax
xorl %r14d, %r14d
movq %r8, %rcx
xorl %ebp, %ebp
subq $0x1, %rcx
jb 0xf697
movl (%rax), %edx
movl %edx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpl %ebp, %esi
cmovbel %ebp, %esi
movl %edx, %edi
shrl $0x1f, %edi
addq %rdi, %r14
testl %edx, %edx
cmovsl %esi, %ebp
addq $0x20, %rax
jmp 0xf66f
cmpl $0x0, 0x14(%rsp)
jne 0xf6d5
movq 0x1194b(%rip), %rax     # 0x20ff0
movq (%rax), %rdi
leaq 0xb031(%rip), %rsi      # 0x1a6e0
leaq 0xb063(%rip), %rdx      # 0x1a719
movq %r14, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x2280
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x10, %r13
pushq $0x10
popq %rbx
xorl %r15d, %r15d
movq 0x20(%rsp), %r12
subq $0x1, %r8
jb 0xf69e
cmpl $0x0, (%r13)
jns 0xf71e
movq 0x10(%r12), %rax
orl $0x40000000, (%rax,%rbx,2) # imm = 0x40000000
movq %r12, %rdi
movq %r15, %rsi
movq %r8, 0x8(%rsp)
callq 0x10c44
leaq 0x1(%r15), %rsi
movq %r12, %rdi
callq 0x10c44
movq 0x8(%rsp), %r8
addq $0x2, %r15
addq $0x14, %rbx
addq $0x20, %r13
jmp 0xf6e4