// File: /workspace/github_c_makefile_star2_O2/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 0x4a6b(%rip), %rbx      # 0x6ff8
movq (%rbx), %rcx
leaq 0x1c5a(%rip), %rdi      # 0x41f1
pushq $0xf
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
leaq 0x5427(%rip), %rdi      # 0x79d0
leaq 0x1ab0(%rip), %rsi      # 0x4060
callq 0x1200
pushq $0x1
popq %rbp
testq %rax, %rax
jne 0x25e1
leaq 0x1c3d(%rip), %rdi      # 0x4201
leaq 0x5405(%rip), %rsi      # 0x79d0
callq 0x33e1
movl %ebp, %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, 0x57b4(%rip)      # 0x7db8
movq %r14, %rdi
callq 0x10e0
movq (%rbx), %rdi
movq 0x57a2(%rip), %rdx      # 0x7db8
leaq 0x1bf6(%rip), %rsi      # 0x4213
xorl %eax, %eax
callq 0x1170
cmpl $0x2, 0x4b35(%rip)      # 0x7160
jge 0x28a2
callq 0x1450
movl 0x4b24(%rip), %eax      # 0x7160
cmpl $0x2, %eax
jge 0x28bc
cmpl $0x0, 0x4b7c(%rip)      # 0x71c8
je 0x2675
cmpl $0x2, %eax
jge 0x28dc
xorl %edi, %edi
callq 0x1c78
testl %eax, %eax
jne 0x25d0
movl 0x4af4(%rip), %eax      # 0x7160
cmpl $0x2, %eax
jge 0x299c
testl %eax, %eax
jg 0x28f6
movslq 0x4ae4(%rip), %rbx      # 0x7168
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x11b0
movq %rax, %r13
movq %rbx, %rdi
callq 0x11b0
movq %rax, 0x5726(%rip)      # 0x7dc8
xorl %r12d, %r12d
pushq $0x1
popq %rbp
xorl %r14d, %r14d
cmpl 0x4aba(%rip), %r14d     # 0x716c
jge 0x287f
movl %r14d, 0xc(%rsp)
movl 0x4aa5(%rip), %ebx      # 0x7168
movslq %ebx, %r14
movq 0x56fb(%rip), %rsi      # 0x7dc8
cmpl $0x2, %r14d
movl %ebp, %ecx
cmovgel %r14d, %ecx
decl %ecx
xorl %edi, %edi
movq 0x56d6(%rip), %rax      # 0x7db8
cqto
idivq %r14
cmpq %rdi, %rcx
je 0x26f5
movq %rax, (%rsi,%rdi,8)
incq %rdi
jmp 0x26db
addq %rax, %rdx
movq 0x56c9(%rip), %rax      # 0x7dc8
movq %rdx, (%rax,%rcx,8)
leaq (,%r14,8), %rdi
callq 0x11b0
movq %rax, %rbp
testq %r14, %r14
cmovleq %r12, %r14
xorl %eax, %eax
cmpq %rax, %r14
je 0x272b
movq %rax, (%rbp,%rax,8)
incq %rax
jmp 0x271c
movq %rbp, %r12
movq %r13, %r14
xorl %r15d, %r15d
movslq %ebx, %rax
cmpq %rax, %r15
jge 0x2763
movq %r14, %rdi
xorl %esi, %esi
leaq -0x1106(%rip), %rdx     # 0x1642
movq %r12, %rcx
callq 0x11d0
incq %r15
movl 0x4a0f(%rip), %ebx      # 0x7168
addq $0x8, %r14
addq $0x8, %r12
jmp 0x2734
xorl %r14d, %r14d
movq 0x488b(%rip), %r15      # 0x6ff8
xorl %r12d, %r12d
movslq %ebx, %rax
cmpq %rax, %r14
jge 0x278f
movq (%r13,%r14,8), %rdi
xorl %esi, %esi
callq 0x1270
incq %r14
movl 0x49db(%rip), %ebx      # 0x7168
jmp 0x2770
movq 0x562a(%rip), %rcx      # 0x7dc0
testq %rax, %rax
cmovleq %r12, %rax
xorpd %xmm0, %xmm0
xorl %edx, %edx
movl 0xc(%rsp), %r14d
cmpq %rdx, %rax
je 0x27b7
addsd (%rcx,%rdx,8), %xmm0
incq %rdx
jmp 0x27a8
movq %rbp, %rdi
movsd %xmm0, 0x10(%rsp)
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 0x1ab4(%rip), %rdx      # 0x42a0
movq %rax, %rcx
callq 0x11e0
movq (%r15), %rdi
incl %r14d
cvtsi2sdq 0x55b5(%rip), %xmm1 # 0x7db8
movsd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
leaq 0x1a9c(%rip), %rsi      # 0x42b0
movq %rbx, %rdx
movl %r14d, %ecx
movb $0x1, %al
callq 0x1170
movl 0x4999(%rip), %ecx      # 0x71c0
testl %ecx, %ecx
pushq $0x1
popq %rbp
jle 0x26ab
movl %r14d, %eax
xorl %edx, %edx
divl %ecx
testl %edx, %edx
jne 0x26ab
movq (%r15), %rdi
leaq 0x1a80(%rip), %rsi      # 0x42cb
movl %r14d, %edx
xorl %eax, %eax
callq 0x1170
movl %r14d, %edi
callq 0x1c78
testl %eax, %eax
jne 0x2983
movq (%r15), %rcx
leaq 0x19d1(%rip), %rdi      # 0x4240
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
jmp 0x26ab
movq %r13, %rdi
callq 0x1030
movq 0x553a(%rip), %rdi      # 0x7dc8
callq 0x1030
pushq $-0x1
popq %rdi
callq 0x1c78
movl %eax, %ebp
jmp 0x25d0
movq (%rbx), %rcx
leaq 0x1979(%rip), %rdi      # 0x4225
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
jmp 0x2631
movq (%rbx), %rcx
leaq 0x197a(%rip), %rdi      # 0x4240
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
movl 0x4889(%rip), %eax      # 0x7160
jmp 0x2645
movq (%rbx), %rcx
leaq 0x1961(%rip), %rdi      # 0x4247
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
jmp 0x2657
movq (%rbx), %rdi
movl 0x4871(%rip), %edx      # 0x7170
leaq 0x195f(%rip), %rsi      # 0x4265
xorl %eax, %eax
callq 0x1170
cmpl $0x0, 0x484c(%rip)      # 0x7160
jle 0x267d
movq (%rbx), %rdi
movq 0x48ac(%rip), %rdx      # 0x71d0
leaq 0x194b(%rip), %rsi      # 0x4276
xorl %eax, %eax
callq 0x1170
cmpl $0x0, 0x4827(%rip)      # 0x7160
jle 0x267d
movq (%rbx), %rdi
movsd 0x483e(%rip), %xmm0     # 0x7188
leaq 0x1937(%rip), %rsi      # 0x4288
movb $0x1, %al
callq 0x1170
cmpl $0x0, 0x4801(%rip)      # 0x7160
jle 0x267d
movq (%rbx), %rdi
movsd 0x4810(%rip), %xmm0     # 0x7180
leaq 0x191d(%rip), %rsi      # 0x4294
movb $0x1, %al
callq 0x1170
jmp 0x267d
movq %r13, %rdi
callq 0x1030
movq 0x5436(%rip), %rdi      # 0x7dc8
callq 0x1030
jmp 0x25d0
movq (%rbx), %rcx
leaq 0x189a(%rip), %rdi      # 0x4240
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
movl 0x47a9(%rip), %eax      # 0x7160
jmp 0x2675