void gradient_hessian_over_v(const int n, const double *expz,
                             const double *expmz, const double *b,
                             double *gradient, double *hessian) {
  int i;
  double grad = 0.0;
  double hess = 0.0;
  for (i = 0; i < n; i++) {
    grad -= b[i] / (1.0 + expz[i]);
    hess += b[i] * b[i] / (2.0 + expmz[i] + expz[i]);
  }
  *gradient = grad;
  *hessian = hess;
}


x86 assembly (clang, -O0):
<gradient_hessian_over_v>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11ca <gradient_hessian_over_v+0xca>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  0xead(%rip),%xmm0
addsd  (%rax,%rcx,8),%xmm0
divsd  %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  0xe64(%rip),%xmm1
addsd  (%rax,%rcx,8),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm1
divsd  %xmm1,%xmm0
addsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1132 <gradient_hessian_over_v+0x32>
movsd  -0x40(%rbp),%xmm0
mov    -0x28(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x48(%rbp),%xmm0
mov    -0x30(%rbp),%rax
movsd  %xmm0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<gradient_hessian_over_v>:
test   %edi,%edi
jle    1172 <gradient_hessian_over_v+0x72>
mov    %edi,%eax
xorpd  %xmm3,%xmm3
xor    %edi,%edi
movsd  0xeec(%rip),%xmm0
movapd 0xef4(%rip),%xmm1
nopl   0x0(%rax)
movsd  (%rcx,%rdi,8),%xmm2
movapd %xmm2,%xmm4
mulsd  %xmm2,%xmm4
movsd  (%rdx,%rdi,8),%xmm5
addsd  %xmm0,%xmm5
movapd %xmm1,%xmm6
unpcklpd %xmm5,%xmm6
movsd  (%rsi,%rdi,8),%xmm5
unpcklpd %xmm5,%xmm5
addpd  %xmm6,%xmm5
unpcklpd %xmm4,%xmm2
divpd  %xmm5,%xmm2
movapd %xmm3,%xmm4
subpd  %xmm2,%xmm4
addpd  %xmm3,%xmm2
movsd  %xmm4,%xmm2
add    $0x1,%rdi
movapd %xmm2,%xmm3
cmp    %rdi,%rax
jne    1120 <gradient_hessian_over_v+0x20>
jmp    1176 <gradient_hessian_over_v+0x76>
xorpd  %xmm2,%xmm2
movlpd %xmm2,(%r8)
movhpd %xmm2,(%r9)
ret

LLVM IR (-O0):
define void @gradient_hessian_over_v(i32 %0, double* %1, double* %2, double* %3, double* %4, double* %5) {
%7 = alloca i32
%8 = alloca double*
%9 = alloca double*
%10 = alloca double*
%11 = alloca double*
%12 = alloca double*
%13 = alloca i32
%14 = alloca double
%15 = alloca double
store i32 %0, i32* %7
store double* %1, double** %8
store double* %2, double** %9
store double* %3, double** %10
store double* %4, double** %11
store double* %5, double** %12
store double 0.000000e+00, double* %14
store double 0.000000e+00, double* %15
store i32 0, i32* %13
br label %16
16: ; preds = %61, %6
%17 = load i32, i32* %13
%18 = load i32, i32* %7
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %64
20: ; preds = %16
%21 = load double*, double** %10
%22 = load i32, i32* %13
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds double, double* %21, i64 %23
%25 = load double, double* %24
%26 = load double*, double** %8
%27 = load i32, i32* %13
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds double, double* %26, i64 %28
%30 = load double, double* %29
%31 = fadd double 1.000000e+00, %30
%32 = fdiv double %25, %31
%33 = load double, double* %14
%34 = fsub double %33, %32
store double %34, double* %14
%35 = load double*, double** %10
%36 = load i32, i32* %13
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds double, double* %35, i64 %37
%39 = load double, double* %38
%40 = load double*, double** %10
%41 = load i32, i32* %13
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double, double* %40, i64 %42
%44 = load double, double* %43
%45 = fmul double %39, %44
%46 = load double*, double** %9
%47 = load i32, i32* %13
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds double, double* %46, i64 %48
%50 = load double, double* %49
%51 = fadd double 2.000000e+00, %50
%52 = load double*, double** %8
%53 = load i32, i32* %13
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds double, double* %52, i64 %54
%56 = load double, double* %55
%57 = fadd double %51, %56
%58 = fdiv double %45, %57
%59 = load double, double* %15
%60 = fadd double %59, %58
store double %60, double* %15
br label %61
61: ; preds = %20
%62 = load i32, i32* %13
%63 = add nsw i32 %62, 1
store i32 %63, i32* %13
br label %16
64: ; preds = %16
%65 = load double, double* %14
%66 = load double*, double** %11
store double %65, double* %66
%67 = load double, double* %15
%68 = load double*, double** %12
store double %67, double* %68
ret void
}

LLVM IR (-O2):
define void @gradient_hessian_over_v(i32 %0, double* nocapture %1, double* nocapture %2, double* nocapture %3, double* nocapture writeonly %4, double* nocapture writeonly %5) {
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %33
8: ; preds = %6
%9 = zext i32 %0 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %31, %10 ]
%12 = phi <2 x double> [ zeroinitializer, %8 ], [ %30, %10 ]
%13 = getelementptr inbounds double, double* %3, i64 %11
%14 = load double, double* %13
%15 = getelementptr inbounds double, double* %1, i64 %11
%16 = load double, double* %15
%17 = fmul double %14, %14
%18 = getelementptr inbounds double, double* %2, i64 %11
%19 = load double, double* %18
%20 = fadd double %19, 2.000000e+00
%21 = insertelement <2 x double> poison, double %16, i64 0
%22 = shufflevector <2 x double> %21, <2 x double> poison, <2 x i32> zeroinitializer
%23 = insertelement <2 x double> <double 1.000000e+00, double poison>, double %20, i64 1
%24 = fadd <2 x double> %22, %23
%25 = insertelement <2 x double> poison, double %14, i64 0
%26 = insertelement <2 x double> %25, double %17, i64 1
%27 = fdiv <2 x double> %26, %24
%28 = fsub <2 x double> %12, %27
%29 = fadd <2 x double> %12, %27
%30 = shufflevector <2 x double> %28, <2 x double> %29, <2 x i32> <i32 0, i32 3>
%31 = add nuw nsw i64 %11, 1
%32 = icmp eq i64 %31, %9
br i1 %32, label %33, label %10
33: ; preds = %10, %6
%34 = phi <2 x double> [ zeroinitializer, %6 ], [ %30, %10 ]
%35 = extractelement <2 x double> %34, i64 0
store double %35, double* %4
%36 = extractelement <2 x double> %34, i64 1
store double %36, double* %5
ret void
}