#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 */
scalar_t__ pow(double, int);
double merit_function(double *x, double *k, double *y, int n,
                      double (*f)(double *, double)) {
  double e = 0.0;
  int i;
  for (i = 0; i < n; i++) {
    e += pow(y[i] - f(x, k[i]), 2);
  }
  return e;
}


x86 assembly (clang, -O0):
<merit_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11a2 <merit_function+0x92>
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rcx
movslq -0x34(%rbp),%rdx
movsd  (%rcx,%rdx,8),%xmm0
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
subsd  %xmm1,%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    113a <merit_function+0x2a>
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merit_function>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %ecx,%ecx
jle    119f <merit_function+0x8f>
mov    %r8,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
mov    %ecx,%ebx
xorps  %xmm0,%xmm0
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movsd  %xmm0,0x8(%rsp)
movsd  (%r15,%rbp,8),%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  (%r12,%rbp,8),%xmm0
mov    %r13,%rdi
call   *%r14
movsd  0x10(%rsp),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1140 <merit_function+0x30>
jmp    11a2 <merit_function+0x92>
xorps  %xmm0,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @merit_function(double* %0, double* %1, double* %2, i32 %3, double (double*, double)* %4) {
%6 = alloca double*
%7 = alloca double*
%8 = alloca double*
%9 = alloca i32
%10 = alloca double (double*, double)*
%11 = alloca double
%12 = alloca i32
store double* %0, double** %6
store double* %1, double** %7
store double* %2, double** %8
store i32 %3, i32* %9
store double (double*, double)* %4, double (double*, double)** %10
store double 0.000000e+00, double* %11
store i32 0, i32* %12
br label %13
13: ; preds = %36, %5
%14 = load i32, i32* %12
%15 = load i32, i32* %9
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %39
17: ; preds = %13
%18 = load double*, double** %8
%19 = load i32, i32* %12
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds double, double* %18, i64 %20
%22 = load double, double* %21
%23 = load double (double*, double)*, double (double*, double)** %10
%24 = load double*, double** %6
%25 = load double*, double** %7
%26 = load i32, i32* %12
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds double, double* %25, i64 %27
%29 = load double, double* %28
%30 = call double %23(double* %24, double %29)
%31 = fsub double %22, %30
%32 = call i64 @pow(double %31, i32 2)
%33 = sitofp i64 %32 to double
%34 = load double, double* %11
%35 = fadd double %34, %33
store double %35, double* %11
br label %36
36: ; preds = %17
%37 = load i32, i32* %12
%38 = add nsw i32 %37, 1
store i32 %38, i32* %12
br label %13
39: ; preds = %13
%40 = load double, double* %11
ret double %40
}

LLVM IR (-O2):
define double @merit_function(double* %0, double* nocapture %1, double* nocapture %2, i32 %3, double (double*, double)* nocapture %4) {
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %23
7: ; preds = %5
%8 = zext i32 %3 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %21, %9 ]
%11 = phi double [ 0.000000e+00, %7 ], [ %20, %9 ]
%12 = getelementptr inbounds double, double* %2, i64 %10
%13 = load double, double* %12
%14 = getelementptr inbounds double, double* %1, i64 %10
%15 = load double, double* %14
%16 = tail call double %4(double* %0, double %15)
%17 = fsub double %13, %16
%18 = tail call i64 @pow(double %17, i32 2)
%19 = sitofp i64 %18 to double
%20 = fadd double %11, %19
%21 = add nuw nsw i64 %10, 1
%22 = icmp eq i64 %21, %8
br i1 %22, label %23, label %9
23: ; preds = %9, %5
%24 = phi double [ 0.000000e+00, %5 ], [ %20, %9 ]
ret double %24
}