// File: /workspace/github_c_makefile_star2_O0/c-zhou[P]oatk/path_finder.c
// Function: parse_organelle_minicircle
static int parse_organelle_minicircle(asg_t *asg, hmm_annot_db_t *annot_db, og_component_v *og_components,
        double *seg_annot_score, scg_meta_t *scg_meta, int n_threads, char *out_pref, int out_opt, 
        double max_eval, double seq_cf, int VERBOSE)
{
    if (og_components->n == 0) {
        if (VERBOSE > 0)
            fprintf(stderr, "[M::%s] no OG component found\n", __func__);
        return 1;
    }

    char *fn_str;
    MYMALLOC(fn_str, strlen(out_pref) + 64);
    //sprintf(fn_str, "%s.%s.stats", out_pref, OG_TYPES[OG_MINI]);
    //FILE *out_stats = fopen1(fn_str);
    sprintf(fn_str, "%s.%s.ctg.fasta", out_pref, OG_TYPES[OG_MINI]);
    FILE *out_ctg = fopen1(fn_str);
    sprintf(fn_str, "%s.%s.ctg.bed", out_pref, OG_TYPES[OG_MINI]);
    FILE *out_ctg_bed = fopen1(fn_str);
    sprintf(fn_str, "%s.%s.gfa", out_pref, OG_TYPES[OG_MINI]);
    FILE *out_gfa = fopen1(fn_str);
    sprintf(fn_str, "%s.%s.bed", out_pref, OG_TYPES[OG_MINI]);
    FILE *out_gfa_bed = fopen1(fn_str);
    free(fn_str);

    uint32_t i, anchor_sid;
    uint64_t n_seg;
    double max_s;
    og_component_t *component;
    asmg_t *asmg;
    hmm_annot_bed6_db_t *bed_annots;

    n_seg = asg->n_seg;
    asmg = scg_meta->scg->utg_asmg;
    
    // find the anchor sequence in the first og component
    component = &og_components->a[0];
    if (component->type != OG_MINI)
        return 1;
    
    max_s = .0;
    anchor_sid = 0;
    for (i = 0; i < component->nv; ++i) {
        uint32_t sid = component->v[i];
        double s = seg_annot_score[sid*4+OG_MINI];
        if (s > max_s) {
            max_s = s;
            anchor_sid = sid;
        }
    }

    if (VERBOSE > 0)
        fprintf(stderr, "[M::%s] anchor sequence found: %s [len %u; score, %.3f]\n", __func__,
                asg->seg[anchor_sid].name, asg->seg[anchor_sid].len, max_s);

    assert(n_seg == asmg->n_vtx);
    for (i = 0; i < n_seg; ++i)
        assert(asg->seg[i].len == asmg->vtx[i].len);

    // check if circular path existed
    int path_exists;
    uint32_t step;
    uint64_t dist;
    path_exists = asmg_path_exists(asmg, anchor_sid<<1, anchor_sid<<1, 0, COMMON_MAX_MINICIRCLE_SIZE, &step, &dist);
    if (VERBOSE > 0)
        fprintf(stderr, "[M::%s] circlar path %s found between anchor sequence in the original assembly graph: r=%u, d=%lu\n",
                __func__, path_exists? "WAS" : "NOT", step, dist);
    if (!path_exists) {
        // WARN assembly graph and anchor sequence will be updated here
        // remember to update asg
        // path_exists = extend_and_assemble_from_anchor_sequence(scg_meta, asg, &anchor_sid);
        if (VERBOSE > 0)
            fprintf(stderr, "[M::%s] circlar path %s found between anchor sequence in the reassembled graph\n",
                    __func__, path_exists? "WAS" : "NOT");
    }

    path_v paths = {0, 0, 0};

    if (path_exists) {
        // align reads and find minicircles
        asmg_clean_consensus(scg_meta->scg->utg_asmg);
        scg_read_alignment(scg_meta->sr_db, scg_meta->ra_db, scg_meta->scg, n_threads, 0);

#ifdef DEBUG_MINICIRCLE_REPEAT_UNIT
        scg_rv_print(scg_meta->ra_db, stderr);
#endif

        scg_consensus(scg_meta->sr_db, scg_meta->scg, 0, 0, 0);
        extract_minicircles_with_anchor(scg_meta->ra_db, scg_meta->scg, anchor_sid, n_threads, &paths);
    }

    // prepare assembly subgraph for output
    asmg_t *o_asmg = asg->asmg;
    asg->asmg = asg_make_asmg_copy(o_asmg, 0);
    asmg_subgraph(asg->asmg, &anchor_sid, 1, 0, 0, 0, 1);
    bed_annots = hmm_annot_bed6_db_init();

    if (paths.n == 0) {
        // not possible to solve the graph
        // likely because the graph is not circularisable or too complicated
        if (VERBOSE > 0)
            fprintf(stderr, "[M::%s] subgraph seeding from %s is unresolvable, output unitigs as unassembled\n",
                    __func__, asg->seg[anchor_sid].name);
        // FIXME sequences removed by graph clean are not included
        asg_print_fa(asg, stdout, 60);
        uint32_t c, v;
        c = 0;
        for (i = 0; i < component->nv; ++i) {
            v = component->v[i];
            if (asg->asmg->vtx[v].del) continue;
            // write sequence
            v <<= 1;
            path_t path = {0, 1, 0, 1, &v, asg->seg[v>>1].len,
                (double) asg->seg[v>>1].len * asg->seg[v>>1].cov, 0};
            print_seq(asg, &path, out_ctg, ++c, 0, 60, 100);
            path_add_hmm_annot_bed6(bed_annots, annot_db, asg, &path, c, 0, 100, OG_MINI, max_eval);
        }
    } else {
        uint32_t b;
        path_sort(&paths);
        // TODO improve the selection criteria
        b = select_best_seq(asg, &paths, 0, out_opt, seq_cf, 0, 0);
        // path_stats(asg, &paths, out_stats);
        print_seq(asg, &paths.a[b], out_ctg, 1, 0, 60, 100);
        path_add_hmm_annot_bed6(bed_annots, annot_db, asg, &paths.a[b], 1, 0, 100, OG_MINI, max_eval);
    }
    
    hmm_annot_print_bed6(bed_annots, out_ctg_bed, 1);

    uint64_t nv = 0;
    char **vlist = asg_vtx_name_list(asg, &nv);
    hmm_annot_formatted_print_sname_list(annot_db, vlist, nv, out_gfa_bed, OG_MINI, max_eval, 1);
    free(vlist);
    asg_print(asg, out_gfa, 0);
    asmg_destroy(asg->asmg);
    asg->asmg = o_asmg; // roll back to the original copy

    path_v_destroy(&paths);
    hmm_annot_bed6_db_destroy(bed_annots);

    //fclose(out_stats);
    fclose(out_ctg);
    fclose(out_ctg_bed);
    fclose(out_gfa);
    fclose(out_gfa_bed);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movsd %xmm0, -0x40(%rbp)
movsd %xmm1, -0x48(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x76e2
cmpl $0x0, 0x20(%rbp)
jle 0x76d6
movq 0x4f93a(%rip), %rax     # 0x56ff8
movq (%rax), %rdi
leaq 0x44e7c(%rip), %rsi     # 0x4c544
leaq 0x44e94(%rip), %rdx     # 0x4c563
movb $0x0, %al
callq 0x3320
movl $0x1, -0x4(%rbp)
jmp 0x7ec6
movq 0x10(%rbp), %rdi
callq 0x3190
movq %rax, %rdi
addq $0x40, %rdi
shlq $0x0, %rdi
callq 0x33b0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq 0x10(%rbp), %rdx
movq 0x4f45a(%rip), %rcx     # 0x56b68
leaq 0x44e69(%rip), %rsi     # 0x4c57e
movb $0x0, %al
callq 0x3510
movq -0x50(%rbp), %rdi
callq 0x73c0
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq 0x10(%rbp), %rdx
movq 0x4f430(%rip), %rcx     # 0x56b68
leaq 0x44e4f(%rip), %rsi     # 0x4c58e
movb $0x0, %al
callq 0x3510
movq -0x50(%rbp), %rdi
callq 0x73c0
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rdi
movq 0x10(%rbp), %rdx
movq 0x4f406(%rip), %rcx     # 0x56b68
leaq 0x44e33(%rip), %rsi     # 0x4c59c
movb $0x0, %al
callq 0x3510
movq -0x50(%rbp), %rdi
callq 0x73c0
movq %rax, -0x68(%rbp)
movq -0x50(%rbp), %rdi
movq 0x10(%rbp), %rdx
movq 0x4f3dc(%rip), %rcx     # 0x56b68
leaq 0x44e13(%rip), %rsi     # 0x4c5a6
movb $0x0, %al
callq 0x3510
movq -0x50(%rbp), %rdi
callq 0x73c0
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rdi
callq 0x3060
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpl $0x3, (%rax)
je 0x77f6
movl $0x1, -0x4(%rbp)
jmp 0x7ec6
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
movl $0x0, -0x78(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jae 0x788c
movq -0x90(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x74(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0xa4(%rbp)
movq -0x28(%rbp), %rax
movl -0xa4(%rbp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xb0(%rbp), %xmm0
ucomisd -0x88(%rbp), %xmm0
jbe 0x787f
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, -0x78(%rbp)
jmp 0x7881
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x780f
cmpl $0x0, 0x20(%rbp)
jle 0x78e4
movq 0x4f75f(%rip), %rax     # 0x56ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x78(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl 0x10(%rax), %r8d
movsd -0x88(%rbp), %xmm0
leaq 0x44cda(%rip), %rsi     # 0x4c5b0
leaq 0x44c86(%rip), %rdx     # 0x4c563
movb $0x1, %al
callq 0x3320
movq -0x80(%rbp), %rax
movq -0x98(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x78f6
jmp 0x7915
leaq 0x44cec(%rip), %rdi     # 0x4c5e9
leaq 0x44cfa(%rip), %rsi     # 0x4c5fe
movl $0x312, %edx            # imm = 0x312
leaq 0x44cfc(%rip), %rcx     # 0x4c60c
callq 0x3220
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpq -0x80(%rbp), %rax
jae 0x7983
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x98(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x74(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x18(%rcx), %rax
jne 0x7957
jmp 0x7976
leaq 0x44d39(%rip), %rdi     # 0x4c697
leaq 0x44c99(%rip), %rsi     # 0x4c5fe
movl $0x314, %edx            # imm = 0x314
leaq 0x44c9b(%rip), %rcx     # 0x4c60c
callq 0x3220
jmp 0x7978
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x791c
movq -0x98(%rbp), %rdi
movl -0x78(%rbp), %esi
shll %esi
movl -0x78(%rbp), %edx
shll %edx
xorl %ecx, %ecx
movl $0xc350, %r8d           # imm = 0xC350
leaq -0xb8(%rbp), %r9
leaq -0xc0(%rbp), %rax
movq %rax, (%rsp)
callq 0x429c0
movl %eax, -0xb4(%rbp)
cmpl $0x0, 0x20(%rbp)
jle 0x7a07
movq 0x4f632(%rip), %rax     # 0x56ff8
movq (%rax), %rdi
movl -0xb4(%rbp), %edx
leaq 0x44d4c(%rip), %rcx     # 0x4c722
leaq 0x44d41(%rip), %rax     # 0x4c71e
cmpl $0x0, %edx
cmovneq %rax, %rcx
movl -0xb8(%rbp), %r8d
movq -0xc0(%rbp), %r9
leaq 0x44cc2(%rip), %rsi     # 0x4c6bb
leaq 0x44b63(%rip), %rdx     # 0x4c563
movb $0x0, %al
callq 0x3320
cmpl $0x0, -0xb4(%rbp)
jne 0x7a52
cmpl $0x0, 0x20(%rbp)
jle 0x7a50
movq 0x4f5db(%rip), %rax     # 0x56ff8
movq (%rax), %rdi
movl -0xb4(%rbp), %edx
leaq 0x44cf5(%rip), %rcx     # 0x4c722
leaq 0x44cea(%rip), %rax     # 0x4c71e
cmpl $0x0, %edx
cmovneq %rax, %rcx
leaq 0x44ce4(%rip), %rsi     # 0x4c726
leaq 0x44b1a(%rip), %rdx     # 0x4c563
movb $0x0, %al
callq 0x3320
jmp 0x7a52
leaq -0xd8(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3240
cmpl $0x0, -0xb4(%rbp)
je 0x7ae4
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0xa6f0
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdx
movl -0x34(%rbp), %ecx
xorl %r8d, %r8d
callq 0x44870
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %r8d
movl %ecx, %edx
callq 0x10290
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0x78(%rbp), %eax
movl %eax, %edx
movl -0x34(%rbp), %ecx
leaq -0xd8(%rbp), %r8
callq 0xa7e0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x31bb0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x78(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
movl $0x1, (%rsp)
callq 0x420a0
movb $0x0, %al
callq 0xc100
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xd8(%rbp)
jne 0x7d3a
cmpl $0x0, 0x20(%rbp)
jle 0x7b8b
movq 0x4f49a(%rip), %rax     # 0x56ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
leaq 0x44bf9(%rip), %rsi     # 0x4c776
leaq 0x449df(%rip), %rdx     # 0x4c563
movb $0x0, %al
callq 0x3320
movq -0x10(%rbp), %rdi
movq 0x4f442(%rip), %rax     # 0x56fd8
movq (%rax), %rsi
movl $0x3c, %edx
callq 0x33d90
movl $0x0, -0xe4(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jae 0x7d35
movq -0x90(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x74(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0xe8(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movl -0xe8(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x7c0a
jmp 0x7d27
movl -0xe8(%rbp), %eax
addl %eax, %eax
movl %eax, -0xe8(%rbp)
movq $0x0, -0x118(%rbp)
movl -0x110(%rbp), %eax
andl $0xc0000000, %eax       # imm = 0xC0000000
incl %eax
movl %eax, -0x110(%rbp)
movb -0x10d(%rbp), %al
andb $-0x41, %al
movb %al, -0x10d(%rbp)
movb -0x10d(%rbp), %al
orb $-0x80, %al
movb %al, -0x10d(%rbp)
leaq -0xe8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xe8(%rbp), %ecx
shrl %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0x10(%rax,%rcx,8), %eax
movl %eax, -0x100(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xe8(%rbp), %ecx
shrl %ecx
leaq (%rcx,%rcx,2), %rdx
movl 0x10(%rax,%rdx,8), %ecx
movl 0x14(%rax,%rdx,8), %eax
cvtsi2sd %rcx, %xmm0
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0xf8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xf0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rdx
movl -0xe4(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0xe4(%rbp)
leaq -0x118(%rbp), %rsi
xorl %r8d, %r8d
movl $0x3c, %r9d
movl $0x64, (%rsp)
callq 0x30610
movq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rdx
movl -0xe4(%rbp), %r8d
movsd -0x40(%rbp), %xmm0
leaq -0x118(%rbp), %rcx
xorl %r9d, %r9d
movl $0x64, (%rsp)
movl $0x3, 0x8(%rsp)
callq 0x30e60
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x7bb4
jmp 0x7de9
leaq -0xd8(%rbp), %rdi
callq 0x2f200
movq -0x10(%rbp), %rdi
movl 0x18(%rbp), %ecx
movsd -0x48(%rbp), %xmm0
leaq -0xd8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
xorl %r9d, %r9d
movl %r9d, %r8d
callq 0x312c0
movl %eax, -0x11c(%rbp)
movq -0x10(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movl -0x11c(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rsi
movq -0x58(%rbp), %rdx
movl $0x1, %ecx
xorl %r8d, %r8d
movl $0x3c, %r9d
movl $0x64, (%rsp)
callq 0x30610
movq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0xc8(%rbp), %rcx
movl -0x11c(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rcx
movsd -0x40(%rbp), %xmm0
movl $0x1, %r8d
xorl %r9d, %r9d
movl $0x64, (%rsp)
movl $0x3, 0x8(%rsp)
callq 0x30e60
movq -0xa0(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl $0x1, %edx
callq 0xd9e0
movq $0x0, -0x128(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x39c10
movq %rax, -0x130(%rbp)
movq -0x18(%rbp), %rdi
movq -0x130(%rbp), %rsi
movq -0x128(%rbp), %rdx
movq -0x70(%rbp), %rcx
movsd -0x40(%rbp), %xmm0
movl $0x3, %r8d
movl $0x1, %r9d
callq 0xd500
movq -0x130(%rbp), %rdi
callq 0x3060
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
xorl %edx, %edx
callq 0x32200
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x3e2e0
movq -0xe0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
leaq -0xd8(%rbp), %rdi
callq 0x2a400
movq -0xa0(%rbp), %rdi
callq 0xc130
movq -0x58(%rbp), %rdi
callq 0x3170
movq -0x60(%rbp), %rdi
callq 0x3170
movq -0x68(%rbp), %rdi
callq 0x3170
movq -0x70(%rbp), %rdi
callq 0x3170
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw %cs:(%rax,%rax)