// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2710, 0x4(%rax)      # imm = 0x2710
jl 0x1a521
jmp 0x1ab67
movq -0x18(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x58(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x30(%rcx), %rcx
movq -0x38(%rbp), %rdx
imulq $0x30, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x74(%rbp)
movq -0x28(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, -0x60(%rbp)
imulq $0xc, -0x60(%rbp), %rdi
callq 0x2320
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x10(%rax), %rsi
imulq $0xc, -0x60(%rbp), %rdx
callq 0x22c0
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x1ab5b
movq -0x80(%rbp), %rax
imulq $0x30, -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movl 0x20(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x1a648
jmp 0x1ab4a
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x1a72c
movq -0x98(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1a6df
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb0(%rbp)
jmp 0x1a6ed
movl $0x2, %eax
movq %rax, -0xb0(%rbp)
jmp 0x1a6ed
movq -0xb0(%rbp), %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x98(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x48(%rbp), %rdx
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x98(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x1a77c
movq -0xa0(%rbp), %rdi
movl -0x64(%rbp), %esi
subl -0x68(%rbp), %esi
movq -0x90(%rbp), %rdx
callq 0x1d6d0
jmp 0x1a79c
movq -0xa0(%rbp), %rdi
movslq -0x68(%rbp), %rax
addq %rax, %rdi
movl -0x64(%rbp), %esi
subl -0x68(%rbp), %esi
movq -0x90(%rbp), %rdx
callq 0x1d860
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x90(%rbp), %rax
movq (%rax), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x28(%rbp), %rdi
callq 0x1dd90
movq -0x28(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x28(%rbp), %rcx
addl 0x10(%rcx), %eax
movq -0x28(%rbp), %rcx
subl 0x34(%rcx), %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x2c(%rcx), %eax
jg 0x1aa6f
cmpq $-0x1, -0x20(%rbp)
je 0x1a808
movq -0x20(%rbp), %rax
cmpq -0x48(%rbp), %rax
jne 0x1aa6f
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movl -0x78(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jg 0x1aa55
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
cmpl -0x70(%rbp), %eax
jle 0x1a83c
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x78(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
cmpq $-0x1, -0x20(%rbp)
jne 0x1a86e
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x1a86e
movq -0x98(%rbp), %rax
movq (%rax), %rcx
addq $-0x1, %rcx
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jne 0x1a8ed
movq -0x28(%rbp), %rax
movslq 0x38(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jne 0x1a8b5
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x28(%rbp), %rax
movslq 0x38(%rax), %rdx
callq 0x20a0
cmpl $0x0, %eax
je 0x1a8bf
movq -0x18(%rbp), %rax
movl $0x3, (%rax)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x1a8eb
movq -0x98(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x58, %rsi
callq 0x1d9b0
cmpl $0x0, %eax
je 0x1a8eb
movq -0x18(%rbp), %rax
movl $0x2, (%rax)
jmp 0x1a8ed
movq -0x18(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
movl 0x38(%rax), %esi
movq -0x18(%rbp), %rdx
addq $0x28, %rdx
callq 0x1d860
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x98(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1aa1b
jmp 0x1a932
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x98(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1aa17
movq -0x98(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
shrq %rcx
movq -0x18(%rbp), %rax
orq 0x60(%rax), %rcx
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
shrq $0x2, %rcx
movq -0x18(%rbp), %rax
orq 0x60(%rax), %rcx
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
shrq $0x4, %rcx
movq -0x18(%rbp), %rax
orq 0x60(%rax), %rcx
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
shrq $0x8, %rcx
movq -0x18(%rbp), %rax
orq 0x60(%rax), %rcx
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
shrq $0x10, %rcx
movq -0x18(%rbp), %rax
orq 0x60(%rax), %rcx
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x68(%rax)
jmp 0x1aa19
jmp 0x1aa1b
movq -0x98(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rdi
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x98(%rbp), %rax
movq (%rax), %rdx
shlq $0x3, %rdx
callq 0x22c0
jmp 0x1aa6d
movl -0x78(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x1aa6b
movl -0x78(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0x1aa6d
jmp 0x1aa6f
movq -0x28(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x2c(%rcx), %eax
jg 0x1aad7
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
subl -0x64(%rbp), %eax
movq -0x28(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq -0x28(%rbp), %rdx
addl 0x2c(%rdx), %ecx
cmpl %ecx, %eax
jg 0x1aad7
cmpq $-0x1, -0x20(%rbp)
je 0x1aaac
movq -0x20(%rbp), %rax
cmpq -0x48(%rbp), %rax
jne 0x1aabc
movq -0x28(%rbp), %rax
movl 0x34(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x1aad7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
callq 0x1a4f0
jmp 0x1aae4
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x58(%rbp), %rcx
movq -0x98(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
movq -0x90(%rbp), %rax
movq %rcx, (%rax)
movl -0x70(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x34(%rax)
movl -0x74(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x38(%rax)
movl -0x6c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x60(%rbp), %rcx
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x10(%rax), %rdi
movq -0xa8(%rbp), %rsi
imulq $0xc, -0x60(%rbp), %rdx
callq 0x22c0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x1a60d
movq -0xa8(%rbp), %rdi
callq 0x2060
addq $0xb0, %rsp
popq %rbp
retq