// File: /workspace/github_c_makefile_star2_O3/stanfordnlp[P]GloVe/src/cooccur.c
// Function: merge_files
int merge_files(int num) {
    int i, size;
    long long counter = 0;
    CRECID *pq, new, old;
    char filename[200];
    FILE **fid, *fout;
    fid = calloc(num, sizeof(FILE));
    pq = malloc(sizeof(CRECID) * num);
    fout = stdout;
    if (verbose > 1) fprintf(stderr, "Merging cooccurrence files: processed 0 lines.");
    
    /* Open all files and add first entry of each to priority queue */
    for (i = 0; i < num; i++) {
        sprintf(filename,"%s_%04d.bin",file_head,i);
        fid[i] = fopen(filename,"rb");
        if (fid[i] == NULL) {log_file_loading_error("file", filename); free_fid(fid, num); free(pq); return 1;}
        fread(&new, sizeof(CREC), 1, fid[i]);
        new.id = i;
        insert(pq,new,i+1);
    }
    
    /* Pop top node, save it in old to see if the next entry is a duplicate */
    size = num;
    old = pq[0];
    i = pq[0].id;
    delete(pq, size);
    fread(&new, sizeof(CREC), 1, fid[i]);
    if (feof(fid[i])) size--;
    else {
        new.id = i;
        insert(pq, new, size);
    }
    
    /* Repeatedly pop top node and fill priority queue until files have reached EOF */
    while (size > 0) {
        counter += merge_write(pq[0], &old, fout); // Only count the lines written to file, not duplicates
        if ((counter%100000) == 0) if (verbose > 1) fprintf(stderr,"\033[39G%lld lines.",counter);
        i = pq[0].id;
        delete(pq, size);
        fread(&new, sizeof(CREC), 1, fid[i]);
        if (feof(fid[i])) size--;
        else {
            new.id = i;
            insert(pq, new, size);
        }
    }
    fwrite(&old, sizeof(CREC), 1, fout);
    fprintf(stderr,"\033[0GMerging cooccurrence files: processed %lld lines.\n",++counter);
    for (i=0;i<num;i++) {
        sprintf(filename,"%s_%04d.bin",file_head,i);
        remove(filename);
    }
    fprintf(stderr,"\n");
    free_fid(fid, num);
    free(pq);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %edi, %r12d
movslq %edi, %rbx
movl $0xd8, %esi
movq %rbx, %rdi
callq 0x1130
movq %rax, 0x18(%rsp)
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %rdi
callq 0x1170
movq %rax, %rbx
movq 0x3a4b(%rip), %rax      # 0x5fd0
movq (%rax), %rax
movq %rax, 0x58(%rsp)
cmpl $0x2, 0x3b6c(%rip)      # 0x6100
jge 0x2677
testl %r12d, %r12d
jle 0x2681
movq %r12, %r14
movl %r12d, %r15d
leaq 0x70(%rsp), %rbp
leaq 0xb1b(%rip), %r12       # 0x30d0
xorl %r13d, %r13d
movq 0x3b69(%rip), %rdx      # 0x6128
movq %rbp, %rdi
leaq 0xafb(%rip), %rsi       # 0x30c4
movl %r13d, %ecx
xorl %eax, %eax
callq 0x11b0
movq %rbp, %rdi
movq %r12, %rsi
callq 0x1190
movq 0x18(%rsp), %rcx
movq %rax, (%rcx,%r13,8)
testq %rax, %rax
je 0x2638
movl $0x10, %esi
movl $0x1, %edx
leaq 0x28(%rsp), %rdi
movq %rax, %rcx
callq 0x10a0
movl %r13d, 0x38(%rsp)
incq %r13
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movl %r13d, %esi
callq 0x22a6
cmpq %r13, %r15
jne 0x25b8
movl 0x10(%rbx), %ebp
movq %r14, %r12
jmp 0x2681
leaq 0xb8c(%rip), %rdi       # 0x31cb
leaq 0x70(%rsp), %rsi
callq 0x1ee4
movq 0x18(%rsp), %rdi
movl %r15d, %esi
callq 0x2d0c
movq %rbx, %rdi
callq 0x1030
movl $0x1, %ebp
movl %ebp, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1c4c
jmp 0x259a
movq 0x10(%rbx), %rax
movq %rax, 0x50(%rsp)
movupd (%rbx), %xmm0
movapd %xmm0, 0x40(%rsp)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x2341
movslq %ebp, %r14
movq 0x18(%rsp), %r15
movq (%r15,%r14,8), %rcx
leaq 0x28(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x10a0
movq (%r15,%r14,8), %rdi
callq 0x1160
testl %eax, %eax
jne 0x26f5
movl %ebp, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x22a6
movl %r12d, %edx
jmp 0x26fa
leal -0x1(%r12), %edx
movl $0x1, %r13d
testl %edx, %edx
jle 0x287a
movq %r12, 0x60(%rsp)
leaq 0x40(%rsp), %rax
movl (%rax), %ecx
movl 0x4(%rax), %eax
xorl %r13d, %r13d
movq %rbx, 0x68(%rsp)
movl (%rbx), %r12d
movl 0x4(%rbx), %r15d
movsd 0x8(%rbx), %xmm0
cmpl %ecx, %r12d
jne 0x2746
cmpl %eax, %r15d
jne 0x2746
addsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
xorl %r14d, %r14d
jmp 0x278e
movq 0x10(%rbx), %rbx
movl $0x1, %r14d
movl %edx, %ebp
movl $0x10, %esi
movl $0x1, %edx
leaq 0x40(%rsp), %rdi
movq 0x58(%rsp), %rcx
movsd %xmm0, 0x20(%rsp)
callq 0x11d0
movl %ebp, %edx
movl %r12d, 0x40(%rsp)
movl %r15d, 0x44(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movq %rbx, 0x50(%rsp)
movl %r12d, 0x20(%rsp)
addq %r14, %r13
movq %r13, %rax
movabsq $0x5d4e8fb00bcbe61d, %rcx # imm = 0x5D4E8FB00BCBE61D
imulq %rcx, %rax
rorq $0x5, %rax
movabsq $0xa7c5ac471b47, %rcx   # imm = 0xA7C5AC471B47
cmpq %rcx, %rax
ja 0x27c7
cmpl $0x2, 0x393f(%rip)      # 0x6100
jge 0x284e
movq %r13, %rbp
movq 0x68(%rsp), %rbx
movslq 0x10(%rbx), %r14
movq %rbx, %rdi
movl %edx, %r12d
movl %edx, %esi
callq 0x2341
movq 0x18(%rsp), %r13
movq (%r13,%r14,8), %rcx
movl $0x10, %esi
movl $0x1, %edx
leaq 0x28(%rsp), %rdi
callq 0x10a0
movq (%r13,%r14,8), %rdi
callq 0x1160
testl %eax, %eax
je 0x2811
decl %r12d
jmp 0x2836
movl %r14d, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x28(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x22a6
movl %r12d, %edx
movl %r15d, %eax
testl %r12d, %r12d
movq %rbp, %r13
movl 0x20(%rsp), %ecx
jg 0x271f
jmp 0x2872
movq 0x37a3(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movl %edx, %ebx
leaq 0x872(%rip), %rsi       # 0x30d3
movq %r13, %rdx
xorl %eax, %eax
callq 0x1150
movl %ebx, %edx
jmp 0x27c7
incq %r13
movq 0x60(%rsp), %r12
leaq 0x40(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
movq 0x58(%rsp), %rcx
callq 0x11d0
movq 0x375e(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x840(%rip), %rsi       # 0x30e4
xorl %ebp, %ebp
movq %r13, %rdx
xorl %eax, %eax
callq 0x1150
testl %r12d, %r12d
movq %r12, %r14
jle 0x28ee
leaq 0x805(%rip), %r15       # 0x30c4
leaq 0x70(%rsp), %r12
xorl %r13d, %r13d
movq 0x385a(%rip), %rdx      # 0x6128
movq %r12, %rdi
movq %r15, %rsi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x11b0
movq %r12, %rdi
callq 0x1050
incl %r13d
cmpl %r13d, %r14d
jne 0x28c7
movq 0x3703(%rip), %rax      # 0x5ff8
movq (%rax), %rsi
movl $0xa, %edi
callq 0x1120
movq 0x18(%rsp), %rdi
movl %r14d, %esi
callq 0x2d0c
movq %rbx, %rdi
callq 0x1030
jmp 0x2663