#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 exp(double);
double sqrt(double);
double myvalgrad(double *g, double *x, int n) {
  double ex, f, t;
  int i;
  f = (double)0;
  for (i = 0; i < n; i++) {
    t = i + 1;
    t = sqrt(t);
    ex = exp(x[i]);
    f += ex - t * x[i];
    g[i] = ex - t;
  }
  return (f);
}


x86 assembly (clang, -O0):
<myvalgrad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e8 <myvalgrad+0xc8>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1040 <sqrt@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1030 <exp@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1142 <myvalgrad+0x22>
movsd  -0x28(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myvalgrad>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edx,%edx
jle    11ba <myvalgrad+0x9a>
mov    %rsi,%r12
mov    %rdi,%r14
mov    %edx,%r15d
xor    %ebx,%ebx
xorpd  %xmm1,%xmm1
jmp    1198 <myvalgrad+0x78>
cs nopw 0x0(%rax,%rax,1)
call   1040 <sqrt@plt>
movsd  %xmm0,(%rsp)
movsd  (%r12,%rbx,8),%xmm0
call   1030 <exp@plt>
movsd  (%r12,%rbx,8),%xmm1
movsd  (%rsp),%xmm3
mulsd  %xmm3,%xmm1
movapd %xmm0,%xmm2
subsd  %xmm1,%xmm2
movsd  0x8(%rsp),%xmm1
addsd  %xmm2,%xmm1
subsd  %xmm3,%xmm0
movsd  %xmm0,(%r14,%rbx,8)
mov    %r13,%rbx
cmp    %r13,%r15
je     11be <myvalgrad+0x9e>
movsd  %xmm1,0x8(%rsp)
lea    0x1(%rbx),%r13
xorps  %xmm0,%xmm0
cvtsi2sd %r13d,%xmm0
ucomisd 0xe4e(%rip),%xmm0
jb     1150 <myvalgrad+0x30>
sqrtsd %xmm0,%xmm0
jmp    1155 <myvalgrad+0x35>
xorpd  %xmm1,%xmm1
movapd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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