// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movq (%rdi), %r13
leaq 0x5e94(%rip), %rdi      # 0x79d0
leaq 0x24fd(%rip), %rsi      # 0x4040
callq 0x1210
testq %rax, %rax
jne 0x1b65
leaq 0x25d2(%rip), %rdi      # 0x4126
leaq 0x5e75(%rip), %rsi      # 0x79d0
callq 0x1904
jmp 0x222f
movq %rax, %rbx
movq 0x6249(%rip), %rax      # 0x7db8
movslq 0x55fa(%rip), %rcx      # 0x7170
cqto
idivq %rcx
imulq %r13, %rax
shlq $0x4, %rax
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x1200
movq 0x6229(%rip), %rax      # 0x7dc0
movq $0x0, (%rax,%r13,8)
movslq 0x55d2(%rip), %r15      # 0x7178
shlq $0x3, %r15
movq %r15, %rdi
callq 0x11c0
testq %rax, %rax
je 0x2227
movq %rax, %r14
movq %r15, %rdi
callq 0x11c0
testq %rax, %rax
jne 0x1be0
movq %rbx, %rdi
callq 0x10e0
movq %r14, %rdi
callq 0x1030
jmp 0x222f
movq %rax, %r15
movq 0x61de(%rip), %rax      # 0x7dc8
cmpq $0x0, (%rax,%r13,8)
jle 0x2217
xorl %ebp, %ebp
movq %rbx, 0x50(%rsp)
movq %r13, 0x48(%rsp)
movl $0x10, %esi
movl $0x1, %edx
leaq 0x60(%rsp), %rdi
movq %rbx, %rcx
callq 0x10a0
movq %rbx, %rdi
callq 0x1190
testl %eax, %eax
jne 0x2217
movslq 0x60(%rsp), %r12
testq %r12, %r12
setle %cl
movl 0x64(%rsp), %eax
testl %eax, %eax
setle %dl
orb %cl, %dl
jne 0x217e
movl %eax, %ecx
decq %r12
movslq 0x5528(%rip), %rax      # 0x7178
leal 0x1(%rax), %edx
movslq %edx, %rdx
imulq %rdx, %r12
movq 0x556f(%rip), %rsi      # 0x71d0
addq %rsi, %rcx
decq %rcx
imulq %rdx, %rcx
testq %rax, %rax
movq %rcx, 0x8(%rsp)
jle 0x1ca2
movq 0x555c(%rip), %rdx      # 0x71d8
leaq (%rdx,%rcx,8), %rcx
leaq (%rdx,%r12,8), %rdx
xorpd %xmm1, %xmm1
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
mulsd (%rcx,%rsi,8), %xmm0
addsd %xmm0, %xmm1
incq %rsi
cmpq %rsi, %rax
jne 0x1c8a
jmp 0x1ca6
xorpd %xmm1, %xmm1
movsd %xmm1, 0x28(%rsp)
movq 0x5525(%rip), %rcx      # 0x71d8
leaq (%rcx,%r12,8), %rdx
movsd (%rdx,%rax,8), %xmm0
movq 0x8(%rsp), %rdx
leaq (%rcx,%rdx,8), %rcx
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
callq 0x1130
movsd 0x20(%rsp), %xmm3
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
addsd 0x28(%rsp), %xmm1
movsd 0x5491(%rip), %xmm0     # 0x7190
ucomisd %xmm0, %xmm3
movapd %xmm1, %xmm2
ja 0x1d32
divsd %xmm0, %xmm3
movsd %xmm1, 0x18(%rsp)
movsd 0x546d(%rip), %xmm1     # 0x7188
movapd %xmm3, %xmm0
callq 0x10d0
movsd 0x18(%rsp), %xmm1
movapd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movq %xmm1, %rcx
movq %xmm2, %rax
btrq $0x3f, %rcx
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rdx # imm = 0x7FF0000000000000
cmpq %rdx, %rax
sete %al
cmpq %rdx, %rcx
je 0x2197
ucomisd %xmm1, %xmm1
jp 0x2197
ucomisd %xmm2, %xmm2
jp 0x2197
testb %al, %al
jne 0x2197
movq %rbp, 0x58(%rsp)
movapd %xmm2, %xmm0
mulsd 0x228c(%rip), %xmm0     # 0x4018
movq 0x602d(%rip), %rax      # 0x7dc0
mulsd %xmm1, %xmm0
addsd (%rax,%r13,8), %xmm0
movsd %xmm0, (%rax,%r13,8)
movslq 0x53ce(%rip), %rax      # 0x7178
testq %rax, %rax
movsd %xmm2, 0x10(%rsp)
jle 0x1fd8
leaq (,%r12,8), %rbx
movq 0x8(%rsp), %rax
leaq (,%rax,8), %r13
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
xorl %ebp, %ebp
movsd %xmm0, 0x18(%rsp)
movq 0x53f3(%rip), %rax      # 0x71d8
leaq (%rax,%r13), %rcx
movsd (%rcx,%rbp,8), %xmm5
mulsd %xmm2, %xmm5
movsd 0x539e(%rip), %xmm6     # 0x7198
movsd 0x537e(%rip), %xmm4     # 0x7180
addq %rbx, %rax
movsd (%rax,%rbp,8), %xmm0
mulsd %xmm2, %xmm0
movapd %xmm0, 0xa0(%rsp)
movq 0x53c2(%rip), %rax      # 0x71e0
addq %rbx, %rax
movsd (%rax,%rbp,8), %xmm0
ucomisd 0x21f2(%rip), %xmm0     # 0x4020
movsd %xmm3, 0x28(%rsp)
movsd %xmm4, 0x20(%rsp)
movapd %xmm6, 0x70(%rsp)
jb 0x1e48
sqrtsd %xmm0, %xmm0
jmp 0x1e65
movapd %xmm5, 0x30(%rsp)
callq 0x1270
movapd 0x70(%rsp), %xmm6
movapd 0x30(%rsp), %xmm5
movsd 0x20(%rsp), %xmm4
movapd %xmm6, %xmm8
xorpd 0x21bd(%rip), %xmm8     # 0x4030
movapd %xmm8, %xmm1
maxsd %xmm5, %xmm1
cmpunordsd %xmm5, %xmm5
movapd %xmm5, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm8, %xmm5
orpd %xmm2, %xmm5
movapd %xmm6, %xmm1
minsd %xmm5, %xmm1
cmpunordsd %xmm5, %xmm5
movapd %xmm5, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm6, %xmm5
orpd %xmm2, %xmm5
mulsd %xmm4, %xmm5
movapd %xmm5, %xmm9
divsd %xmm0, %xmm9
movsd %xmm9, (%r14,%rbp,8)
movq 0x5316(%rip), %rax      # 0x71e0
addq %r13, %rax
movsd (%rax,%rbp,8), %xmm0
ucomisd 0x2146(%rip), %xmm0     # 0x4020
jb 0x1ee2
sqrtsd %xmm0, %xmm0
jmp 0x1f27
movapd %xmm5, 0x30(%rsp)
movapd %xmm8, 0x90(%rsp)
movapd %xmm9, 0x80(%rsp)
callq 0x1270
movapd 0x80(%rsp), %xmm9
movapd 0x90(%rsp), %xmm8
movapd 0x70(%rsp), %xmm6
movapd 0x30(%rsp), %xmm5
movsd 0x20(%rsp), %xmm4
movsd 0x28(%rsp), %xmm3
movapd %xmm8, %xmm1
movapd 0xa0(%rsp), %xmm7
maxsd %xmm7, %xmm1
cmpunordsd %xmm7, %xmm7
movapd %xmm7, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm8, %xmm7
orpd %xmm2, %xmm7
movapd %xmm6, %xmm1
minsd %xmm7, %xmm1
cmpunordsd %xmm7, %xmm7
movapd %xmm7, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm6, %xmm7
orpd %xmm2, %xmm7
mulsd %xmm7, %xmm4
movapd %xmm4, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r15,%rbp,8)
addsd %xmm9, %xmm3
movsd 0x18(%rsp), %xmm0
addsd %xmm1, %xmm0
movq 0x5246(%rip), %rax      # 0x71e0
leaq (%rax,%rbx), %rcx
mulsd %xmm5, %xmm5
addsd (%rcx,%rbp,8), %xmm5
movsd %xmm5, (%rcx,%rbp,8)
addq %r13, %rax
mulsd %xmm4, %xmm4
addsd (%rax,%rbp,8), %xmm4
movsd %xmm4, (%rax,%rbp,8)
incq %rbp
movslq 0x51b1(%rip), %rax      # 0x7178
cmpq %rax, %rbp
movsd 0x10(%rsp), %xmm2
jl 0x1dd8
jmp 0x1fe0
xorpd %xmm3, %xmm3
xorpd %xmm0, %xmm0
movq %xmm3, %rcx
btrq $0x3f, %rcx
movabsq $0x7fefffffffffffff, %rsi # imm = 0x7FEFFFFFFFFFFFFF
cmpq %rsi, %rcx
setg %cl
movq %xmm0, %rdx
btrq $0x3f, %rdx
cmpq %rsi, %rdx
setg %dl
orb %cl, %dl
testq %rax, %rax
setle %cl
orb %dl, %cl
jne 0x2054
movq 0x51bb(%rip), %rdx      # 0x71d8
movq 0x8(%rsp), %rcx
leaq (%rdx,%rcx,8), %rcx
leaq (%rdx,%r12,8), %rdx
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
subsd (%r14,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
movsd (%rcx,%rsi,8), %xmm0
subsd (%r15,%rsi,8), %xmm0
movsd %xmm0, (%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x202c
leaq (,%r12,8), %rbx
movq 0x517d(%rip), %rcx      # 0x71e0
addq %rbx, %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x1fad(%rip), %xmm0     # 0x4020
movq 0x48(%rsp), %r13
movq 0x58(%rsp), %rbp
jb 0x2085
sqrtsd %xmm0, %xmm0
jmp 0x2090
callq 0x1270
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movq %xmm1, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
jge 0x21b9
movslq 0x50bc(%rip), %rax      # 0x7178
addq 0x5115(%rip), %rbx      # 0x71d8
movsd (%rbx,%rax,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%rax,8)
movq 0x8(%rsp), %rcx
leaq (,%rcx,8), %rbx
movq 0x50fb(%rip), %rcx      # 0x71e0
addq %rbx, %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x1f2b(%rip), %xmm0     # 0x4020
jb 0x20fd
sqrtsd %xmm0, %xmm0
jmp 0x2108
callq 0x1270
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movq %xmm1, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
jge 0x21e8
movslq 0x5044(%rip), %rax      # 0x7178
addq 0x509d(%rip), %rbx      # 0x71d8
movsd (%rbx,%rax,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%rax,8)
mulsd %xmm2, %xmm2
movq 0x508c(%rip), %rcx      # 0x71e0
leaq (%rcx,%r12,8), %rdx
movsd (%rdx,%rax,8), %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, (%rdx,%rax,8)
movq 0x8(%rsp), %rdx
leaq (%rcx,%rdx,8), %rcx
addsd (%rcx,%rax,8), %xmm2
movsd %xmm2, (%rcx,%rax,8)
movq 0x50(%rsp), %rbx
incq %rbp
movq 0x5c40(%rip), %rax      # 0x7dc8
cmpq (%rax,%r13,8), %rbp
jl 0x1c01
jmp 0x2217
movq 0x4e5a(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
movl $0x38, %esi
movl $0x1, %edx
leaq 0x1f7f(%rip), %rdi      # 0x4131
callq 0x1250
jmp 0x217e
movq 0x4e38(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
movl $0x15, %esi
movl $0x1, %edx
leaq 0x229c(%rip), %rdi      # 0x4470
callq 0x1250
movsd 0x10(%rsp), %xmm2
xorpd %xmm1, %xmm1
jmp 0x20b5
movq 0x4e09(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
movl $0x15, %esi
movl $0x1, %edx
leaq 0x226d(%rip), %rdi      # 0x4470
callq 0x1250
movsd 0x10(%rsp), %xmm2
xorpd %xmm1, %xmm1
jmp 0x212d
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x10e0
xorl %edi, %edi
callq 0x11b0