// File: /workspace/github_c_makefile_star2_O2/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, %r14
leaq 0x13c14(%rip), %rsi     # 0x1f311
callq 0x23e0
testq %rax, %rax
je 0xba11
movq %rax, %r15
leaq 0x40(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xb72d
xorl %eax, %eax
callq 0x2986
movq 0x40(%rsp), %rdi
callq 0x2973
testl %eax, %eax
je 0xba29
movq %r15, %rdi
callq 0x11940
leaq 0x48(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x20f0
cmpl $0x1, %eax
je 0xb766
xorl %eax, %eax
callq 0x2986
movl (%r14), %ebx
leal -0x1(%rbx), %eax
imulq %rbx, %rax
shrq %rax
movq %rax, 0x10(%rsp)
leal (,%rax,4), %edi
pushq $0x4
popq %rsi
callq 0x2290
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %r12
leal (%rbx,%rbx), %eax
movl %eax, 0x18(%rsp)
xorl %ebp, %ebp
movq %r15, 0x20(%rsp)
cmpq %r12, %rbp
jae 0xb961
movl $0x11000, %edx          # imm = 0x11000
leaq 0x50(%rsp), %rdi
pushq $0x1
popq %rsi
movq %r15, %rcx
callq 0x20f0
movq %rax, %r13
xorl %r15d, %r15d
cmpl %r13d, %r15d
jae 0xb957
cmpq %r12, %rbp
jae 0xb957
movl %r15d, %ebx
movl 0x1c(%rsp), %eax
cmpb %al, 0x60(%rsp,%rbx)
jb 0xb94b
movl 0x50(%rsp,%rbx), %esi
movl 0x54(%rsp,%rbx), %edx
movq %r14, %rdi
leaq 0xc(%rsp), %rcx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
callq 0x100d2
movl 0x58(%rsp,%rbx), %esi
movl 0x5c(%rsp,%rbx), %edx
movq %r14, %rdi
leaq 0x8(%rsp), %rcx
leaq 0x30(%rsp), %r8
xorl %r9d, %r9d
callq 0x100d2
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %eax
cmpl %eax, %ecx
sete %dl
cmpl $-0x1, %ecx
sete %sil
orb %dl, %sil
jne 0xb94b
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %rdi
cmpl %eax, %ecx
jbe 0xb864
movl %eax, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
movq %rdi, 0x38(%rsp)
movq %r8, 0x30(%rsp)
movl %ecx, %edx
movq %r8, %rsi
jmp 0xb86e
movl %eax, %edx
movq %rdi, %rsi
movl %ecx, %eax
movq %r8, %rdi
movq 0x10(%rsp), %r9
movq %rdi, %xmm0
movq 0x12930(%rip), %xmm3    # 0x1e1b0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x12934(%rip), %xmm4    # 0x1e1c0
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq 0x8(%r14), %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 0x12911(%rip), %xmm5    # 0x1e1e0
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
imull %r9d, %esi
notl %eax
addl 0x18(%rsp), %eax
imulq %rcx, %rax
notq %rcx
movq 0x28(%rsp), %rdi
leaq (%rdi,%rsi,4), %rsi
andq $-0x2, %rax
leaq (%rsi,%rax,2), %rax
leaq (%rax,%rdx,4), %rax
incl (%rax,%rcx,4)
addl $0x11, %r15d
incq %rbp
jmp 0xb7c3
movq 0x20(%rsp), %r15
jmp 0xb79f
movq %r15, %rdi
callq 0x2150
movq 0x10(%rsp), %rbx
movl %ebx, %r12d
movq %r12, %rdi
callq 0x2330
movq %rax, %r14
xorl %eax, %eax
movsd 0x12cba(%rip), %xmm0    # 0x1e640
pushq $0x1
popq %rcx
movq 0x28(%rsp), %rdi
cmpq %r12, %rax
je 0xba0a
xorl %r15d, %r15d
movq %rax, %r8
xorl %esi, %esi
xorl %r9d, %r9d
xorl %r10d, %r10d
xorl %edx, %edx
cmpq $0x4, %r15
je 0xb9dc
movl %r8d, %r11d
movl (%rdi,%r11,4), %r11d
cmpl %r10d, %r11d
jbe 0xb9c0
movl %r10d, %esi
movl %ecx, %r9d
movl %r15d, %edx
jmp 0xb9d1
jne 0xb9c7
incl %r9d
jmp 0xb9ce
cmpl %esi, %r11d
cmoval %r11d, %esi
movl %r10d, %r11d
incq %r15
addq %rbx, %r8
movl %r11d, %r10d
jmp 0xb9a3
movb $-0x1, %r11b
cmpl $0x1, %r9d
jne 0xba01
movl %r10d, %r8d
cvtsi2sd %r8, %xmm1
mulsd %xmm0, %xmm1
movl %esi, %esi
cvtsi2sd %rsi, %xmm2
ucomisd %xmm2, %xmm1
jbe 0xba01
movl %edx, %r11d
movb %r11b, (%r14,%rax)
incq %rax
jmp 0xb98e
callq 0x2060
jmp 0xba14
xorl %r14d, %r14d
movq %r14, %rax
addq $0x11058, %rsp          # imm = 0x11058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1c5c8(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x127fd(%rip), %rsi     # 0x1e237
leaq 0x12ee8(%rip), %rdx     # 0x1e929
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x22c0
jmp 0xba14