// File: /workspace/github_c_makefile_star2_O1/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 0x5b48(%rip), %r14      # 0x6ff8
movq (%r14), %rcx
leaq 0x2d17(%rip), %rdi      # 0x41d1
pushq $0xf
popq %rsi
pushq $0x1
popq %rdx
callq 0x1250
leaq 0x6504(%rip), %rdi      # 0x79d0
leaq 0x2b6d(%rip), %rsi      # 0x4040
callq 0x1210
testq %rax, %rax
jne 0x1502
leaq 0x2cfd(%rip), %rdi      # 0x41e1
leaq 0x64e5(%rip), %rsi      # 0x79d0
callq 0x1904
pushq $0x1
popq %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x1200
movq %rbx, %rdi
callq 0x1070
shrq $0x4, %rax
movq %rax, 0x6893(%rip)      # 0x7db8
movq %rbx, %rdi
callq 0x10e0
movq (%r14), %rdi
movq 0x6881(%rip), %rdx      # 0x7db8
leaq 0x2cb5(%rip), %rsi      # 0x41f3
xorl %eax, %eax
callq 0x1180
cmpl $0x2, 0x5c1c(%rip)      # 0x7168
jl 0x1563
movq (%r14), %rcx
leaq 0x2cad(%rip), %rdi      # 0x4205
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1250
callq 0x12c0
cmpl $0x2, 0x5bf9(%rip)      # 0x7168
jge 0x1822
cmpl $0x0, 0x5c4c(%rip)      # 0x71c8
je 0x15a7
cmpl $0x2, 0x5be3(%rip)      # 0x7168
jge 0x183c
xorl %edi, %edi
callq 0x2236
testl %eax, %eax
jne 0x14f3
cmpl $0x2, 0x5bc7(%rip)      # 0x7168
jge 0x18cb
cmpl $0x0, 0x5bba(%rip)      # 0x7168
jg 0x1856
cmpl $0x0, 0x5bad(%rip)      # 0x7168
jg 0x1872
cmpl $0x0, 0x5ba0(%rip)      # 0x7168
jg 0x188f
cmpl $0x0, 0x5b93(%rip)      # 0x7168
jg 0x18ad
movslq 0x5b8e(%rip), %rbx      # 0x7170
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x11c0
movq %rax, %r13
movq %rbx, %rdi
callq 0x11c0
movq %rax, 0x67c8(%rip)      # 0x7dc8
leaq 0x51a(%rip), %r14       # 0x1b21
xorl %ebp, %ebp
cmpl 0x5b65(%rip), %ebp      # 0x7174
jge 0x17f9
movslq 0x5b54(%rip), %rbx      # 0x7170
cmpq $0x2, %rbx
jl 0x164b
leal -0x1(%rbx), %eax
movslq %eax, %rsi
movq 0x6799(%rip), %rdi      # 0x7dc8
xorl %ecx, %ecx
movq 0x6780(%rip), %rax      # 0x7db8
cqto
idivq %rbx
movq %rax, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x1631
jmp 0x164d
xorl %ecx, %ecx
movl %ebp, 0xc(%rsp)
movq 0x6760(%rip), %rax      # 0x7db8
cqto
idivq %rbx
addq %rax, %rdx
movq 0x6761(%rip), %rax      # 0x7dc8
movq %rdx, (%rax,%rcx,8)
leaq (,%rbx,8), %rdi
callq 0x11c0
movq %rax, %r12
testl %ebx, %ebx
jle 0x168d
xorl %eax, %eax
movq %rax, (%r12,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x1681
cmpl $0x0, 0x5adc(%rip)      # 0x7170
jle 0x16c5
movq %r13, %rbx
movq %r12, %r15
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r15, %rcx
callq 0x11e0
incq %rbp
movslq 0x5ab8(%rip), %rax      # 0x7170
addq $0x8, %r15
addq $0x8, %rbx
cmpq %rax, %rbp
jl 0x169e
movslq 0x5aa4(%rip), %rax      # 0x7170
testq %rax, %rax
jle 0x16ee
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
xorl %esi, %esi
callq 0x1280
incq %rbx
movslq 0x5a87(%rip), %rax      # 0x7170
cmpq %rax, %rbx
jl 0x16d3
xorpd %xmm0, %xmm0
testq %rax, %rax
movl 0xc(%rsp), %ebp
jle 0x1711
movq 0x66be(%rip), %rcx      # 0x7dc0
xorl %edx, %edx
addsd (%rcx,%rdx,8), %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x1704
movsd %xmm0, 0x10(%rsp)
movq %r12, %rdi
callq 0x1030
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x11a0
movq %rbx, %rdi
callq 0x1040
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
pushq $0x50
popq %rsi
leaq 0x2b3a(%rip), %rdx      # 0x4280
movq %rax, %rcx
callq 0x11f0
movq 0x58a3(%rip), %r12      # 0x6ff8
movq (%r12), %rdi
incl %ebp
cvtsi2sdq 0x6654(%rip), %xmm1 # 0x7db8
movsd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
movb $0x1, %r15b
leaq 0x2b18(%rip), %rsi      # 0x4290
movq %rbx, %rdx
movl %ebp, %ecx
movb $0x1, %al
callq 0x1180
movl 0x5a36(%rip), %ecx      # 0x71c0
testl %ecx, %ecx
jle 0x1798
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
testl %edx, %edx
je 0x17a3
testb %r15b, %r15b
jne 0x1609
jmp 0x1819
movq (%r12), %rdi
leaq 0x2afd(%rip), %rsi      # 0x42ab
movl %ebp, %edx
xorl %eax, %eax
callq 0x1180
movl %ebp, %edi
callq 0x2236
testl %eax, %eax
je 0x17e1
movl %eax, %ebx
movq %r13, %rdi
callq 0x1030
movq 0x65f5(%rip), %rdi      # 0x7dc8
callq 0x1030
xorl %r15d, %r15d
movl %ebx, 0x8(%rsp)
jmp 0x1798
movq (%r12), %rcx
leaq 0x2a34(%rip), %rdi      # 0x4220
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x1250
jmp 0x1798
movq %r13, %rdi
callq 0x1030
movq 0x65c0(%rip), %rdi      # 0x7dc8
callq 0x1030
pushq $-0x1
popq %rdi
callq 0x2236
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
jmp 0x14f3
movq (%r14), %rcx
leaq 0x29f4(%rip), %rdi      # 0x4220
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x1250
jmp 0x1575
movq (%r14), %rcx
leaq 0x29e1(%rip), %rdi      # 0x4227
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
callq 0x1250
jmp 0x158b
movq (%r14), %rdi
movl 0x5919(%rip), %edx      # 0x7178
leaq 0x29df(%rip), %rsi      # 0x4245
xorl %eax, %eax
callq 0x1180
jmp 0x15b4
movq (%r14), %rdi
movq 0x5954(%rip), %rdx      # 0x71d0
leaq 0x29d3(%rip), %rsi      # 0x4256
xorl %eax, %eax
callq 0x1180
jmp 0x15c1
movq (%r14), %rdi
movsd 0x58f6(%rip), %xmm0     # 0x7190
leaq 0x29c7(%rip), %rsi      # 0x4268
movb $0x1, %al
callq 0x1180
jmp 0x15ce
movq (%r14), %rdi
movsd 0x58d0(%rip), %xmm0     # 0x7188
leaq 0x29b5(%rip), %rsi      # 0x4274
movb $0x1, %al
callq 0x1180
jmp 0x15db
movq (%r14), %rcx
leaq 0x294b(%rip), %rdi      # 0x4220
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x1250
jmp 0x15a7