// File: /workspace/github_c_makefile_star2_O3/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 $0x418, %rsp            # imm = 0x418
movl %edi, %ebx
movq 0x270e(%rip), %rax      # 0x3fd0
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x2827(%rip), %rdi      # 0x40f8
shlq $0x4, %rdi
callq 0x1140
movq %rax, 0x8(%rsp)
movl %ebx, 0x4(%rsp)
movslq %ebx, %r13
movl $0xd8, %esi
movq %r13, %rdi
callq 0x10f0
movq %rax, %rbx
movl %r13d, %r12d
testl %r13d, %r13d
jle 0x1948
leaq 0x30(%rsp), %r15
leaq 0x722(%rip), %rbp       # 0x202c
xorl %r14d, %r14d
movq 0x2804(%rip), %rdx      # 0x4118
movq %r15, %rdi
leaq 0x702(%rip), %rsi       # 0x2020
movl %r14d, %ecx
xorl %eax, %eax
callq 0x1180
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1160
movq %rax, (%rbx,%r14,8)
testq %rax, %rax
je 0x1a74
incq %r14
cmpq %r14, %r12
jne 0x190d
cmpl $0x0, 0x27a1(%rip)      # 0x40f0
jle 0x1956
callq 0x1507
cmpl $0x0, 0x4(%rsp)
jle 0x1ab1
xorl %r14d, %r14d
movq %rbx, 0x10(%rsp)
movq %r12, 0x20(%rsp)
movq %r14, 0x18(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
movq (%rbx,%r12,8), %r15
movq %r15, %rdi
callq 0x1120
testl %eax, %eax
jne 0x19e9
movq 0x2768(%rip), %rax      # 0x40f8
cqto
idivq %r13
testq %rax, %rax
jle 0x19e9
movq %r14, %rbp
shlq $0x4, %rbp
addq 0x8(%rsp), %rbp
xorl %ebx, %ebx
movl $0x10, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x1080
movq %r15, %rdi
callq 0x1120
testl %eax, %eax
jne 0x19e1
incq %rbx
movq 0x2725(%rip), %rax      # 0x40f8
cqto
idivq %r13
addq $0x10, %rbp
cmpq %rax, %rbx
jl 0x19a8
addq %rbx, %r14
movq 0x10(%rsp), %rbx
incq %r12
cmpq %r13, %r12
jne 0x1979
testq %r14, %r14
je 0x1b05
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x1815
testq %r14, %r14
movq 0x28(%rsp), %r12
jle 0x1a37
movq 0x8(%rsp), %r15
movq %r14, %rbx
movl $0x10, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rcx
callq 0x11a0
addq $0x10, %r15
decq %rbx
jne 0x1a19
addq 0x18(%rsp), %r14
cmpl $0x0, 0x26ad(%rip)      # 0x40f0
jg 0x1a57
cmpl $0x0, 0x4(%rsp)
movq 0x10(%rsp), %rbx
jg 0x196e
jmp 0x1ab4
movq 0x259a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x5d1(%rip), %rsi       # 0x2039
movq %r14, %rdx
xorl %eax, %eax
callq 0x1110
jmp 0x1a45
leaq 0x5b4(%rip), %rdi       # 0x202f
leaq 0x30(%rsp), %rsi
callq 0x1667
movq 0x8(%rsp), %rdi
callq 0x1030
movq %rbx, %rdi
movl %r12d, %esi
callq 0x1ec8
movl $0x1, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
movq 0x253d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x584(%rip), %rsi       # 0x2049
movq %r14, %rdx
xorl %eax, %eax
callq 0x1110
movq 0x2522(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x596(%rip), %rdi       # 0x2076
movl $0x2, %esi
movl $0x1, %edx
callq 0x11a0
movq 0x8(%rsp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x1a9f
movq 0x24ec(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x533(%rip), %rsi       # 0x2049
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
cmpl $0x0, 0x4(%rsp)
movq 0x20(%rsp), %r13
jle 0x1acf
leaq 0x4eb(%rip), %r14       # 0x2020
leaq 0x30(%rsp), %r15
xorl %r12d, %r12d
movq (%rbx,%r12,8), %rdi
callq 0x10b0
movq 0x25cb(%rip), %rdx      # 0x4118
movq %r15, %rdi
movq %r14, %rsi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1180
movq %r15, %rdi
callq 0x1050
incq %r12
cmpq %r12, %r13
jne 0x1b3d
jmp 0x1acf