// File: /workspace/github_c_makefile_star2_O1/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 0x26ee(%rip), %rax      # 0x3fd0
movq (%rax), %r12
movq 0x2814(%rip), %rdi      # 0x4100
shlq $0x4, %rdi
callq 0x1150
movq %rax, 0x8(%rsp)
movl %ebx, 0x14(%rsp)
movslq %ebx, %r13
movl $0xd8, %esi
movq %r13, %rdi
callq 0x1100
movq %rax, %r14
movl %r13d, %ebx
testl %r13d, %r13d
jle 0x1960
leaq 0x30(%rsp), %rbp
xorl %r15d, %r15d
movq 0x27f8(%rip), %rdx      # 0x4120
movq %rbp, %rdi
leaq 0x6ee(%rip), %rsi       # 0x2020
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1190
movq %rbp, %rdi
leaq 0x6e6(%rip), %rsi       # 0x202c
callq 0x1170
movq %rax, (%r14,%r15,8)
testq %rax, %rax
je 0x1a7e
incq %r15
cmpq %r15, %rbx
jne 0x1921
movq %rbx, 0x28(%rsp)
cmpl $0x0, 0x278c(%rip)      # 0x40f8
jle 0x1973
callq 0x1524
xorl %ebx, %ebx
movq %r12, 0x20(%rsp)
cmpl $0x0, 0x14(%rsp)
movq %rbx, 0x18(%rsp)
jle 0x1a05
xorl %r15d, %r15d
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
callq 0x1130
testl %eax, %eax
jne 0x19fb
movq 0x2761(%rip), %rax      # 0x4100
cqto
idivq %r13
testq %rax, %rax
jle 0x19fb
movq %r15, %rbp
shlq $0x4, %rbp
addq 0x8(%rsp), %rbp
xorl %r12d, %r12d
movq (%r14,%rbx,8), %rcx
movl $0x10, %esi
movl $0x1, %edx
movq %rbp, %rdi
callq 0x1080
movq (%r14,%rbx,8), %rdi
callq 0x1130
testl %eax, %eax
jne 0x19f3
incq %r12
movq 0x271b(%rip), %rax      # 0x4100
cqto
idivq %r13
addq $0x10, %rbp
cmpq %rax, %r12
jl 0x19b8
addq %r12, %r15
movq 0x20(%rsp), %r12
incq %rbx
cmpq %r13, %rbx
jne 0x198b
jmp 0x1a08
xorl %r15d, %r15d
testq %r15, %r15
je 0x1abc
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x1835
testq %r15, %r15
jle 0x1a49
movq 0x8(%rsp), %rbp
movq %r15, %rbx
movl $0x10, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r12, %rcx
callq 0x11b0
addq $0x10, %rbp
decq %rbx
jne 0x1a2b
movq 0x18(%rsp), %rbx
addq %r15, %rbx
cmpl $0x0, 0x26a0(%rip)      # 0x40f8
jle 0x197a
movq 0x2593(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x5ca(%rip), %rsi       # 0x2039
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1120
jmp 0x197a
leaq 0x5aa(%rip), %rdi       # 0x202f
leaq 0x30(%rsp), %rsi
callq 0x1682
movq 0x8(%rsp), %rdi
callq 0x1030
movq %r14, %rdi
movl %ebx, %esi
callq 0x1eae
movl $0x1, %ebp
movl %ebp, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2535(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x57c(%rip), %rsi       # 0x2049
xorl %ebp, %ebp
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1120
cmpl $0x0, 0x14(%rsp)
movq 0x28(%rsp), %rbx
jle 0x1b26
leaq 0x532(%rip), %r15       # 0x2020
leaq 0x30(%rsp), %r12
xorl %r13d, %r13d
movq (%r14,%r13,8), %rdi
callq 0x10b0
movq 0x261a(%rip), %rdx      # 0x4120
movq %r12, %rdi
movq %r15, %rsi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x1190
movq %r12, %rdi
callq 0x1050
incq %r13
cmpq %r13, %rbx
jne 0x1af6
movq 0x24cb(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x53f(%rip), %rdi       # 0x2076
movl $0x2, %esi
movl $0x1, %edx
callq 0x11b0
movq 0x8(%rsp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
jmp 0x1aa8