// File: /workspace/github_c_makefile_star2_O2/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 0xeb03
testq %rbx, %rbx
movq %rax, 0x10(%rsp)
jne 0x1ad0d
movq %rax, %rdi
callq 0xefa5
jmp 0x1ad1b
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0xf408
movsd 0x4f5d(%rip), %xmm0     # 0x1fc80
movq 0x18(%rsp), %rdi
movq %rax, 0x28(%rsp)
movq %rax, %rsi
movl $0x2710, %edx           # imm = 0x2710
xorl %ecx, %ecx
callq 0x35fb
cmpl $0xf4241, %eax          # imm = 0xF4241
movl $0xf4240, %ecx          # imm = 0xF4240
cmovgel %eax, %ecx
movq 0xd2a8(%rip), %rax      # 0x27ff8
movq (%rax), %rdi
leaq 0x58ff(%rip), %rsi      # 0x20659
leaq 0x592b(%rip), %rdx      # 0x2068c
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 0x1ada7
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
pushq $0x1
popq %rdx
callq 0xf408
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 0x389b
movq %rax, %rbp
andl $0x0, 0x8(%rsp)
movq %rax, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movl $0x2710, %edx           # imm = 0x2710
movsd 0x4a38(%rip), %xmm0     # 0x1f820
movl $0xc350, %ecx           # imm = 0xC350
leaq 0x8(%rsp), %r8
callq 0x4038
movq %rax, %r14
incl %r13d
movq %rbx, %rdi
leaq 0x5898(%rip), %rsi      # 0x2069f
movq 0x20(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x24a0
movq %rbx, %rdi
leaq 0x31f0(%rip), %rsi      # 0x1e010
callq 0x23e0
movq %rax, %rbx
movl 0x8(%rsp), %edx
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rcx
callq 0x4862
movq %rbx, %rdi
callq 0x2150
movq %rbp, %rdi
callq 0x35b6
movq %r12, %rdi
callq 0xe2a0
movq %r14, %rbx
addq $0x10, %rbx
xorl %ebp, %ebp
movl 0x8(%rsp), %eax
cmpq %rax, %rbp
jae 0x1ae75
movq (%rbx), %rdi
callq 0x2060
incq %rbp
addq $0x18, %rbx
jmp 0x1ae5b
movq %r14, %rdi
callq 0x2060
movl 0x8(%rsp), %eax
addl %eax, %r15d
testl %eax, %eax
movq 0x30(%rsp), %rbx
jne 0x1ad8d
movq 0x10(%rsp), %rdi
callq 0xe20a
movq %rbx, %rdi
callq 0x2060
movq 0xd14e(%rip), %rax      # 0x27ff8
movq (%rax), %rdi
leaq 0x57f7(%rip), %rsi      # 0x206ab
leaq 0x57d1(%rip), %rdx      # 0x2068c
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