// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movsd %xmm0, 0x20(%rsp)
movq %rsi, %r13
movq %rdi, 0x28(%rsp)
cmpl $0x2, %r9d
pushq $0x1
popq %r12
movl %r12d, %r15d
movl %r9d, 0x8(%rsp)
cmovgel %r9d, %r15d
callq 0x1a8fd
movsd %xmm0, 0x38(%rsp)
callq 0x1a894
movsd %xmm0, 0x30(%rsp)
movq %r13, 0x18(%rsp)
movq %r13, %rdi
movl %r14d, %esi
movl %ebp, %edx
movl %ebx, %ecx
movsd 0x10(%rsp), %xmm0
movl %r12d, %r8d
callq 0x2ae3
movl $0x88, %esi
movq %r15, %rdi
callq 0x2250
movq %rax, %rbx
movq %r15, 0x10(%rsp)
imulq $0x88, %r15, %r15
xorl %r14d, %r14d
pushq $0x1
popq %r13
pushq $0x18
popq %r12
pushq $0x70
popq %rbp
movq %r13, %rdi
pushq $0x48
popq %rsi
callq 0x2250
movq %rax, (%rbx,%r14)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2250
movq %rax, 0x8(%rbx,%r14)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2250
movq %rax, 0x10(%rbx,%r14)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2250
movq %rax, 0x18(%rbx,%r14)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2250
movq %rax, 0x20(%rbx,%r14)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2250
movq %rax, 0x28(%rbx,%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rbx,%r14)
movups %xmm0, 0x40(%rbx,%r14)
movups %xmm0, 0x50(%rbx,%r14)
andq $0x0, 0x80(%rbx,%r14)
movups %xmm0, 0x60(%rbx,%r14)
movups %xmm0, 0x70(%rbx,%r14)
addq $0x88, %r14
cmpq %r14, %r15
jne 0x2ec8
movq 0x28(%rsp), %r12
movq %r12, 0x40(%rsp)
movq 0x18(%rsp), %r14
movq %r14, 0x48(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq %rbx, 0x58(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x21326(%rip), %rdi     # 0x242c0
xorl %esi, %esi
callq 0x2500
testl %eax, %eax
jne 0x3438
movq (%r12), %rcx
leaq 0xf8e5(%rip), %rsi      # 0x12899
leaq 0x40(%rsp), %rdx
movq 0x10(%rsp), %rdi
callq 0x1ad68
leaq 0x212f6(%rip), %rdi     # 0x242c0
callq 0x2180
movl 0x8(%rsp), %eax
cmpl $0x2, %eax
jl 0x3007
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 0x2fe6
incq %r13
addq $0x88, %rcx
cmpq %rax, %r13
jne 0x2fe4
movq (%r14), %r14
movq 0x18(%r14), %rdi
callq 0x2060
andq $0x0, 0x18(%r14)
movq 0x20(%r14), %rdi
callq 0x2060
andq $0x0, 0x20(%r14)
movq (%r14), %rax
movq %r14, 0x8(%rsp)
movq 0x10(%r14), %r14
testq %rax, %rax
je 0x304a
leaq 0x10(%r14), %rcx
andl $0x80000000, (%rcx)     # imm = 0x80000000
addq $0x20, %rcx
decq %rax
jne 0x303b
movq (%r12), %rax
testq %rax, %rax
je 0x30a9
xorl %ecx, %ecx
movq 0x10(%r12), %rsi
imulq $0x58, %rcx, %rdi
movl 0x38(%rsi,%rdi), %edx
testq %rdx, %rdx
je 0x30a1
movq 0x50(%rsi,%rdi), %rsi
xorl %edi, %edi
movq (%rsi,%rdi,8), %r8
shlq $0x4, %r8
andq $-0x20, %r8
movl 0x10(%r14,%r8), %r9d
leal 0x1(%r9), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andl $0x80000000, %r9d       # imm = 0x80000000
orl %r10d, %r9d
movl %r9d, 0x10(%r14,%r8)
incq %rdi
cmpq %rdi, %rdx
jne 0x306e
incq %rcx
cmpq %rax, %rcx
jne 0x3055
movq 0x8(%rsp), %rax
movq (%rax), %r15
testq %r15, %r15
je 0x30ef
leaq 0x18(%r14), %r13
movq (%r13), %rdi
callq 0x2060
movl -0x8(%r13), %ebp
movl %ebp, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
shlq $0x3, %rdi
callq 0x2320
movq %rax, (%r13)
andl $0x80000000, %ebp       # imm = 0x80000000
movl %ebp, -0x8(%r13)
addq $0x20, %r13
decq %r15
jne 0x30ba
movq 0x8(%rsp), %rax
movq (%rax), %rdi
pushq $0x4
popq %rsi
callq 0x2250
movq %rax, 0x18(%rsp)
movq (%r12), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x31c7
xorl %eax, %eax
movq 0x10(%r12), %r8
imulq $0x58, %rax, %r9
movl 0x38(%r8,%r9), %esi
testq %rsi, %rsi
je 0x31b4
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, %r15
orq %r9, %r15
movl (%r8,%r10,2), %r13d
andl $0x1, %r13d
orq %r15, %r13
movq %r11, %r15
shlq $0x5, %r15
movq 0x18(%r14,%r15), %rbp
movl 0x10(%r14,%r15), %r12d
movl %r12d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leal 0x1(%r12), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %r12d      # imm = 0x80000000
orl %edx, %r12d
movl %r12d, 0x10(%r14,%r15)
movq %r13, (%rbp,%rcx,8)
testb $0x1, (%r8,%r10,2)
jne 0x31ab
movq 0x18(%rsp), %rcx
incl (%rcx,%r11,4)
addq $0x2, %r10
cmpq %r10, %rsi
jne 0x314a
incq %rax
cmpq 0x20(%rsp), %rax
movq 0x28(%rsp), %r12
jne 0x3118
movq 0x8(%rsp), %rax
movq (%rax), %rcx
testq %rcx, %rcx
movq 0x18(%rsp), %rdi
je 0x3208
addq $0x10, %r14
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %eax, %eax
cmpl $0x0, (%rdi,%rdx,4)
sete %al
shll $0x1f, %eax
movl (%r14), %r8d
andl %esi, %r8d
orl %eax, %r8d
movl %r8d, (%r14)
incq %rdx
addq $0x20, %r14
cmpq %rdx, %rcx
jne 0x31e4
callq 0x2060
movq 0x20ddc(%rip), %r12     # 0x23ff0
movq (%r12), %rdi
leaq 0x1b4f7(%rip), %rsi     # 0x1e716
leaq 0x1b4da(%rip), %r15     # 0x1e700
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x58(%rbx), %rcx
addq 0x30(%rbx), %rcx
addq 0x80(%rbx), %rcx
leaq 0x1b4f6(%rip), %rsi     # 0x1e740
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x60(%rbx), %rcx
addq 0x38(%rbx), %rcx
leaq 0x1b505(%rip), %rsi     # 0x1e76c
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x68(%rbx), %rcx
addq 0x40(%rbx), %rcx
leaq 0x1b514(%rip), %rsi     # 0x1e798
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x70(%rbx), %rcx
addq 0x48(%rbx), %rcx
leaq 0x1b523(%rip), %rsi     # 0x1e7c4
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x78(%rbx), %rcx
addq 0x50(%rbx), %rcx
leaq 0x1b532(%rip), %rsi     # 0x1e7f0
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
cmpl $0x0, 0xa8(%rsp)
je 0x3457
movq (%r12), %rdi
movq 0x30(%rbx), %rcx
leaq 0x1b537(%rip), %rsi     # 0x1e81c
leaq 0x1b414(%rip), %r15     # 0x1e700
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x38(%rbx), %rcx
leaq 0x1b467(%rip), %rsi     # 0x1e76c
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x40(%rbx), %rcx
leaq 0x1b47a(%rip), %r14     # 0x1e798
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x48(%rbx), %rcx
leaq 0x1b48a(%rip), %r13     # 0x1e7c4
movq %r13, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x50(%rbx), %rcx
leaq 0x1b49a(%rip), %rbp     # 0x1e7f0
movq %rbp, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x58(%rbx), %rcx
leaq 0x1b4d6(%rip), %rsi     # 0x1e848
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x60(%rbx), %rcx
leaq 0x1b3e1(%rip), %rsi     # 0x1e76c
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x68(%rbx), %rcx
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x70(%rbx), %rcx
movq %r13, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x78(%rbx), %rcx
movq %rbp, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %rdi
movq 0x80(%rbx), %rcx
leaq 0x1b48e(%rip), %rsi     # 0x1e874
movq %r15, %rdx
xorl %eax, %eax
callq 0x2290
movq (%r12), %r14
callq 0x1a894
subsd 0x30(%rsp), %xmm0
leaq 0x1b49a(%rip), %rsi     # 0x1e8a0
movq %r14, %rdi
movq %r15, %rdx
movb $0x1, %al
callq 0x2290
movq (%r12), %r14
callq 0x1a8fd
subsd 0x38(%rsp), %xmm0
leaq 0x1b4a8(%rip), %rsi     # 0x1e8d1
movq %r14, %rdi
movq %r15, %rdx
movb $0x1, %al
callq 0x2290
jmp 0x3457
movq 0x20bb1(%rip), %rax     # 0x23ff0
movq (%rax), %rdi
leaq 0x1b294(%rip), %rsi     # 0x1e6dd
leaq 0x1b2b0(%rip), %rdx     # 0x1e700
xorl %eax, %eax
callq 0x2290
movl 0x10(%rsp), %eax
imulq $0x88, %rax, %r15
xorl %r12d, %r12d
movq (%rbx,%r12), %rdi
xorl %esi, %esi
callq 0x1392d
movq 0x28(%rbx,%r12), %r14
testq %r14, %r14
je 0x34a6
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,%r12), %rax
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x8(%rbx,%r12), %rdi
callq 0x2060
movq 0x10(%rbx,%r12), %rax
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x10(%rbx,%r12), %rdi
callq 0x2060
movq 0x18(%rbx,%r12), %rax
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x18(%rbx,%r12), %rdi
callq 0x2060
movq 0x20(%rbx,%r12), %rax
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x20(%rbx,%r12), %rdi
callq 0x2060
addq $0x88, %r12
cmpq %r12, %r15
jne 0x3465
movq %rbx, %rdi
callq 0x2060
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq