// File: /workspace/github_c_makefile_star2_O2/stanfordnlp[P]GloVe/src/shuffle.c
// Function: shuffle_by_chunks
int shuffle_by_chunks(void) {
    if (seed == 0) {
        seed = time(0);
    }
    fprintf(stderr, "Using random seed %d\n", seed);
    srand(seed);
    long i = 0, l = 0;
    int fidcounter = 0;
    char filename[MAX_STRING_LENGTH];
    CREC *array;
    FILE *fin = stdin, *fid;
    array = malloc(sizeof(CREC) * array_size);
    
    fprintf(stderr,"SHUFFLING COOCCURRENCES\n");
    if (verbose > 0) fprintf(stderr,"array size: %lld\n", array_size);
    sprintf(filename,"%s_%04d.bin",file_head, fidcounter);
    fid = fopen(filename,"w");
    if (fid == NULL) {
        log_file_loading_error("file", filename);
        free(array);
        return 1;
    }
    if (verbose > 1) fprintf(stderr, "Shuffling by chunks: processed 0 lines.");
    
    while (1) { //Continue until EOF
        if (i >= array_size) {// If array is full, shuffle it and save to temporary file
            shuffle(array, i);
            l += i;
            if (verbose > 1) fprintf(stderr, "\033[22Gprocessed %ld lines.", l);
            write_chunk(array,i,fid);
            fclose(fid);
            fidcounter++;
            sprintf(filename,"%s_%04d.bin",file_head, fidcounter);
            fid = fopen(filename,"w");
            if (fid == NULL) {
                log_file_loading_error("file", filename);
                free(array);
                return 1;
            }
            i = 0;
        }
        fread(&array[i], sizeof(CREC), 1, fin);
        if (feof(fin)) break;
        i++;
    }
    shuffle(array, i); //Last chunk may be smaller than array_size
    write_chunk(array,i,fid);
    l += i;
    if (verbose > 1) fprintf(stderr, "\033[22Gprocessed %ld lines.\n", l);
    if (verbose > 1) fprintf(stderr, "Wrote %d temporary file(s).\n", fidcounter + 1);
    fclose(fid);
    free(array);
    return shuffle_merge(fidcounter + 1); // Merge and shuffle together temporary files
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3e8, %rsp            # imm = 0x3E8
movl 0x2ace(%rip), %edx      # 0x4110
testl %edx, %edx
jne 0x1656
xorl %edi, %edi
callq 0x1130
movq %rax, %rdx
movl %edx, 0x2aba(%rip)      # 0x4110
movq 0x299b(%rip), %r12      # 0x3ff8
movq (%r12), %rdi
leaq 0xa11(%rip), %rsi       # 0x2079
xorl %eax, %eax
callq 0x1110
movl 0x2a9b(%rip), %edi      # 0x4110
callq 0x10e0
movq 0x2957(%rip), %rax      # 0x3fd8
movq (%rax), %r14
movq 0x2a6d(%rip), %rdi      # 0x40f8
shlq $0x4, %rdi
callq 0x1140
movq %rax, %rbx
movq (%r12), %rcx
leaq 0x9ed(%rip), %rdi       # 0x208f
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x11a0
cmpl $0x0, 0x2a3c(%rip)      # 0x40f0
jg 0x183d
movq 0x2a57(%rip), %rdx      # 0x4118
leaq 0x958(%rip), %rsi       # 0x2020
movq %rsp, %r15
movq %r15, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1180
leaq 0x9dc(%rip), %rsi       # 0x20ba
movq %r15, %rdi
callq 0x1160
testq %rax, %rax
je 0x17c3
movq %rax, %r15
cmpl $0x2, 0x29f7(%rip)      # 0x40f0
jge 0x185b
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq 0x29ea(%rip), %rbp      # 0x40f8
jl 0x1777
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1306
addq %rbp, %r13
cmpl $0x2, 0x29cb(%rip)      # 0x40f0
jge 0x17a3
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x12bc
movq %r15, %rdi
callq 0x10b0
incl %r12d
movq 0x29d1(%rip), %rdx      # 0x4118
movq %rsp, %r15
movq %r15, %rdi
leaq 0x8cc(%rip), %rsi       # 0x2020
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1180
movq %r15, %rdi
leaq 0x952(%rip), %rsi       # 0x20ba
callq 0x1160
testq %rax, %rax
je 0x17c3
movq %rax, %r15
xorl %ebp, %ebp
movq %rbp, %rdi
shlq $0x4, %rdi
addq %rbx, %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x1080
movq %r14, %rdi
callq 0x1120
testl %eax, %eax
jne 0x17ef
incq %rbp
jmp 0x1707
movq 0x284e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x930(%rip), %rsi       # 0x20e4
movq %r13, %rdx
xorl %eax, %eax
callq 0x1110
jmp 0x1727
leaq 0x86a(%rip), %rdi       # 0x2034
movq %rsp, %rsi
callq 0x1d51
movq %rbx, %rdi
callq 0x1030
pushq $0x1
popq %rax
addq $0x3e8, %rsp            # imm = 0x3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1306
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x12bc
cmpl $0x2, 0x28e1(%rip)      # 0x40f0
jge 0x1876
movq %r15, %rdi
callq 0x10b0
movq %rbx, %rdi
callq 0x1030
incl %r12d
movl %r12d, %edi
addq $0x3e8, %rsp            # imm = 0x3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x138e
movq (%r12), %rdi
movq 0x28b0(%rip), %rdx      # 0x40f8
leaq 0x859(%rip), %rsi       # 0x20a8
xorl %eax, %eax
callq 0x1110
jmp 0x16ba
movq (%r12), %rcx
leaq 0x856(%rip), %rdi       # 0x20bc
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x11a0
jmp 0x16ff
addq %r13, %rbp
movq 0x2778(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x874(%rip), %rsi       # 0x20fe
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1110
cmpl $0x2, 0x2855(%rip)      # 0x40f0
jl 0x1811
movq 0x2750(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leal 0x1(%r12), %edx
leaq 0x862(%rip), %rsi       # 0x2119
xorl %eax, %eax
callq 0x1110
jmp 0x1811