// File: /workspace/github_c_makefile_star2_O3/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 $0x408, %rsp            # imm = 0x408
movl 0x2f29(%rip), %edx      # 0x4110
testl %edx, %edx
jne 0x11fb
xorl %edi, %edi
callq 0x1130
movq %rax, %rdx
movl %edx, 0x2f15(%rip)      # 0x4110
movq 0x2df6(%rip), %r12      # 0x3ff8
movq (%r12), %rdi
leaq 0xe6c(%rip), %rsi       # 0x2079
xorl %eax, %eax
callq 0x1110
movl 0x2ef6(%rip), %edi      # 0x4110
callq 0x10e0
movq 0x2db2(%rip), %rax      # 0x3fd8
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x2ec3(%rip), %rdi      # 0x40f8
shlq $0x4, %rdi
callq 0x1140
movq %rax, %r14
movq (%r12), %rcx
leaq 0xe43(%rip), %rdi       # 0x208f
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x11a0
cmpl $0x0, 0x2e92(%rip)      # 0x40f0
jg 0x1481
movq 0x2ead(%rip), %rdx      # 0x4118
leaq 0xdae(%rip), %rsi       # 0x2020
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1180
leaq 0xe30(%rip), %rsi       # 0x20ba
movq %rbx, %rdi
callq 0x1160
testq %rax, %rax
je 0x143c
movq %rax, %r15
cmpl $0x2, 0x2e4b(%rip)      # 0x40f0
jge 0x149f
pushq $0x10
popq %r13
pushq $0x1
popq %rbx
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
movq %r14, (%rsp)
cmpq 0x2e27(%rip), %rbp      # 0x40f8
jl 0x136d
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1815
addq %rbp, 0x8(%rsp)
cmpl $0x2, 0x2e02(%rip)      # 0x40f0
jge 0x13a4
testq %rbp, %rbp
jle 0x1317
movq (%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x11a0
addq $0x10, %r14
decq %rbp
jne 0x12fd
movq %r15, %rdi
callq 0x10b0
movq 0x10(%rsp), %r14
incl %r14d
movq 0x2dea(%rip), %rdx      # 0x4118
leaq 0x20(%rsp), %r15
movq %r15, %rdi
leaq 0xce3(%rip), %rsi       # 0x2020
movl %r14d, %ecx
xorl %eax, %eax
callq 0x1180
movq %r15, %rdi
leaq 0xd69(%rip), %rsi       # 0x20ba
callq 0x1160
testq %rax, %rax
je 0x1452
movq %rax, %r15
movq %r14, 0x10(%rsp)
xorl %ebp, %ebp
movq (%rsp), %r14
movq %rbp, %rdi
shlq $0x4, %rdi
movq %r14, %r12
addq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq 0x18(%rsp), %r14
movq %r14, %rcx
callq 0x1080
movq %r14, %rdi
callq 0x1120
testl %eax, %eax
jne 0x13c6
incq %rbp
movq %r12, %r14
jmp 0x12ca
movq 0x2c4d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd2f(%rip), %rsi       # 0x20e4
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x1110
jmp 0x12f4
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1815
testq %rbp, %rbp
jle 0x13ff
pushq $0x10
popq %r12
pushq $0x1
popq %r14
movq (%rsp), %r13
movq %rbp, %rbx
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x11a0
addq $0x10, %r13
decq %rbx
jne 0x13e5
cmpl $0x2, 0x2cea(%rip)      # 0x40f0
movq 0x10(%rsp), %rbx
jge 0x14ba
movq %r15, %rdi
callq 0x10b0
movq (%rsp), %rdi
callq 0x1030
incl %ebx
movl %ebx, %edi
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x18a8
leaq 0xbf1(%rip), %rdi       # 0x2034
leaq 0x20(%rsp), %rsi
callq 0x1667
movq %r14, %rdi
jmp 0x1467
leaq 0xbdb(%rip), %rdi       # 0x2034
leaq 0x20(%rsp), %rsi
callq 0x1667
movq (%rsp), %rdi
callq 0x1030
pushq $0x1
popq %rax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r12), %rdi
movq 0x2c6c(%rip), %rdx      # 0x40f8
leaq 0xc15(%rip), %rsi       # 0x20a8
xorl %eax, %eax
callq 0x1110
jmp 0x1264
movq (%r12), %rcx
leaq 0xc12(%rip), %rdi       # 0x20bc
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x11a0
jmp 0x12ab
addq 0x8(%rsp), %rbp
movq 0x2b32(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc2e(%rip), %rsi       # 0x20fe
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1110
cmpl $0x2, 0x2c0f(%rip)      # 0x40f0
jl 0x1411
movq 0x2b0a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leal 0x1(%rbx), %edx
leaq 0xc1e(%rip), %rsi       # 0x2119
xorl %eax, %eax
callq 0x1110
jmp 0x1411