// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11078, %rsp          # imm = 0x11078
movl %r9d, 0x18(%rsp)
movl %r8d, %ebx
movl %ecx, %r15d
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, %r14
leaq 0x1d955(%rip), %rsi     # 0x23449
callq 0x23e0
testq %rax, %rax
je 0x5f4d
movq %rax, %r12
leaq 0x60(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0x5b28
xorl %eax, %eax
callq 0x4b91
movq 0x60(%rsp), %rdi
callq 0x4b7e
testl %eax, %eax
je 0x5f55
movl %ebp, 0x8(%rsp)
movl %ebx, 0x2c(%rsp)
movq %r12, %rdi
callq 0x166b8
leaq 0x68(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r12, 0x40(%rsp)
movq %r12, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0x5b72
xorl %eax, %eax
callq 0x4b91
movl $0x10, %edi
callq 0x2330
movq %rax, %rbx
movl %r15d, (%rax)
movl (%r13), %r12d
movl %r12d, 0x4(%rax)
movq %r12, %rdi
shlq $0x4, %rdi
callq 0x2330
movq %rbx, 0x50(%rsp)
movq %rax, (%rsp)
movq %rax, 0x8(%rbx)
testq %r12, %r12
movl %r15d, %ebp
je 0x5bf1
movq (%rsp), %rax
leaq 0x8(%rax), %rbx
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x8(%r13), %rax
movq (%rax,%r14), %rdi
movl %ebp, %esi
callq 0x4b01
movl %r15d, -0x8(%rbx)
movl %eax, -0x4(%rbx)
movl %eax, %edi
movl $0x8, %esi
callq 0x2290
movq %rax, (%rbx)
incq %r15
addq $0x10, %rbx
addq $0x58, %r14
cmpq %r15, %r12
jne 0x5bbc
movl %ebp, 0x14(%rsp)
movq %r12, 0x38(%rsp)
movq 0x68(%rsp), %rbx
testq %rbx, %rbx
je 0x5d2e
movl 0x8(%rsp), %eax
movq %rax, 0x8(%rsp)
movl 0x14(%rsp), %eax
movq %rax, 0x58(%rsp)
xorl %r15d, %r15d
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
leaq 0x70(%rsp), %rdi
movq 0x40(%rsp), %rcx
callq 0x20f0
movq %rax, %rbp
testl %ebp, %ebp
je 0x5d25
movl $0x11, %r14d
leal -0x11(%r14), %r12d
movl 0x18(%rsp), %eax
cmpb %al, 0x80(%rsp,%r12)
jae 0x5c73
incq %r15
cmpl %ebp, %r14d
jae 0x5d25
addl $0x11, %r14d
cmpq %rbx, %r15
jb 0x5c47
jmp 0x5d25
movl 0x70(%rsp,%r12), %esi
movl 0x74(%rsp,%r12), %edx
movq %r13, %rdi
leaq 0x34(%rsp), %rcx
leaq 0x48(%rsp), %r8
xorl %r9d, %r9d
callq 0x14b8d
movl 0x78(%rsp,%r12), %esi
movl 0x7c(%rsp,%r12), %edx
movq %r13, %rdi
leaq 0x30(%rsp), %rcx
leaq 0x20(%rsp), %r8
xorl %r9d, %r9d
callq 0x14b8d
movl 0x34(%rsp), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rcx
je 0x5c59
cmpl 0x30(%rsp), %ecx
jne 0x5c59
movq 0x48(%rsp), %rdx
movq 0x20(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x5ce3
movq %rsi, 0x48(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %rax
jmp 0x5ce9
movq %rdx, %rax
movq %rsi, %rdx
subq %rax, %rdx
cmpq 0x8(%rsp), %rdx
jae 0x5c59
shlq $0x4, %rcx
movq (%rsp), %rdx
movq 0x8(%rdx,%rcx), %rcx
xorl %edx, %edx
movq 0x58(%rsp), %rsi
divq %rsi
incq (%rcx,%rax,8)
movq 0x20(%rsp), %rax
xorl %edx, %edx
divq %rsi
decq (%rcx,%rax,8)
jmp 0x5c59
cmpq %rbx, %r15
jb 0x5c1d
movq 0x40(%rsp), %rdi
callq 0x2150
movq 0x38(%rsp), %r9
testq %r9, %r9
je 0x5f36
xorl %r10d, %r10d
movl 0x14(%rsp), %r8d
movl 0x2c(%rsp), %eax
movq %r10, %rdx
shlq $0x4, %rdx
movq (%rsp), %rcx
movl 0x4(%rcx,%rdx), %ecx
cmpq $0x2, %rcx
jb 0x5d88
movq (%rsp), %rsi
movq 0x8(%rsi,%rdx), %rdx
movq (%rdx), %rsi
movl $0x1, %edi
addq (%rdx,%rdi,8), %rsi
movq %rsi, (%rdx,%rdi,8)
incq %rdi
cmpq %rdi, %rcx
jne 0x5d78
incq %r10
cmpq %r9, %r10
jne 0x5d52
xorl %edx, %edx
divl %r8d
movl %eax, 0x8(%rsp)
cmpl $0x2, %eax
jl 0x5eec
xorl %eax, %eax
movq %rax, 0x18(%rsp)
shlq $0x4, %rax
movq (%rsp), %rcx
movq 0x8(%rcx,%rax), %r15
movslq 0x4(%rcx,%rax), %r12
movl 0x8(%rsp), %r13d
cmpl %r13d, %r12d
cmovll %r12d, %r13d
movl %r13d, %ebp
sarl %ebp
movslq %r13d, %r14
leaq (,%r14,8), %rdi
callq 0x2330
movq %rax, %rdi
testq %r12, %r12
jle 0x5e23
movl %ebp, %esi
leaq (,%rsi,8), %rax
movq %r15, %r8
subq %rax, %r8
xorl %r9d, %r9d
xorl %ecx, %ecx
movq (%r15,%r9,8), %rax
movq %rax, (%rdi,%r9,8)
addq %rax, %rcx
leaq 0x1(%r9), %r10
cmpq %rsi, %r9
jb 0x5e19
movq %rcx, %rax
cqto
idivq %r10
movq %rax, (%r8,%r9,8)
movq %r10, %r9
cmpq %r10, %r14
jne 0x5df9
jmp 0x5e25
xorl %ecx, %ecx
movl %r13d, %eax
notl %eax
andl $0x1, %eax
movl %r12d, %r8d
subl %ebp, %r8d
addl %eax, %r8d
xorl %esi, %esi
cmpl %r8d, %ebp
jge 0x5e94
movl %r12d, %eax
subl %r13d, %eax
addl %ebp, %eax
movl %r13d, %r9d
subl %ebp, %r9d
movslq %ebp, %r10
movslq %eax, %r11
movslq %r8d, %rbx
xorl %esi, %esi
movq %rcx, %rax
cqto
idivq %r14
movq %rax, (%r15,%r10,8)
cmpq %r11, %r10
jge 0x5e8c
movslq %esi, %rsi
subq (%rdi,%rsi,8), %rcx
leal (%r9,%r10), %eax
cltq
movq (%r15,%rax,8), %rax
movq %rax, (%rdi,%rsi,8)
addq %rax, %rcx
incl %esi
cmpl %r13d, %esi
movl $0x0, %eax
cmovel %eax, %esi
incq %r10
cmpq %r10, %rbx
jne 0x5e56
cmpl %r12d, %r8d
movl $0x0, %r10d
jge 0x5ed1
movslq %r8d, %r8
addl %r12d, %ebp
movslq %esi, %rsi
subq (%rdi,%rsi,8), %rcx
movl %ebp, %eax
subl %r8d, %eax
movslq %eax, %r9
movq %rcx, %rax
cqto
idivq %r9
movq %rax, (%r15,%r8,8)
incl %esi
cmpl %r13d, %esi
cmovel %r10d, %esi
incq %r8
cmpq %r12, %r8
jne 0x5ea5
callq 0x2060
movq 0x18(%rsp), %rax
incq %rax
movq 0x38(%rsp), %r9
cmpq %r9, %rax
jne 0x5da4
testq %r9, %r9
je 0x5f36
xorl %eax, %eax
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rax, %rsi
shlq $0x4, %rsi
movq (%rsp), %rdx
movl 0x4(%rdx,%rsi), %edx
testq %rdx, %rdx
je 0x5f2e
movq (%rsp), %rdi
movq 0x8(%rdi,%rsi), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
orq %rdi, (%rsi,%r8,8)
incq %r8
addq %rcx, %rdi
cmpq %r8, %rdx
jne 0x5f1f
incq %rax
cmpq %r9, %rax
jne 0x5efd
movq 0x50(%rsp), %rax
addq $0x11078, %rsp          # imm = 0x11078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2799
callq 0x2771