// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/syncerr.c
// Function: read_error_correction
void read_error_correction(sr_db_t *sr_db, scg_t *g, double max_edist, uint32_t err_mer_c, uint32_t max_err_c, 
        uint32_t err_arc_c, double max_arc_f, int n_threads, FILE *fo, int verbose)
{
    if (n_threads <= 0)
        n_threads = 1;

#ifdef DEBUG_SYNCMER_CORRECTION
    if (n_threads > 1) {
        n_threads = 1;
        fprintf(stderr, "[DEBUG_SYNCMER_CORRECTION::%s] set thread number to one for debugging mode\n", __func__);
    }
#endif

    double realtime1 = realtime();
    double cputime1 = cputime();

    // mark errors in syncmer database
    find_error_syncmers(g, err_mer_c, max_err_c, err_arc_c, max_arc_f, 1);
    
    // sort arcs by coverage for each source vertex
    // DFS then will visit the high coverage paths first
    // this will hopefully increase the chance of error correction for complex regions capped by MAX_DFS_PATH
    // TODO does this really help?
    /***
    uint64_t i, n, na;
    asmg_arc_t *a;
    asmg_t *asmg;
    asmg = g->utg_asmg;
    for (i = 0, n = 2 * asmg->n_vtx; i < n; ++i) {
        na = asmg_arc_n(asmg, i);
        if (na) {
            a = asmg_arc_a(asmg, i);
            qsort(a, na, sizeof(asmg_arc_t), arc_cov_cmpfunc);
        }
    }
    **/

    int t;
    // shared data
    ec_shared_t shared;

    ec_cached_t *cached;
    MYCALLOC(cached, n_threads);
    for (t = 0; t < n_threads; ++t) {
        MYCALLOC(cached[t].conf, 1);
        MYCALLOC(cached[t].seq, 1);
        MYCALLOC(cached[t].c_seq, 1);
        MYCALLOC(cached[t].c_kmer, 1);
        MYCALLOC(cached[t].c_mpos, 1);
        MYCALLOC(cached[t].dfs, 1);
        MYBZERO(cached[t].stats, 11);
    }

    shared.sr_db = sr_db;
    shared.g = g;
    shared.max_edist = max_edist;
    shared.cache = cached;
    shared.fo = fo;
    
    if (pthread_mutex_init(&mutex, NULL) != 0) {
        fprintf(stderr, "[E::%s] pthread mutex init failed\n", __func__);
        goto do_clean;
    }
    kt_for(n_threads, read_error_correction_analysis_thread, &shared, sr_db->n);
    pthread_mutex_destroy(&mutex);
    
    /***
    uint64_t rids[] = {0, 9, 15, 21, 31}; //{9007, 58477, 59602, 89548, 129398, 192, 15778, 24013, 25430, 44358, 102676, 103790, 129066, 140012};
    for (int r = 0; r < sizeof(rids) / sizeof(*rids); ++r)
        read_error_correction_analysis_thread(&shared, rids[r], 0);
    **/

    int j;
    long *stats;
    stats = cached[0].stats;
    for (t = 1; t < n_threads; ++t)
        for (j = 0; j < 11; ++j)
            stats[j] += cached[t].stats[j];
    
    // update syncmer database
    update_syncmer_db(sr_db, g->scm_db);

    // print error correction summary results
    fprintf(stderr, "[M::%s] Error Correction Summary Results\n", __func__);
    fprintf(stderr, "[M::%s] total number of error blocks : %ld\n", __func__, stats[0] + stats[5] + stats[10]);
    fprintf(stderr, "[M::%s]                - uncorrected : %ld\n", __func__, stats[1] + stats[6]);
    fprintf(stderr, "[M::%s]                  - corrected : %ld\n", __func__, stats[2] + stats[7]);
    fprintf(stderr, "[M::%s]             - ambiguous seqs : %ld\n", __func__, stats[3] + stats[8]);
    fprintf(stderr, "[M::%s]             - ambiguous path : %ld\n", __func__, stats[4] + stats[9]);
    // more verbose information
    if (verbose) {
        fprintf(stderr, "[M::%s] error blocks in the tail end : %ld\n", __func__, stats[0]);
        fprintf(stderr, "[M::%s]                - uncorrected : %ld\n", __func__, stats[1]);
        fprintf(stderr, "[M::%s]                  - corrected : %ld\n", __func__, stats[2]);
        fprintf(stderr, "[M::%s]             - ambiguous seqs : %ld\n", __func__, stats[3]);
        fprintf(stderr, "[M::%s]             - ambiguous path : %ld\n", __func__, stats[4]);
        fprintf(stderr, "[M::%s]   error blocks in the middle : %ld\n", __func__, stats[5]);
        fprintf(stderr, "[M::%s]                - uncorrected : %ld\n", __func__, stats[6]);
        fprintf(stderr, "[M::%s]                  - corrected : %ld\n", __func__, stats[7]);
        fprintf(stderr, "[M::%s]             - ambiguous seqs : %ld\n", __func__, stats[8]);
        fprintf(stderr, "[M::%s]             - ambiguous path : %ld\n", __func__, stats[9]);
        fprintf(stderr, "[M::%s]      error blocks overlapped : %ld\n", __func__, stats[10]);
        fprintf(stderr, "[M::%s]   error correction  CPU time : %.3f sec\n", __func__, cputime() - cputime1);
        fprintf(stderr, "[M::%s]   error correction real time : %.3f sec\n", __func__, realtime() - realtime1);
    }

do_clean:
    for (t = 0; t < n_threads; ++t) {
        // do free
        wf_config_destroy(cached[t].conf, 0);
        dfs_info_destroy(cached[t].dfs);
        free(cached[t].seq->s);
        free(cached[t].seq);
        free(cached[t].c_seq->s);
        free(cached[t].c_seq);
        free(cached[t].c_kmer->a);
        free(cached[t].c_kmer);
        free(cached[t].c_mpos->a);
        free(cached[t].c_mpos);
    }
    free(cached);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm1, 0x10(%rsp)
movl %r8d, %ebx
movl %ecx, %ebp
movl %edx, %r15d
movsd %xmm0, (%rsp)
movq %rsi, %r14
movq %rdi, 0x20(%rsp)
cmpl $0x2, %r9d
pushq $0x1
popq %r12
movl %r12d, %r13d
movl %r9d, 0x8(%rsp)
cmovgel %r9d, %r13d
callq 0x1c8d1
movsd %xmm0, 0x30(%rsp)
callq 0x1c868
movsd %xmm0, 0x28(%rsp)
movq %r14, 0x18(%rsp)
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %ebx, %ecx
movsd 0x10(%rsp), %xmm0
movl %r12d, %r8d
callq 0x2ad3
movl $0x88, %esi
movq %r13, %rdi
callq 0x2250
movq %rax, %rbx
movq %r13, 0x38(%rsp)
imulq $0x88, %r13, %r15
xorl %r14d, %r14d
pushq $0x1
popq %r12
pushq $0x18
popq %rbp
pushq $0x70
popq %r13
movq %r12, %rdi
pushq $0x48
popq %rsi
callq 0x2250
movq %rax, (%rbx,%r14)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2250
movq %rax, 0x8(%rbx,%r14)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2250
movq %rax, 0x10(%rbx,%r14)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2250
movq %rax, 0x18(%rbx,%r14)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2250
movq %rax, 0x20(%rbx,%r14)
movq %r12, %rdi
movq %r13, %rsi
callq 0x2250
movq %rax, 0x28(%rbx,%r14)
addq $0x88, %r14
cmpq %r14, %r15
jne 0x2ec9
movq %r15, 0x10(%rsp)
movq 0x20(%rsp), %r13
movq %r13, 0x40(%rsp)
movq 0x18(%rsp), %r14
movq %r14, 0x48(%rsp)
movsd (%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq %rbx, 0x58(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x23347(%rip), %rdi     # 0x262b8
xorl %esi, %esi
callq 0x24f0
testl %eax, %eax
jne 0x308e
movq (%r13), %rcx
leaq 0x103cd(%rip), %rsi     # 0x13358
leaq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rdi
callq 0x1cd38
leaq 0x23317(%rip), %rdi     # 0x262b8
callq 0x2180
movl 0x8(%rsp), %eax
cmpl $0x2, %eax
jl 0x2fde
movl %eax, %eax
movq %rbx, %rcx
addq $0xb8, %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
addq %rsi, 0x30(%rbx,%rdx,8)
incq %rdx
cmpq $0xb, %rdx
jne 0x2fbd
incq %r12
addq $0x88, %rcx
cmpq %rax, %r12
jne 0x2fbb
movq (%r14), %rbp
movq 0x18(%rbp), %rdi
callq 0x2060
andq $0x0, 0x18(%rbp)
movq 0x20(%rbp), %rdi
callq 0x2060
andq $0x0, 0x20(%rbp)
movq (%rbp), %rax
movq 0x10(%rbp), %r12
testq %rax, %rax
je 0x301e
leaq 0x10(%r12), %rcx
andl $0x80000000, (%rcx)     # imm = 0x80000000
addq $0x20, %rcx
decq %rax
jne 0x300f
movq (%r13), %rax
testq %rax, %rax
je 0x307c
xorl %ecx, %ecx
movq 0x10(%r13), %rsi
imulq $0x58, %rcx, %rdi
movl 0x38(%rsi,%rdi), %edx
testq %rdx, %rdx
je 0x3074
movq 0x50(%rsi,%rdi), %rsi
xorl %edi, %edi
movq (%rsi,%rdi,8), %r8
shlq $0x4, %r8
andq $-0x20, %r8
movl 0x10(%r12,%r8), %r9d
leal 0x1(%r9), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andl $0x80000000, %r9d       # imm = 0x80000000
orl %r10d, %r9d
movl %r9d, 0x10(%r12,%r8)
incq %rdi
cmpq %rdi, %rdx
jne 0x3041
incq %rcx
cmpq %rax, %rcx
jne 0x3029
movq (%rbp), %r14
testq %r14, %r14
movq %rbp, (%rsp)
jne 0x30b2
xorl %r15d, %r15d
jmp 0x30f2
movq 0x22f5b(%rip), %rax     # 0x25ff0
movq (%rax), %rdi
leaq 0x1d6ce(%rip), %rsi     # 0x2076d
leaq 0x1d6ea(%rip), %rdx     # 0x20790
xorl %eax, %eax
callq 0x2280
jmp 0x32cc
leaq 0x18(%r12), %r15
movq (%r15), %rdi
callq 0x2060
movl -0x8(%r15), %ebp
movl %ebp, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
shlq $0x3, %rdi
callq 0x2310
movq %rax, (%r15)
andl $0x80000000, %ebp       # imm = 0x80000000
movl %ebp, -0x8(%r15)
addq $0x20, %r15
decq %r14
jne 0x30b7
movq (%rsp), %rbp
movq (%rbp), %r15
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x2250
movq (%r13), %rcx
movq %rcx, 0x18(%rsp)
testq %rcx, %rcx
je 0x31c6
movq %rbx, 0x8(%rsp)
xorl %edx, %edx
movq 0x10(%r13), %r8
imulq $0x58, %rdx, %r9
movl 0x38(%r8,%r9), %esi
testq %rsi, %rsi
je 0x31a7
leaq (%r8,%r9), %r10
movq 0x50(%r8,%r9), %rdi
movq 0x40(%r8,%r9), %r8
movq (%r10), %r9
shlq $0x20, %r9
addq %rsi, %rsi
xorl %r10d, %r10d
movq (%rdi,%r10,4), %r11
shrq %r11
movq %r10, %r14
orq %r9, %r14
movl (%r8,%r10,2), %r15d
andl $0x1, %r15d
orq %r14, %r15
movq %r11, %r14
shlq $0x5, %r14
movq 0x18(%r12,%r14), %rbp
movl 0x10(%r12,%r14), %r13d
movl %r13d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leal 0x1(%r13), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
andl $0x80000000, %r13d      # imm = 0x80000000
orl %ebx, %r13d
movl %r13d, 0x10(%r12,%r14)
movq %r15, (%rbp,%rcx,8)
testb $0x1, (%r8,%r10,2)
jne 0x319e
incl (%rax,%r11,4)
addq $0x2, %r10
cmpq %r10, %rsi
jne 0x3143
incq %rdx
cmpq 0x18(%rsp), %rdx
movq 0x20(%rsp), %r13
jne 0x3116
movq (%rsp), %rcx
movq (%rcx), %r15
movq 0x8(%rsp), %rbx
testq %r15, %r15
je 0x31fb
addq $0x10, %r12
xorl %ecx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %esi, %esi
cmpl $0x0, (%rax,%rcx,4)
sete %sil
shll $0x1f, %esi
movl (%r12), %edi
andl %edx, %edi
orl %esi, %edi
movl %edi, (%r12)
incq %rcx
addq $0x20, %r12
cmpq %rcx, %r15
jne 0x31d6
movq %rax, %rdi
callq 0x2060
movq 0x22de6(%rip), %r12     # 0x25ff0
movq (%r12), %rdi
leaq 0x1d591(%rip), %rsi     # 0x207a6
leaq 0x1d574(%rip), %r14     # 0x20790
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x58(%rbx), %rcx
addq 0x30(%rbx), %rcx
addq 0x80(%rbx), %rcx
leaq 0x1d590(%rip), %rsi     # 0x207d0
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x60(%rbx), %rcx
addq 0x38(%rbx), %rcx
leaq 0x1d59f(%rip), %rsi     # 0x207fc
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x68(%rbx), %rcx
addq 0x40(%rbx), %rcx
leaq 0x1d5ae(%rip), %rsi     # 0x20828
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x70(%rbx), %rcx
addq 0x48(%rbx), %rcx
leaq 0x1d5bd(%rip), %rsi     # 0x20854
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x78(%rbx), %rcx
addq 0x50(%rbx), %rcx
leaq 0x1d5cc(%rip), %rsi     # 0x20880
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
cmpl $0x0, 0xa8(%rsp)
jne 0x339c
xorl %r15d, %r15d
movq 0x10(%rsp), %r12
movq (%rbx,%r15), %rdi
xorl %esi, %esi
callq 0x143ed
movq 0x28(%rbx,%r15), %r14
testq %r14, %r14
je 0x3315
movq 0x20(%r14), %rdi
callq 0x2060
movq 0x38(%r14), %rdi
callq 0x2060
movq 0x50(%r14), %rdi
callq 0x2060
movq 0x68(%r14), %rdi
callq 0x2060
movq %r14, %rdi
callq 0x2060
movq 0x8(%rbx,%r15), %rax
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x8(%rbx,%r15), %rdi
callq 0x2060
movq 0x10(%rbx,%r15), %rax
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x10(%rbx,%r15), %rdi
callq 0x2060
movq 0x18(%rbx,%r15), %rax
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x18(%rbx,%r15), %rdi
callq 0x2060
movq 0x20(%rbx,%r15), %rax
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x20(%rbx,%r15), %rdi
callq 0x2060
addq $0x88, %r15
cmpq %r15, %r12
jne 0x32d4
movq %rbx, %rdi
callq 0x2060
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r12), %rdi
movq 0x30(%rbx), %rcx
leaq 0x1d501(%rip), %rsi     # 0x208ac
leaq 0x1d3de(%rip), %r14     # 0x20790
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x38(%rbx), %rcx
leaq 0x1d431(%rip), %rsi     # 0x207fc
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x40(%rbx), %rcx
leaq 0x1d444(%rip), %r15     # 0x20828
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x48(%rbx), %rcx
leaq 0x1d454(%rip), %r13     # 0x20854
movq %r13, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x50(%rbx), %rcx
leaq 0x1d464(%rip), %rbp     # 0x20880
movq %rbp, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x58(%rbx), %rcx
leaq 0x1d4a0(%rip), %rsi     # 0x208d8
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x60(%rbx), %rcx
leaq 0x1d3ab(%rip), %rsi     # 0x207fc
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x68(%rbx), %rcx
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x70(%rbx), %rcx
movq %r13, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x78(%rbx), %rcx
movq %rbp, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x80(%rbx), %rcx
leaq 0x1d458(%rip), %rsi     # 0x20904
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %r15
callq 0x1c868
subsd 0x28(%rsp), %xmm0
leaq 0x1d464(%rip), %rsi     # 0x20930
movq %r15, %rdi
movq %r14, %rdx
movb $0x1, %al
callq 0x2280
movq (%r12), %r15
callq 0x1c8d1
subsd 0x30(%rsp), %xmm0
leaq 0x1d472(%rip), %rsi     # 0x20961
movq %r15, %rdi
movq %r14, %rdx
movb $0x1, %al
callq 0x2280
jmp 0x32cc