// File: /workspace/github_c_makefile_star2_O2/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
movl %r9d, %r15d
movsd %xmm1, 0x18(%rsp)
movl %r8d, %ebx
movl %ecx, %ebp
movl %edx, %r14d
movsd %xmm0, 0x20(%rsp)
movq %rsi, %r13
movq %rdi, 0x10(%rsp)
cmpl $0x2, %r9d
pushq $0x1
popq %r12
cmovll %r12d, %r15d
callq 0x16c3d
movsd %xmm0, 0x38(%rsp)
callq 0x16bd4
movsd %xmm0, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movq %r13, %rdi
movl %r14d, %esi
movl %ebp, %edx
movl %ebx, %ecx
movsd 0x18(%rsp), %xmm0
movl %r12d, %r8d
callq 0xf4d3
movl $0x88, %esi
movq %r15, %rdi
callq 0x2250
movq %rax, %rbx
movq %r15, 0x8(%rsp)
imulq $0x88, %r15, %rax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
pushq $0x1
popq %r13
pushq $0x48
popq %rbp
pushq $0x18
popq %r12
pushq $0x70
popq %r14
cmpq %r15, 0x18(%rsp)
je 0xf83a
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2250
movq %rax, (%rbx,%r15)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2250
movq %rax, 0x8(%rbx,%r15)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2250
movq %rax, 0x10(%rbx,%r15)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2250
movq %rax, 0x18(%rbx,%r15)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2250
movq %rax, 0x20(%rbx,%r15)
movq %r13, %rdi
movq %r14, %rsi
callq 0x2250
movq %rax, 0x28(%rbx,%r15)
addq $0x88, %r15
jmp 0xf7cb
movq 0x10(%rsp), %r14
movq %r14, 0x40(%rsp)
movq 0x28(%rsp), %r15
movq %r15, 0x48(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq %rbx, 0x58(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x11a45(%rip), %rdi     # 0x212b8
xorl %esi, %esi
callq 0x24f0
testl %eax, %eax
jne 0xf8d5
movq (%r14), %rcx
leaq 0x565(%rip), %rsi       # 0xfded
leaq 0x40(%rsp), %rdx
movq 0x8(%rsp), %r14
movl %r14d, %edi
callq 0x170a0
leaq 0x11a17(%rip), %rdi     # 0x212b8
callq 0x2180
movq %rbx, %rax
addq $0xb8, %rax
cmpq %r14, %r13
je 0xf8f9
xorl %ecx, %ecx
cmpq $0xb, %rcx
je 0xf8ca
movq (%rax,%rcx,8), %rdx
addq %rdx, 0x30(%rbx,%rcx,8)
incq %rcx
jmp 0xf8b6
incq %r13
addq $0x88, %rax
jmp 0xf8af
movq 0x11714(%rip), %rax     # 0x20ff0
movq (%rax), %rdi
leaq 0xae47(%rip), %rsi      # 0x1a72d
leaq 0xae63(%rip), %rdx      # 0x1a750
xorl %eax, %eax
callq 0x2280
jmp 0xfbb6
movq (%r15), %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
leaq 0x10(%r14), %rcx
subq $0x1, %rax
jb 0xf93a
andl $0x80000000, (%rcx)     # imm = 0x80000000
addq $0x20, %rcx
jmp 0xf928
movq 0x10(%rsp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
cmpq %rax, %rcx
je 0xf99b
movq 0x10(%rsp), %rdx
movq 0x10(%rdx), %rsi
imulq $0x58, %rcx, %rdi
movq 0x50(%rsi,%rdi), %rdx
movl 0x38(%rsi,%rdi), %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0xf996
movq (%rdx,%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
jmp 0xf961
incq %rcx
jmp 0xf944
movq 0x8(%rsp), %rax
movq (%rax), %r12
leaq 0x18(%r14), %r13
subq $0x1, %r12
jb 0xf9df
movq (%r13), %rdi
callq 0x2060
movl -0x8(%r13), %ebp
movl %ebp, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
shlq $0x3, %rdi
callq 0x2310
movq %rax, (%r13)
andl $0x80000000, %ebp       # imm = 0x80000000
movl %ebp, -0x8(%r13)
addq $0x20, %r13
jmp 0xf9a7
movq 0x8(%rsp), %rax
movq (%rax), %rdi
pushq $0x4
popq %rsi
callq 0x2250
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x20(%rsp)
xorl %edx, %edx
cmpq 0x20(%rsp), %rdx
je 0xfaab
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %r9
movq %rdx, 0x28(%rsp)
imulq $0x58, %rdx, %r10
movq (%r9,%r10), %rsi
movq 0x50(%r9,%r10), %rdi
movq 0x40(%r9,%r10), %r8
movl 0x38(%r9,%r10), %r9d
shlq $0x20, %rsi
addq %r9, %r9
xorl %r10d, %r10d
cmpq %r10, %r9
je 0xfa9e
movq (%rdi,%r10,4), %r11
shrq %r11
movq %r10, %r12
orq %rsi, %r12
movl (%r8,%r10,2), %r13d
andl $0x1, %r13d
orq %r12, %r13
movq %r11, %r12
shlq $0x5, %r12
movq 0x18(%r14,%r12), %rbp
movl 0x10(%r14,%r12), %r15d
movl %r15d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leal 0x1(%r15), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %r15d      # imm = 0x80000000
orl %edx, %r15d
movl %r15d, 0x10(%r14,%r12)
movq %r13, (%rbp,%rcx,8)
testb $0x1, (%r8,%r10,2)
jne 0xfa98
incl (%rax,%r11,4)
addq $0x2, %r10
jmp 0xfa38
movq 0x28(%rsp), %rdx
incq %rdx
jmp 0xf9fe
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
addq $0x10, %r14
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
je 0xfae5
xorl %edi, %edi
cmpl $0x0, (%rax,%rdx,4)
sete %dil
shll $0x1f, %edi
movl (%r14), %r8d
andl %esi, %r8d
orl %edi, %r8d
movl %r8d, (%r14)
incq %rdx
addq $0x20, %r14
jmp 0xfabe
movq %rax, %rdi
callq 0x2060
movq 0x114fc(%rip), %r12     # 0x20ff0
movq (%r12), %rdi
leaq 0xac67(%rip), %rsi      # 0x1a766
leaq 0xac4a(%rip), %r14      # 0x1a750
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x58(%rbx), %rcx
addq 0x30(%rbx), %rcx
addq 0x80(%rbx), %rcx
leaq 0xac66(%rip), %rsi      # 0x1a790
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x60(%rbx), %rcx
addq 0x38(%rbx), %rcx
leaq 0xac75(%rip), %rsi      # 0x1a7bc
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x68(%rbx), %rcx
addq 0x40(%rbx), %rcx
leaq 0xac84(%rip), %rsi      # 0x1a7e8
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x70(%rbx), %rcx
addq 0x48(%rbx), %rcx
leaq 0xac93(%rip), %rsi      # 0x1a814
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x78(%rbx), %rcx
addq 0x50(%rbx), %rcx
leaq 0xaca2(%rip), %rsi      # 0x1a840
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
cmpl $0x0, 0xa8(%rsp)
jne 0xfc88
xorl %r15d, %r15d
cmpq %r15, 0x18(%rsp)
je 0xfc71
movq (%rbx,%r15), %rdi
xorl %esi, %esi
callq 0x10d4a
movq 0x28(%rbx,%r15), %r14
testq %r14, %r14
je 0xfc05
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
jmp 0xfbb9
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 0xabd5(%rip), %rsi      # 0x1a86c
leaq 0xaab2(%rip), %r14      # 0x1a750
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x38(%rbx), %rcx
leaq 0xab05(%rip), %rsi      # 0x1a7bc
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x40(%rbx), %rcx
leaq 0xab18(%rip), %r15      # 0x1a7e8
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x48(%rbx), %rcx
leaq 0xab28(%rip), %r13      # 0x1a814
movq %r13, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x50(%rbx), %rcx
leaq 0xab38(%rip), %rbp      # 0x1a840
movq %rbp, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x58(%rbx), %rcx
leaq 0xab74(%rip), %rsi      # 0x1a898
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %rdi
movq 0x60(%rbx), %rcx
leaq 0xaa7f(%rip), %rsi      # 0x1a7bc
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 0xab2c(%rip), %rsi      # 0x1a8c4
movq %r14, %rdx
xorl %eax, %eax
callq 0x2280
movq (%r12), %r15
callq 0x16bd4
subsd 0x30(%rsp), %xmm0
leaq 0xab38(%rip), %rsi      # 0x1a8f0
movq %r15, %rdi
movq %r14, %rdx
movb $0x1, %al
callq 0x2280
movq (%r12), %r15
callq 0x16c3d
subsd 0x38(%rsp), %xmm0
leaq 0xab46(%rip), %rsi      # 0x1a921
movq %r15, %rdi
movq %r14, %rdx
movb $0x1, %al
callq 0x2280
jmp 0xfbb6