void grad(int np, double *b, double *g, double p0,
          void (*funct1)(int, double *, double *), double tolder) {
  double tol, p1;
  int i;
  tol = tolder;
  for (i = 1; i <= np; i++) {
    b[i] += tol;
    (*funct1)(np, b, &p1);
    g[i] = (p1 - p0) / tol;
    b[i] -= tol;
  }
}


x86 assembly (clang, -O0):
<grad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movsd  %xmm1,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     11ab <grad+0xab>
movsd  -0x38(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   *%rax
movsd  -0x40(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
divsd  -0x38(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x38(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1132 <grad+0x32>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grad>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edi,%edi
jle    1196 <grad+0x96>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r12d
lea    0x1(%r12),%ebp
mov    $0x1,%r13d
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,(%rsp)
nopl   0x0(%rax,%rax,1)
movsd  (%rbx,%r13,8),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rbx,%r13,8)
mov    %r12d,%edi
mov    %rbx,%rsi
lea    0x10(%rsp),%rdx
call   *%r14
movsd  (%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
subsd  %xmm0,%xmm2
divsd  %xmm1,%xmm2
movsd  %xmm2,(%r15,%r13,8)
movsd  (%rbx,%r13,8),%xmm2
subsd  %xmm1,%xmm2
movsd  %xmm2,(%rbx,%r13,8)
add    $0x1,%r13
cmp    %r13,%rbp
jne    1140 <grad+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @grad(i32 %0, double* %1, double* %2, double %3, void (i32, double*, double*)* %4, double %5) {
%7 = alloca i32
%8 = alloca double*
%9 = alloca double*
%10 = alloca double
%11 = alloca void (i32, double*, double*)*
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca i32
store i32 %0, i32* %7
store double* %1, double** %8
store double* %2, double** %9
store double %3, double* %10
store void (i32, double*, double*)* %4, void (i32, double*, double*)** %11
store double %5, double* %12
%16 = load double, double* %12
store double %16, double* %13
store i32 1, i32* %15
br label %17
17: ; preds = %48, %6
%18 = load i32, i32* %15
%19 = load i32, i32* %7
%20 = icmp sle i32 %18, %19
br i1 %20, label %21, label %51
21: ; preds = %17
%22 = load double, double* %13
%23 = load double*, double** %8
%24 = load i32, i32* %15
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds double, double* %23, i64 %25
%27 = load double, double* %26
%28 = fadd double %27, %22
store double %28, double* %26
%29 = load void (i32, double*, double*)*, void (i32, double*, double*)** %11
%30 = load i32, i32* %7
%31 = load double*, double** %8
%32 = load double, double* %14
%33 = load double, double* %10
%34 = fsub double %32, %33
%35 = load double, double* %13
%36 = fdiv double %34, %35
%37 = load double*, double** %9
%38 = load i32, i32* %15
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds double, double* %37, i64 %39
store double %36, double* %40
%41 = load double, double* %13
%42 = load double*, double** %8
%43 = load i32, i32* %15
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds double, double* %42, i64 %44
%46 = load double, double* %45
%47 = fsub double %46, %41
store double %47, double* %45
br label %48
48: ; preds = %21
%49 = load i32, i32* %15
%50 = add nsw i32 %49, 1
store i32 %50, i32* %15
br label %17
51: ; preds = %17
ret void
}

LLVM IR (-O2):
define void @grad(i32 %0, double* %1, double* nocapture writeonly %2, double %3, void (i32, double*, double*)* nocapture %4, double %5) {
%7 = alloca double
%8 = bitcast double* %7 to i8*
%9 = icmp slt i32 %0, 1
br i1 %9, label %26, label %10
10: ; preds = %6
%11 = add nuw i32 %0, 1
%12 = zext i32 %11 to i64
br label %13
13: ; preds = %10, %13
%14 = phi i64 [ 1, %10 ], [ %24, %13 ]
%15 = getelementptr inbounds double, double* %1, i64 %14
%16 = load double, double* %15
%17 = fadd double %16, %5
store double %17, double* %15
%18 = load double, double* %7
%19 = fsub double %18, %3
%20 = fdiv double %19, %5
%21 = getelementptr inbounds double, double* %2, i64 %14
store double %20, double* %21
%22 = load double, double* %15
%23 = fsub double %22, %5
store double %23, double* %15
%24 = add nuw nsw i64 %14, 1
%25 = icmp eq i64 %24, %12
br i1 %25, label %26, label %13
26: ; preds = %13, %6
ret void
}