// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/syncerr.c
// Function: dfs_search
static void dfs_search(asmg_t *asmg, syncmer_t *scms, dfs_info_t *dfs_info, uint64_t sink, wf_config_t *conf)
{
    // bounded by MAX_DFS_PATH
    if (dfs_info->n_path >= MAX_DFS_PATH)
        return;

    uint64_t i, source, arc_n, w, l0, n0, d0;
    int l_seq, ls, s0, t_end0, q_end0, score;
    asmg_arc_t *arc, *a;
    kstring_t *c_seq;
    kvec64_t *c_path;
    char *k_seq;
    wf_diag1_t *wf_diag1;

    c_seq = &dfs_info->c_seq;
    l0 = c_seq->l;
    c_path = &dfs_info->c_path;
    n0 = c_path->n;
    source = c_path->a[n0-1];
    arc = asmg_arc_a(asmg, source);
    arc_n = asmg_arc_n(asmg, source);
    t_end0 = conf->t_end;
    q_end0 = conf->q_end;
    s0 = conf->score;
    d0 = conf->wf_diag->n;
    // make a copy of DP matrix
    MYMALLOC(wf_diag1, d0);
    memcpy(wf_diag1, conf->wf_diag->a, sizeof(wf_diag1_t) * d0);

    // process each arc
    for (i = 0; i < arc_n; ++i) {
        a = &arc[i];
        if (a->del)
            continue;
        w = a->w;
        ls = a->ls;
        l_seq = asmg->vtx[w>>1].len;
        k_seq = asmg->vtx[w>>1].seq;

        // push sink id
        kv_push(uint64_t, *c_path, w);

        // push sequence
        if (w&1)
            kputsn_rev(k_seq, l_seq - ls, c_seq);
        else
            kputsn(&k_seq[ls], l_seq - ls, c_seq);

        conf->qs = c_seq->s;
        conf->ql = c_seq->l;
        // conf->t_end and conf->q_end will be zero if not aligned
        wf_ed_core(conf);

#ifdef DEBUG_SYNCMER_EC_DETAIL
        fprintf(stderr, "[DEBUG_SYNCMER_EC_DETAIL::%s] u%lu%c u%lu%c L_SEQ=%d LS=%d EXT=%d\n", 
                __func__, source>>1, "+-"[source&1], w>>1, "+-"[w&1], l_seq, ls, l_seq - ls);
        fprintf(stderr, "[DEBUG_SYNCMER_EC_DETAIL::%s] ED_ALIGNMENT: ED=%d BW=%d tL=%d t_EN=%d qL=%d q_EN=%d\n",
                __func__, conf->score, conf->bw, conf->tl, conf->t_end, conf->ql, conf->q_end);
        fprintf(stderr, "[DEBUG_SYNCMER_EC_DETAIL::%s] ERROR CORRECTION RESULT - ORIGINAL  SEQ: %.*s\n",
                __func__, conf->tl, conf->ts);
        fprintf(stderr, "[DEBUG_SYNCMER_EC_DETAIL::%s] ERROR CORRECTION RESULT - CORRECTED SEQ: %.*s\n",
                __func__, (int) conf->ql, conf->qs);
#endif

        // calculate real score considering target sequence clipping
        score = conf->score + conf->tl - conf->t_end;
        if (score <= conf->bw && (sink == UINT64_MAX || sink == w)) {
#ifdef DEBUG_SYNCMER_EC_DETAIL
            fprintf(stderr, "[DEBUG_SYNCMER_EC_DETAIL::%s] ERROR CORRECTION RESULT - OPTIMUM ALIGNMENT: score=%d\n",
                    __func__, score);
#endif
            // this is an optimum alignment
            // also update optimum path info when score == edist 
            // to get the complete synmcer list for trailing errors
#ifdef DEBUG_SYNCMER_CORRECTION
            // reset edist
            // this is necessary only ifdef DEBUG_SYNCMER_CORRECTION
            // as edist could be updated before this
            if (!dfs_info->status)
                dfs_info->edist = INT32_MAX;
#endif
            dfs_info->status = EC_SUCCESS; // update EC_STATUS
            if (score <= dfs_info->edist) {
                if (conf->t_end > t_end0) // otherwise only an extension
                    dfs_info->s_edist = dfs_info->edist;
                dfs_info->edist = score;
                // for trailing errors the last syncmer may need to be removed
                // because the syncmer sequence is only partially mapped
                if (sink == UINT64_MAX && conf->q_end < conf->ql)
                    --c_path->n;
                // if the secondary alignment is as good as the optimum
                if (dfs_info->edist == dfs_info->s_edist) {
                    // need to compare to the query sequence to decide
                    // if the alignment sequence is ambiguous
                    if (conf->q_end != dfs_info->opt_seq.l || 
                            strncmp(c_seq->s, dfs_info->opt_seq.s, conf->q_end))
                        dfs_info->status = EC_AMBISEQ;
                    // need to compare to the optimum path to decide
                    // if syncmer path is ambiguous
                    if (dfs_info->status == EC_SUCCESS && 
                            kvcmp(c_path, &dfs_info->opt_path))
                        dfs_info->status = EC_AMBISNQ;
                }
                dfs_info->opt_seq.l = 0;
                kputsn(c_seq->s, conf->q_end, &dfs_info->opt_seq);
                kv_copy(uint64_t, dfs_info->opt_path, *c_path);
            } else if (score < dfs_info->s_edist) {
                dfs_info->s_edist = score;
            }
        }
#ifdef DEBUG_SYNCMER_CORRECTION
        else if (!dfs_info->status) { // in debug mode this will be copied if EC_FAILURE
            if (score <= dfs_info->edist) {
                dfs_info->edist = score;
                dfs_info->opt_seq.l = 0;
                // the alignment may fail at this point
                // copy the entire sequence instead of the mapped piece
                kputsn(c_seq->s, c_seq->l, &dfs_info->opt_seq);
                kv_copy(uint64_t, dfs_info->opt_path, *c_path);
            }
        }
#endif
        
        if (conf->score <= conf->bw &&                     // stop dfs if edit distance exceeds threshold
                conf->ql - l_seq <= conf->tl + conf->bw && // stop dfs if target and query sequence overlap exceeds threshold
                ((sink != UINT64_MAX && sink != w) ||      // stop dfs if target end reached and sink reached for middle errors
                 conf->t_end < conf->tl))                  // stop dfs if target end reached for trailing errors
            // further dfs
            dfs_search(asmg, scms, dfs_info, sink, conf);
        else
            // increase path counter when a dfs search stopped
            dfs_info->n_path++;

        // reset conf and dfs_info
        c_path->n = n0;
        c_seq->l = l0;
        conf->t_end = t_end0;
        conf->q_end = q_end0;
        conf->score = s0;
        conf->wf_diag->n = d0;
        memcpy(conf->wf_diag->a, wf_diag1, sizeof(wf_diag1_t) * d0);
    }
    
    free(wf_diag1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, 0x8(%rsp)
cmpl $0x270f, 0x4(%rsi)      # imm = 0x270F
jle 0x12e20
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0x10(%rsi), %rax
movq %rax, 0x60(%rsp)
movq 0x40(%rsi), %rcx
movq 0x50(%rsi), %rax
movq %rcx, 0x58(%rsp)
movq -0x8(%rax,%rcx,8), %rax
movq 0x30(%rdi), %rcx
movq (%rcx,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rbp
shlq $0x4, %rbp
addq 0x28(%rdi), %rbp
movq 0x38(%rdi), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, 0x30(%rsp)
movsd 0x34(%rbx), %xmm0
movaps %xmm0, 0x70(%rsp)
movl 0x30(%rbx), %eax
movl %eax, 0x24(%rsp)
movq 0x18(%rbx), %r14
movq (%r14), %rax
movq %rax, 0x50(%rsp)
leaq (,%rax,4), %rax
leaq (%rax,%rax,2), %r13
movq %r13, %rdi
callq 0x2310
movq 0x10(%r14), %rsi
movq %rax, 0x28(%rsp)
movq %rax, %rdi
movq %r13, 0x48(%rsp)
movq %r13, %rdx
callq 0x22b0
cmpq $0x0, 0x30(%rsp)
je 0x1333d
movaps 0x70(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
movq %r12, 0x10(%rsp)
movq %rbp, 0x40(%rsp)
movq %rbx, 0x38(%rsp)
leaq (%r14,%r14,2), %rax
shlq $0x4, %rax
testb $0x40, 0x23(%rbp,%rax)
jne 0x13309
addq %rbp, %rax
movq 0x8(%rax), %rsi
movq 0x18(%rax), %r8
movq 0x10(%r12), %rax
movq %rsi, %rcx
shrq %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x10(%rax,%rcx,8), %r13
movq 0x18(%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movq 0x40(%r15), %rcx
movq 0x50(%r15), %rax
cmpq 0x48(%r15), %rcx
jne 0x12f5b
movq %rsi, %rbx
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x48(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r8, %r12
callq 0x2350
movq %r12, %r8
movq %rbx, %rsi
movq 0x38(%rsp), %rbx
movq %rax, 0x50(%r15)
movq 0x40(%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x40(%r15)
movq %rsi, (%rax,%rcx,8)
testb $0x1, %sil
movq %rsi, 0x68(%rsp)
jne 0x12f9b
movq 0x18(%rsp), %rax
subl %r8d, %eax
movq 0x10(%r15), %rdi
movslq %eax, %rbp
leaq (%rdi,%rbp), %rax
incq %rax
cmpq 0x18(%r15), %rax
jae 0x13041
movq 0x20(%r15), %rax
jmp 0x13090
movq 0x18(%rsp), %rax
movl %eax, %r12d
subl %r8d, %r12d
movq 0x10(%r15), %rbp
movslq %r12d, %r8
leaq (%r8,%rbp), %rax
incq %rax
cmpq 0x18(%r15), %rax
jb 0x13009
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x18(%r15)
movq 0x20(%r15), %rdi
movq %r8, %rbp
callq 0x2350
movq %rbp, %r8
movq %rax, 0x20(%r15)
movq 0x10(%r15), %rbp
testl %r12d, %r12d
jle 0x1303c
movslq %ebp, %rax
leaq 0x1(%r8), %rcx
leaq 0xceb4(%rip), %rdi      # 0x1fed0
movsbq -0x2(%r13,%rcx), %rdx
movb (%rdi,%rdx), %dl
movq 0x20(%r15), %rsi
movb %dl, (%rsi,%rax)
incq %rax
decq %rcx
cmpq $0x1, %rcx
ja 0x1301c
movq 0x10(%r15), %rbp
addq %r8, %rbp
jmp 0x130a8
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x18(%r15)
movq 0x20(%r15), %rdi
movq %r8, %r12
callq 0x2350
movq %r12, %r8
movq %rax, 0x20(%r15)
movq 0x10(%r15), %rdi
addq %rax, %rdi
movslq %r8d, %rax
addq %rax, %r13
movq %r13, %rsi
movq %rbp, %rdx
callq 0x22b0
addq 0x10(%r15), %rbp
movq 0x8(%rsp), %r13
cmpq $-0x1, %r13
sete %r12b
movq %rbp, 0x10(%r15)
movq 0x20(%r15), %rax
movb $0x0, (%rax,%rbp)
movq 0x20(%r15), %rax
movq %rax, 0x8(%rbx)
movl 0x10(%r15), %eax
movl %eax, 0x14(%rbx)
movq %rbx, %rdi
callq 0x1443c
movl 0x2c(%rbx), %eax
movl 0x30(%rbx), %ecx
movl 0x10(%rbx), %edx
addl %ecx, %edx
movl 0x34(%rbx), %esi
subl %esi, %edx
cmpq %r13, 0x68(%rsp)
sete %bpl
orb %r12b, %bpl
cmpl %eax, %edx
jg 0x132a6
testb %bpl, %bpl
je 0x132a6
movl $0x1, (%r15)
movl 0x8(%r15), %edi
cmpl %edi, %edx
jle 0x13127
cmpl 0xc(%r15), %edx
jge 0x132a6
movl %edx, 0xc(%r15)
jmp 0x132a6
cmpl 0x20(%rsp), %esi
jle 0x13131
movl %edi, 0xc(%r15)
movl %edx, 0x8(%r15)
movslq 0x38(%rbx), %r13
cmpq $-0x1, 0x8(%rsp)
jne 0x1314b
cmpl 0x14(%rbx), %r13d
jge 0x1314b
decq 0x40(%r15)
cmpl 0xc(%r15), %edx
jne 0x131a7
movl $0x3, %r12d
cmpq %r13, 0x28(%r15)
jne 0x131a4
movq 0x20(%r15), %rdi
movq 0x38(%r15), %rsi
movq %r13, %rdx
callq 0x20a0
testl %eax, %eax
jne 0x131a4
movq 0x40(%r15), %rax
movl $0x2, %r12d
cmpq 0x58(%r15), %rax
jne 0x131a4
testq %rax, %rax
je 0x131a7
movq 0x50(%r15), %rcx
movq 0x68(%r15), %rdx
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
cmpq (%rdx,%rsi,8), %rdi
jne 0x131a4
incq %rsi
cmpq %rsi, %rax
jne 0x13190
jmp 0x131a7
movl %r12d, (%r15)
movq $0x0, 0x28(%r15)
movq 0x20(%r15), %r12
leaq 0x1(%r13), %rax
cmpq 0x30(%r15), %rax
jae 0x131c5
movq 0x38(%r15), %rax
xorl %edi, %edi
jmp 0x1320e
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x30(%r15)
movq 0x38(%r15), %rdi
callq 0x2350
movq %rax, 0x38(%r15)
movq 0x28(%r15), %rdi
addq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x22b0
addq 0x28(%r15), %r13
movq %r13, 0x28(%r15)
movq 0x38(%r15), %rax
movb $0x0, (%rax,%r13)
movq 0x40(%r15), %rdx
movq 0x68(%r15), %rdi
cmpq %rdx, 0x60(%r15)
jae 0x1328f
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x60(%r15)
leaq 0x8(,%rax,8), %rsi
callq 0x2350
movq %rax, %rdi
movq %rax, 0x68(%r15)
movq 0x40(%r15), %rdx
movq %rdx, 0x58(%r15)
movq 0x50(%r15), %rsi
shlq $0x3, %rdx
callq 0x22b0
movl 0x2c(%rbx), %eax
movl 0x30(%rbx), %ecx
cmpl %eax, %ecx
jg 0x132ba
movl 0x10(%rbx), %ecx
movl 0x14(%rbx), %edx
subl 0x18(%rsp), %edx
addl %ecx, %eax
cmpl %eax, %edx
jle 0x13319
incl 0x4(%r15)
movq 0x10(%rsp), %r12
movq 0x58(%rsp), %rax
movq %rax, 0x40(%r15)
movq 0x60(%rsp), %rax
movq %rax, 0x10(%r15)
movaps 0x70(%rsp), %xmm0
movlps %xmm0, 0x34(%rbx)
movl 0x24(%rsp), %eax
movl %eax, 0x30(%rbx)
movq 0x18(%rbx), %rax
movq 0x50(%rsp), %rcx
movq %rcx, (%rax)
movq 0x10(%rax), %rdi
movq 0x28(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x22b0
movq 0x40(%rsp), %rbp
incq %r14
cmpq 0x30(%rsp), %r14
jne 0x12ed8
jmp 0x1333d
testb %bpl, %bpl
je 0x13323
cmpl %ecx, 0x34(%rbx)
jge 0x132ba
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rcx
callq 0x12def
jmp 0x132c3
movq 0x28(%rsp), %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2060