// File: /workspace/github_c_makefile_star2_O2/stanfordnlp[P]GloVe/src/shuffle.c
// Function: shuffle_merge
int shuffle_merge(int num) {
    long i, j, k, l = 0;
    int fidcounter = 0;
    CREC *array;
    char filename[MAX_STRING_LENGTH];
    FILE **fid, *fout = stdout;
    
    array = malloc(sizeof(CREC) * array_size);
    fid = calloc(num, sizeof(FILE));
    for (fidcounter = 0; fidcounter < num; fidcounter++) { //num = number of temporary files to merge
        sprintf(filename,"%s_%04d.bin",file_head, fidcounter);
        fid[fidcounter] = fopen(filename, "rb");
        if (fid[fidcounter] == NULL) {
            log_file_loading_error("temp file", filename);
            free(array);
            free_fid(fid, num);
            return 1;
        }
    }
    if (verbose > 0) fprintf(stderr, "Merging temp files: processed %ld lines.", l);
    
    while (1) { //Loop until EOF in all files
        i = 0;
        //Read at most array_size values into array, roughly array_size/num from each temp file
        for (j = 0; j < num; j++) {
            if (feof(fid[j])) continue;
            for (k = 0; k < array_size / num; k++){
                fread(&array[i], sizeof(CREC), 1, fid[j]);
                if (feof(fid[j])) break;
                i++;
            }
        }
        if (i == 0) break;
        l += i;
        shuffle(array, i); // Shuffles lines between temp files
        write_chunk(array,i,fout);
        if (verbose > 0) fprintf(stderr, "\033[31G%ld lines.", l);
    }
    fprintf(stderr, "\033[0GMerging temp files: processed %ld lines.", l);
    for (fidcounter = 0; fidcounter < num; fidcounter++) {
        fclose(fid[fidcounter]);
        sprintf(filename,"%s_%04d.bin",file_head, fidcounter);
        remove(filename);
    }
    fprintf(stderr, "\n\n");
    free(array);
    free(fid);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movl %edi, %ebp
movq 0x2c28(%rip), %rax      # 0x3fd0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x2d41(%rip), %rdi      # 0x40f8
shlq $0x4, %rdi
callq 0x1140
movq %rax, 0x8(%rsp)
movslq %ebp, %r12
movl $0xd8, %esi
movq %r12, %rdi
callq 0x10f0
movq %rax, 0x10(%rsp)
testl %r12d, %r12d
movl $0x0, %eax
cmovgl %ebp, %eax
movq %rax, 0x28(%rsp)
leaq 0xc2f(%rip), %rbx       # 0x2020
leaq 0x40(%rsp), %r14
leaq 0xc2f(%rip), %r15       # 0x202c
xorl %r13d, %r13d
cmpq %r13, 0x28(%rsp)
je 0x1476
movq 0x2d0a(%rip), %rdx      # 0x4118
movq %r14, %rdi
movq %rbx, %rsi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x1180
movq %r14, %rdi
movq %r15, %rsi
callq 0x1160
movq 0x10(%rsp), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
testq %rax, %rax
jne 0x1400
leaq 0xbee(%rip), %rdi       # 0x202f
leaq 0x40(%rsp), %rsi
callq 0x1d51
movq 0x8(%rsp), %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
movl %ebp, %esi
callq 0x1d18
pushq $0x1
popq %rax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x2c73(%rip)      # 0x40f0
jle 0x1499
movq 0x2b72(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbbd(%rip), %rsi       # 0x204d
xorl %edx, %edx
xorl %eax, %eax
callq 0x1110
testq %r12, %r12
movl $0x0, %ebp
cmovgq %r12, %rbp
pushq $0x1
popq %r15
xorl %ebx, %ebx
movq 0x10(%rsp), %r13
movq %rbp, 0x38(%rsp)
movq %rbx, 0x20(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
cmpq %rbp, %r14
je 0x1537
movq (%r13,%r14,8), %rbx
movq %rbx, %rdi
callq 0x1120
testl %eax, %eax
jne 0x1532
movq 0x18(%rsp), %r13
shlq $0x4, %r13
addq 0x8(%rsp), %r13
xorl %ebp, %ebp
movq 0x2c05(%rip), %rax      # 0x40f8
cqto
idivq %r12
cmpq %rax, %rbp
jge 0x1523
movq %r13, %rdi
pushq $0x10
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1080
movq %rbx, %rdi
callq 0x1120
testl %eax, %eax
jne 0x1523
incq %rbp
addq $0x10, %r13
jmp 0x14ec
addq %rbp, 0x18(%rsp)
movq 0x10(%rsp), %r13
movq 0x38(%rsp), %rbp
incq %r14
jmp 0x14c6
movq 0x18(%rsp), %r14
testq %r14, %r14
je 0x1595
movq 0x20(%rsp), %rbx
addq %r14, %rbx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x1306
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq 0x30(%rsp), %rdx
callq 0x12bc
cmpl $0x0, 0x2b81(%rip)      # 0x40f0
jle 0x14b5
movq 0x2a7c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xab3(%rip), %rsi       # 0x2039
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1110
jmp 0x14b5
movq 0x2a5c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xaa3(%rip), %rsi       # 0x2049
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
leaq 0xa67(%rip), %rbx       # 0x2020
leaq 0x40(%rsp), %r14
xorl %r15d, %r15d
cmpq %r15, 0x28(%rsp)
je 0x15f6
movq (%r13,%r15,8), %rdi
callq 0x10b0
movq 0x2b3f(%rip), %rdx      # 0x4118
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1180
movq %r14, %rdi
callq 0x1050
incq %r15
jmp 0x15c1
movq 0x29fb(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xa6f(%rip), %rdi       # 0x2076
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x11a0
movq 0x8(%rsp), %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x1464