void lstm_backward(const int n, const double *ws, const double *store,
                   const double *delta_h, const double *delta_y, double *d_ws,
                   double *d_hidden, double *d_inputs) {
  for (int i = 0; i < n; i++) {
    const double x = store[0];
    const double g_i = store[1];
    const double g_f = store[2];
    const double g_o = store[3];
    const double h_old = store[4];
    const double h_add = store[5];
    const double h_keep = store[6];
    const double h_new = store[7];
    const double ws_keep = ws[0];
    const double ws_add = ws[1];
    const double dy = delta_y[0];
    const double dh_new = dy * g_o + delta_h[0];
    const double dg_o = dy * h_new;
    const double dh_keep = dh_new * ws_keep;
    const double dh_add = dh_new * ws_add;
    const double dws_keep = dh_new * h_keep;
    const double dws_add = dh_new * h_add;
    const double dx = dh_add * g_i;
    const double dg_i = dh_add * x;
    const double dg_f = dh_keep * h_old;
    const double dh_old = dh_keep * g_f;
    d_ws[0] = dws_keep;
    d_ws[1] = dws_add;
    d_hidden[0] = dh_old;
    d_inputs[0] = dx * 2;
    d_inputs[1] = dg_i;
    d_inputs[2] = dg_f;
    d_inputs[3] = dg_o;
    store += 8;
    ws += 2;
    delta_y += 1;
    delta_h += 1;
    d_ws += 2;
    d_hidden += 1;
    d_inputs += 4;
  }
}


x86 assembly (clang, -O0):
<lstm_backward>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    139a <lstm_backward+0x29a>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
movsd  0x8(%rax),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x18(%rbp),%rax
movsd  0x10(%rax),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x18(%rbp),%rax
movsd  0x18(%rax),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x18(%rbp),%rax
movsd  0x20(%rax),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x18(%rbp),%rax
movsd  0x28(%rax),%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x18(%rbp),%rax
movsd  0x30(%rax),%xmm0
movsd  %xmm0,-0x70(%rbp)
mov    -0x18(%rbp),%rax
movsd  0x38(%rax),%xmm0
movsd  %xmm0,-0x78(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x80(%rbp)
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
mov    -0x28(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x90(%rbp)
ff
movsd  -0x90(%rbp),%xmm0
ff
movsd  -0x58(%rbp),%xmm2
mov    -0x20(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x98(%rbp)
ff
movsd  -0x90(%rbp),%xmm0
ff
mulsd  -0x78(%rbp),%xmm0
movsd  %xmm0,-0xa0(%rbp)
ff
movsd  -0x98(%rbp),%xmm0
ff
mulsd  -0x80(%rbp),%xmm0
movsd  %xmm0,-0xa8(%rbp)
ff
movsd  -0x98(%rbp),%xmm0
ff
mulsd  -0x88(%rbp),%xmm0
ff
movsd  %xmm0,-0xb0(%rbp)
ff
movsd  -0x98(%rbp),%xmm0
ff
mulsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0xb8(%rbp)
ff
movsd  -0x98(%rbp),%xmm0
ff
mulsd  -0x68(%rbp),%xmm0
movsd  %xmm0,-0xc0(%rbp)
ff
movsd  -0xb0(%rbp),%xmm0
ff
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0xc8(%rbp)
ff
movsd  -0xb0(%rbp),%xmm0
ff
mulsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0xd0(%rbp)
ff
movsd  -0xa8(%rbp),%xmm0
ff
mulsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0xd8(%rbp)
ff
movsd  -0xa8(%rbp),%xmm0
ff
mulsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0xe0(%rbp)
ff
movsd  -0xb8(%rbp),%xmm0
ff
mov    -0x30(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0xc0(%rbp),%xmm0
ff
mov    -0x30(%rbp),%rax
movsd  %xmm0,0x8(%rax)
movsd  -0xe0(%rbp),%xmm0
ff
mov    0x10(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  0xd0b(%rip),%xmm0
mulsd  -0xc8(%rbp),%xmm0
ff
mov    0x18(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0xd0(%rbp),%xmm0
ff
mov    0x18(%rbp),%rax
movsd  %xmm0,0x8(%rax)
movsd  -0xd8(%rbp),%xmm0
ff
mov    0x18(%rbp),%rax
movsd  %xmm0,0x10(%rax)
movsd  -0xa0(%rbp),%xmm0
ff
mov    0x18(%rbp),%rax
movsd  %xmm0,0x18(%rax)
mov    -0x18(%rbp),%rax
add    $0x40,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x30(%rbp)
mov    0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,0x10(%rbp)
mov    0x18(%rbp),%rax
add    $0x20,%rax
mov    %rax,0x18(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    112e <lstm_backward+0x2e>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lstm_backward>:
test   %edi,%edi
jle    11bb <lstm_backward+0xbb>
mov    0x10(%rsp),%r11
mov    0x8(%rsp),%r10
xor    %eax,%eax
movapd 0xee3(%rip),%xmm8
nopl   (%rax)
movupd 0x28(%rdx,%rax,8),%xmm1
movsd  (%r8,%rax,1),%xmm2
movsd  0x18(%rdx,%rax,8),%xmm3
mulsd  %xmm2,%xmm3
addsd  (%rcx,%rax,1),%xmm3
movsd  (%rsi,%rax,2),%xmm0
mulsd  %xmm3,%xmm0
movsd  0x8(%rsi,%rax,2),%xmm5
mulsd  %xmm3,%xmm5
unpcklpd %xmm3,%xmm3
movsd  0x8(%rdx,%rax,8),%xmm6
mulsd  %xmm5,%xmm6
movsd  0x20(%rdx,%rax,8),%xmm7
movhpd 0x38(%rdx,%rax,8),%xmm7
movapd %xmm0,%xmm4
unpcklpd %xmm2,%xmm4
mulpd  %xmm7,%xmm4
mulsd  0x10(%rdx,%rax,8),%xmm0
shufpd $0x1,%xmm1,%xmm1
mulpd  %xmm3,%xmm1
movapd %xmm8,%xmm2
movhpd (%rdx,%rax,8),%xmm2
movupd %xmm1,(%r9,%rax,2)
movsd  %xmm0,(%r10,%rax,1)
unpcklpd %xmm5,%xmm6
mulpd  %xmm2,%xmm6
movupd %xmm6,(%r11,%rax,4)
movupd %xmm4,0x10(%r11,%rax,4)
add    $0x8,%rax
add    $0xffffffff,%edi
jne    1120 <lstm_backward+0x20>
ret

LLVM IR (-O0):
define void @lstm_backward(i32 %0, double* %1, double* %2, double* %3, double* %4, double* %5, double* %6, double* %7) {
%9 = alloca i32
%10 = alloca double*
%11 = alloca double*
%12 = alloca double*
%13 = alloca double*
%14 = alloca double*
%15 = alloca double*
%16 = alloca double*
%17 = alloca i32
%18 = alloca double
%19 = alloca double
%20 = alloca double
%21 = alloca double
%22 = alloca double
%23 = alloca double
%24 = alloca double
%25 = alloca double
%26 = alloca double
%27 = alloca double
%28 = alloca double
%29 = alloca double
%30 = alloca double
%31 = alloca double
%32 = alloca double
%33 = alloca double
%34 = alloca double
%35 = alloca double
%36 = alloca double
%37 = alloca double
%38 = alloca double
store i32 %0, i32* %9
store double* %1, double** %10
store double* %2, double** %11
store double* %3, double** %12
store double* %4, double** %13
store double* %5, double** %14
store double* %6, double** %15
store double* %7, double** %16
store i32 0, i32* %17
br label %39
39: ; preds = %146, %8
%40 = load i32, i32* %17
%41 = load i32, i32* %9
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %149
43: ; preds = %39
%44 = load double*, double** %11
%45 = getelementptr inbounds double, double* %44, i64 0
%46 = load double, double* %45
store double %46, double* %18
%47 = load double*, double** %11
%48 = getelementptr inbounds double, double* %47, i64 1
%49 = load double, double* %48
store double %49, double* %19
%50 = load double*, double** %11
%51 = getelementptr inbounds double, double* %50, i64 2
%52 = load double, double* %51
store double %52, double* %20
%53 = load double*, double** %11
%54 = getelementptr inbounds double, double* %53, i64 3
%55 = load double, double* %54
store double %55, double* %21
%56 = load double*, double** %11
%57 = getelementptr inbounds double, double* %56, i64 4
%58 = load double, double* %57
store double %58, double* %22
%59 = load double*, double** %11
%60 = getelementptr inbounds double, double* %59, i64 5
%61 = load double, double* %60
store double %61, double* %23
%62 = load double*, double** %11
%63 = getelementptr inbounds double, double* %62, i64 6
%64 = load double, double* %63
store double %64, double* %24
%65 = load double*, double** %11
%66 = getelementptr inbounds double, double* %65, i64 7
%67 = load double, double* %66
store double %67, double* %25
%68 = load double*, double** %10
%69 = getelementptr inbounds double, double* %68, i64 0
%70 = load double, double* %69
store double %70, double* %26
%71 = load double*, double** %10
%72 = getelementptr inbounds double, double* %71, i64 1
%73 = load double, double* %72
store double %73, double* %27
%74 = load double*, double** %13
%75 = getelementptr inbounds double, double* %74, i64 0
%76 = load double, double* %75
store double %76, double* %28
%77 = load double, double* %28
%78 = load double, double* %21
%79 = load double*, double** %12
%80 = getelementptr inbounds double, double* %79, i64 0
%81 = load double, double* %80
%82 = call double @llvm.fmuladd.f64(double %77, double %78, double %81)
store double %82, double* %29
%83 = load double, double* %28
%84 = load double, double* %25
%85 = fmul double %83, %84
store double %85, double* %30
%86 = load double, double* %29
%87 = load double, double* %26
%88 = fmul double %86, %87
store double %88, double* %31
%89 = load double, double* %29
%90 = load double, double* %27
%91 = fmul double %89, %90
store double %91, double* %32
%92 = load double, double* %29
%93 = load double, double* %24
%94 = fmul double %92, %93
store double %94, double* %33
%95 = load double, double* %29
%96 = load double, double* %23
%97 = fmul double %95, %96
store double %97, double* %34
%98 = load double, double* %32
%99 = load double, double* %19
%100 = fmul double %98, %99
store double %100, double* %35
%101 = load double, double* %32
%102 = load double, double* %18
%103 = fmul double %101, %102
store double %103, double* %36
%104 = load double, double* %31
%105 = load double, double* %22
%106 = fmul double %104, %105
store double %106, double* %37
%107 = load double, double* %31
%108 = load double, double* %20
%109 = fmul double %107, %108
store double %109, double* %38
%110 = load double, double* %33
%111 = load double*, double** %14
%112 = getelementptr inbounds double, double* %111, i64 0
store double %110, double* %112
%113 = load double, double* %34
%114 = load double*, double** %14
%115 = getelementptr inbounds double, double* %114, i64 1
store double %113, double* %115
%116 = load double, double* %38
%117 = load double*, double** %15
%118 = getelementptr inbounds double, double* %117, i64 0
store double %116, double* %118
%119 = load double, double* %35
%120 = fmul double %119, 2.000000e+00
%121 = load double*, double** %16
%122 = getelementptr inbounds double, double* %121, i64 0
store double %120, double* %122
%123 = load double, double* %36
%124 = load double*, double** %16
%125 = getelementptr inbounds double, double* %124, i64 1
store double %123, double* %125
%126 = load double, double* %37
%127 = load double*, double** %16
%128 = getelementptr inbounds double, double* %127, i64 2
store double %126, double* %128
%129 = load double, double* %30
%130 = load double*, double** %16
%131 = getelementptr inbounds double, double* %130, i64 3
store double %129, double* %131
%132 = load double*, double** %11
%133 = getelementptr inbounds double, double* %132, i64 8
store double* %133, double** %11
%134 = load double*, double** %10
%135 = getelementptr inbounds double, double* %134, i64 2
store double* %135, double** %10
%136 = load double*, double** %13
%137 = getelementptr inbounds double, double* %136, i64 1
store double* %137, double** %13
%138 = load double*, double** %12
%139 = getelementptr inbounds double, double* %138, i64 1
store double* %139, double** %12
%140 = load double*, double** %14
%141 = getelementptr inbounds double, double* %140, i64 2
store double* %141, double** %14
%142 = load double*, double** %15
%143 = getelementptr inbounds double, double* %142, i64 1
store double* %143, double** %15
%144 = load double*, double** %16
%145 = getelementptr inbounds double, double* %144, i64 4
store double* %145, double** %16
br label %146
146: ; preds = %43
%147 = load i32, i32* %17
%148 = add nsw i32 %147, 1
store i32 %148, i32* %17
br label %39
149: ; preds = %39
ret void
}

LLVM IR (-O2):
define void @lstm_backward(i32 %0, double* nocapture %1, double* nocapture %2, double* nocapture %3, double* nocapture %4, double* nocapture writeonly %5, double* nocapture writeonly %6, double* nocapture writeonly %7) {
%9 = icmp sgt i32 %0, 0
br i1 %9, label %11, label %10
10: ; preds = %11, %8
ret void
11: ; preds = %8, %11
%12 = phi double* [ %62, %11 ], [ %1, %8 ]
%13 = phi double* [ %61, %11 ], [ %2, %8 ]
%14 = phi double* [ %64, %11 ], [ %3, %8 ]
%15 = phi double* [ %63, %11 ], [ %4, %8 ]
%16 = phi double* [ %65, %11 ], [ %5, %8 ]
%17 = phi double* [ %66, %11 ], [ %6, %8 ]
%18 = phi double* [ %67, %11 ], [ %7, %8 ]
%19 = phi i32 [ %68, %11 ], [ 0, %8 ]
%20 = load double, double* %13
%21 = getelementptr inbounds double, double* %13, i64 1
%22 = load double, double* %21
%23 = getelementptr inbounds double, double* %13, i64 2
%24 = load double, double* %23
%25 = getelementptr inbounds double, double* %13, i64 3
%26 = load double, double* %25
%27 = getelementptr inbounds double, double* %13, i64 4
%28 = load double, double* %27
%29 = getelementptr inbounds double, double* %13, i64 5
%30 = bitcast double* %29 to <2 x double>*
%31 = load <2 x double>, <2 x double>* %30
%32 = getelementptr inbounds double, double* %13, i64 7
%33 = load double, double* %32
%34 = load double, double* %12
%35 = getelementptr inbounds double, double* %12, i64 1
%36 = load double, double* %35
%37 = load double, double* %15
%38 = load double, double* %14
%39 = tail call double @llvm.fmuladd.f64(double %37, double %26, double %38)
%40 = fmul double %34, %39
%41 = fmul double %36, %39
%42 = insertelement <2 x double> poison, double %39, i64 0
%43 = shufflevector <2 x double> %42, <2 x double> poison, <2 x i32> zeroinitializer
%44 = fmul <2 x double> %31, %43
%45 = fmul double %22, %41
%46 = insertelement <2 x double> poison, double %28, i64 0
%47 = insertelement <2 x double> %46, double %33, i64 1
%48 = insertelement <2 x double> poison, double %40, i64 0
%49 = insertelement <2 x double> %48, double %37, i64 1
%50 = fmul <2 x double> %47, %49
%51 = fmul double %24, %40
%52 = shufflevector <2 x double> %44, <2 x double> poison, <2 x i32> <i32 1, i32 0>
%53 = bitcast double* %16 to <2 x double>*
store <2 x double> %52, <2 x double>* %53
store double %51, double* %17
%54 = insertelement <2 x double> <double 2.000000e+00, double poison>, double %20, i64 1
%55 = insertelement <2 x double> poison, double %45, i64 0
%56 = insertelement <2 x double> %55, double %41, i64 1
%57 = fmul <2 x double> %54, %56
%58 = bitcast double* %18 to <2 x double>*
store <2 x double> %57, <2 x double>* %58
%59 = getelementptr inbounds double, double* %18, i64 2
%60 = bitcast double* %59 to <2 x double>*
store <2 x double> %50, <2 x double>* %60
%61 = getelementptr inbounds double, double* %13, i64 8
%62 = getelementptr inbounds double, double* %12, i64 2
%63 = getelementptr inbounds double, double* %15, i64 1
%64 = getelementptr inbounds double, double* %14, i64 1
%65 = getelementptr inbounds double, double* %16, i64 2
%66 = getelementptr inbounds double, double* %17, i64 1
%67 = getelementptr inbounds double, double* %18, i64 4
%68 = add nuw nsw i32 %19, 1
%69 = icmp eq i32 %68, %0
br i1 %69, label %10, label %11
}