// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110e0, %rsp          # imm = 0x110E0
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x20f3e(%rip), %rsi     # 0x32311
callq 0x23f0
movq %rax, -0x110b0(%rbp)
cmpq $0x0, -0x110b0(%rbp)
jne 0x113f6
movq $0x0, -0x8(%rbp)
jmp 0x118ce
movq -0x110b0(%rbp), %rcx
leaq -0x88(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x20f0
movl %eax, -0x2c(%rbp)
cmpl $0x1, -0x2c(%rbp)
je 0x11423
movb $0x0, %al
callq 0x2c00
movq -0x88(%rbp), %rdi
callq 0x2bc0
cmpl $0x0, %eax
jne 0x11460
movq 0x27bbd(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
leaq 0x1fdd2(%rip), %rsi     # 0x31217
leaq 0x204c5(%rip), %rdx     # 0x31911
movb $0x0, %al
callq 0x22c0
movq $0x0, -0x8(%rbp)
jmp 0x118ce
movq -0x110b0(%rbp), %rdi
callq 0x1bac0
movq -0x110b0(%rbp), %rcx
leaq -0x70(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x20f0
movl %eax, -0x2c(%rbp)
cmpl $0x1, -0x2c(%rbp)
je 0x11496
movb $0x0, %al
callq 0x2c00
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl -0x30(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
imulq %rcx, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
shll $0x2, %eax
movl %eax, %eax
movl %eax, %edi
movl $0x4, %esi
callq 0x2290
movq %rax, -0x11098(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0x11770
leaq -0x11090(%rbp), %rdi
movq -0x110b0(%rbp), %rcx
movl $0x1, %esi
movl $0x11000, %edx          # imm = 0x11000
callq 0x20f0
movl %eax, -0x2c(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl -0x2c(%rbp), %ecx
movb %al, -0x110c1(%rbp)
jae 0x1153c
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
setb %al
movb %al, -0x110c1(%rbp)
movb -0x110c1(%rbp), %al
testb $0x1, %al
jne 0x1154b
jmp 0x1176b
movl -0x20(%rbp), %eax
movzbl -0x11080(%rbp,%rax), %eax
movzbl -0x19(%rbp), %ecx
cmpl %ecx, %eax
jge 0x11563
jmp 0x11751
movq -0x18(%rbp), %rdi
movl -0x20(%rbp), %eax
movl -0x11090(%rbp,%rax), %esi
movl -0x20(%rbp), %eax
movl -0x1108c(%rbp,%rax), %edx
leaq -0x38(%rbp), %rcx
leaq -0x60(%rbp), %r8
xorl %r9d, %r9d
callq 0x19980
movq -0x18(%rbp), %rdi
movl -0x20(%rbp), %eax
movl -0x11088(%rbp,%rax), %esi
movl -0x20(%rbp), %eax
movl -0x11084(%rbp,%rax), %edx
leaq -0x3c(%rbp), %rcx
leaq -0x68(%rbp), %r8
xorl %r9d, %r9d
callq 0x19980
movl -0x38(%rbp), %eax
cmpl -0x3c(%rbp), %eax
je 0x1174f
cmpl $-0x1, -0x38(%rbp)
je 0x1174f
movl -0x38(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jbe 0x11607
movl -0x38(%rbp), %eax
movl %eax, -0x110b4(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x110b4(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x110c0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x110c0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %xmm0
movaps 0x1fb8d(%rip), %xmm3    # 0x311a0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x1fb91(%rip), %xmm2    # 0x311b0
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x38(%rbp), %ecx
imulq $0x58, %rcx, %rcx
movq 0x8(%rax,%rcx), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm4
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm4, %xmm1
divsd %xmm1, %xmm0
movsd 0x1ff9e(%rip), %xmm1    # 0x31600
ucomisd %xmm1, %xmm0
setbe %al
movzbl %al, %eax
movl %eax, -0x40(%rbp)
movq -0x68(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x58, %rcx, %rcx
movq 0x8(%rax,%rcx), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd 0x1ff45(%rip), %xmm1    # 0x31600
ucomisd %xmm1, %xmm0
seta %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x44(%rbp)
movl -0x40(%rbp), %eax
shll %eax
cmpl $0x0, -0x44(%rbp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x48(%rbp)
movq -0x11098(%rbp), %rax
movq %rax, -0x110d0(%rbp)
movl -0x48(%rbp), %eax
imull -0x34(%rbp), %eax
movl %eax, %eax
movl %eax, %ecx
movl -0x30(%rbp), %eax
shll %eax
subl -0x38(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl -0x38(%rbp), %edx
imulq %rdx, %rax
movl $0x2, %esi
cqto
idivq %rsi
movq %rax, %rdx
movq -0x110d0(%rbp), %rax
addq %rdx, %rcx
movl -0x3c(%rbp), %edx
addq %rdx, %rcx
movl -0x38(%rbp), %edx
subq %rdx, %rcx
subq $0x1, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x11751
movl -0x20(%rbp), %eax
addl $0x11, %eax
movl %eax, -0x20(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x1151b
jmp 0x114e6
movq -0x110b0(%rbp), %rdi
callq 0x2150
movl -0x34(%rbp), %eax
movl %eax, %edi
shlq $0x0, %rdi
callq 0x2340
movq %rax, -0x110a8(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x118b7
movl $0x0, -0x24(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x4, -0x28(%rbp)
jae 0x11854
movq -0x11098(%rbp), %rax
movl -0x28(%rbp), %ecx
imull -0x34(%rbp), %ecx
addl -0x20(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1109c(%rbp)
movl -0x1109c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jbe 0x11816
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x1109c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl $0x1, -0x54(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x11844
movl -0x1109c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jne 0x1182c
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x11842
movl -0x1109c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jbe 0x11840
movl -0x1109c(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x11842
jmp 0x11844
jmp 0x11846
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x117c7
cmpl $0x1, -0x54(%rbp)
jne 0x11887
movl -0x4c(%rbp), %eax
cvtsi2sd %rax, %xmm0
movsd 0x1fdbe(%rip), %xmm1    # 0x31628
mulsd %xmm1, %xmm0
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x11887
movl -0x24(%rbp), %eax
movl %eax, -0x110d4(%rbp)
jmp 0x11894
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x110d4(%rbp)
jmp 0x11894
movl -0x110d4(%rbp), %eax
movb %al, %dl
movq -0x110a8(%rbp), %rax
movl -0x20(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x11798
movq -0x11098(%rbp), %rdi
callq 0x2050
movq -0x110a8(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x110e0, %rsp          # imm = 0x110E0
popq %rbp
retq
nopl (%rax,%rax)