// File: /workspace/github_c_makefile_star2_O3/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 0x12d2b
testq %rbx, %rbx
movq %rax, 0x10(%rsp)
jne 0x37d9
movq %rax, %rdi
callq 0x132af
jmp 0x37e7
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1373c
movsd 0x205b1(%rip), %xmm0    # 0x23da0
movq 0x18(%rsp), %rdi
movq %rax, 0x28(%rsp)
movq %rax, %rsi
movl $0x2710, %edx           # imm = 0x2710
xorl %ecx, %ecx
callq 0x582e
cmpl $0xf4241, %eax          # imm = 0xF4241
movl $0xf4240, %ecx          # imm = 0xF4240
cmovgel %eax, %ecx
movq 0x287dc(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0x20fd3(%rip), %rsi     # 0x247f9
leaq 0x20fff(%rip), %rdx     # 0x2482c
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 0x3873
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
pushq $0x1
popq %rdx
callq 0x1373c
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 0x5ac9
movq %rax, %rbp
andl $0x0, 0x8(%rsp)
movq %rax, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movl $0x2710, %edx           # imm = 0x2710
movsd 0x20024(%rip), %xmm0    # 0x238d8
movl $0xc350, %ecx           # imm = 0xC350
leaq 0x8(%rsp), %r8
callq 0x6285
movq %rax, %r14
incl %r13d
movq %rbx, %rdi
leaq 0x20f6c(%rip), %rsi     # 0x2483f
movq 0x20(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x24a0
movq %rbx, %rdi
leaq 0x1e724(%rip), %rsi     # 0x22010
callq 0x23e0
movq %rax, %rbx
movl 0x8(%rsp), %edx
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rcx
callq 0x6f46
movq %rbx, %rdi
callq 0x2150
movq %rbp, %rdi
callq 0x57df
movq %r12, %rdi
callq 0x122d1
cmpl $0x0, 0x8(%rsp)
je 0x3943
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 0x392b
movq %r14, %rdi
callq 0x2060
movl 0x8(%rsp), %eax
addl %eax, %r15d
testl %eax, %eax
movq 0x30(%rsp), %rbx
jne 0x3859
movq 0x10(%rsp), %rdi
callq 0x12233
movq %rbx, %rdi
callq 0x2060
movq 0x28680(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0x20ec9(%rip), %rsi     # 0x2484b
leaq 0x20ea3(%rip), %rdx     # 0x2482c
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