// File: /workspace/github_c_makefile_star2_O3/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 $0xc8, %rsp
movq (%rdi), %r13
leaq 0x5e34(%rip), %rdi      # 0x79d0
leaq 0x24dd(%rip), %rsi      # 0x4080
callq 0x1200
testq %rax, %rax
jne 0x1bc5
leaq 0x25b2(%rip), %rdi      # 0x4166
leaq 0x5e15(%rip), %rsi      # 0x79d0
callq 0x1969
jmp 0x2213
movq %rax, %rbx
movq 0x61e9(%rip), %rax      # 0x7db8
movslq 0x5592(%rip), %rcx      # 0x7168
cqto
idivq %rcx
imulq %r13, %rax
shlq $0x4, %rax
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x11f0
movq 0x61c9(%rip), %rax      # 0x7dc0
movq $0x0, (%rax,%r13,8)
movslq 0x556a(%rip), %r15      # 0x7170
shlq $0x3, %r15
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
je 0x220b
movq %rax, %r14
movq %r15, %rdi
callq 0x11b0
testq %rax, %rax
jne 0x1c40
movq %rbx, %rdi
callq 0x10e0
movq %r14, %rdi
callq 0x1030
jmp 0x2213
movq %rax, %r15
movq 0x617e(%rip), %rax      # 0x7dc8
cmpq $0x0, (%rax,%r13,8)
jle 0x21fb
xorl %ebp, %ebp
movq %rbx, 0x60(%rsp)
movq %r13, 0x58(%rsp)
movl $0x10, %esi
movl $0x1, %edx
leaq 0x70(%rsp), %rdi
movq %rbx, %rcx
callq 0x10a0
movq %rbx, %rdi
callq 0x1180
testl %eax, %eax
jne 0x21fb
movslq 0x70(%rsp), %r12
testq %r12, %r12
setle %cl
movl 0x74(%rsp), %eax
testl %eax, %eax
setle %dl
orb %cl, %dl
jne 0x2166
movl %eax, %ecx
decq %r12
movslq 0x54c0(%rip), %rax      # 0x7170
leal 0x1(%rax), %edx
movslq %edx, %rdx
imulq %rdx, %r12
movq 0x550f(%rip), %rsi      # 0x71d0
addq %rsi, %rcx
decq %rcx
imulq %rdx, %rcx
movq %rcx, %rdx
movq 0x5503(%rip), %rcx      # 0x71d8
testq %rax, %rax
movq %rdx, 0x8(%rsp)
jle 0x1d05
leaq (%rcx,%rdx,8), %rdx
leaq (%rcx,%r12,8), %rsi
xorpd %xmm1, %xmm1
xorl %edi, %edi
movsd (%rsi,%rdi,8), %xmm0
mulsd (%rdx,%rdi,8), %xmm0
addsd %xmm0, %xmm1
incq %rdi
cmpq %rdi, %rax
jne 0x1ced
jmp 0x1d09
xorpd %xmm1, %xmm1
movsd %xmm1, 0x40(%rsp)
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, 0x10(%rsp)
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
callq 0x1120
movsd 0x30(%rsp), %xmm3
movsd 0x10(%rsp), %xmm2
subsd %xmm0, %xmm2
addsd 0x40(%rsp), %xmm2
movsd 0x542d(%rip), %xmm1     # 0x7188
ucomisd %xmm1, %xmm3
movapd %xmm2, %xmm0
ja 0x1d8a
divsd %xmm1, %xmm3
movsd 0x540f(%rip), %xmm1     # 0x7180
movapd %xmm3, %xmm0
movsd %xmm2, 0x10(%rsp)
callq 0x10d0
movsd 0x10(%rsp), %xmm2
mulsd %xmm2, %xmm0
movq %xmm2, %rcx
movq %xmm0, %rax
btrq $0x3f, %rcx
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rdx # imm = 0x7FF0000000000000
cmpq %rdx, %rax
sete %al
cmpq %rdx, %rcx
je 0x217c
ucomisd %xmm2, %xmm2
jp 0x217c
ucomisd %xmm0, %xmm0
jp 0x217c
testb %al, %al
jne 0x217c
movq %rbp, 0x68(%rsp)
movapd %xmm0, %xmm1
mulsd 0x2234(%rip), %xmm0     # 0x4018
movq 0x5fd5(%rip), %rax      # 0x7dc0
mulsd %xmm2, %xmm0
addsd (%rax,%r13,8), %xmm0
movsd %xmm0, (%rax,%r13,8)
movslq 0x536e(%rip), %rax      # 0x7170
testq %rax, %rax
movapd %xmm1, 0x20(%rsp)
jle 0x1fc1
movq 0x53c8(%rip), %rcx      # 0x71e0
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd %xmm1, 0x90(%rsp)
leaq (,%r12,8), %rbx
movq 0x8(%rsp), %rax
leaq (,%rax,8), %r13
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
movq 0x5391(%rip), %rax      # 0x71d8
leaq (%rax,%r13), %rdx
movsd (%rdx,%rbp,8), %xmm0
movaps %xmm0, 0x30(%rsp)
movsd 0x5333(%rip), %xmm0     # 0x7190
movaps %xmm0, 0xb0(%rsp)
movsd 0x530b(%rip), %xmm0     # 0x7178
movaps %xmm0, 0x40(%rsp)
addq %rbx, %rax
movsd (%rax,%rbp,8), %xmm0
movaps %xmm0, 0xa0(%rsp)
addq %rbx, %rcx
movsd (%rcx,%rbp,8), %xmm0
ucomisd 0x218e(%rip), %xmm0     # 0x4020
movapd %xmm1, 0x10(%rsp)
jb 0x1ea3
xorps %xmm4, %xmm4
sqrtsd %xmm0, %xmm4
jmp 0x1eac
callq 0x1260
movapd %xmm0, %xmm4
movq 0x532d(%rip), %rax      # 0x71e0
addq %r13, %rax
movsd (%rax,%rbp,8), %xmm0
ucomisd 0x215d(%rip), %xmm0     # 0x4020
jb 0x1ecb
sqrtsd %xmm0, %xmm0
jmp 0x1ee2
movapd %xmm4, 0x80(%rsp)
callq 0x1260
movapd 0x80(%rsp), %xmm4
unpcklpd %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0]
movapd 0xb0(%rsp), %xmm2
movapd %xmm2, %xmm0
xorpd 0x2145(%rip), %xmm0     # 0x4040
movapd 0x30(%rsp), %xmm3
unpcklpd 0xa0(%rsp), %xmm3       # xmm3 = xmm3[0],mem[0]
mulpd 0x90(%rsp), %xmm3
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm0, %xmm1
maxpd %xmm3, %xmm1
cmpunordpd %xmm3, %xmm3
andpd %xmm3, %xmm0
andnpd %xmm1, %xmm3
orpd %xmm0, %xmm3
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
movapd %xmm2, %xmm0
minpd %xmm3, %xmm0
cmpunordpd %xmm3, %xmm3
andpd %xmm3, %xmm2
andnpd %xmm0, %xmm3
orpd %xmm2, %xmm3
movapd 0x40(%rsp), %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm3, %xmm2
movapd %xmm2, %xmm0
divpd %xmm4, %xmm0
movlpd %xmm0, (%r14,%rbp,8)
movhpd %xmm0, (%r15,%rbp,8)
movapd 0x10(%rsp), %xmm1
addpd %xmm0, %xmm1
movq 0x5260(%rip), %rcx      # 0x71e0
leaq (%rcx,%rbx), %rax
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
addsd (%rax,%rbp,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
leaq (%rcx,%r13), %rax
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
mulsd %xmm2, %xmm2
addsd (%rax,%rbp,8), %xmm2
movsd %xmm2, (%rax,%rbp,8)
incq %rbp
movslq 0x51ba(%rip), %rax      # 0x7170
cmpq %rax, %rbp
jl 0x1e40
jmp 0x1fc5
xorpd %xmm1, %xmm1
movq %xmm1, %rcx
btrq $0x3f, %rcx
movabsq $0x7fefffffffffffff, %rsi # imm = 0x7FEFFFFFFFFFFFFF
cmpq %rsi, %rcx
setg %cl
punpckhqdq %xmm1, %xmm1    # xmm1 = xmm1[1,1]
movq %xmm1, %rdx
btrq $0x3f, %rdx
cmpq %rsi, %rdx
setg %dl
testq %rax, %rax
setle %sil
orb %cl, %sil
orb %dl, %sil
jne 0x2040
movq 0x51cf(%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 0x2018
leaq (,%r12,8), %rbx
movq 0x5191(%rip), %rcx      # 0x71e0
addq %rbx, %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x1fc1(%rip), %xmm0     # 0x4020
movq 0x58(%rsp), %r13
movq 0x68(%rsp), %rbp
jb 0x2071
sqrtsd %xmm0, %xmm0
jmp 0x2076
callq 0x1260
movq 0x8(%rsp), %rdx
movapd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movq %xmm1, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
jge 0x219e
movslq 0x50c7(%rip), %rax      # 0x7170
addq 0x5128(%rip), %rbx      # 0x71d8
movsd (%rbx,%rax,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%rax,8)
leaq (,%rdx,8), %rbx
movq 0x5113(%rip), %rcx      # 0x71e0
addq %rbx, %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x1f43(%rip), %xmm0     # 0x4020
jb 0x20e5
sqrtsd %xmm0, %xmm0
jmp 0x20ea
callq 0x1260
movapd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movq %xmm1, %rax
btrq $0x3f, %rax
movabsq $0x7ff0000000000000, %rcx # imm = 0x7FF0000000000000
cmpq %rcx, %rax
jge 0x21cc
movslq 0x5054(%rip), %rax      # 0x7170
addq 0x50b5(%rip), %rbx      # 0x71d8
movsd (%rbx,%rax,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%rax,8)
mulsd %xmm2, %xmm2
movq 0x50a4(%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 0x60(%rsp), %rbx
incq %rbp
movq 0x5c58(%rip), %rax      # 0x7dc8
cmpq (%rax,%r13,8), %rbp
jl 0x1c61
jmp 0x21fb
movq 0x4e75(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
movl $0x38, %esi
movl $0x1, %edx
leaq 0x1fda(%rip), %rdi      # 0x4171
callq 0x1240
jmp 0x2166
movq 0x4e53(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
movl $0x15, %esi
movl $0x1, %edx
leaq 0x22f7(%rip), %rdi      # 0x44b0
callq 0x1240
movq 0x8(%rsp), %rdx
xorpd %xmm1, %xmm1
jmp 0x20a2
movq 0x4e25(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
movl $0x15, %esi
movl $0x1, %edx
leaq 0x22c9(%rip), %rdi      # 0x44b0
callq 0x1240
movapd 0x20(%rsp), %xmm2
xorpd %xmm1, %xmm1
jmp 0x2115
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x10e0
xorl %edi, %edi
callq 0x11a0