#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 */
double cos(double);
double sqrt(int);
double fun2(double **vars, int n) {
  int i;
  double sum = 0, temp = 1;
  for (i = 0; i < n; ++i) {
    temp *= cos(vars[i][0] / sqrt(i + 1));
    sum += vars[i][0] * vars[i][0];
  }
  sum = sum / 4000 - temp + 1;
  return sum;
}


x86 assembly (clang, -O0):
<fun2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xec1(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d2 <fun2+0xb2>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1040 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
divsd  %xmm1,%xmm0
call   1030 <cos@plt>
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsd  (%rax),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsd  (%rax),%xmm2
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114b <fun2+0x2b>
movsd  -0x18(%rbp),%xmm0
movsd  0xe29(%rip),%xmm1
divsd  %xmm1,%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  0xe10(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fun2>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %esi,%esi
jle    11bb <fun2+0x9b>
mov    %rdi,%r14
mov    %esi,%r15d
xorpd  %xmm0,%xmm0
movsd  0xebb(%rip),%xmm2
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsd  %xmm2,0x10(%rsp)
movsd  %xmm0,(%rsp)
mov    (%r14,%rbx,8),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0x1(%rbx),%r12
mov    %r12d,%edi
call   1040 <sqrt@plt>
movsd  0x8(%rsp),%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1030 <cos@plt>
movsd  0x10(%rsp),%xmm2
mulsd  %xmm0,%xmm2
mov    (%r14,%rbx,8),%rax
movsd  (%rax),%xmm0
mulsd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
mov    %r12,%rbx
cmp    %r12,%r15
jne    1150 <fun2+0x30>
jmp    11c7 <fun2+0xa7>
movsd  0xe3d(%rip),%xmm2
xorpd  %xmm0,%xmm0
divsd  0xe39(%rip),%xmm0
subsd  %xmm2,%xmm0
addsd  0xe25(%rip),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define double @fun2(double** nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %23
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ 0, %4 ], [ %13, %6 ]
%8 = phi double [ 1.000000e+00, %4 ], [ %18, %6 ]
%9 = phi double [ 0.000000e+00, %4 ], [ %21, %6 ]
%10 = getelementptr inbounds double*, double** %0, i64 %7
%11 = load double*, double** %10
%12 = load double, double* %11
%13 = add nuw nsw i64 %7, 1
%14 = trunc i64 %13 to i32
%15 = tail call double @sqrt(i32 %14)
%16 = fdiv double %12, %15
%17 = tail call double @cos(double %16)
%18 = fmul double %8, %17
%19 = load double*, double** %10
%20 = load double, double* %19
%21 = tail call double @llvm.fmuladd.f64(double %20, double %20, double %9)
%22 = icmp eq i64 %13, %5
br i1 %22, label %23, label %6
23: ; preds = %6, %2
%24 = phi double [ 0.000000e+00, %2 ], [ %21, %6 ]
%25 = phi double [ 1.000000e+00, %2 ], [ %18, %6 ]
%26 = fdiv double %24, 4.000000e+03
%27 = fsub double %26, %25
%28 = fadd double %27, 1.000000e+00
ret double %28
}