// File: /workspace/github_c_makefile_star2_O2/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, %r13
movl %r8d, 0x4c(%rsp)
movl %ecx, 0xc(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbp
movl 0x100(%rsp), %ebx
leaq 0x80(%rsp), %r14
leaq 0x70(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x27b8
movq 0xcfb7(%rip), %r12      # 0x27ff8
movq (%r12), %rdi
cvtsi2sdq (%r14), %xmm0
mulsd 0x533e(%rip), %xmm0     # 0x20390
leaq 0x5699(%rip), %rsi      # 0x206f2
leaq 0x56ad(%rip), %r14      # 0x2070d
movq %r14, %rdx
movb $0x1, %al
callq 0x22c0
movq (%r12), %rdi
cvtsi2sdq (%r15), %xmm0
mulsd 0x5315(%rip), %xmm0     # 0x20390
leaq 0x5694(%rip), %rsi      # 0x20716
movq %r14, %rdx
movb $0x1, %al
callq 0x22c0
testl %ebx, %ebx
jne 0x1b507
movq %rbp, %rdi
movl 0xc(%rsp), %esi
callq 0xeb03
movq %rax, 0x40(%rsp)
movq %r13, %rdi
callq 0x2160
movq %rax, %r12
leaq 0x23(%rax), %r15
movq %r15, %rdi
callq 0x2330
movq %rax, 0x58(%rsp)
movq %r15, %rdi
callq 0x2330
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0x2330
movq %rax, %r14
cmpl $0x0, 0xf0(%rsp)
movq %r13, 0x60(%rsp)
je 0x1b134
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x1b179
movq %rbx, %rdi
callq 0x2160
cmpq %r12, %rax
jbe 0x1b11d
movq %rax, %r15
movq %r14, %rdi
callq 0x2060
addq $0x23, %r15
movq %r15, %rdi
callq 0x2330
movq %rax, %r14
movq 0xced4(%rip), %r15      # 0x27ff8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x20c0
jmp 0x1b1c6
leaq 0x5612(%rip), %rsi      # 0x2074d
movq %r14, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x24a0
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq 0x30(%rsp), %rdx
movl 0xc(%rsp), %ecx
movq %r14, %r8
callq 0x1acd4
leaq 0x55f5(%rip), %rsi      # 0x2075d
movq %r14, %rdi
movq %r13, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x24a0
jmp 0x1b1bf
leaq 0x55f6(%rip), %rsi      # 0x20776
movq %r14, %rdi
movq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x24a0
leaq 0x2e7a(%rip), %rsi      # 0x1e010
movq %r14, %rdi
callq 0x23e0
testq %rax, %rax
je 0x1b6c4
movq %rax, %rbx
movq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x111f5
movq %rbx, %rdi
callq 0x2150
movq 0xce32(%rip), %r15      # 0x27ff8
pushq $0x1
popq %r13
movq 0x40(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0xf408
movl (%rax), %ecx
cmpl $0xafc9, %ecx           # imm = 0xAFC9
jae 0x1b52b
movq %rax, %rbx
movq %r14, 0x10(%rsp)
movq %rbp, 0x38(%rsp)
movl 0xd0(%rsp), %ebp
movl 0xc8(%rsp), %r14d
leaq 0xd422(%rip), %rsi      # 0x28630
leaq 0xd46b(%rip), %rdx      # 0x28680
movq %rax, %rdi
callq 0x103af
movl %r13d, %edi
callq 0x1b6e1
movq %rbx, %rdi
callq 0xe2a0
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0xa9c0
movq %rax, 0x50(%rsp)
pushq $0x3
popq %rdi
movl %r13d, %edx
movl %ebp, 0x28(%rsp)
xorl %esi, %esi
movq %r12, %rbp
cmpl %r14d, %esi
movq 0x60(%rsp), %r13
jge 0x1b5bf
movslq %esi, %rax
movq 0xc0(%rsp), %rcx
movslq (%rcx,%rax,4), %rbx
movq 0xd3de(%rip), %rcx      # 0x28650
imulq $0xa, %rbx, %rax
cmpq %rax, %rcx
jae 0x1b2a9
testl %edi, %edi
je 0x1b590
movl %edx, %r12d
testl %esi, %esi
movl %esi, 0x2c(%rsp)
movl %edi, 0x24(%rsp)
jle 0x1b2e9
movl %esi, %eax
movq 0xc0(%rsp), %rdx
movl -0x4(%rdx,%rax,4), %ebx
leaq 0x5466(%rip), %r14      # 0x2070d
jmp 0x1b31c
movl %edx, %r12d
movl 0x28(%rsp), %edx
decl %edx
sete %cl
cmpq %rax, 0xd3b4(%rip)      # 0x28670
seta %al
orb %cl, %al
movl 0xd0(%rsp), %ecx
jne 0x1b2cc
movl %edx, %ecx
movzbl %al, %eax
addl %eax, %esi
movl %esi, 0x2c(%rsp)
pushq $0x3
popq %rax
movl %eax, 0x24(%rsp)
movl %ecx, 0x28(%rsp)
leaq 0x5426(%rip), %r14      # 0x2070d
jmp 0x1b34a
movl %ebx, %eax
cltd
pushq $0xa
popq %rsi
idivl %esi
movl %eax, %ebx
cmpl $0x3e9, %eax            # imm = 0x3E9
leaq 0x540e(%rip), %r14      # 0x2070d
jge 0x1b306
movl $0x3e8, %ebx            # imm = 0x3E8
movq 0xc0(%rsp), %rax
movl (%rax), %eax
cmpl %eax, %ebx
jle 0x1b31c
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %ebx
movq (%r15), %rdi
leaq 0x55df(%rip), %rsi      # 0x20905
movq %r14, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x55f2(%rip), %rsi      # 0x2092c
movq %r14, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x22c0
decl 0x24(%rsp)
movq (%r15), %rdi
leaq 0x5614(%rip), %rsi      # 0x20968
movq %r14, %rdx
movl %r12d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x22c0
movq 0x58(%rsp), %rbp
movq %rbp, %rdi
leaq 0x5623(%rip), %rsi      # 0x20996
movq %r13, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x24a0
movzbl 0x4c(%rsp), %r15d
movq 0x38(%rsp), %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movq 0x30(%rsp), %rdx
movq 0x50(%rsp), %rcx
movl 0xc(%rsp), %r8d
movl %r15d, %r9d
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 %rbx
pushq %rbp
pushq 0x110(%rsp)
pushq 0x110(%rsp)
callq 0x1a802
addq $0x40, %rsp
testl %eax, %eax
movq 0x40(%rsp), %rbp
jne 0x1b469
movq 0x18(%rsp), %rdi
leaq 0x55a9(%rip), %rsi      # 0x2099f
movq %r13, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x24a0
cmpl $0x0, 0xf8(%rsp)
jne 0x1b457
movq 0x10(%rsp), %r14
movq %r14, %rdi
leaq 0x5590(%rip), %rsi      # 0x209ac
movq %r13, %rdx
movl %r12d, %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 %r15d, %ecx
movq 0x20(%rsp), %r8
movl %ebx, %r9d
pushq %r14
callq 0x1aeda
popq %rcx
popq %rdx
jmp 0x1b469
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x20c0
movq 0xcb88(%rip), %r13      # 0x27ff8
movq (%r13), %rdi
leaq 0x5544(%rip), %rsi      # 0x209bf
leaq 0x528b(%rip), %rdx      # 0x2070d
movl %r12d, %ecx
xorl %eax, %eax
callq 0x22c0
movq %rbp, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
callq 0xf408
movq %rax, %rbx
movq 0xd1ac(%rip), %r15      # 0x28650
movq %rax, %rdi
leaq 0xd182(%rip), %rsi      # 0x28630
leaq 0xd1cb(%rip), %rdx      # 0x28680
callq 0x103af
cmpq 0xd18f(%rip), %r15      # 0x28650
movl 0x24(%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 0x1b6e1
movq %rbx, %rdi
callq 0xe2a0
incl %r12d
movl %r15d, %edi
movq %r13, %r15
movl 0xc8(%rsp), %r14d
movl %r12d, %edx
movl 0x2c(%rsp), %esi
jmp 0x1b24e
movq 0xcaea(%rip), %rax      # 0x27ff8
movq (%rax), %rdi
leaq 0x5219(%rip), %rsi      # 0x20731
leaq 0x51ee(%rip), %rdx      # 0x2070d
xorl %eax, %eax
callq 0x22c0
jmp 0x1b094
movq (%r15), %rdi
leaq 0x5251(%rip), %rsi      # 0x20786
leaq 0x51d1(%rip), %rbx      # 0x2070d
movq %rbx, %rdx
movl $0xafc8, %r8d           # imm = 0xAFC8
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x5261(%rip), %rsi      # 0x207b7
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x528f(%rip), %rsi      # 0x207f9
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x52de(%rip), %rsi      # 0x2085c
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
pushq $0x1
popq %rax
jmp 0x1b68d
movq (%r15), %rdi
leaq 0x52dd(%rip), %rsi      # 0x20877
leaq 0x516c(%rip), %rbx      # 0x2070d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
movq (%r15), %rdi
leaq 0x52fd(%rip), %rsi      # 0x208b2
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22c0
leaq 0x541c(%rip), %rsi      # 0x209e2
movq 0x18(%rsp), %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x24a0
cmpl $0x0, 0xc(%rsp)
je 0x1b614
movq %rbp, %rdi
callq 0xe20a
movq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0xeb03
movq %rax, %rbp
pushq $0x1
popq %rdx
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0xf408
movq %rax, %rbx
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x1000a
jmp 0x1b627
pushq $0x1
popq %rdx
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0xf408
movq %rax, %rbx
leaq 0x29e2(%rip), %rsi      # 0x1e010
movq 0x18(%rsp), %rdi
callq 0x23e0
testq %rax, %rax
je 0x1b69f
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x11110
movq %r14, %rdi
callq 0x2150
movq %rbx, %rdi
callq 0xe2a0
movq %rbp, %rdi
callq 0xe20a
movq 0x50(%rsp), %rdi
callq 0x12287
movq 0x18(%rsp), %rdi
callq 0x2060
movq 0x58(%rsp), %rdi
callq 0x2060
movq 0x10(%rsp), %rdi
callq 0x2060
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rdi
leaq 0x32b5(%rip), %rsi      # 0x1e95e
leaq 0x505d(%rip), %rdx      # 0x2070d
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x22c0
pushq $0x1
popq %rdi
callq 0x24b0
movq 0xc92d(%rip), %rax      # 0x27ff8
movq (%rax), %rdi
leaq 0x3289(%rip), %rsi      # 0x1e95e
leaq 0x5031(%rip), %rdx      # 0x2070d
movq %r14, %rcx
jmp 0x1b6b5