// File: /workspace/github_c_makefile_star2_O3/stanfordnlp[P]GloVe/src/glove.c
// Function: train_glove
int train_glove(void) {
    long long a, file_size;
    int save_params_return_code;
    int b;
    FILE *fin;
    real total_cost = 0;

    fprintf(stderr, "TRAINING MODEL\n");
    
    fin = fopen(input_file, "rb");
    if (fin == NULL) {log_file_loading_error("cooccurrence file", input_file); return 1;}
    fseeko(fin, 0, SEEK_END);
    file_size = ftello(fin);
    num_lines = file_size/(sizeof(CREC)); // Assuming the file isn't corrupt and consists only of CREC's
    fclose(fin);
    fprintf(stderr,"Read %lld lines.\n", num_lines);
    if (verbose > 1) fprintf(stderr,"Initializing parameters...");
    initialize_parameters();
    if (verbose > 1) fprintf(stderr,"done.\n");
    if (save_init_param) {
        if (verbose > 1) fprintf(stderr,"Saving initial parameters... ");
        save_params_return_code = save_params(0);
        if (save_params_return_code != 0)
            return save_params_return_code;
        if (verbose > 1) fprintf(stderr,"done.\n");
    }
    if (verbose > 0) fprintf(stderr,"vector size: %d\n", vector_size);
    if (verbose > 0) fprintf(stderr,"vocab size: %lld\n", vocab_size);
    if (verbose > 0) fprintf(stderr,"x_max: %lf\n", x_max);
    if (verbose > 0) fprintf(stderr,"alpha: %lf\n", alpha);
    pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
    lines_per_thread = (long long *) malloc(num_threads * sizeof(long long));
    
    time_t rawtime;
    struct tm *info;
    char time_buffer[80];
    // Lock-free asynchronous SGD
    for (b = 0; b < num_iter; b++) {
        total_cost = 0;
        for (a = 0; a < num_threads - 1; a++) lines_per_thread[a] = num_lines / num_threads;
        lines_per_thread[a] = num_lines / num_threads + num_lines % num_threads;
        long long *thread_ids = (long long*)malloc(sizeof(long long) * num_threads);
        for (a = 0; a < num_threads; a++) thread_ids[a] = a;
        for (a = 0; a < num_threads; a++) pthread_create(&pt[a], NULL, glove_thread, (void *)&thread_ids[a]);
        for (a = 0; a < num_threads; a++) pthread_join(pt[a], NULL);
        for (a = 0; a < num_threads; a++) total_cost += cost[a];
        free(thread_ids);

        time(&rawtime);
        info = localtime(&rawtime);
        strftime(time_buffer,80,"%x - %I:%M.%S%p", info);
        fprintf(stderr, "%s, iter: %03d, cost: %lf\n", time_buffer,  b+1, total_cost/num_lines);

        if (checkpoint_every > 0 && (b + 1) % checkpoint_every == 0) {
            fprintf(stderr,"    saving intermediate parameters for iter %03d...", b+1);
            save_params_return_code = save_params(b+1);
            if (save_params_return_code != 0) {
                free(pt);
                free(lines_per_thread);
                return save_params_return_code;
            }
            fprintf(stderr,"done.\n");
        }
    }
    free(pt);
    free(lines_per_thread);
    return save_params(-1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq 0x5b5a(%rip), %r15      # 0x6ff8
movq (%r15), %rcx
leaq 0x2d69(%rip), %rdi      # 0x4211
pushq $0xf
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
leaq 0x6516(%rip), %rdi      # 0x79d0
leaq 0x2bbf(%rip), %rsi      # 0x4080
callq 0x1200
pushq $0x1
popq %rbx
testq %rax, %rax
jne 0x14f2
leaq 0x2d4c(%rip), %rdi      # 0x4221
leaq 0x64f4(%rip), %rsi      # 0x79d0
callq 0x1969
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x11f0
movq %r14, %rdi
callq 0x1070
shrq $0x4, %rax
movq %rax, 0x68a3(%rip)      # 0x7db8
movq %r14, %rdi
callq 0x10e0
movq (%r15), %rdi
movq 0x6891(%rip), %rdx      # 0x7db8
leaq 0x2d05(%rip), %rsi      # 0x4233
xorl %eax, %eax
callq 0x1170
cmpl $0x2, 0x5c24(%rip)      # 0x7160
jl 0x1553
movq (%r15), %rcx
leaq 0x2cfd(%rip), %rdi      # 0x4245
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
callq 0x12b0
movl 0x5c02(%rip), %eax      # 0x7160
cmpl $0x2, %eax
jge 0x1847
cmpl $0x0, 0x5c5a(%rip)      # 0x71c8
je 0x1597
cmpl $0x2, %eax
jge 0x1867
xorl %edi, %edi
callq 0x221a
testl %eax, %eax
jne 0x14e1
movl 0x5bd2(%rip), %eax      # 0x7160
cmpl $0x2, %eax
jge 0x190e
testl %eax, %eax
jg 0x1881
movslq 0x5bc2(%rip), %rbx      # 0x7168
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x11b0
movq %rax, %r12
movq %rbx, %rdi
callq 0x11b0
movq %rax, 0x6804(%rip)      # 0x7dc8
cmpl $0x0, 0x5ba1(%rip)      # 0x716c
jle 0x1825
leaq 0x5a9(%rip), %r14       # 0x1b81
xorl %ebp, %ebp
movl %ebp, 0x14(%rsp)
movslq 0x5b83(%rip), %rbp      # 0x7168
movq 0x67dc(%rip), %rcx      # 0x7dc8
cmpq $0x2, %rbp
jl 0x1614
leal -0x1(%rbp), %eax
movslq %eax, %rsi
xorl %edi, %edi
movq 0x67b7(%rip), %rax      # 0x7db8
cqto
idivq %rbp
movq %rax, (%rcx,%rdi,8)
incq %rdi
cmpq %rdi, %rsi
jne 0x15fa
jmp 0x1616
xorl %esi, %esi
movq 0x679b(%rip), %rax      # 0x7db8
cqto
idivq %rbp
addq %rax, %rdx
movq %rdx, (%rcx,%rsi,8)
leaq (,%rbp,8), %rdi
callq 0x11b0
movq %rax, %r13
testl %ebp, %ebp
jle 0x1736
leal 0x1(%rbp), %eax
andl $-0x2, %eax
leaq -0x1(%rbp), %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x2a03(%rip), %xmm5     # 0x4060
pxor %xmm5, %xmm0
xorl %ecx, %ecx
movdqa 0x29e5(%rip), %xmm1     # 0x4050
pcmpeqd %xmm6, %xmm6
movdqa 0x29f9(%rip), %xmm7     # 0x4070
movdqa %xmm1, %xmm2
pxor %xmm5, %xmm2
movdqa %xmm2, %xmm3
pcmpgtd %xmm0, %xmm3
pcmpeqd %xmm0, %xmm2
pshufd $0xf5, %xmm2, %xmm4     # xmm4 = xmm2[1,1,3,3]
pand %xmm3, %xmm4
pshufd $0xf5, %xmm3, %xmm2     # xmm2 = xmm3[1,1,3,3]
por %xmm4, %xmm2
movd %xmm2, %edx
notl %edx
testb $0x1, %dl
je 0x16ad
movq %rcx, (%r13,%rcx,8)
pxor %xmm6, %xmm2
pextrw $0x4, %xmm2, %edx
testb $0x1, %dl
je 0x16c4
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13,%rcx,8)
addq $0x2, %rcx
paddq %xmm7, %xmm1
cmpq %rcx, %rax
jne 0x1677
testl %ebp, %ebp
jle 0x1736
movq %r12, 0x8(%rsp)
movq %r12, %rbx
movq %r13, %r12
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r12, %rcx
callq 0x11d0
incq %r15
movslq 0x5a6b(%rip), %rbp      # 0x7168
addq $0x8, %r12
addq $0x8, %rbx
cmpq %rbp, %r15
jl 0x16e3
testl %ebp, %ebp
movq 0x58e5(%rip), %r15      # 0x6ff8
movq 0x8(%rsp), %r12
jle 0x1736
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
xorl %esi, %esi
callq 0x1270
incq %rbx
movslq 0x5a37(%rip), %rbp      # 0x7168
cmpq %rbp, %rbx
jl 0x171c
pxor %xmm0, %xmm0
testq %rbp, %rbp
jle 0x1755
movq 0x667a(%rip), %rax      # 0x7dc0
xorl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rbp
jne 0x1748
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
callq 0x1030
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x1190
movq %rbx, %rdi
callq 0x1040
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
pushq $0x50
popq %rsi
leaq 0x2b36(%rip), %rdx      # 0x42c0
movq %rax, %rcx
callq 0x11e0
movq (%r15), %rdi
movl 0x14(%rsp), %ebp
incl %ebp
cvtsi2sdq 0x6614(%rip), %xmm1 # 0x7db8
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
leaq 0x2b1b(%rip), %rsi      # 0x42d0
movq %rbx, %rdx
movl %ebp, %ecx
movb $0x1, %al
callq 0x1170
movl 0x59f9(%rip), %ecx      # 0x71c0
testl %ecx, %ecx
jle 0x17d5
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
testl %edx, %edx
je 0x17e3
cmpl 0x5991(%rip), %ebp      # 0x716c
jl 0x15da
jmp 0x181c
movq (%r15), %rdi
leaq 0x2afe(%rip), %rsi      # 0x42eb
movl %ebp, %edx
xorl %eax, %eax
callq 0x1170
movl %ebp, %edi
callq 0x221a
testl %eax, %eax
jne 0x192e
movq (%r15), %rcx
leaq 0x2a51(%rip), %rdi      # 0x4260
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
jmp 0x17d5
movq 0x65a5(%rip), %rbx      # 0x7dc8
jmp 0x1828
movq %rax, %rbx
movq %r12, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
pushq $-0x1
popq %rdi
callq 0x221a
movl %eax, %ebx
jmp 0x14e1
movq (%r15), %rcx
leaq 0x2a0f(%rip), %rdi      # 0x4260
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
movl 0x58fe(%rip), %eax      # 0x7160
jmp 0x1567
movq (%r15), %rcx
leaq 0x29f6(%rip), %rdi      # 0x4267
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
jmp 0x1579
movq (%r15), %rdi
movl 0x58e6(%rip), %edx      # 0x7170
leaq 0x29f4(%rip), %rsi      # 0x4285
xorl %eax, %eax
callq 0x1170
cmpl $0x0, 0x58c1(%rip)      # 0x7160
jle 0x159f
movq (%r15), %rdi
movq 0x5921(%rip), %rdx      # 0x71d0
leaq 0x29e0(%rip), %rsi      # 0x4296
xorl %eax, %eax
callq 0x1170
cmpl $0x0, 0x589c(%rip)      # 0x7160
jle 0x159f
movq (%r15), %rdi
movq 0x58b3(%rip), %xmm0     # 0x7188
leaq 0x29cc(%rip), %rsi      # 0x42a8
movb $0x1, %al
callq 0x1170
cmpl $0x0, 0x5876(%rip)      # 0x7160
jle 0x159f
movq (%r15), %rdi
movq 0x5885(%rip), %xmm0     # 0x7180
leaq 0x29b2(%rip), %rsi      # 0x42b4
movb $0x1, %al
callq 0x1170
jmp 0x159f
movq (%r15), %rcx
leaq 0x2948(%rip), %rdi      # 0x4260
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
movl 0x5837(%rip), %eax      # 0x7160
jmp 0x1597
movq %r12, %rdi
callq 0x1030
movq 0x648b(%rip), %rdi      # 0x7dc8
callq 0x1030
pushq $0x1
popq %rbx
jmp 0x14e1