// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movl %edi, -0x8(%rbp)
movq $0x0, -0x18(%rbp)
movslq -0x8(%rbp), %rdi
movl $0xd8, %esi
callq 0x1110
movq %rax, -0x128(%rbp)
movslq -0x8(%rbp), %rax
imulq $0x18, %rax, %rdi
callq 0x1150
movq %rax, -0x20(%rbp)
movq 0x54cd(%rip), %rax      # 0x6fd0
movq (%rax), %rax
movq %rax, -0x130(%rbp)
cmpl $0x1, 0x55dc(%rip)      # 0x70f0
jle 0x1b2e
movq 0x54db(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x255e(%rip), %rsi      # 0x4085
movb $0x0, %al
callq 0x1130
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x1c30
leaq -0x120(%rbp), %rdi
movq 0x55c9(%rip), %rdx      # 0x7118
movl -0xc(%rbp), %ecx
leaq 0x255b(%rip), %rsi      # 0x40b4
movb $0x0, %al
callq 0x1190
leaq -0x120(%rbp), %rdi
leaq 0x2552(%rip), %rsi      # 0x40c0
callq 0x1170
movq %rax, %rdx
movq -0x128(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x128(%rbp), %rax
movslq -0xc(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x1bce
leaq -0x120(%rbp), %rsi
leaq 0x2616(%rip), %rdi      # 0x41bb
callq 0x3500
movq -0x128(%rbp), %rdi
movl -0x8(%rbp), %esi
callq 0x34a0
movq -0x20(%rbp), %rdi
callq 0x1030
movl $0x1, -0x4(%rbp)
jmp 0x1ea4
movq -0x128(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq -0x38(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x1090
movl -0xc(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movl -0xc(%rbp), %esi
addl $0x1, %esi
leaq -0x38(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x16f0
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x1b35
movl -0x8(%rbp), %eax
movl %eax, -0x10(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x50(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x48(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xc(%rbp)
movq -0x20(%rbp), %rdi
movl -0x10(%rbp), %esi
callq 0x17d0
movq -0x128(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq -0x38(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x1090
movq -0x128(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1140
cmpl $0x0, %eax
je 0x1cad
movl -0x10(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x1cdc
movl -0xc(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movl -0x10(%rbp), %esi
leaq -0x38(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x16f0
jmp 0x1cde
cmpl $0x0, -0x10(%rbp)
jle 0x1dea
movq -0x20(%rbp), %rax
movq -0x130(%rbp), %rsi
leaq -0x50(%rbp), %rdi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x1a20
cltq
addq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl $0x186a0, %ecx          # imm = 0x186A0
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0x1d5a
cmpl $0x1, 0x53b6(%rip)      # 0x70f0
jle 0x1d58
movq 0x52b5(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x2372(%rip), %rsi      # 0x40c3
movb $0x0, %al
callq 0x1130
jmp 0x1d5a
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xc(%rbp)
movq -0x20(%rbp), %rdi
movl -0x10(%rbp), %esi
callq 0x17d0
movq -0x128(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq -0x38(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x1090
movq -0x128(%rbp), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1140
cmpl $0x0, %eax
je 0x1db6
movl -0x10(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x1de5
movl -0xc(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movl -0x10(%rbp), %esi
leaq -0x38(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x16f0
jmp 0x1cde
movq -0x130(%rbp), %rcx
leaq -0x50(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x11b0
movq 0x51ed(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
leaq 0x22b3(%rip), %rsi      # 0x40d4
movb $0x0, %al
callq 0x1130
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x1e6d
leaq -0x120(%rbp), %rdi
movq 0x52d3(%rip), %rdx      # 0x7118
movl -0xc(%rbp), %ecx
leaq 0x2265(%rip), %rsi      # 0x40b4
movb $0x0, %al
callq 0x1190
leaq -0x120(%rbp), %rdi
callq 0x1050
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x1e2f
movq 0x5184(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x253e(%rip), %rsi      # 0x43bc
movb $0x0, %al
callq 0x1130
movq -0x128(%rbp), %rdi
movl -0x8(%rbp), %esi
callq 0x34a0
movq -0x20(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq