#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
float expf(float);
void arraydiff_kernel_logistic_bwd_f32(size_t dim, const float *x,
                                       const float *dy, float *dx) {
  for (size_t i = 0; i < dim; i++) {
    float x_i = x[i];
    float y_i = 1.0f / (1.0f + expf(-x_i));
    dx[i] += y_i * (1.0f - y_i);
  }
}


x86 assembly (clang, -O0):
<arraydiff_kernel_logistic_bwd_f32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    11c1 <arraydiff_kernel_logistic_bwd_f32+0xb1>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
movss  %xmm0,-0x2c(%rbp)
movss  -0x2c(%rbp),%xmm0
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
call   1030 <expf@plt>
movss  0xe91(%rip),%xmm1
addss  %xmm0,%xmm1
movss  0xe85(%rip),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,-0x30(%rbp)
movss  -0x30(%rbp),%xmm0
movss  0xe6f(%rip),%xmm2
subss  -0x30(%rbp),%xmm2
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1130 <arraydiff_kernel_logistic_bwd_f32+0x20>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arraydiff_kernel_logistic_bwd_f32>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1175 <arraydiff_kernel_logistic_bwd_f32+0x65>
mov    %rcx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movss  (%r14,%rbx,4),%xmm0
xorps  0xec3(%rip),%xmm0
call   1030 <expf@plt>
movss  0xec6(%rip),%xmm2
addss  %xmm2,%xmm0
movaps %xmm2,%xmm1
divss  %xmm0,%xmm1
movaps %xmm2,%xmm0
subss  %xmm1,%xmm0
mulss  %xmm1,%xmm0
addss  (%r12,%rbx,4),%xmm0
movss  %xmm0,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1130 <arraydiff_kernel_logistic_bwd_f32+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @arraydiff_kernel_logistic_bwd_f32(i64 %0, float* %1, float* %2, float* %3) {
%5 = alloca i64
%6 = alloca float*
%7 = alloca float*
%8 = alloca float*
%9 = alloca i64
%10 = alloca float
%11 = alloca float
store i64 %0, i64* %5
store float* %1, float** %6
store float* %2, float** %7
store float* %3, float** %8
store i64 0, i64* %9
br label %12
12: ; preds = %34, %4
%13 = load i64, i64* %9
%14 = load i64, i64* %5
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %37
16: ; preds = %12
%17 = load float*, float** %6
%18 = load i64, i64* %9
%19 = getelementptr inbounds float, float* %17, i64 %18
%20 = load float, float* %19
store float %20, float* %10
%21 = load float, float* %10
%22 = fneg float %21
%23 = call float @expf(float %22)
%24 = fadd float 1.000000e+00, %23
%25 = fdiv float 1.000000e+00, %24
store float %25, float* %11
%26 = load float, float* %11
%27 = load float, float* %11
%28 = fsub float 1.000000e+00, %27
%29 = load float*, float** %8
%30 = load i64, i64* %9
%31 = getelementptr inbounds float, float* %29, i64 %30
%32 = load float, float* %31
%33 = call float @llvm.fmuladd.f32(float %26, float %28, float %32)
store float %33, float* %31
br label %34
34: ; preds = %16
%35 = load i64, i64* %9
%36 = add i64 %35, 1
store i64 %36, i64* %9
br label %12
37: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @arraydiff_kernel_logistic_bwd_f32(i64 %0, float* nocapture %1, float* nocapture readnone %2, float* nocapture %3) {
%5 = icmp eq i64 %0, 0
br i1 %5, label %6, label %7
6: ; preds = %7, %4
ret void
7: ; preds = %4, %7
%8 = phi i64 [ %19, %7 ], [ 0, %4 ]
%9 = getelementptr inbounds float, float* %1, i64 %8
%10 = load float, float* %9
%11 = fneg float %10
%12 = tail call float @expf(float %11)
%13 = fadd float %12, 1.000000e+00
%14 = fdiv float 1.000000e+00, %13
%15 = fsub float 1.000000e+00, %14
%16 = getelementptr inbounds float, float* %3, i64 %8
%17 = load float, float* %16
%18 = tail call float @llvm.fmuladd.f32(float %14, float %15, float %17)
store float %18, float* %16
%19 = add nuw i64 %8, 1
%20 = icmp eq i64 %19, %0
br i1 %20, label %6, label %7
}