// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]yahs/yahs.c
// Function: contig_error_break
int contig_error_break(char *agp, char *fai, char *link_file, uint32_t ml, char *out)
{
    uint32_t i, ec_round, err_no, bp_n;
    sdict_t *sdict;
    asm_dict_t *dict;
    int dist_thres;

    sdict = make_sdict_from_index(fai, ml);
    dict = agp? make_asm_dict_from_agp(sdict, agp, 1) : make_asm_dict_from_sdict(sdict);
    dist_thres = estimate_dist_thres_from_file(link_file, dict, ec_min_frac, ec_resolution, 0);
    dist_thres = MAX(dist_thres, ec_min_window);
    fprintf(stderr, "[I::%s] dist threshold for contig error break: %d\n", __func__, dist_thres);

    char* out1 = (char *) malloc(strlen(out) + 35);
    ec_round = err_no = 0;
    while (1) {
        if (ec_round) dict = make_asm_dict_from_agp(sdict, out1, 1);
        link_mat_t *link_mat = link_mat_from_file(link_file, dict, dist_thres, ec_bin, .0, ec_move_avg, 0);
#ifdef DEBUG_ERROR_BREAK
        fprintf(stderr, "[DEBUG_ERROR_BREAK::%s] ec_round %u link matrix\n", __func__, ec_round);
        print_link_mat(link_mat, dict, stderr);
#endif
        bp_n = 0;
        bp_t *breaks = detect_break_points(link_mat, ec_bin, ec_merge_thresh, ec_fold_thresh, ec_dual_break_thresh, &bp_n);
#ifdef DEBUG
        fprintf(stderr, "[DEBUG::%s] number contig breaks in round %u: %u\n", __func__, ec_round + 1, bp_n);
#endif
        sprintf(out1, "%s_%02d.agp", out, ++ec_round);
        FILE *agp_out = fopen(out1, "w");
        write_break_agp(dict, breaks, bp_n, agp_out);
        fclose(agp_out);
        
        link_mat_destroy(link_mat);
        asm_destroy(dict);
        for (i = 0; i < bp_n; ++i)
            free(breaks[i].p);
        free(breaks);
        
        err_no += bp_n;
#ifdef DEBUG_ERROR_BREAK
        fprintf(stderr, "[DEBUG_ERROR_BREAK::%s] bp_n %d\n", __func__, bp_n);
#endif
        if (!bp_n)
            break;
    }
    sd_destroy(sdict);
    free(out1);

    fprintf(stderr, "[I::%s] performed %u round assembly error correction. Made %u breaks \n", __func__, ec_round, err_no);

    return ec_round;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rdi, %rbx
movq %rsi, %rdi
movl %ecx, %esi
callq 0x11f82
testq %rbx, %rbx
movq %rax, 0x10(%rsp)
jne 0x3863
movq %rax, %rdi
callq 0x12528
jmp 0x3871
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x129a4
movsd 0x1f71f(%rip), %xmm0    # 0x22f98
movq 0x18(%rsp), %rdi
movq %rax, 0x28(%rsp)
movq %rax, %rsi
movl $0x2710, %edx           # imm = 0x2710
xorl %ecx, %ecx
callq 0x58a4
cmpl $0xf4241, %eax          # imm = 0xF4241
movl $0xf4240, %ecx          # imm = 0xF4240
cmovgel %eax, %ecx
movq 0x27752(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0x1fdf9(%rip), %rsi     # 0x236a9
leaq 0x1fe25(%rip), %rdx     # 0x236dc
movl %ecx, 0xc(%rsp)
xorl %eax, %eax
callq 0x22c0
movq 0x20(%rsp), %rdi
callq 0x2160
leaq 0x23(%rax), %rdi
callq 0x2330
movq %rax, %rbx
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %rax, 0x30(%rsp)
testl %r13d, %r13d
je 0x38fd
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
pushq $0x1
popq %rdx
callq 0x129a4
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %r12
movq %r12, %rsi
movl 0xc(%rsp), %edx
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x5b3f
movq %rax, %rbp
andl $0x0, 0x8(%rsp)
movq %rax, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movl $0x2710, %edx           # imm = 0x2710
movsd 0x1ef6a(%rip), %xmm0    # 0x228a8
movl $0xc350, %ecx           # imm = 0xC350
leaq 0x8(%rsp), %r8
callq 0x62f6
movq %rax, %r14
incl %r13d
movq %rbx, %rdi
leaq 0x1fd92(%rip), %rsi     # 0x236ef
movq 0x20(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x24a0
movq %rbx, %rdi
leaq 0x1d69a(%rip), %rsi     # 0x21010
callq 0x23e0
movq %rax, %rbx
movl 0x8(%rsp), %edx
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rcx
callq 0x6c15
movq %rbx, %rdi
callq 0x2150
movq %rbp, %rdi
callq 0x5855
movq %r12, %rdi
callq 0x11528
cmpl $0x0, 0x8(%rsp)
je 0x39cd
leaq 0x10(%r14), %rbx
xorl %ebp, %ebp
movq (%rbx), %rdi
callq 0x2060
incq %rbp
movl 0x8(%rsp), %eax
addq $0x18, %rbx
cmpq %rax, %rbp
jb 0x39b5
movq %r14, %rdi
callq 0x2060
movl 0x8(%rsp), %eax
addl %eax, %r15d
testl %eax, %eax
movq 0x30(%rsp), %rbx
jne 0x38e3
movq 0x10(%rsp), %rdi
callq 0x1148c
movq %rbx, %rdi
callq 0x2060
movq 0x275f6(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0x1fcef(%rip), %rsi     # 0x236fb
leaq 0x1fcc9(%rip), %rdx     # 0x236dc
movl %r13d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x22c0
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq