// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %rdx, 0x8(%rsp)
cmpl $0x270f, 0x4(%rsi)      # imm = 0x270F
jle 0x1237e
addq $0x78, %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, 0x68(%rsp)
movq 0x40(%rsi), %rcx
movq 0x50(%rsi), %rax
movq %rcx, 0x60(%rsp)
movq -0x8(%rax,%rcx,8), %rax
movq 0x30(%rdi), %rcx
movq 0x38(%rdi), %rdx
movq (%rcx,%rax,8), %rcx
leaq (%rcx,%rcx,2), %r13
shlq $0x4, %r13
addq 0x28(%rdi), %r13
movq (%rdx,%rax,8), %rax
movq %rax, 0x38(%rsp)
movl 0x34(%rbx), %eax
movl %eax, 0x14(%rsp)
movl 0x38(%rbx), %eax
movl %eax, 0x24(%rsp)
movl 0x30(%rbx), %eax
movl %eax, 0x20(%rsp)
movq 0x18(%rbx), %r14
movq (%r14), %rax
movq %rax, 0x58(%rsp)
leaq (,%rax,4), %rax
leaq (%rax,%rax,2), %rbp
movq %rbp, %rdi
callq 0x2320
movq 0x10(%r14), %rsi
movq %rax, 0x30(%rsp)
movq %rax, %rdi
movq %rbp, 0x50(%rsp)
movq %rbp, %rdx
callq 0x22c0
cmpq $0x0, 0x38(%rsp)
je 0x12881
movq %r13, %rcx
xorl %r14d, %r14d
movq %r12, 0x28(%rsp)
movq %r13, 0x48(%rsp)
leaq (%r14,%r14,2), %rax
shlq $0x4, %rax
testb $0x40, 0x23(%rcx,%rax)
jne 0x12840
addq %rcx, %rax
movq 0x8(%rax), %rbp
movq 0x18(%rax), %rsi
movq 0x10(%r12), %rax
movq %rbp, %rcx
shrq %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x10(%rax,%rcx,8), %r13
movq 0x18(%rax,%rcx,8), %r12
movq 0x48(%r15), %rax
cmpq %rax, 0x40(%r15)
jne 0x1249d
movq %rsi, 0x18(%rsp)
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x48(%r15)
movq 0x50(%r15), %rdi
shlq $0x3, %rsi
callq 0x2360
movq 0x18(%rsp), %rsi
movq %rax, 0x50(%r15)
movq 0x40(%r15), %rax
movq 0x50(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x40(%r15)
movq %rbp, (%rcx,%rax,8)
movq %rbp, 0x40(%rsp)
testb $0x1, %bpl
movq %r12, 0x70(%rsp)
jne 0x1254b
movl %r12d, %eax
subl %esi, %eax
movq 0x10(%r15), %rcx
movslq %eax, %rbp
leaq (%rcx,%rbp), %rax
incq %rax
cmpq 0x18(%r15), %rax
jb 0x1252d
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 %rsi, 0x18(%rsp)
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x18(%r15)
movq 0x20(%r15), %rdi
callq 0x2360
movq 0x18(%rsp), %rsi
movq %rax, 0x20(%r15)
movslq %esi, %rax
addq %rax, %r13
movq 0x20(%r15), %rdi
addq 0x10(%r15), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x22c0
jmp 0x125d6
subl %esi, %r12d
movq 0x10(%r15), %rax
movslq %r12d, %rbp
addq %rbp, %rax
incq %rax
cmpq 0x18(%r15), %rax
jb 0x125a6
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
callq 0x2360
movq %rax, 0x20(%r15)
testl %r12d, %r12d
jle 0x125d6
movslq 0x10(%r15), %rax
leaq 0x1(%rbp), %rcx
leaq 0xb8d6(%rip), %rdi      # 0x1de90
movsbq -0x2(%r13,%rcx), %rdx
movb (%rdi,%rdx), %dl
movq 0x20(%r15), %rsi
movb %dl, (%rsi,%rax)
incq %rax
decq %rcx
cmpq $0x1, %rcx
jg 0x125ba
movq 0x8(%rsp), %r13
cmpq $-0x1, %r13
sete %r12b
addq 0x10(%r15), %rbp
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 0x1397c
movl 0x10(%rbx), %eax
movl 0x34(%rbx), %ecx
addl 0x30(%rbx), %eax
subl %ecx, %eax
movq 0x40(%rsp), %rsi
cmpq %r13, %rsi
sete %dl
cmpl 0x2c(%rbx), %eax
jg 0x127d4
orb %dl, %r12b
je 0x127d4
movl $0x1, (%r15)
movl 0x8(%r15), %edx
cmpl %edx, %eax
jle 0x12654
cmpl 0xc(%r15), %eax
jge 0x127d4
movl %eax, 0xc(%r15)
jmp 0x127d4
cmpl 0x14(%rsp), %ecx
jle 0x1265e
movl %edx, 0xc(%r15)
movl %eax, 0x8(%r15)
cmpq $-0x1, 0x8(%rsp)
jne 0x12676
movl 0x38(%rbx), %ecx
cmpl 0x14(%rbx), %ecx
jge 0x12676
decq 0x40(%r15)
cmpl 0xc(%r15), %eax
jne 0x126d8
movslq 0x38(%rbx), %rdx
cmpq %rdx, 0x28(%r15)
jne 0x12697
movq 0x20(%r15), %rdi
movq 0x38(%r15), %rsi
callq 0x20a0
testl %eax, %eax
je 0x1269e
movl $0x3, (%r15)
cmpl $0x1, (%r15)
jne 0x126d8
movq 0x40(%r15), %rax
cmpq 0x58(%r15), %rax
jne 0x126d1
testq %rax, %rax
je 0x126d8
movq 0x50(%r15), %rcx
movq 0x68(%r15), %rdx
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
cmpq (%rdx,%rsi,8), %rdi
jne 0x126d1
incq %rsi
cmpq %rsi, %rax
jne 0x126bd
jmp 0x126d8
movl $0x2, (%r15)
movq $0x0, 0x28(%r15)
movq 0x20(%r15), %rbp
movslq 0x38(%rbx), %r13
leaq 0x1(%r13), %rax
cmpq 0x30(%r15), %rax
jb 0x12737
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 0x2360
movq %rax, 0x38(%r15)
movq 0x38(%r15), %rdi
addq 0x28(%r15), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x22c0
addq 0x28(%r15), %r13
movq %r13, 0x28(%r15)
movq 0x38(%r15), %rax
movb $0x0, (%rax,%r13)
movq 0x40(%r15), %rax
cmpq %rax, 0x60(%r15)
jae 0x127b6
decq %rax
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, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x60(%r15)
movq 0x68(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2360
movq %rax, 0x68(%r15)
movq 0x40(%r15), %rdx
movq 0x50(%r15), %rsi
movq %rdx, 0x58(%r15)
movq 0x68(%r15), %rdi
shlq $0x3, %rdx
callq 0x22c0
movq 0x40(%rsp), %rsi
movl 0x2c(%rbx), %ecx
cmpl %ecx, 0x30(%rbx)
jg 0x127ec
movl 0x10(%rbx), %eax
movl 0x14(%rbx), %edx
subl 0x70(%rsp), %edx
addl %eax, %ecx
cmpl %ecx, %edx
jle 0x12850
incl 0x4(%r15)
movq 0x28(%rsp), %r12
movq 0x60(%rsp), %rax
movq %rax, 0x40(%r15)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%r15)
movl 0x14(%rsp), %eax
movl %eax, 0x34(%rbx)
movl 0x24(%rsp), %eax
movl %eax, 0x38(%rbx)
movl 0x20(%rsp), %eax
movl %eax, 0x30(%rbx)
movq 0x18(%rbx), %rax
movq 0x58(%rsp), %rcx
movq %rcx, (%rax)
movq 0x10(%rax), %rdi
movq 0x30(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x22c0
movq 0x48(%rsp), %rcx
incq %r14
cmpq 0x38(%rsp), %r14
jne 0x1242d
jmp 0x12881
cmpq $-0x1, 0x8(%rsp)
je 0x1285f
cmpq 0x8(%rsp), %rsi
jne 0x12864
cmpl %eax, 0x34(%rbx)
jge 0x127ec
movq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rcx
callq 0x12353
jmp 0x127f5
movq 0x30(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2060