// File: /workspace/github_c_makefile_star2_O2/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 0xf14e
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %rbx
movq %rsi, %r15
leaq 0x10(%rsi), %rax
movq %rax, 0x20(%rsp)
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
imulq $0x30, (%rcx,%rax,8), %r13
addq 0x28(%rdi), %r13
movq %rdi, 0x30(%rsp)
movq 0x38(%rdi), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, 0x68(%rsp)
movq 0x34(%rbx), %rcx
movq %rcx, 0x38(%rsp)
movq %rcx, %xmm0
movdqa %xmm0, 0x70(%rsp)
movl 0x30(%rbx), %eax
movl %eax, 0x14(%rsp)
movq 0x18(%rbx), %r14
movq (%r14), %rax
movq %rax, 0x48(%rsp)
imulq $0xc, %rax, %r12
movq %r12, %rdi
callq 0x2310
movq 0x10(%r14), %rsi
movq %rax, 0x28(%rsp)
movq %rax, %rdi
movq %r12, 0x40(%rsp)
movq %r12, %rdx
callq 0x22b0
leaq 0x28(%r15), %rax
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
movq %r13, 0x50(%rsp)
cmpq 0x68(%rsp), %r12
je 0xf4b8
imulq $0x30, %r12, %rax
testb $0x40, 0x23(%r13,%rax)
jne 0xf48f
addq %r13, %rax
movq 0x8(%rax), %r13
movq 0x18(%rax), %r14
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movq %r13, %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
movl 0x18(%rax,%rcx), %edx
movl %edx, 0x4(%rsp)
movq 0x10(%rax,%rcx), %rbp
movq 0x40(%r15), %rcx
movq 0x50(%r15), %rax
cmpq 0x48(%r15), %rcx
jne 0xf26e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x48(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2350
movq %rax, 0x50(%r15)
movq 0x40(%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x40(%r15)
movq %r13, (%rax,%rcx,8)
testb $0x1, %r13b
jne 0xf29c
movslq %r14d, %rax
addq %rax, %rbp
movl 0x4(%rsp), %esi
subl %r14d, %esi
movq %rbp, %rdi
movq 0x20(%rsp), %rdx
callq 0x10bb0
jmp 0xf2b0
movl 0x4(%rsp), %esi
subl %r14d, %esi
movq %rbp, %rdi
movq 0x20(%rsp), %rdx
callq 0x10afe
movq 0x8(%rsp), %r14
cmpq $-0x1, %r14
sete %bpl
movq 0x20(%r15), %rax
movq %rax, 0x8(%rbx)
movl 0x10(%r15), %eax
movl %eax, 0x14(%rbx)
movq %rbx, %rdi
callq 0x10d99
movl 0x2c(%rbx), %eax
movl 0x30(%rbx), %ecx
movl 0x10(%rbx), %edx
addl %ecx, %edx
movl 0x34(%rbx), %esi
subl %esi, %edx
cmpq %r14, %r13
sete %r13b
orb %bpl, %r13b
cmpl %eax, %edx
jg 0xf42f
testb %r13b, %r13b
je 0xf42f
movl $0x1, (%r15)
movl 0x8(%r15), %edi
cmpl %edi, %edx
jle 0xf321
cmpl 0xc(%r15), %edx
jge 0xf42f
movl %edx, 0xc(%r15)
jmp 0xf42f
cmpl 0x38(%rsp), %esi
jle 0xf32b
movl %edi, 0xc(%r15)
movl %edx, 0x8(%r15)
movslq 0x38(%rbx), %rbp
cmpq $-0x1, 0x8(%rsp)
jne 0xf344
cmpl 0x14(%rbx), %ebp
jge 0xf344
decq 0x40(%r15)
cmpl 0xc(%r15), %edx
jne 0xf3a1
pushq $0x3
popq %rcx
movq 0x18(%rsp), %rax
cmpq %rbp, (%rax)
jne 0xf39e
movq 0x20(%r15), %rdi
movq 0x38(%r15), %rsi
movq %rbp, %rdx
callq 0x20a0
pushq $0x3
popq %rcx
testl %eax, %eax
jne 0xf39e
movq 0x40(%r15), %rax
pushq $0x2
popq %rcx
cmpq 0x58(%r15), %rax
jne 0xf39e
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0xf3a1
movq 0x50(%r15), %rdx
movq 0x68(%r15), %rsi
movq (%rdx,%rcx,8), %rdx
leaq 0x1(%rcx), %rdi
cmpq (%rsi,%rcx,8), %rdx
movq %rdi, %rcx
je 0xf37d
pushq $0x2
popq %rcx
movl %ecx, (%r15)
andq $0x0, 0x28(%r15)
movq 0x20(%r15), %rdi
movl %ebp, %esi
movq 0x18(%rsp), %rdx
callq 0x10bb0
movq 0x40(%r15), %rdx
movq 0x68(%r15), %rdi
cmpq %rdx, 0x60(%r15)
jae 0xf418
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 0xf443
movl 0x10(%rbx), %ecx
movl 0x14(%rbx), %edx
subl 0x4(%rsp), %edx
addl %ecx, %eax
cmpl %eax, %edx
jle 0xf497
incl 0x4(%r15)
movq 0x58(%rsp), %rax
movq %rax, 0x40(%r15)
movq 0x60(%rsp), %rax
movq %rax, 0x10(%r15)
movdqa 0x70(%rsp), %xmm0
movq %xmm0, 0x34(%rbx)
movl 0x14(%rsp), %eax
movl %eax, 0x30(%rbx)
movq 0x18(%rbx), %rax
movq 0x48(%rsp), %rcx
movq %rcx, (%rax)
movq 0x10(%rax), %rdi
movq 0x28(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x22b0
movq 0x50(%rsp), %r13
incq %r12
jmp 0xf1f4
testb %r13b, %r13b
je 0xf4a1
cmpl %ecx, 0x34(%rbx)
jge 0xf443
movq 0x30(%rsp), %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rcx
callq 0xf11d
jmp 0xf447
movq 0x28(%rsp), %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2060