// File: /workspace/github_c_makefile_star2_O2/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, %r15
imulq $0x18, %rbx, %rdi
callq 0x1170
movq %rax, %r14
movq 0x47f3(%rip), %rax      # 0x5fd0
movq (%rax), %rax
movq %rax, 0x40(%rsp)
cmpl $0x2, 0x4914(%rip)      # 0x6100
jge 0x1934
testl %r12d, %r12d
movl $0x0, %eax
movq %r12, 0x20(%rsp)
cmovgl %r12d, %eax
movq %rax, 0x48(%rsp)
leaq 0x70(%rsp), %r12
leaq 0x18ac(%rip), %r13      # 0x30c0
pushq $0x1
popq %rbp
xorl %ebx, %ebx
cmpq %rbx, 0x48(%rsp)
je 0x18c6
movq 0x48fd(%rip), %rdx      # 0x6128
movq %r12, %rdi
leaq 0x187f(%rip), %rsi      # 0x30b4
movl %ebx, %ecx
xorl %eax, %eax
callq 0x11b0
movq %r12, %rdi
movq %r13, %rsi
callq 0x1190
movq %rax, (%r15,%rbx,8)
testq %rax, %rax
je 0x188b
leaq 0x28(%rsp), %rdi
pushq $0x10
popq %rsi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x10a0
movl %ebx, 0x38(%rsp)
incq %rbx
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movl %ebx, %esi
callq 0x1522
jmp 0x1819
leaq 0x1929(%rip), %rdi      # 0x31bb
leaq 0x70(%rsp), %rsi
callq 0x2ae1
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x2aa8
movq %r14, %rdi
callq 0x1030
pushq $0x1
popq %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r14), %rax
movq %rax, 0x60(%rsp)
movups (%r14), %xmm0
movaps %xmm0, 0x50(%rsp)
movslq 0x10(%r14), %rbx
movq %r14, %rdi
movq 0x20(%rsp), %r12
movl %r12d, %esi
callq 0x15b5
movq (%r15,%rbx,8), %rcx
leaq 0x28(%rsp), %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
callq 0x10a0
movq (%r15,%rbx,8), %rdi
callq 0x1160
testl %eax, %eax
jne 0x1955
movl %ebx, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movl %r12d, %esi
callq 0x1522
movl %r12d, %ebp
jmp 0x195a
movq 0x46bd(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x1740(%rip), %rdi      # 0x3085
pushq $0x2e
popq %rsi
pushq $0x1
popq %rdx
callq 0x11d0
jmp 0x17f2
leal -0x1(%r12), %ebp
pushq $0x1
popq %r12
xorl %r13d, %r13d
testl %ebp, %ebp
jle 0x1a22
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
leaq 0x50(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x1746
movl %eax, %eax
addq %rax, %r13
movq %r13, %rax
xorl %edx, %edx
movl $0x186a0, %ecx          # imm = 0x186A0
divq %rcx
testq %rdx, %rdx
jne 0x19a9
cmpl $0x2, 0x4759(%rip)      # 0x6100
jge 0x19fe
movslq 0x10(%r14), %rbx
movq %r14, %rdi
movl %ebp, %esi
callq 0x15b5
movq (%r15,%rbx,8), %rcx
leaq 0x28(%rsp), %rdi
pushq $0x10
popq %rsi
movq %r12, %rdx
callq 0x10a0
movq (%r15,%rbx,8), %rdi
callq 0x1160
testl %eax, %eax
jne 0x1a1b
movl %ebx, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
callq 0x1522
jmp 0x1961
movq 0x45f3(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x16b4(%rip), %rsi      # 0x30c3
movq %r13, %rdx
xorl %eax, %eax
callq 0x1150
jmp 0x19a9
decl %ebp
jmp 0x1961
leaq 0x50(%rsp), %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq 0x40(%rsp), %rcx
callq 0x11d0
movq 0x45ba(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
incq %r13
leaq 0x1689(%rip), %rsi      # 0x30d4
movq %r13, %rdx
xorl %eax, %eax
callq 0x1150
leaq 0x1658(%rip), %rbx      # 0x30b4
leaq 0x70(%rsp), %r12
xorl %ebp, %ebp
cmpl %ebp, 0x48(%rsp)
je 0x1a8b
movq 0x46b8(%rip), %rdx      # 0x6128
movq %r12, %rdi
movq %rbx, %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x11b0
movq %r12, %rdi
callq 0x1050
incl %ebp
jmp 0x1a63
movq 0x4566(%rip), %rax      # 0x5ff8
movq (%rax), %rsi
pushq $0xa
popq %rdi
callq 0x1120
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x2aa8
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x18b4