// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/yahs.c
// Function: run_yahs
int run_yahs(char *fai, char *agp, char *link_file, uint32_t ml, uint8_t mq, char *out, int *resolutions, int nr, int rr, re_cuts_t *re_cuts, int8_t *telo_ends, uint32_t d_min_cell, double d_mass_frac, int no_contig_ec, int no_scaffold_ec, int no_mem_check)
{
    int ec_round, resolution, re, r, rn, rc, ex;
    uint64_t n50;
    char *out_fn, *out_agp, *out_agp_break;
    double noise;
    FILE *fo;
    sdict_t *sdict;
    asm_dict_t *dict;
    cov_norm_t *cov_norm;
    long rss_total, rss_limit;  
    
    ram_limit(&rss_total, &rss_limit);
    fprintf(stderr, "[I::%s] RAM total: %.3fGB\n", __func__, (double) rss_total / GB);
    fprintf(stderr, "[I::%s] RAM limit: %.3fGB\n", __func__, (double) rss_limit / GB);
    if (no_mem_check)
        fprintf(stderr, "[I::%s] RAM check disabled\n", __func__);

    sdict = make_sdict_from_index(fai, ml);
    out_fn = (char *) malloc(strlen(out) + 35);
    out_agp = (char *) malloc(strlen(out) + 35);
    out_agp_break = (char *) malloc(strlen(out) + 35);
    
    if (no_contig_ec == 0) {
#ifdef DEBUG
        fprintf(stderr, "[DEBUG::%s] perform contig error break...\n", __func__);
#endif
        sprintf(out_agp_break, "%s_inital_break", out);
        ec_round = contig_error_break(agp, fai, link_file, ml, out_agp_break);
        sprintf(out_agp_break, "%s_inital_break_%02d.agp", out, ec_round);
#ifdef DEBUG
        fprintf(stderr, "[DEBUG::%s] contig error break done\n", __func__);
#endif
    } else {
#ifdef DEBUG
        fprintf(stderr, "[DEBUG::%s] no contig error break...\n", __func__);
#endif
        if (agp != 0) {
#ifdef DEBUG
            fprintf(stderr, "[DEBUG::%s] use input AGP file\n", __func__);
#endif
            if (strlen(agp) > strlen(out)) {
                free(out_agp_break);
                out_agp_break = (char *) malloc(strlen(agp) + 35);
            }
            sprintf(out_agp_break, "%s", agp);
        } else {
#ifdef DEBUG
            fprintf(stderr, "[DEBUG::%s] make AGP file from input FASTA file\n", __func__);
#endif
            sprintf(out_agp_break, "%s_no_break.agp", out);
            fo = fopen(out_agp_break, "w");
            if (fo == NULL) {
                fprintf(stderr, "[E::%s] cannot open file %s for writing\n", __func__, out_agp_break);
                exit(EXIT_FAILURE);
            }
            write_sdict_to_agp(sdict, fo);
            fclose(fo);
        }
    }

    dict = make_asm_dict_from_agp(sdict, out_agp_break, 1);
    if (dict->n > MAX_N_SEQ) {
        fprintf(stderr, "[E::%s] sequence number exceeds limit (%d > %d)\n", __func__, dict->n, MAX_N_SEQ);
        fprintf(stderr, "[E::%s] consider removing short sequences before scaffolding, or\n", __func__);
        fprintf(stderr, "[E::%s] running without error correction (--no-contig-ec) if due to excessive contig error breaks\n", __func__);
        fprintf(stderr, "[E::%s] program halted...\n", __func__);
        return 1;
    }
    asm_sd_stats(dict, n_stats, l_stats);
    print_asm_stats(n_stats, l_stats, 1);
    asm_destroy(dict);

    cov_norm = cov_norm_from_file(link_file, sdict);

    r = rc = 0;
    rn = rr;
    ex = max_extra_try;
    while (r < nr) {
        resolution = resolutions[r];

        // dict = make_asm_dict_from_agp(sdict, out_agp_break, 1);
        if (n_stats[4] < resolution * 10) {
            if (!ex) {
                fprintf(stderr, "[I::%s] assembly N50 (%lu) too small. End of scaffolding.\n", __func__, n_stats[4]);
                fprintf(stderr, "[I::%s] consider running with increased memory limit if there was a memory issue.\n", __func__);
                break;
            } else {
                if (r > 0)
                    resolution = resolutions[r - 1]; // use the previous resolution level
                else {
                    resolution /= 10;
                    if (resolution < 1000)
                        resolution = 1000;
                    if (resolution > resolutions[0])
                        resolution = resolutions[0] / 2;
                }

                fprintf(stderr, "[I::%s] assembly N50 (%lu) too small.\n", __func__, n_stats[4]);
                fprintf(stderr, "[I::%s] do an extra round scaffolding with resolution = %d\n", __func__, resolution);
                ex--; // increase extra try number
            }
        } else {
            rn--;
            if (!rn || n_stats[8] > resolution * 10) {
                r++; // move to the next resolution level
                rn = rr;
            }
            ex = max_extra_try; // reset extra try number
        }

        rc++;
        fprintf(stderr, "[I::%s] scaffolding round %d resolution = %d\n", __func__, rc, resolution);
        
        sprintf(out_fn, "%s_r%02d", out, rc);
        // noise per unit
        re = run_scaffolding(fai, out_agp_break, link_file, cov_norm, ml, mq, re_cuts, telo_ends, out_fn, resolution, 
                &noise, d_min_cell, d_mass_frac, rss_limit, no_mem_check);
        if (!re) {
            sprintf(out_agp, "%s_r%02d.agp", out, rc);
            if (no_scaffold_ec == 0) {
#ifdef DEBUG
                fprintf(stderr, "[DEBUG::%s] perform scaffold error break\n", __func__);
#endif

                sprintf(out_agp_break, "%s_r%02d_break.agp", out, rc);
                scaffold_error_break(fai, link_file, ml, mq, out_agp, resolution, noise, out_agp_break);
#ifdef DEBUG
                fprintf(stderr, "[DEBUG::%s] scaffold error break done\n", __func__);
#endif

            } else {
#ifdef DEBUG
                fprintf(stderr, "[DEBUG::%s] no scaffold error break\n", __func__);
#endif
                sprintf(out_agp_break, "%s", out_agp);
            }
        }
        // asm_destroy(dict);

        fprintf(stderr, "[I::%s] scaffolding round %d done\n", __func__, rc);

        dict = make_asm_dict_from_agp(sdict, out_agp_break, 1);
        n50 = n_stats[4]; // old n50
        asm_sd_stats(dict, n_stats, l_stats);
        // no improvements in a retry round 
        if (ex < max_extra_try && n50 == n_stats[4])
            ex = 0;
        print_asm_stats(n_stats, l_stats, 0);
        asm_destroy(dict);

        /*** 
        if (re && re != ENOMEM_ERR) {
            fprintf(stderr, "[I::%s] Reach data limit for HiC contact density estimation in lower resulutions. End of scaffolding.\n", __func__);
            break;
        }
        **/
    }

#ifdef DEBUG
    fprintf(stderr, "[DEBUG::%s] make final output...\n", __func__);
#endif

    sprintf(out_agp, "%s_scaffolds_final.agp", out);
    // output sorted agp by scaffold size instead of file copy
    // file_copy(out_agp_break, out_agp);
    if (ml > 0) {
        // add short sequences to dict
#ifdef DEBUG
        fprintf(stderr, "[DEBUG::%s] add unused short sequences back...\n", __func__);
#endif
        sd_destroy(sdict);
        sdict = make_sdict_from_index(fai, 0);
        dict = make_asm_dict_from_agp(sdict, out_agp_break, 1);
        add_unplaced_short_seqs(dict, ml);
    } else {
        dict = make_asm_dict_from_agp(sdict, out_agp_break, 1);
    }
    fo = fopen(out_agp, "w");
    if (fo == NULL) {
        fprintf(stderr, "[E::%s] cannot open file %s for writing\n", __func__, out_agp);
        exit(EXIT_FAILURE);
    }
    write_sorted_agp(dict, fo);
    fclose(fo);
    
    asm_destroy(dict);
    sd_destroy(sdict);
    cov_norm_destroy(cov_norm);

    free(out_agp);
    free(out_fn);
    free(out_agp_break);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movsd %xmm0, 0x68(%rsp)
movq %r9, 0x18(%rsp)
movl %r8d, 0x4c(%rsp)
movl %ecx, %r13d
movq %rdx, 0x30(%rsp)
movq %rsi, %r14
movq %rdi, 0x38(%rsp)
movl 0x100(%rsp), %r15d
leaq 0x80(%rsp), %rbx
leaq 0x70(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x496d
movq 0x285f8(%rip), %rbp     # 0x2bff8
movq (%rbp), %rdi
cvtsi2sdq (%rbx), %xmm0
mulsd 0x20acf(%rip), %xmm0    # 0x244e0
leaq 0x20e7a(%rip), %rsi     # 0x24892
leaq 0x20e8e(%rip), %rbx     # 0x248ad
movq %rbx, %rdx
movb $0x1, %al
callq 0x22c0
movq (%rbp), %rdi
cvtsi2sdq (%r12), %xmm0
mulsd 0x20aa5(%rip), %xmm0    # 0x244e0
leaq 0x20e74(%rip), %rsi     # 0x248b6
movq %rbx, %rdx
movb $0x1, %al
callq 0x22c0
testl %r15d, %r15d
je 0x3a6a
movq (%rbp), %rdi
leaq 0x20e75(%rip), %rsi     # 0x248d1
leaq 0x20e4a(%rip), %rdx     # 0x248ad
xorl %eax, %eax
callq 0x22c0
movq 0x38(%rsp), %r15
movq %r15, %rdi
movl %r13d, %esi
callq 0x12d2b
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
callq 0x2160
movq %rax, %rbx
leaq 0x23(%rax), %r12
movq %r12, %rdi
callq 0x2330
movq %rax, 0x60(%rsp)
movq %r12, %rdi
callq 0x2330
movq %rax, 0x58(%rsp)
movq %r12, %rdi
callq 0x2330
movl %r13d, %r12d
movq %rax, %r13
cmpl $0x0, 0xf0(%rsp)
movl %r12d, 0xc(%rsp)
jne 0x3b14
leaq 0x20e1c(%rip), %rsi     # 0x248ed
movq %r13, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x24a0
movq %r14, %rdi
movq %r15, %rsi
movq 0x30(%rsp), %rdx
movl %r12d, %ecx
movq %r13, %r8
callq 0x37a0
leaq 0x20e02(%rip), %rsi     # 0x248fd
movq %r13, %rdi
movq %rbp, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x24a0
movq 0x10(%rsp), %r12
jmp 0x3ba3
testq %r14, %r14
jne 0x3b67
leaq 0x20df6(%rip), %rsi     # 0x24916
movq %r13, %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rdx
xorl %eax, %eax
callq 0x24a0
leaq 0x1e4d7(%rip), %rsi     # 0x22010
movq %r13, %rdi
callq 0x23e0
testq %rax, %rax
je 0x4070
movq %rax, %rbx
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x15f48
movq %rbx, %rdi
callq 0x2150
jmp 0x3ba3
movq %r14, %rdi
callq 0x2160
cmpq %rbx, %rax
movq 0x18(%rsp), %rbp
jbe 0x3b93
movq %rax, %r12
movq %r13, %rdi
callq 0x2060
addq $0x23, %r12
movq %r12, %rdi
callq 0x2330
movq %rax, %r13
movq 0x10(%rsp), %r12
movq %r13, %rdi
movq %r14, %rsi
callq 0x20c0
pushq $0x1
popq %rbx
movq %r12, %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0x1373c
movl (%rax), %ecx
cmpl $0xafc9, %ecx           # imm = 0xAFC9
jb 0x3c26
movq 0x28434(%rip), %r15     # 0x2bff8
movq (%r15), %rdi
leaq 0x20d58(%rip), %rsi     # 0x24926
leaq 0x20cd8(%rip), %r14     # 0x248ad
movq %r14, %rdx
movl $0xafc8, %r8d           # imm = 0xAFC8
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x20d68(%rip), %rsi     # 0x24957
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x20d96(%rip), %rsi     # 0x24999
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x20de5(%rip), %rsi     # 0x249fc
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x401c
movq %rax, %r14
leaq 0x28a00(%rip), %rsi     # 0x2c630
leaq 0x28a49(%rip), %rdx     # 0x2c680
movq %rax, %rdi
callq 0x14ed9
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x40b7
movq %r14, %rdi
callq 0x122d1
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0xe8b6
movq %rax, 0x50(%rsp)
cmpl $0x0, 0xc8(%rsp)
jle 0x3f4f
movl 0xd0(%rsp), %eax
pushq $0x3
popq %r15
movl %ebx, %r14d
xorl %edx, %edx
movl %eax, 0x24(%rsp)
movq %r13, 0x40(%rsp)
movslq %edx, %rax
movq 0xc0(%rsp), %rcx
movslq (%rcx,%rax,4), %r12
movq 0x289b0(%rip), %rcx     # 0x2c650
imulq $0xa, %r12, %rax
cmpq %rax, %rcx
jae 0x3ccb
testl %r15d, %r15d
je 0x4030
testl %edx, %edx
movl %edx, 0x2c(%rsp)
jle 0x3d0f
movl %edx, %eax
movq 0xc0(%rsp), %rdx
movl -0x4(%rdx,%rax,4), %r12d
jmp 0x3d40
movl 0x24(%rsp), %esi
decl %esi
sete %cl
cmpq %rax, 0x28995(%rip)     # 0x2c670
seta %al
orb %cl, %al
movl 0xd0(%rsp), %ecx
jne 0x3ceb
movl %esi, %ecx
movzbl %al, %eax
addl %eax, %edx
movl %edx, 0x2c(%rsp)
pushq $0x3
popq %rax
movl %eax, 0x28(%rsp)
movl %ecx, 0x24(%rsp)
movq 0x282f2(%rip), %r15     # 0x2bff8
leaq 0x20ba0(%rip), %rbx     # 0x248ad
jmp 0x3d86
movl %r12d, %eax
cltd
pushq $0xa
popq %rsi
idivl %esi
movl %eax, %r12d
cmpl $0x3e9, %eax            # imm = 0x3E9
jge 0x3d28
movl $0x3e8, %r12d           # imm = 0x3E8
movq 0xc0(%rsp), %rax
movl (%rax), %eax
cmpl %eax, %r12d
jle 0x3d40
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %r12d
movq 0x282b1(%rip), %r13     # 0x2bff8
movq (%r13), %rdi
leaq 0x20d53(%rip), %rsi     # 0x24aa5
leaq 0x20b54(%rip), %rbx     # 0x248ad
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r13), %rdi
leaq 0x20d5e(%rip), %rsi     # 0x24acc
movq %rbx, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x22c0
decl %r15d
movl %r15d, 0x28(%rsp)
movq %r13, %r15
movq (%r15), %rdi
leaq 0x20d78(%rip), %rsi     # 0x24b08
movq %rbx, %rdx
movl %r14d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x22c0
movq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x20d87(%rip), %rsi     # 0x24b36
movq %rbp, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x24a0
movzbl 0x4c(%rsp), %r9d
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rbp
movq %rbp, %rsi
movq 0x30(%rsp), %rdx
movq 0x50(%rsp), %rcx
movl 0xc(%rsp), %r8d
movl %r9d, 0x48(%rsp)
movsd 0x68(%rsp), %xmm0
movl 0x100(%rsp), %eax
pushq %rax
pushq 0x78(%rsp)
movl 0xf8(%rsp), %eax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
pushq %r12
pushq %r13
pushq 0x110(%rsp)
pushq 0x110(%rsp)
callq 0x1f4c0
addq $0x40, %rsp
testl %eax, %eax
movq %rbx, %rdx
jne 0x3ebc
movq %r15, %rbx
movq 0x58(%rsp), %r13
movq %r13, %rdi
leaq 0x20d01(%rip), %rsi     # 0x24b3f
movq 0x18(%rsp), %r15
movq %r15, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x24a0
cmpl $0x0, 0xf8(%rsp)
jne 0x3ea2
movq 0x40(%rsp), %rbp
movq %rbp, %rdi
leaq 0x20ce3(%rip), %rsi     # 0x24b4c
movq %r15, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x24a0
movsd 0x78(%rsp), %xmm0
subq $0x8, %rsp
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl 0x14(%rsp), %edx
movl 0x50(%rsp), %ecx
movq %r13, %r8
movl %r12d, %r9d
pushq %rbp
callq 0x1f8c0
popq %rcx
popq %rdx
jmp 0x3eb2
movq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x20c0
leaq 0x209f4(%rip), %rdx     # 0x248ad
movq %rbx, %r15
movq (%r15), %rdi
leaq 0x20c99(%rip), %rsi     # 0x24b5f
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22c0
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rdx
callq 0x1373c
movq %rax, %rbx
movq 0x28763(%rip), %r15     # 0x2c650
movq %rax, %rdi
leaq 0x28739(%rip), %rsi     # 0x2c630
leaq 0x28782(%rip), %rdx     # 0x2c680
callq 0x14ed9
cmpq 0x28746(%rip), %r15     # 0x2c650
movl 0x28(%rsp), %ecx
movl %ecx, %r15d
movl $0x0, %eax
cmovel %eax, %r15d
cmpl $0x3, %ecx
pushq $0x3
popq %rax
cmovgel %eax, %r15d
xorl %edi, %edi
callq 0x40b7
movq %rbx, %rdi
callq 0x122d1
incl %r14d
movl 0x2c(%rsp), %edx
cmpl 0xc8(%rsp), %edx
movq %rbp, %r13
movq 0x18(%rsp), %rbp
jl 0x3c8a
leaq 0x20c2c(%rip), %rsi     # 0x24b82
movq 0x58(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x24a0
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
movq 0x38(%rsp), %rbx
je 0x3fa7
movq %r12, %rdi
callq 0x12233
movq %rbx, %rdi
xorl %esi, %esi
callq 0x12d2b
movq %rax, %r12
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r13, %rsi
callq 0x1373c
movq %rax, %rbx
movq %rax, %rdi
movl %ebp, %esi
callq 0x14ab6
jmp 0x3fb8
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x1373c
movq %rax, %rbx
leaq 0x1e051(%rip), %rsi     # 0x22010
movq %r15, %rdi
callq 0x23e0
testq %rax, %rax
je 0x409a
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x15e2b
movq %r14, %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x122d1
movq %r12, %rdi
callq 0x12233
movq 0x50(%rsp), %rdi
callq 0x16f99
movq %r15, %rdi
callq 0x2060
movq 0x60(%rsp), %rdi
callq 0x2060
movq %r13, %rdi
callq 0x2060
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x27fc1(%rip), %r14     # 0x2bff8
movq (%r14), %rdi
leaq 0x209d6(%rip), %rsi     # 0x24a17
leaq 0x20865(%rip), %rbx     # 0x248ad
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0x209f6(%rip), %rsi     # 0x24a52
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq 0x10(%rsp), %r12
jmp 0x3f4f
movq 0x27f81(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0x1e925(%rip), %rsi     # 0x229a6
leaq 0x20825(%rip), %rdx     # 0x248ad
movq %r13, %rcx
xorl %eax, %eax
callq 0x22c0
pushq $0x1
popq %rdi
callq 0x24b0
movq 0x27f57(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0x1e8fb(%rip), %rsi     # 0x229a6
leaq 0x207fb(%rip), %rdx     # 0x248ad
movq %r15, %rcx
jmp 0x408b