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


x86 assembly (clang, -O0):
<mygrad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1196 <mygrad+0x76>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1040 <sqrt@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1030 <exp@plt>
subsd  -0x20(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113a <mygrad+0x1a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mygrad>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edx,%edx
jle    1186 <mygrad+0x66>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%r12d
xor    %ebx,%ebx
jmp    116a <mygrad+0x4a>
xchg   %ax,%ax
call   1040 <sqrt@plt>
movsd  %xmm0,0x8(%rsp)
movsd  (%r14,%rbx,8),%xmm0
call   1030 <exp@plt>
subsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%r15,%rbx,8)
mov    %r13,%rbx
cmp    %r13,%r12
je     1186 <mygrad+0x66>
lea    0x1(%rbx),%r13
xorps  %xmm0,%xmm0
cvtsi2sd %r13d,%xmm0
ucomisd 0xe82(%rip),%xmm0
jb     1140 <mygrad+0x20>
sqrtsd %xmm0,%xmm0
jmp    1145 <mygrad+0x25>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @mygrad(double* nocapture writeonly %0, double* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %19
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %7
%8 = phi i64 [ 0, %5 ], [ %9, %7 ]
%9 = add nuw nsw i64 %8, 1
%10 = trunc i64 %9 to i32
%11 = sitofp i32 %10 to double
%12 = tail call double @sqrt(double %11)
%13 = getelementptr inbounds double, double* %1, i64 %8
%14 = load double, double* %13
%15 = tail call double @exp(double %14)
%16 = fsub double %15, %12
%17 = getelementptr inbounds double, double* %0, i64 %8
store double %16, double* %17
%18 = icmp eq i64 %9, %6
br i1 %18, label %19, label %7
19: ; preds = %7, %3
ret void
}