// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/link.c
// Function: calc_link_directs_from_file
int8_t *calc_link_directs_from_file(const char *f, asm_dict_t *dict, uint8_t mq)
{
    uint32_t i, j, k, m, n, na, i0, i1, b0, b1, b, ma, sma, n_ma;
    uint64_t p0, p1, pair_n, pair_c, inter_c;
    int64_t magic_number;
    uint8_t buffer[BUFF_SIZE * 17];
    uint32_t *link, l;
    int8_t *directs;
    FILE *fp;

    fp = fopen(f, "r");
    if (fp == NULL)
        return 0;
    
    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__);
        return 0;
    }
    file_seek_skip_sdict(fp);
    m = fread(&pair_n, sizeof(uint64_t), 1, fp);
    if (m != 1) bin_fread_error();

    n = dict->n;
    na = (long) n * (n - 1) / 2;
    link = (uint32_t *) calloc(na << 2, sizeof(uint32_t));
    pair_c = inter_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 (i0 > i1) {
                    SWAP(uint32_t, i0, i1);
                    SWAP(uint64_t, p0, p1);
                }
                b0 = !((double) p0 / dict->s[i0].len > .5);
                b1 = !((double) p1 / dict->s[i1].len > .5);
                b = (b0 << 1) | (!b1);
                ++link[b * na + (long) (n * 2 - i0 - 1) * i0 / 2 + i1 - i0 - 1];
                
                ++inter_c;
            }
        }
    }

#ifdef DEBUG
    fprintf(stderr, "[DEBUG::%s] %lu read pairs processed, %lu inter links \n", __func__, pair_c, inter_c);
#endif
    fclose(fp);
    
    directs = (int8_t *) malloc(na * sizeof(int8_t));
    for (i = 0; i < na; ++i) {
        ma = sma = n_ma = j = 0;
        for (k = 0; k < 4; ++k) {
            l = link[k * na + i];
            if (l > ma) {
                sma = ma;
                ma = l;
                n_ma = 1;
                j = k;
            } else if (l == ma) {
                ++n_ma;
            } else if (l > sma) {
                sma = l;
            }
        }
        
        directs[i] = n_ma == 1 && ma * .9 > sma? j : -1;
    }
    free(link);
#ifdef DEBUG_ORIEN
    int8_t d;
    for (i0 = 0; i0 < n; ++i0) {
        for (i1 = i0 + 1; i1 < n; ++i1) {
            d = directs[(long) (n * 2 - i0 - 1) * i0 / 2 + i1 - i0 - 1];
            if (d >=0)
                fprintf(stderr, "[DEBUG_ORIEN::%s] #Oriens: %s %s %hhi \n", __func__, dict->s[i0].name, dict->s[i1].name, d);
        }
    }
#endif
    return directs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11058, %rsp          # imm = 0x11058
movl %edx, 0x1c(%rsp)
movq %rsi, 0x10(%rsp)
leaq 0x13e2e(%rip), %rsi     # 0x23449
callq 0x23e0
testq %rax, %rax
je 0xf991
movq %rax, %r15
leaq 0x40(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xf64f
xorl %eax, %eax
callq 0x4b91
movq 0x40(%rsp), %rdi
callq 0x4b7e
testl %eax, %eax
je 0xf98c
movq %r15, %rdi
callq 0x166b8
leaq 0x48(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xf68c
xorl %eax, %eax
callq 0x4b91
movq 0x10(%rsp), %rax
movl (%rax), %r14d
leal -0x1(%r14), %eax
imulq %r14, %rax
shrq %rax
movq %rax, 0x8(%rsp)
leal (,%rax,4), %edi
movl $0x4, %esi
callq 0x2290
movq %rax, %rbx
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0xf8a9
leal (%r14,%r14), %eax
movl %eax, 0x18(%rsp)
xorl %ebp, %ebp
movq %r15, 0x38(%rsp)
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
leaq 0x50(%rsp), %rdi
movq %r15, %rcx
callq 0x20f0
movq %rax, %r12
testl %r12d, %r12d
je 0xf899
movl $0x11, %r14d
leal -0x11(%r14), %r13d
movl 0x1c(%rsp), %eax
cmpb %al, 0x60(%rsp,%r13)
jb 0xf882
movl 0x50(%rsp,%r13), %esi
movl 0x54(%rsp,%r13), %edx
movq 0x10(%rsp), %r15
movq %r15, %rdi
leaq 0x4(%rsp), %rcx
leaq 0x30(%rsp), %r8
xorl %r9d, %r9d
callq 0x14b8d
movl 0x58(%rsp,%r13), %esi
movl 0x5c(%rsp,%r13), %edx
movq %r15, %rdi
movq %rsp, %rcx
leaq 0x28(%rsp), %r8
xorl %r9d, %r9d
callq 0x14b8d
movl 0x4(%rsp), %ecx
movl (%rsp), %eax
cmpl %eax, %ecx
sete %dl
cmpl $-0x1, %ecx
sete %sil
orb %dl, %sil
jne 0xf882
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %rdi
cmpl %eax, %ecx
jbe 0xf799
movl %eax, 0x4(%rsp)
movl %ecx, (%rsp)
movq %rdi, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movl %ecx, %edx
movq %r8, %rsi
jmp 0xf7a3
movl %eax, %edx
movq %rdi, %rsi
movl %ecx, %eax
movq %r8, %rdi
movq 0x8(%rsp), %r9
movq %rdi, %xmm0
movq 0x12a2b(%rip), %xmm3    # 0x221e0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x12a2f(%rip), %xmm4    # 0x221f0
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %r8
movl %eax, %ecx
imulq $0x58, %rcx, %rdi
movq 0x8(%r8,%rdi), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
xorl %edi, %edi
movsd 0x12a0f(%rip), %xmm5    # 0x22218
ucomisd %xmm5, %xmm1
setbe %dil
movq %rsi, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movl %edx, %edx
imulq $0x58, %rdx, %rsi
movq 0x8(%r8,%rsi), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
xorl %esi, %esi
ucomisd %xmm5, %xmm1
seta %sil
leal (%rsi,%rdi,2), %esi
movl %r9d, %edi
imull %esi, %edi
notl %eax
addl 0x18(%rsp), %eax
imulq %rcx, %rax
notq %rcx
leaq (%rbx,%rdi,4), %rsi
andq $-0x2, %rax
leaq (%rsi,%rax,2), %rax
leaq (%rax,%rdx,4), %rax
incl (%rax,%rcx,4)
incq %rbp
cmpl %r12d, %r14d
jae 0xf899
addl $0x11, %r14d
cmpq 0x20(%rsp), %rbp
jb 0xf703
cmpq 0x20(%rsp), %rbp
movq 0x38(%rsp), %r15
jb 0xf6da
movq %r15, %rdi
callq 0x2150
movq 0x8(%rsp), %r15
movl %r15d, %edi
callq 0x2330
movq %rax, %r14
testl %r15d, %r15d
je 0xf982
movl %r15d, %eax
xorl %ecx, %ecx
movsd 0x12db1(%rip), %xmm0    # 0x22688
xorl %r8d, %r8d
xorl %edx, %edx
xorl %r11d, %r11d
xorl %r9d, %r9d
xorl %esi, %esi
movl %esi, %edi
movl %r11d, %esi
movq %r15, %r10
imulq %r8, %r10
addq %rcx, %r10
movl %r10d, %r11d
movl (%rbx,%r11,4), %r11d
cmpl %esi, %r11d
ja 0xf926
cmpl %edi, %r11d
movl %edi, %ebp
cmoval %r11d, %ebp
xorl %r12d, %r12d
cmpl %esi, %r11d
sete %r12b
cmovnel %ebp, %edi
addl %r12d, %r9d
incq %r8
addq %r15, %r10
cmpq $0x4, %r8
jne 0xf8f3
jmp 0xf945
movl %r8d, %edx
leaq 0x1(%r8), %rdi
movl $0x1, %r9d
cmpq $0x3, %r8
movq %rdi, %r8
jne 0xf8e4
movb $0x3, %dl
movl %esi, %edi
movl %r11d, %esi
jmp 0xf94e
movb $-0x1, %r8b
cmpl $0x1, %r9d
jne 0xf972
movl %esi, %esi
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
movl %edi, %esi
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
mulsd %xmm0, %xmm1
movb $-0x1, %r8b
ucomisd %xmm2, %xmm1
jbe 0xf972
movl %edx, %r8d
movb %r8b, (%r14,%rcx)
incq %rcx
cmpq %rax, %rcx
jne 0xf8d7
movq %rbx, %rdi
callq 0x2060
jmp 0xf994
callq 0x2983
xorl %r14d, %r14d
movq %r14, %rax
addq $0x11058, %rsp          # imm = 0x11058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq