// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
leaq 0x72af(%rip), %rdi      # 0x89b0
leaq 0x3938(%rip), %rsi      # 0x5040
callq 0x11d0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x1734
leaq 0x3a07(%rip), %rdi      # 0x5126
leaq 0x728a(%rip), %rsi      # 0x89b0
callq 0x4120
xorl %eax, %eax
movl %eax, %edi
callq 0x1170
movq -0x68(%rbp), %rdi
movq 0x7659(%rip), %rax      # 0x8d98
movslq 0x6a0a(%rip), %rcx      # 0x8150
cqto
idivq %rcx
movq %rax, %rsi
imulq -0x30(%rbp), %rsi
shlq $0x4, %rsi
xorl %edx, %edx
callq 0x11c0
movq 0x763b(%rip), %rax      # 0x8da0
movq -0x30(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movslq 0x69e0(%rip), %rdi      # 0x8158
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0x70(%rbp)
xorl %eax, %eax
cmpq -0x70(%rbp), %rax
jne 0x179f
movq -0x68(%rbp), %rdi
callq 0x10d0
xorl %eax, %eax
movl %eax, %edi
callq 0x1170
movslq 0x69b2(%rip), %rdi      # 0x8158
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0x78(%rbp)
xorl %eax, %eax
cmpq -0x78(%rbp), %rax
jne 0x17d6
movq -0x68(%rbp), %rdi
callq 0x10d0
movq -0x70(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
movl %eax, %edi
callq 0x1170
movq $0x0, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x75bf(%rip), %rcx      # 0x8da8
movq -0x30(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jge 0x1f4c
movq -0x68(%rbp), %rcx
leaq -0x40(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x1090
movq -0x68(%rbp), %rdi
callq 0x1150
cmpl $0x0, %eax
je 0x1821
jmp 0x1f4c
cmpl $0x1, -0x40(%rbp)
jl 0x182d
cmpl $0x1, -0x3c(%rbp)
jge 0x1832
jmp 0x1f3b
movslq -0x40(%rbp), %rax
subq $0x1, %rax
movl 0x6918(%rip), %ecx      # 0x8158
addl $0x1, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
movq %rax, -0x20(%rbp)
movslq -0x3c(%rbp), %rax
subq $0x1, %rax
addq 0x6953(%rip), %rax      # 0x81b0
movl 0x68f5(%rip), %ecx      # 0x8158
addl $0x1, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
movq %rax, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movslq 0x68cc(%rip), %rcx      # 0x8158
cmpq %rcx, %rax
jge 0x18d9
movq 0x6920(%rip), %rax      # 0x81b8
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x690c(%rip), %rax      # 0x81b8
movq -0x18(%rbp), %rcx
addq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x48(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x1881
movq 0x68d8(%rip), %rax      # 0x81b8
movslq 0x6871(%rip), %rcx      # 0x8158
addq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x68c1(%rip), %rax      # 0x81b8
movslq 0x685a(%rip), %rcx      # 0x8158
addq -0x28(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x38(%rbp), %xmm0
callq 0x1100
movaps %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
subsd %xmm1, %xmm0
addsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x38(%rbp), %xmm0
ucomisd 0x6831(%rip), %xmm0     # 0x8170
jbe 0x1950
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
jmp 0x1977
movsd -0x38(%rbp), %xmm0
divsd 0x6813(%rip), %xmm0     # 0x8170
movsd 0x6803(%rip), %xmm1     # 0x8168
callq 0x10c0
mulsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x48(%rbp), %xmm0
ucomisd %xmm0, %xmm0
setp %al
testb $0x1, %al
jne 0x1a3e
movsd -0x50(%rbp), %xmm0
ucomisd %xmm0, %xmm0
setp %al
testb $0x1, %al
jne 0x1a3e
movsd -0x48(%rbp), %xmm1
movaps %xmm1, %xmm0
movaps 0x3675(%rip), %xmm2     # 0x5030
pand %xmm2, %xmm0
movq %xmm1, %rdx
movl $0x1, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x0, %rdx
cmovll %eax, %ecx
movsd 0x3643(%rip), %xmm1     # 0x5020
xorl %eax, %eax
ucomisd %xmm1, %xmm0
setnp %sil
sete %dl
testb %sil, %dl
cmovnel %ecx, %eax
cmpl $0x0, %eax
jne 0x1a3e
movsd -0x50(%rbp), %xmm1
movaps %xmm1, %xmm0
movaps 0x362c(%rip), %xmm2     # 0x5030
pand %xmm2, %xmm0
movq %xmm1, %rdx
movl $0x1, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x0, %rdx
cmovll %eax, %ecx
movsd 0x35fa(%rip), %xmm1     # 0x5020
xorl %eax, %eax
ucomisd %xmm1, %xmm0
setnp %sil
sete %dl
testb %sil, %dl
cmovnel %ecx, %eax
cmpl $0x0, %eax
je 0x1a5b
movq 0x65b3(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq 0x36e2(%rip), %rsi      # 0x5131
movb $0x0, %al
callq 0x1140
jmp 0x1f3b
movsd 0x35a5(%rip), %xmm0     # 0x5008
mulsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm2
movq 0x732c(%rip), %rax      # 0x8da0
movq -0x30(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movslq 0x66a8(%rip), %rcx      # 0x8158
cmpq %rcx, %rax
jge 0x1cea
movsd -0x50(%rbp), %xmm2
movq 0x66f3(%rip), %rax      # 0x81b8
movq -0x18(%rbp), %rcx
addq -0x28(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm2
movsd 0x669e(%rip), %xmm0     # 0x8178
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
movaps %xmm1, %xmm4
movaps %xmm2, %xmm3
cmpunordsd %xmm3, %xmm3
movaps %xmm3, %xmm0
pand %xmm4, %xmm0
maxsd %xmm2, %xmm1
pandn %xmm1, %xmm3
por %xmm0, %xmm3
movsd 0x6661(%rip), %xmm2     # 0x8178
movaps %xmm2, %xmm4
movaps %xmm3, %xmm0
cmpunordsd %xmm0, %xmm0
movaps %xmm0, %xmm1
pand %xmm4, %xmm1
minsd %xmm3, %xmm2
pandn %xmm2, %xmm0
por %xmm1, %xmm0
mulsd 0x6623(%rip), %xmm0     # 0x8160
movsd %xmm0, -0x58(%rbp)
movsd -0x50(%rbp), %xmm2
movq 0x666a(%rip), %rax      # 0x81b8
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm2
movsd 0x6615(%rip), %xmm0     # 0x8178
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
movaps %xmm1, %xmm4
movaps %xmm2, %xmm3
cmpunordsd %xmm3, %xmm3
movaps %xmm3, %xmm0
pand %xmm4, %xmm0
maxsd %xmm2, %xmm1
pandn %xmm1, %xmm3
por %xmm0, %xmm3
movsd 0x65d8(%rip), %xmm2     # 0x8178
movaps %xmm2, %xmm4
movaps %xmm3, %xmm0
cmpunordsd %xmm0, %xmm0
movaps %xmm0, %xmm1
pand %xmm4, %xmm1
minsd %xmm3, %xmm2
pandn %xmm2, %xmm0
por %xmm1, %xmm0
mulsd 0x659a(%rip), %xmm0     # 0x8160
movsd %xmm0, -0x60(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movq 0x65e1(%rip), %rax      # 0x81c0
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x1230
movaps %xmm0, %xmm1
movsd -0xa8(%rbp), %xmm0
divsd %xmm1, %xmm0
movq -0x70(%rbp), %rax
movq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movq 0x659f(%rip), %rax      # 0x81c0
movq -0x18(%rbp), %rcx
addq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x1230
movaps %xmm0, %xmm1
movsd -0xa0(%rbp), %xmm0
divsd %xmm1, %xmm0
movq -0x78(%rbp), %rax
movq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
movq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x78(%rbp), %rax
movq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x58(%rbp), %xmm0
movsd -0x58(%rbp), %xmm2
movq 0x652c(%rip), %rax      # 0x81c0
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x60(%rbp), %xmm0
movsd -0x60(%rbp), %xmm2
movq 0x6501(%rip), %rax      # 0x81c0
movq -0x18(%rbp), %rcx
addq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x1aa5
movsd -0x80(%rbp), %xmm0
ucomisd %xmm0, %xmm0
setp %al
testb $0x1, %al
jne 0x1e2a
movsd -0x80(%rbp), %xmm1
movaps %xmm1, %xmm0
movaps 0x3323(%rip), %xmm2     # 0x5030
pand %xmm2, %xmm0
movq %xmm1, %rdx
movl $0x1, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x0, %rdx
cmovll %eax, %ecx
movsd 0x32f1(%rip), %xmm1     # 0x5020
xorl %eax, %eax
ucomisd %xmm1, %xmm0
setnp %sil
sete %dl
testb %sil, %dl
cmovnel %ecx, %eax
cmpl $0x0, %eax
jne 0x1e2a
movsd -0x88(%rbp), %xmm0
ucomisd %xmm0, %xmm0
setp %al
testb $0x1, %al
jne 0x1e2a
movsd -0x88(%rbp), %xmm1
movaps %xmm1, %xmm0
movaps 0x32bc(%rip), %xmm2     # 0x5030
pand %xmm2, %xmm0
movq %xmm1, %rdx
movl $0x1, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x0, %rdx
cmovll %eax, %ecx
movsd 0x328a(%rip), %xmm1     # 0x5020
xorl %eax, %eax
ucomisd %xmm1, %xmm0
setnp %sil
sete %dl
testb %sil, %dl
cmovnel %ecx, %eax
cmpl $0x0, %eax
jne 0x1e2a
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movslq 0x6397(%rip), %rcx      # 0x8158
cmpq %rcx, %rax
jge 0x1e28
movq -0x70(%rbp), %rax
movq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x63de(%rip), %rax      # 0x81b8
movq -0x18(%rbp), %rcx
addq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x78(%rbp), %rax
movq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x63b4(%rip), %rax      # 0x81b8
movq -0x18(%rbp), %rcx
addq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x1db6
jmp 0x1e2a
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq 0x6382(%rip), %rax      # 0x81c0
movslq 0x6313(%rip), %rcx      # 0x8158
addq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x1230
movaps %xmm0, %xmm1
movsd -0xb8(%rbp), %xmm0
divsd %xmm1, %xmm0
callq 0x1f70
movaps %xmm0, %xmm1
movq 0x6347(%rip), %rax      # 0x81b8
movslq 0x62e0(%rip), %rcx      # 0x8158
addq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movq 0x6322(%rip), %rax      # 0x81c0
movslq 0x62b3(%rip), %rcx      # 0x8158
addq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x1230
movaps %xmm0, %xmm1
movsd -0xb0(%rbp), %xmm0
divsd %xmm1, %xmm0
callq 0x1f70
movaps %xmm0, %xmm1
movq 0x62e7(%rip), %rax      # 0x81b8
movslq 0x6280(%rip), %rcx      # 0x8158
addq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x50(%rbp), %xmm0
mulsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
movq 0x62bb(%rip), %rax      # 0x81c0
movslq 0x624c(%rip), %rcx      # 0x8158
addq -0x20(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x50(%rbp), %xmm0
movq 0x629a(%rip), %rax      # 0x81c0
movslq 0x622b(%rip), %rcx      # 0x8158
addq -0x28(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x17de
movq -0x70(%rbp), %rdi
callq 0x1030
movq -0x78(%rbp), %rdi
callq 0x1030
movq -0x68(%rbp), %rdi
callq 0x10d0
xorl %eax, %eax
movl %eax, %edi
callq 0x1170