// File: /workspace/github_c_makefile_star2_O2/stanfordnlp[P]GloVe/src/glove.c
// Function: glove_thread
void *glove_thread(void *vid) {
    long long a, b ,l1, l2;
    long long id = *(long long*)vid;
    CREC cr;
    real diff, fdiff, temp1, temp2;
    FILE *fin;
    fin = fopen(input_file, "rb");
    if (fin == NULL) {
        // TODO: exit all the threads or somehow mark that glove failed
        log_file_loading_error("input file", input_file);
        pthread_exit(NULL);
    }
    fseeko(fin, (num_lines / num_threads * id) * (sizeof(CREC)), SEEK_SET); //Threads spaced roughly equally throughout file
    cost[id] = 0;
    
    real* W_updates1 = (real*)malloc(vector_size * sizeof(real));
    if (NULL == W_updates1){
        fclose(fin);
        pthread_exit(NULL);
    }
    real* W_updates2 = (real*)malloc(vector_size * sizeof(real));
    if (NULL == W_updates2){
        fclose(fin);
        free(W_updates1);
        pthread_exit(NULL);
    }
    for (a = 0; a < lines_per_thread[id]; a++) {
        fread(&cr, sizeof(CREC), 1, fin);
        if (feof(fin)) break;
        if (cr.word1 < 1 || cr.word2 < 1) { continue; }
        
        /* Get location of words in W & gradsq */
        l1 = (cr.word1 - 1LL) * (vector_size + 1); // cr word indices start at 1
        l2 = ((cr.word2 - 1LL) + vocab_size) * (vector_size + 1); // shift by vocab_size to get separate vectors for context words
        
        /* Calculate cost, save diff for gradients */
        diff = 0;
        for (b = 0; b < vector_size; b++) diff += W[b + l1] * W[b + l2]; // dot product of word and context word vector
        diff += W[vector_size + l1] + W[vector_size + l2] - log(cr.val); // add separate bias for each word
        fdiff = (cr.val > x_max) ? diff : pow(cr.val / x_max, alpha) * diff; // multiply weighting function (f) with diff

        // Check for NaN and inf() in the diffs.
        if (isnan(diff) || isnan(fdiff) || isinf(diff) || isinf(fdiff)) {
            fprintf(stderr,"Caught NaN in diff for kdiff for thread. Skipping update");
            continue;
        }

        cost[id] += 0.5 * fdiff * diff; // weighted squared error
        
        /* Adaptive gradient updates */
        real W_updates1_sum = 0;
        real W_updates2_sum = 0;
        for (b = 0; b < vector_size; b++) {
            // learning rate times gradient for word vectors
            temp1 = fmin(fmax(fdiff * W[b + l2], -grad_clip_value), grad_clip_value) * eta;
            temp2 = fmin(fmax(fdiff * W[b + l1], -grad_clip_value), grad_clip_value) * eta;
            // adaptive updates
            W_updates1[b] = temp1 / sqrt(gradsq[b + l1]);
            W_updates2[b] = temp2 / sqrt(gradsq[b + l2]);
            W_updates1_sum += W_updates1[b];
            W_updates2_sum += W_updates2[b];
            gradsq[b + l1] += temp1 * temp1;
            gradsq[b + l2] += temp2 * temp2;
        }
        if (!isnan(W_updates1_sum) && !isinf(W_updates1_sum) && !isnan(W_updates2_sum) && !isinf(W_updates2_sum)) {
            for (b = 0; b < vector_size; b++) {
                W[b + l1] -= W_updates1[b];
                W[b + l2] -= W_updates2[b];
            }
        }

        // updates for bias terms
        W[vector_size + l1] -= check_nan(fdiff / sqrt(gradsq[vector_size + l1]));
        W[vector_size + l2] -= check_nan(fdiff / sqrt(gradsq[vector_size + l2]));
        fdiff *= fdiff;
        gradsq[vector_size + l1] += fdiff;
        gradsq[vector_size + l2] += fdiff;
        
    }
    free(W_updates1);
    free(W_updates2);
    
    fclose(fin);
    pthread_exit(NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq (%rdi), %r13
leaq 0x6373(%rip), %rdi      # 0x79d0
leaq 0x29fc(%rip), %rsi      # 0x4060
callq 0x1200
testq %rax, %rax
jne 0x1686
leaq 0x2ad1(%rip), %rdi      # 0x4146
leaq 0x6354(%rip), %rsi      # 0x79d0
callq 0x33e1
jmp 0x1c31
movq %rax, %rbx
movq 0x6728(%rip), %rax      # 0x7db8
movslq 0x5ad1(%rip), %rcx      # 0x7168
cqto
idivq %rcx
imulq %r13, %rax
shlq $0x4, %rax
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x11f0
movq 0x6708(%rip), %rax      # 0x7dc0
andq $0x0, (%rax,%r13,8)
movslq 0x5aac(%rip), %r15      # 0x7170
shlq $0x3, %r15
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
je 0x1c29
movq %rax, %r14
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
jne 0x16fe
movq %rbx, %rdi
callq 0x10e0
movq %r14, %rdi
callq 0x1030
jmp 0x1c31
movq %rax, %r15
xorl %ebp, %ebp
movq %rbx, 0x68(%rsp)
movq %r13, 0x60(%rsp)
movq 0x66b4(%rip), %rax      # 0x7dc8
cmpq (%rax,%r13,8), %rbp
jge 0x1c19
leaq 0x80(%rsp), %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x10a0
movq %rbx, %rdi
callq 0x1180
testl %eax, %eax
jne 0x1c19
movslq 0x80(%rsp), %r12
testq %r12, %r12
setle %cl
movl 0x84(%rsp), %eax
testl %eax, %eax
setle %dl
orb %cl, %dl
jne 0x1bee
movl %eax, %ecx
decq %r12
movslq 0x59fe(%rip), %rax      # 0x7170
leal 0x1(%rax), %edx
movslq %edx, %rsi
movq %r12, %rdi
imulq %rsi, %rdi
movq 0x5a4a(%rip), %rdx      # 0x71d0
addq %rdx, %rcx
decq %rcx
movq %rcx, 0x70(%rsp)
movq %rsi, 0x78(%rsp)
imulq %rsi, %rcx
movq 0x5a37(%rip), %rsi      # 0x71d8
testq %rax, %rax
movl $0x0, %edx
cmovgq %rax, %rdx
movq %rcx, 0x8(%rsp)
leaq (%rsi,%rcx,8), %rcx
movq %rdi, (%rsp)
leaq (%rsi,%rdi,8), %rsi
xorpd %xmm1, %xmm1
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x17dc
movsd (%rsi,%rdi,8), %xmm0
mulsd (%rcx,%rdi,8), %xmm0
addsd %xmm0, %xmm1
incq %rdi
jmp 0x17c4
movq %rbp, 0x38(%rsp)
movsd (%rsi,%rax,8), %xmm0
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x10(%rsp)
callq 0x1120
movsd 0x40(%rsp), %xmm2
movsd 0x10(%rsp), %xmm1
movsd 0x50(%rsp), %xmm3
subsd %xmm0, %xmm3
addsd %xmm3, %xmm1
movsd 0x595b(%rip), %xmm0     # 0x7188
ucomisd %xmm0, %xmm2
movapd %xmm1, %xmm3
ja 0x1860
divsd %xmm0, %xmm2
movsd %xmm1, 0x10(%rsp)
movsd 0x5937(%rip), %xmm1     # 0x7180
movapd %xmm2, %xmm0
callq 0x10d0
movsd 0x10(%rsp), %xmm1
movapd %xmm0, %xmm3
mulsd %xmm1, %xmm3
movq %xmm1, %rcx
movq %xmm3, %rax
btrq $0x3f, %rcx
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rdx # imm = 0x7FF0000000000000
cmpq %rdx, %rax
sete %al
cmpq %rdx, %rcx
je 0x1bf6
ucomisd %xmm1, %xmm1
jp 0x1bf6
ucomisd %xmm3, %xmm3
jp 0x1bf6
testb %al, %al
jne 0x1bf6
movapd %xmm3, %xmm0
mulsd 0x2763(%rip), %xmm0     # 0x4018
movq 0x6504(%rip), %rax      # 0x7dc0
mulsd %xmm1, %xmm0
addsd (%rax,%r13,8), %xmm0
movsd %xmm0, (%rax,%r13,8)
movapd %xmm3, %xmm0
unpcklpd %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0]
movapd %xmm0, 0xc0(%rsp)
pxor %xmm4, %xmm4
movq (%rsp), %rbx
movq 0x8(%rsp), %rbp
xorl %r13d, %r13d
movaps %xmm3, 0x20(%rsp)
movslq 0x5877(%rip), %rax      # 0x7170
cmpq %rax, %r13
jge 0x1a75
movq 0x58cf(%rip), %rax      # 0x71d8
movsd (%rax,%rbp,8), %xmm5
movsd 0x587a(%rip), %xmm3     # 0x7190
movsd 0x585a(%rip), %xmm1     # 0x7178
movsd (%rax,%rbx,8), %xmm2
movq 0x58b6(%rip), %rax      # 0x71e0
movsd (%rax,%rbx,8), %xmm0
ucomisd 0x26e9(%rip), %xmm0     # 0x4020
movdqa %xmm4, 0x10(%rsp)
movaps %xmm1, 0x50(%rsp)
movaps %xmm2, 0x40(%rsp)
movaps %xmm3, 0xa0(%rsp)
movaps %xmm5, 0x90(%rsp)
jb 0x195f
sqrtsd %xmm0, %xmm1
jmp 0x1968
callq 0x1260
movapd %xmm0, %xmm1
movq 0x5871(%rip), %rax      # 0x71e0
movsd (%rax,%rbp,8), %xmm0
ucomisd 0x26a4(%rip), %xmm0     # 0x4020
jb 0x1984
sqrtsd %xmm0, %xmm0
jmp 0x199b
movapd %xmm1, 0xb0(%rsp)
callq 0x1260
movapd 0xb0(%rsp), %xmm1
movapd 0x20(%rsp), %xmm3
movapd 0x10(%rsp), %xmm4
movapd 0x50(%rsp), %xmm5
movapd 0x40(%rsp), %xmm6
movapd 0xa0(%rsp), %xmm7
movapd 0x90(%rsp), %xmm2
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movapd %xmm7, %xmm1
xorpd 0x267b(%rip), %xmm1     # 0x4050
unpcklpd %xmm2, %xmm6            # xmm6 = xmm6[0],xmm2[0]
mulpd 0xc0(%rsp), %xmm6
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd %xmm1, %xmm2
maxpd %xmm6, %xmm2
cmpunordpd %xmm6, %xmm6
andpd %xmm6, %xmm1
andnpd %xmm2, %xmm6
orpd %xmm1, %xmm6
unpcklpd %xmm7, %xmm7            # xmm7 = xmm7[0,0]
movapd %xmm7, %xmm1
minpd %xmm6, %xmm1
cmpunordpd %xmm6, %xmm6
andpd %xmm6, %xmm7
andnpd %xmm1, %xmm6
orpd %xmm7, %xmm6
unpcklpd %xmm5, %xmm5            # xmm5 = xmm5[0,0]
mulpd %xmm6, %xmm5
movapd %xmm5, %xmm1
divpd %xmm0, %xmm1
movhpd %xmm1, (%r14,%r13,8)
movlpd %xmm1, (%r15,%r13,8)
addpd %xmm1, %xmm4
movq 0x579d(%rip), %rax      # 0x71e0
movapd %xmm5, %xmm0
unpckhpd %xmm5, %xmm0            # xmm0 = xmm0[1],xmm5[1]
mulsd %xmm0, %xmm0
addsd (%rax,%rbx,8), %xmm0
movsd %xmm0, (%rax,%rbx,8)
mulsd %xmm5, %xmm5
addsd (%rax,%rbp,8), %xmm5
movsd %xmm5, (%rax,%rbp,8)
incq %r13
incq %rbp
incq %rbx
jmp 0x18f2
pand 0x25b3(%rip), %xmm4     # 0x4030
pcmpgtd 0x25bb(%rip), %xmm4     # 0x4040
movmskpd %xmm4, %ecx
testl %ecx, %ecx
movq 0x60(%rsp), %r13
movq 0x38(%rsp), %rbp
je 0x1ac2
movq (%rsp), %rcx
leaq (,%rcx,8), %rbx
movq 0x5736(%rip), %rcx      # 0x71e0
addq %rbx, %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x2566(%rip), %xmm0     # 0x4020
jb 0x1b1b
sqrtsd %xmm0, %xmm0
jmp 0x1b26
movq 0x570f(%rip), %rsi      # 0x71d8
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rdx
imulq %rdi, %rdx
leaq (%rsi,%rdx,8), %rdx
imulq %rdi, %r12
leaq (%rsi,%r12,8), %rsi
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x1a97
movsd (%rsi,%rdi,8), %xmm0
subsd (%r14,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
movsd (%rdx,%rdi,8), %xmm0
subsd (%r15,%rdi,8), %xmm0
movsd %xmm0, (%rdx,%rdi,8)
incq %rdi
jmp 0x1af1
callq 0x1260
movapd 0x20(%rsp), %xmm3
divsd %xmm0, %xmm3
movapd %xmm3, %xmm0
callq 0x1c38
movslq 0x5636(%rip), %rax      # 0x7170
addq 0x5697(%rip), %rbx      # 0x71d8
movsd (%rbx,%rax,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rbx,%rax,8)
movq 0x8(%rsp), %rcx
leaq (,%rcx,8), %rbx
movq 0x567d(%rip), %rcx      # 0x71e0
addq %rbx, %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x24ad(%rip), %xmm0     # 0x4020
jb 0x1b7b
sqrtsd %xmm0, %xmm0
jmp 0x1b80
callq 0x1260
movapd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x1c38
movslq 0x55d6(%rip), %rax      # 0x7170
addq 0x5637(%rip), %rbx      # 0x71d8
movsd (%rbx,%rax,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rbx,%rax,8)
movapd 0x20(%rsp), %xmm1
mulsd %xmm1, %xmm1
movq 0x5620(%rip), %rcx      # 0x71e0
movq (%rsp), %rdx
leaq (%rcx,%rdx,8), %rdx
movsd (%rdx,%rax,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rdx,%rax,8)
movq 0x8(%rsp), %rdx
leaq (%rcx,%rdx,8), %rcx
addsd (%rcx,%rax,8), %xmm1
movsd %xmm1, (%rcx,%rax,8)
movq 0x68(%rsp), %rbx
incq %rbp
jmp 0x170d
movq 0x53fb(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
leaq 0x254a(%rip), %rdi      # 0x4151
pushq $0x38
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
movq 0x38(%rsp), %rbp
jmp 0x1bee
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x10e0
xorl %edi, %edi
callq 0x11a0