// File: /workspace/github_c_makefile_star2_O1/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 $0x148, %rsp            # imm = 0x148
movl %edi, %r14d
movslq %edi, %rbx
movl $0xd8, %esi
movq %rbx, %rdi
callq 0x1140
movq %rax, 0x28(%rsp)
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %rdi
callq 0x1180
movq %rax, 0x20(%rsp)
movq 0x3a2f(%rip), %rax      # 0x5fd0
movq (%rax), %rax
movq %rax, 0x70(%rsp)
cmpl $0x2, 0x3b58(%rip)      # 0x6108
jge 0x271d
movq %r14, 0x18(%rsp)
testl %r14d, %r14d
movq 0x20(%rsp), %r14
jle 0x265b
movl 0x18(%rsp), %r12d
leaq 0xaef(%rip), %r15       # 0x30c4
leaq 0x80(%rsp), %r13
leaq 0xaec(%rip), %rbp       # 0x30d0
xorl %ebx, %ebx
movq 0x3b43(%rip), %rdx      # 0x6130
movq %r13, %rdi
movq %r15, %rsi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x11c0
movq %r13, %rdi
movq %rbp, %rsi
callq 0x11a0
movq 0x28(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
testq %rax, %rax
je 0x26d9
movl $0x10, %esi
movl $0x1, %edx
leaq 0x50(%rsp), %rdi
movq %rax, %rcx
callq 0x10a0
movl %ebx, 0x60(%rsp)
incq %rbx
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x50(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
movl %ebx, %esi
callq 0x22bb
cmpq %rbx, %r12
jne 0x25e6
movq 0x10(%r14), %rax
movq %rax, 0x40(%rsp)
movupd (%r14), %xmm0
movapd %xmm0, 0x30(%rsp)
movslq 0x10(%r14), %rbx
movq %r14, %rdi
movq %r14, %r15
movq 0x18(%rsp), %r14
movl %r14d, %esi
callq 0x235b
movq 0x28(%rsp), %r12
movq (%r12,%rbx,8), %rcx
leaq 0x50(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x10a0
movq (%r12,%rbx,8), %rdi
callq 0x1170
testl %eax, %eax
jne 0x2727
movl %ebx, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x50(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
callq 0x22bb
movl %r14d, %ebp
jmp 0x272b
leaq 0xaeb(%rip), %rdi       # 0x31cb
leaq 0x80(%rsp), %rsi
callq 0x1eed
movl $0x1, %ebx
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x2cfe
movq %r14, %rdi
callq 0x1030
movl %ebx, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1c55
jmp 0x25b6
leal -0x1(%r14), %ebp
movl $0x1, %r13d
testl %ebp, %ebp
jle 0x2885
movabsq $0x5d4e8fb00bcbe61d, %r15 # imm = 0x5D4E8FB00BCBE61D
xorl %r13d, %r13d
movq 0x20(%rsp), %r14
movl (%r14), %r12d
movl 0x4(%r14), %ebx
movsd 0x8(%r14), %xmm0
cmpl 0x30(%rsp), %r12d
jne 0x2776
cmpl 0x34(%rsp), %ebx
jne 0x2776
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
xorl %r14d, %r14d
jmp 0x27cd
movl %ebp, 0x6c(%rsp)
movq %r15, %rbp
movq %r13, %r15
movq 0x10(%r14), %r13
movl $0x1, %r14d
movl $0x10, %esi
movl $0x1, %edx
leaq 0x30(%rsp), %rdi
movq 0x70(%rsp), %rcx
movsd %xmm0, 0x78(%rsp)
callq 0x11e0
movl %r12d, 0x30(%rsp)
movl %ebx, 0x34(%rsp)
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq %r13, 0x40(%rsp)
movq %r15, %r13
movq %rbp, %r15
movl 0x6c(%rsp), %ebp
addq %r14, %r13
movq %r13, %rax
imulq %r15, %rax
rorq $0x5, %rax
movabsq $0xa7c5ac471b47, %rcx   # imm = 0xA7C5AC471B47
cmpq %rcx, %rax
ja 0x27f3
cmpl $0x2, 0x3917(%rip)      # 0x6108
jge 0x2862
movq 0x20(%rsp), %r14
movslq 0x10(%r14), %r12
movq %r14, %rdi
movl %ebp, %esi
callq 0x235b
movq 0x28(%rsp), %rbx
movq (%rbx,%r12,8), %rcx
movl $0x10, %esi
movl $0x1, %edx
leaq 0x50(%rsp), %rdi
callq 0x10a0
movq (%rbx,%r12,8), %rdi
callq 0x1170
testl %eax, %eax
je 0x2834
decl %ebp
jmp 0x2858
movl %r12d, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x50(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
callq 0x22bb
testl %ebp, %ebp
jg 0x274b
jmp 0x2882
movq 0x378f(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x860(%rip), %rsi       # 0x30d3
movq %r13, %rdx
xorl %eax, %eax
callq 0x1160
jmp 0x27f3
incq %r13
leaq 0x30(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
movq 0x70(%rsp), %rcx
callq 0x11e0
movq 0x3753(%rip), %r15      # 0x5ff8
movq (%r15), %rdi
leaq 0x835(%rip), %rsi       # 0x30e4
xorl %ebx, %ebx
movq %r13, %rdx
xorl %eax, %eax
callq 0x1160
movq 0x18(%rsp), %r14
testl %r14d, %r14d
jle 0x28fb
leaq 0x7f8(%rip), %r12       # 0x30c4
leaq 0x80(%rsp), %r13
xorl %ebp, %ebp
movq 0x3853(%rip), %rdx      # 0x6130
movq %r13, %rdi
movq %r12, %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x11c0
movq %r13, %rdi
callq 0x1050
incl %ebp
cmpl %ebp, %r14d
jne 0x28d6
movq (%r15), %rsi
movl $0xa, %edi
callq 0x1130
movq 0x20(%rsp), %r14
jmp 0x26f2