// File: /workspace/github_c_makefile_star2_O0/c-zhou[P]yahs/break.c
// Function: link_mat_from_file
link_mat_t *link_mat_from_file(const char *f, asm_dict_t *dict, uint32_t dist_thres, uint32_t resolution, double noise, uint32_t move_avg, uint8_t mq)
{
    FILE *fp;
    uint32_t i, j, m, n, i0, i1;
    uint64_t p0, p1, pair_n, pair_c, intra_c;
    int64_t magic_number;
    uint8_t buffer[BUFF_SIZE * 17];

    fp = fopen(f, "r");
    if (fp == NULL) {
        fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, f);
        exit(EXIT_FAILURE);
    }

    m = fread(&magic_number, sizeof(int64_t), 1, fp);
    if (m != 1) bin_fread_error();
    if (!is_valid_bin_header(magic_number)) {
        fprintf(stderr, "[E::%s] not a valid BIN file\n", __func__);
        exit(EXIT_FAILURE);
    }
    file_seek_skip_sdict(fp);
    m = fread(&pair_n, sizeof(uint64_t), 1, fp);
    if (m != 1) bin_fread_error();

    link_mat_t *link_mat = (link_mat_t *) malloc(sizeof(link_mat_t));
    link_mat->b = resolution;
    link_mat->n = dict->n;
    link_mat->link = (link_t *) malloc(link_mat->n * sizeof(link_t));
    for (i = 0; i < link_mat->n; ++i) {
        n = div_ceil(dict->s[i].len, resolution);
        link_mat->link[i].s = i;
        link_mat->link[i].n = n;
        link_mat->link[i].link = (int64_t *) calloc(n, sizeof(int64_t));
    }

    pair_c = intra_c = 0;
    while (pair_c < pair_n) {
        m = fread(buffer, sizeof(uint8_t), BUFF_SIZE * 17, fp);

        for (i = 0; i < m && pair_c < pair_n; i += 17, ++pair_c) {
            if (*(uint8_t *) (buffer + i + 16) < mq)
                continue;

            sd_coordinate_conversion(dict, *(uint32_t *) (buffer + i),     *(uint32_t *) (buffer + i + 4),  &i0, &p0, 0);
            sd_coordinate_conversion(dict, *(uint32_t *) (buffer + i + 8), *(uint32_t *) (buffer + i + 12), &i1, &p1, 0);

            if (i0 == i1 && i0 != UINT32_MAX) {
                if (p0 > p1)
                    SWAP(uint64_t, p0, p1);
                if (p1 - p0 < dist_thres) {
                    link_mat->link[i0].link[p0 / resolution] += 1;
                    link_mat->link[i1].link[p1 / resolution] -= 1;
                    ++intra_c;
                }
            }
        }
    }
    fclose(fp);

#ifdef DEBUG
    fprintf(stderr, "[DEBUG::%s] %lu read pairs processed, intra links: %lu \n", __func__, pair_c, intra_c);
#endif
    
    int64_t *link;
    for (i = 0; i < link_mat->n; ++i) {
        link = link_mat->link[i].link;
        n = link_mat->link[i].n;
        for (j = 1; j < n; ++j)
            link[j] += link[j - 1];
    }

#ifdef REMOVE_NOISE
    double l;
    noise = noise * dist_thres * (dist_thres + resolution) / 2;
    for (i = 0; i < link_mat->n; ++i) {
        link = link_mat->link[i].link;
        n = link_mat->link[i].n;
        for (j = 0; j < n; ++j) {
            l = (double) link[j] - noise;
            link[j] = (uint32_t) MAX(l, .1);
        }
    }
#endif

    int32_t ma_k = move_avg / resolution;
    if (ma_k > 1)
        for (i = 0; i < link_mat->n; ++i)
            calc_moving_average(link_mat->link[i].link, link_mat->link[i].n, ma_k);

    for (i = 0; i < link_mat->n; ++i) {
        int64_t *link_c = link_mat->link[i].link;
        n = link_mat->link[i].n;
        for (j = 0; j < n; ++j)
            link_c[j] |= (int64_t) j << 32;
    }
    
    return link_mat;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110c0, %rsp          # imm = 0x110C0
movb %r9b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movb %al, -0x25(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x2d83e(%rip), %rsi     # 0x32311
callq 0x23f0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x4b10
movq 0x3450e(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rcx
leaq 0x2c6d8(%rip), %rsi     # 0x311d0
leaq 0x2cbed(%rip), %rdx     # 0x316ec
movb $0x0, %al
callq 0x22c0
movl $0x1, %edi
callq 0x24c0
movq -0x30(%rbp), %rcx
leaq -0x78(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x20f0
movl %eax, -0x3c(%rbp)
cmpl $0x1, -0x3c(%rbp)
je 0x4b37
movb $0x0, %al
callq 0x2c00
movq -0x78(%rbp), %rdi
callq 0x2bc0
cmpl $0x0, %eax
jne 0x4b6e
movq 0x344ac(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
leaq 0x2c6c1(%rip), %rsi     # 0x31217
leaq 0x2cb8f(%rip), %rdx     # 0x316ec
movb $0x0, %al
callq 0x22c0
movl $0x1, %edi
callq 0x24c0
movq -0x30(%rbp), %rdi
callq 0x1bac0
movq -0x30(%rbp), %rcx
leaq -0x60(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x20f0
movl %eax, -0x3c(%rbp)
cmpl $0x1, -0x3c(%rbp)
je 0x4b9e
movb $0x0, %al
callq 0x2c00
movl $0x10, %edi
callq 0x2340
movq %rax, -0x11088(%rbp)
movl -0x18(%rbp), %ecx
movq -0x11088(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl (%rax), %ecx
movq -0x11088(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x11088(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edi
shlq $0x4, %rdi
callq 0x2340
movq %rax, %rcx
movq -0x11088(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x11088(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x4c97
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movl -0x18(%rbp), %esi
callq 0x2aa0
movl %eax, -0x40(%rbp)
movl -0x34(%rbp), %ecx
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x40(%rbp), %ecx
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x40(%rbp), %eax
movl %eax, %edi
movl $0x8, %esi
callq 0x2290
movq %rax, %rcx
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4bf5
movq $0x0, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x4e76
leaq -0x11080(%rbp), %rdi
movq -0x30(%rbp), %rcx
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
callq 0x20f0
movl %eax, -0x3c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %ecx
xorl %eax, %eax
cmpl -0x3c(%rbp), %ecx
movb %al, -0x110a9(%rbp)
jae 0x4cfa
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
setb %al
movb %al, -0x110a9(%rbp)
movb -0x110a9(%rbp), %al
testb $0x1, %al
jne 0x4d09
jmp 0x4e71
movl -0x34(%rbp), %eax
movzbl -0x11070(%rbp,%rax), %eax
movzbl -0x25(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4d21
jmp 0x4e57
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x11080(%rbp,%rax), %esi
movl -0x34(%rbp), %eax
movl -0x1107c(%rbp,%rax), %edx
leaq -0x44(%rbp), %rcx
leaq -0x50(%rbp), %r8
xorl %r9d, %r9d
callq 0x19980
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %eax
movl -0x11078(%rbp,%rax), %esi
movl -0x34(%rbp), %eax
movl -0x11074(%rbp,%rax), %edx
leaq -0x48(%rbp), %rcx
leaq -0x58(%rbp), %r8
xorl %r9d, %r9d
callq 0x19980
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jne 0x4e55
cmpl $-0x1, -0x44(%rbp)
je 0x4e55
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jbe 0x4daf
movq -0x50(%rbp), %rax
movq %rax, -0x11090(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x11090(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
subq -0x50(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
jae 0x4e53
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x44(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x110c0(%rbp)
movq -0x50(%rbp), %rax
movl -0x18(%rbp), %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq -0x110c0(%rbp), %rax
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x48(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x110b8(%rbp)
movq -0x58(%rbp), %rax
movl -0x18(%rbp), %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq -0x110b8(%rbp), %rax
movq (%rax,%rcx,8), %rdx
subq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x4e55
jmp 0x4e57
movl -0x34(%rbp), %eax
addl $0x11, %eax
movl %eax, -0x34(%rbp)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x4cd9
jmp 0x4ca7
movq -0x30(%rbp), %rdi
callq 0x2150
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x11088(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x4f23
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x11098(%rbp)
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x40(%rbp)
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x4f13
movq -0x11098(%rbp), %rax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq -0x11098(%rbp), %rax
movl -0x38(%rbp), %ecx
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x4edb
jmp 0x4f15
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4e86
movl -0x24(%rbp), %eax
xorl %edx, %edx
divl -0x18(%rbp)
movl %eax, -0x1109c(%rbp)
cmpl $0x1, -0x1109c(%rbp)
jle 0x4f99
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x11088(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x4f97
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %esi
movl -0x1109c(%rbp), %edx
callq 0x5050
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4f41
jmp 0x4f99
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x11088(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x5033
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x110a8(%rbp)
movq -0x11088(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x5023
movl -0x38(%rbp), %eax
movl %eax, %edx
shlq $0x20, %rdx
movq -0x110a8(%rbp), %rax
movl -0x38(%rbp), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x4ff5
jmp 0x5025
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4fa0
movq -0x11088(%rbp), %rax
addq $0x110c0, %rsp          # imm = 0x110C0
popq %rbp
retq
nopw %cs:(%rax,%rax)