// File: /workspace/github_c_makefile_star2_O1/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 0x4a5d
movq 0x2756e(%rip), %rbp     # 0x2aff8
movq (%rbp), %rdi
cvtsi2sdq (%rbx), %xmm0
mulsd 0x1f8f5(%rip), %xmm0    # 0x23390
leaq 0x1fca0(%rip), %rsi     # 0x23742
leaq 0x1fcb4(%rip), %rbx     # 0x2375d
movq %rbx, %rdx
movb $0x1, %al
callq 0x22c0
movq (%rbp), %rdi
cvtsi2sdq (%r12), %xmm0
mulsd 0x1f8cb(%rip), %xmm0    # 0x23390
leaq 0x1fc9a(%rip), %rsi     # 0x23766
movq %rbx, %rdx
movb $0x1, %al
callq 0x22c0
testl %r15d, %r15d
je 0x3af4
movq (%rbp), %rdi
leaq 0x1fc9b(%rip), %rsi     # 0x23781
leaq 0x1fc70(%rip), %rdx     # 0x2375d
xorl %eax, %eax
callq 0x22c0
movq 0x38(%rsp), %r15
movq %r15, %rdi
movl %r13d, %esi
callq 0x11f82
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 0x3b9e
leaq 0x1fc42(%rip), %rsi     # 0x2379d
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 0x382a
leaq 0x1fc28(%rip), %rsi     # 0x237ad
movq %r13, %rdi
movq %rbp, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x24a0
movq 0x10(%rsp), %r12
jmp 0x3c2d
testq %r14, %r14
jne 0x3bf1
leaq 0x1fc1c(%rip), %rsi     # 0x237c6
movq %r13, %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rdx
xorl %eax, %eax
callq 0x24a0
leaq 0x1d44d(%rip), %rsi     # 0x21010
movq %r13, %rdi
callq 0x23e0
testq %rax, %rax
je 0x40fa
movq %rax, %rbx
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x150b1
movq %rbx, %rdi
callq 0x2150
jmp 0x3c2d
movq %r14, %rdi
callq 0x2160
cmpq %rbx, %rax
movq 0x18(%rsp), %rbp
jbe 0x3c1d
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 0x129a4
movl (%rax), %ecx
cmpl $0xafc9, %ecx           # imm = 0xAFC9
jb 0x3cb0
movq 0x273aa(%rip), %r15     # 0x2aff8
movq (%r15), %rdi
leaq 0x1fb7e(%rip), %rsi     # 0x237d6
leaq 0x1fafe(%rip), %r14     # 0x2375d
movq %r14, %rdx
movl $0xafc8, %r8d           # imm = 0xAFC8
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x1fb8e(%rip), %rsi     # 0x23807
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x1fbbc(%rip), %rsi     # 0x23849
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x1fc0b(%rip), %rsi     # 0x238ac
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
jmp 0x40a6
movq %rax, %r14
leaq 0x27976(%rip), %rsi     # 0x2b630
leaq 0x279bf(%rip), %rdx     # 0x2b680
movq %rax, %rdi
callq 0x1407f
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x4141
movq %r14, %rdi
callq 0x11528
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0xda74
movq %rax, 0x50(%rsp)
cmpl $0x0, 0xc8(%rsp)
jle 0x3fd9
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 0x27926(%rip), %rcx     # 0x2b650
imulq $0xa, %r12, %rax
cmpq %rax, %rcx
jae 0x3d55
testl %r15d, %r15d
je 0x40ba
testl %edx, %edx
movl %edx, 0x2c(%rsp)
jle 0x3d99
movl %edx, %eax
movq 0xc0(%rsp), %rdx
movl -0x4(%rdx,%rax,4), %r12d
jmp 0x3dca
movl 0x24(%rsp), %esi
decl %esi
sete %cl
cmpq %rax, 0x2790b(%rip)     # 0x2b670
seta %al
orb %cl, %al
movl 0xd0(%rsp), %ecx
jne 0x3d75
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 0x27268(%rip), %r15     # 0x2aff8
leaq 0x1f9c6(%rip), %rbx     # 0x2375d
jmp 0x3e10
movl %r12d, %eax
cltd
pushq $0xa
popq %rsi
idivl %esi
movl %eax, %r12d
cmpl $0x3e9, %eax            # imm = 0x3E9
jge 0x3db2
movl $0x3e8, %r12d           # imm = 0x3E8
movq 0xc0(%rsp), %rax
movl (%rax), %eax
cmpl %eax, %r12d
jle 0x3dca
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %r12d
movq 0x27227(%rip), %r13     # 0x2aff8
movq (%r13), %rdi
leaq 0x1fb79(%rip), %rsi     # 0x23955
leaq 0x1f97a(%rip), %rbx     # 0x2375d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r13), %rdi
leaq 0x1fb84(%rip), %rsi     # 0x2397c
movq %rbx, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x22c0
decl %r15d
movl %r15d, 0x28(%rsp)
movq %r13, %r15
movq (%r15), %rdi
leaq 0x1fb9e(%rip), %rsi     # 0x239b8
movq %rbx, %rdx
movl %r14d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x22c0
movq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x1fbad(%rip), %rsi     # 0x239e6
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 0x1e608
addq $0x40, %rsp
testl %eax, %eax
movq %rbx, %rdx
jne 0x3f46
movq %r15, %rbx
movq 0x58(%rsp), %r13
movq %r13, %rdi
leaq 0x1fb27(%rip), %rsi     # 0x239ef
movq 0x18(%rsp), %r15
movq %r15, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x24a0
cmpl $0x0, 0xf8(%rsp)
jne 0x3f2c
movq 0x40(%rsp), %rbp
movq %rbp, %rdi
leaq 0x1fb09(%rip), %rsi     # 0x239fc
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 0x1ea1a
popq %rcx
popq %rdx
jmp 0x3f3c
movq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x20c0
leaq 0x1f81a(%rip), %rdx     # 0x2375d
movq %rbx, %r15
movq (%r15), %rdi
leaq 0x1fabf(%rip), %rsi     # 0x23a0f
movl %r14d, %ecx
xorl %eax, %eax
callq 0x22c0
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rdx
callq 0x129a4
movq %rax, %rbx
movq 0x276d9(%rip), %r15     # 0x2b650
movq %rax, %rdi
leaq 0x276af(%rip), %rsi     # 0x2b630
leaq 0x276f8(%rip), %rdx     # 0x2b680
callq 0x1407f
cmpq 0x276bc(%rip), %r15     # 0x2b650
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 0x4141
movq %rbx, %rdi
callq 0x11528
incl %r14d
movl 0x2c(%rsp), %edx
cmpl 0xc8(%rsp), %edx
movq %rbp, %r13
movq 0x18(%rsp), %rbp
jl 0x3d14
leaq 0x1fa52(%rip), %rsi     # 0x23a32
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 0x4031
movq %r12, %rdi
callq 0x1148c
movq %rbx, %rdi
xorl %esi, %esi
callq 0x11f82
movq %rax, %r12
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r13, %rsi
callq 0x129a4
movq %rax, %rbx
movq %rax, %rdi
movl %ebp, %esi
callq 0x13c7c
jmp 0x4042
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x129a4
movq %rax, %rbx
leaq 0x1cfc7(%rip), %rsi     # 0x21010
movq %r15, %rdi
callq 0x23e0
testq %rax, %rax
je 0x4124
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x14f94
movq %r14, %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x11528
movq %r12, %rdi
callq 0x1148c
movq 0x50(%rsp), %rdi
callq 0x160f0
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 0x26f37(%rip), %r14     # 0x2aff8
movq (%r14), %rdi
leaq 0x1f7fc(%rip), %rsi     # 0x238c7
leaq 0x1f68b(%rip), %rbx     # 0x2375d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r14), %rdi
leaq 0x1f81c(%rip), %rsi     # 0x23902
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq 0x10(%rsp), %r12
jmp 0x3fd9
movq 0x26ef7(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0x1d863(%rip), %rsi     # 0x2196e
leaq 0x1f64b(%rip), %rdx     # 0x2375d
movq %r13, %rcx
xorl %eax, %eax
callq 0x22c0
pushq $0x1
popq %rdi
callq 0x24b0
movq 0x26ecd(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0x1d839(%rip), %rsi     # 0x2196e
leaq 0x1f621(%rip), %rdx     # 0x2375d
movq %r15, %rcx
jmp 0x4115