// File: /workspace/github_c_makefile_star2_O1/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, 0x24(%rsp)
movq %rsi, 0x8(%rsp)
leaq 0x13c0a(%rip), %rsi     # 0x2240f
callq 0x23e0
testq %rax, %rax
je 0xeb37
movq %rax, %r15
leaq 0x40(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xe839
xorl %eax, %eax
callq 0x4c81
movq 0x40(%rsp), %rdi
callq 0x4c6e
testl %eax, %eax
je 0xeb32
movq %r15, %rdi
callq 0x15804
leaq 0x48(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xe876
xorl %eax, %eax
callq 0x4c81
movq 0x8(%rsp), %rax
movl (%rax), %r14d
leal -0x1(%r14), %eax
imulq %r14, %rax
shrq %rax
movq %rax, 0x28(%rsp)
leal (,%rax,4), %edi
movl $0x4, %esi
callq 0x2290
movq %rax, %rbx
movq 0x48(%rsp), %rax
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0xea7f
leal (%r14,%r14), %eax
movl %eax, 0x20(%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 0xea6f
movl $0x11, %r14d
leal -0x11(%r14), %r13d
movl 0x24(%rsp), %eax
cmpb %al, 0x60(%rsp,%r13)
jb 0xea58
movl 0x50(%rsp,%r13), %esi
movl 0x54(%rsp,%r13), %edx
movq 0x8(%rsp), %r15
movq %r15, %rdi
leaq 0x4(%rsp), %rcx
leaq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0x13d50
movl 0x58(%rsp,%r13), %esi
movl 0x5c(%rsp,%r13), %edx
movq %r15, %rdi
movq %rsp, %rcx
leaq 0x10(%rsp), %r8
xorl %r9d, %r9d
callq 0x13d50
movl 0x4(%rsp), %eax
movl (%rsp), %ecx
cmpl %ecx, %eax
sete %dl
cmpl $-0x1, %eax
sete %sil
orb %dl, %sil
jne 0xea58
cmpl %ecx, %eax
jbe 0xe97c
movl %ecx, 0x4(%rsp)
movl %eax, (%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
movsd 0x12836(%rip), %xmm3    # 0x211c0
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x1283b(%rip), %xmm4    # 0x211d0
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
movl 0x4(%rsp), %eax
imulq $0x58, %rax, %rcx
movsd 0x8(%rsi,%rcx), %xmm0
unpcklps %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 %edx, %edx
movsd 0x1281b(%rip), %xmm5    # 0x211f8
ucomisd %xmm5, %xmm1
setbe %dl
movsd 0x10(%rsp), %xmm0
unpcklps %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 (%rsp), %ecx
imulq $0x58, %rcx, %rdi
movsd 0x8(%rsi,%rdi), %xmm0
unpcklps %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,%rdx,2), %edx
movq 0x28(%rsp), %rsi
imull %edx, %esi
movl %eax, %edx
notl %edx
addl 0x20(%rsp), %edx
imulq %rax, %rdx
notq %rax
leaq (%rbx,%rsi,4), %rsi
andq $-0x2, %rdx
leaq (%rsi,%rdx,2), %rdx
leaq (%rdx,%rcx,4), %rcx
incl (%rcx,%rax,4)
incq %rbp
cmpl %r12d, %r14d
jae 0xea6f
addl $0x11, %r14d
cmpq 0x30(%rsp), %rbp
jb 0xe8ed
cmpq 0x30(%rsp), %rbp
movq 0x38(%rsp), %r15
jb 0xe8c4
movq %r15, %rdi
callq 0x2150
movq 0x28(%rsp), %r15
movl %r15d, %edi
callq 0x2330
movq %rax, %r14
testl %r15d, %r15d
je 0xeb28
movl %r15d, %eax
xorl %ecx, %ecx
movsd 0x12ba3(%rip), %xmm0    # 0x21650
movq %rcx, %r8
xorl %r9d, %r9d
xorl %edx, %edx
xorl %edi, %edi
xorl %r10d, %r10d
xorl %esi, %esi
movl %r8d, %r11d
movl (%rbx,%r11,4), %r11d
cmpl %edi, %r11d
jbe 0xead8
movl $0x1, %r10d
movl %edi, %esi
movl %r11d, %edi
movl %r9d, %edx
jmp 0xeae6
jne 0xeadf
incl %r10d
jmp 0xeae6
cmpl %esi, %r11d
cmoval %r11d, %esi
incq %r9
addq %r15, %r8
cmpq $0x4, %r9
jne 0xeabc
movb $-0x1, %r8b
cmpl $0x1, %r10d
jne 0xeb1c
movl %edi, %edi
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
mulsd %xmm0, %xmm1
movl %esi, %esi
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
ucomisd %xmm2, %xmm1
jbe 0xeb1c
movl %edx, %r8d
movb %r8b, (%r14,%rcx)
incq %rcx
cmpq %rax, %rcx
jne 0xeaad
movq %rbx, %rdi
callq 0x2060
jmp 0xeb3a
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