#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 */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ kinetic_energy(unsigned long const, double *, double *);
scalar_t__ malloc(unsigned long const);
int test_kinetic_energy() {
  const unsigned long ndim = 100000;
  double *mtm;
  unsigned long i;
  double kin_eng, kin_eng_test;
  mtm = (double *)malloc(ndim * sizeof(double));
  kin_eng = 0;
  for (i = 0; i < ndim; ++i) {
    mtm[i] = 1.;
    kin_eng += mtm[i] * mtm[i];
  }
  kin_eng *= 0.5;
  kinetic_energy(ndim, mtm, &kin_eng_test);
  if (kin_eng != kin_eng_test) {
    free(mtm);
    return EXIT_FAILURE;
  }
  free(mtm);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<test_kinetic_energy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x186a0,-0x10(%rbp)
mov    $0xc3500,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x186a0,-0x20(%rbp)
jae    11be <test_kinetic_energy+0x8e>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  0xe8c(%rip),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    115e <test_kinetic_energy+0x2e>
movsd  0xe3a(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
mov    $0x186a0,%edi
lea    -0x30(%rbp),%rdx
call   1050 <kinetic_energy@plt>
movsd  -0x28(%rbp),%xmm0
ucomisd -0x30(%rbp),%xmm0
jne    11fd <test_kinetic_energy+0xcd>
jp     11fd <test_kinetic_energy+0xcd>
jmp    1217 <test_kinetic_energy+0xe7>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <test_kinetic_energy+0xfc>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_kinetic_energy>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xc3500,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
xorpd  %xmm1,%xmm1
mov    $0x4,%eax
movabs $0x3ff0000000000000,%rcx
movsd  0xea1(%rip),%xmm0
nop
mov    %rcx,-0x20(%rbx,%rax,8)
addsd  %xmm0,%xmm1
mov    %rcx,-0x18(%rbx,%rax,8)
addsd  %xmm0,%xmm1
mov    %rcx,-0x10(%rbx,%rax,8)
addsd  %xmm0,%xmm1
mov    %rcx,-0x8(%rbx,%rax,8)
addsd  %xmm0,%xmm1
mov    %rcx,(%rbx,%rax,8)
addsd  %xmm0,%xmm1
add    $0x5,%rax
cmp    $0x186a4,%rax
jne    1160 <test_kinetic_energy+0x30>
mulsd  0xe68(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
lea    0x10(%rsp),%rdx
mov    $0x186a0,%edi
mov    %rbx,%rsi
call   1050 <kinetic_energy@plt>
movsd  0x8(%rsp),%xmm0
ucomisd 0x10(%rsp),%xmm0
mov    0x2e15(%rip),%rax
mov    0x2e06(%rip),%r14
cmovne %rax,%r14
cmovp  %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%r14),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @test_kinetic_energy() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca double*
%4 = alloca i64
%5 = alloca double
%6 = alloca double
store i64 100000, i64* %2
%7 = call i64 @malloc(i64 800000)
%8 = inttoptr i64 %7 to double*
store double* %8, double** %3
store double 0.000000e+00, double* %5
store i64 0, i64* %4
br label %9
9: ; preds = %26, %0
%10 = load i64, i64* %4
%11 = icmp ult i64 %10, 100000
br i1 %11, label %12, label %29
12: ; preds = %9
%13 = load double*, double** %3
%14 = load i64, i64* %4
%15 = getelementptr inbounds double, double* %13, i64 %14
store double 1.000000e+00, double* %15
%16 = load double*, double** %3
%17 = load i64, i64* %4
%18 = getelementptr inbounds double, double* %16, i64 %17
%19 = load double, double* %18
%20 = load double*, double** %3
%21 = load i64, i64* %4
%22 = getelementptr inbounds double, double* %20, i64 %21
%23 = load double, double* %22
%24 = load double, double* %5
%25 = call double @llvm.fmuladd.f64(double %19, double %23, double %24)
store double %25, double* %5
br label %26
26: ; preds = %12
%27 = load i64, i64* %4
%28 = add i64 %27, 1
store i64 %28, i64* %4
br label %9
29: ; preds = %9
%30 = load double, double* %5
%31 = fmul double %30, 5.000000e-01
store double %31, double* %5
%32 = load double*, double** %3
%33 = call i32 @kinetic_energy(i64 100000, double* %32, double* %6)
%34 = load double, double* %5
%35 = load double, double* %6
%36 = fcmp une double %34, %35
br i1 %36, label %37, label %41
37: ; preds = %29
%38 = load double*, double** %3
%39 = call i32 @free(double* %38)
%40 = load i32, i32* @EXIT_FAILURE
store i32 %40, i32* %1
br label %45
41: ; preds = %29
%42 = load double*, double** %3
%43 = call i32 @free(double* %42)
%44 = load i32, i32* @EXIT_SUCCESS
store i32 %44, i32* %1
br label %45
45: ; preds = %41, %37
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @test_kinetic_energy() {
%1 = alloca double
%2 = bitcast double* %1 to i8*
%3 = tail call i64 @malloc(i64 800000)
%4 = inttoptr i64 %3 to double*
br label %5
5: ; preds = %5, %0
%6 = phi double [ 0.000000e+00, %0 ], [ %21, %5 ]
%7 = phi i64 [ 0, %0 ], [ %22, %5 ]
%8 = getelementptr inbounds double, double* %4, i64 %7
store double 1.000000e+00, double* %8
%9 = fadd double %6, 1.000000e+00
%10 = add nuw nsw i64 %7, 1
%11 = getelementptr inbounds double, double* %4, i64 %10
store double 1.000000e+00, double* %11
%12 = fadd double %9, 1.000000e+00
%13 = add nuw nsw i64 %7, 2
%14 = getelementptr inbounds double, double* %4, i64 %13
store double 1.000000e+00, double* %14
%15 = fadd double %12, 1.000000e+00
%16 = add nuw nsw i64 %7, 3
%17 = getelementptr inbounds double, double* %4, i64 %16
store double 1.000000e+00, double* %17
%18 = fadd double %15, 1.000000e+00
%19 = add nuw nsw i64 %7, 4
%20 = getelementptr inbounds double, double* %4, i64 %19
store double 1.000000e+00, double* %20
%21 = fadd double %18, 1.000000e+00
%22 = add nuw nsw i64 %7, 5
%23 = icmp eq i64 %22, 100000
br i1 %23, label %24, label %5
24: ; preds = %5
%25 = fmul double %21, 5.000000e-01
%26 = call i32 @kinetic_energy(i64 100000, double* %4, double* %1)
%27 = load double, double* %1
%28 = fcmp une double %25, %27
%29 = call i32 @free(double* %4)
%30 = load i32, i32* @EXIT_FAILURE
%31 = load i32, i32* @EXIT_SUCCESS
%32 = select i1 %28, i32 %30, i32 %31
ret i32 %32
}