#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 */
struct timespec {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
double RAND_MAX;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ elapsed_time_hr(struct timespec, struct timespec, char *);
int /*<<< orphan*/ free(double *);
scalar_t__ malloc(unsigned int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rand();
int /*<<< orphan*/ rapl_deinit();
int /*<<< orphan*/ rapl_end();
int /*<<< orphan*/ rapl_init();
int /*<<< orphan*/ rapl_start();
int /*<<< orphan*/ srand(int);
scalar_t__ time(int /*<<< orphan*/ *);
int double_dot_product(unsigned int size) {
  int i;
  double *v1 = (double *)malloc(size * sizeof(double));
  double *v2 = (double *)malloc(size * sizeof(double));
  double result = 0.0;
  if (v1 == NULL || v2 == NULL) {
    printf("Out Of Memory: could not allocate space for the two arrays.\n");
    return 0;
  }
  srand((int)time(NULL));
  struct timespec start, end;
  for (i = 0; i < size; i++) {
    v1[i] = (double)rand() / (double)(RAND_MAX / 10.0);
    v2[i] = (double)rand() / (double)(RAND_MAX / 10.0);
  }
  rapl_init();
  rapl_start();
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  for (i = 0; i < size; i++) {
    result = result + v1[i] * v2[i];
  }
  clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  rapl_end();
  rapl_deinit();
  printf("Dot product result: %f\n", result);
  elapsed_time_hr(start, end, "Double dot product.");
  free(v1);
  free(v2);
  return 0;
}


x86 assembly (clang, -O0):
<double_dot_product>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     120d <double_dot_product+0x4d>
cmpq   $0x0,-0x20(%rbp)
jne    1227 <double_dot_product+0x67>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    138b <double_dot_product+0x1cb>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    12c0 <double_dot_product+0x100>
mov    $0x0,%al
call   10e0 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2d8b(%rip),%rax
movsd  (%rax),%xmm1
movsd  0xd97(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    $0x0,%al
call   10e0 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2d57(%rip),%rax
movsd  (%rax),%xmm1
movsd  0xd63(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    123e <double_dot_product+0x7e>
mov    $0x0,%al
call   10c0 <rapl_init@plt>
mov    $0x0,%al
call   1070 <rapl_start@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1050 <clock_gettime@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    132d <double_dot_product+0x16d>
movsd  -0x28(%rbp),%xmm1
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12e7 <double_dot_product+0x127>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    $0x0,%al
call   10b0 <rapl_end@plt>
mov    $0x0,%al
call   10d0 <rapl_deinit@plt>
movsd  -0x28(%rbp),%xmm0
lea    0xcec(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%edi
mov    -0x38(%rbp),%esi
lea    0xcf0(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double_dot_product>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
lea    0x0(,%r15,8),%ebp
mov    %ebp,%edi
call   10b0 <malloc@plt>
mov    %rax,%r12
mov    %ebp,%edi
call   10b0 <malloc@plt>
test   %r12,%r12
je     12d6 <double_dot_product+0x106>
mov    %rax,%r14
test   %rax,%rax
je     12d6 <double_dot_product+0x106>
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
test   %r15d,%r15d
je     12e7 <double_dot_product+0x117>
mov    %r15d,%r13d
mov    0x2db8(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   10f0 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  (%rbx),%xmm1
movsd  0xda5(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,(%r12,%rbp,8)
xor    %eax,%eax
call   10f0 <rand@plt>
movsd  (%rbx),%xmm0
divsd  0xd84(%rip),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,(%r14,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1240 <double_dot_product+0x70>
xor    %eax,%eax
call   10d0 <rapl_init@plt>
xor    %eax,%eax
call   1080 <rapl_start@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1060 <clock_gettime@plt>
test   %r15d,%r15d
je     1306 <double_dot_product+0x136>
lea    -0x1(%r13),%rcx
mov    %r13d,%eax
and    $0x3,%eax
cmp    $0x3,%rcx
jae    130f <double_dot_product+0x13f>
xorpd  %xmm3,%xmm3
xor    %ecx,%ecx
jmp    136f <double_dot_product+0x19f>
lea    0xd57(%rip),%rdi
call   1050 <puts@plt>
jmp    13f5 <double_dot_product+0x225>
xor    %eax,%eax
call   10d0 <rapl_init@plt>
xor    %eax,%eax
call   1080 <rapl_start@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1060 <clock_gettime@plt>
xorpd  %xmm3,%xmm3
jmp    1397 <double_dot_product+0x1c7>
and    $0xfffffffc,%r13d
xorpd  %xmm3,%xmm3
xor    %ecx,%ecx
nopl   0x0(%rax)
movsd  (%r12,%rcx,8),%xmm0
movsd  0x8(%r12,%rcx,8),%xmm1
mulsd  (%r14,%rcx,8),%xmm0
mulsd  0x8(%r14,%rcx,8),%xmm1
addsd  %xmm3,%xmm0
movsd  0x10(%r12,%rcx,8),%xmm2
mulsd  0x10(%r14,%rcx,8),%xmm2
addsd  %xmm0,%xmm1
movsd  0x18(%r12,%rcx,8),%xmm3
mulsd  0x18(%r14,%rcx,8),%xmm3
addsd  %xmm1,%xmm2
addsd  %xmm2,%xmm3
add    $0x4,%rcx
cmp    %rcx,%r13
jne    1320 <double_dot_product+0x150>
test   %rax,%rax
je     1397 <double_dot_product+0x1c7>
lea    (%r14,%rcx,8),%rdx
lea    (%r12,%rcx,8),%rcx
xor    %esi,%esi
xchg   %ax,%ax
movsd  (%rcx,%rsi,8),%xmm0
mulsd  (%rdx,%rsi,8),%xmm0
addsd  %xmm0,%xmm3
add    $0x1,%rsi
cmp    %rsi,%rax
jne    1380 <double_dot_product+0x1b0>
movsd  %xmm3,0x10(%rsp)
mov    0x2c3c(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1060 <clock_gettime@plt>
xor    %eax,%eax
call   10c0 <rapl_end@plt>
xor    %eax,%eax
call   10e0 <rapl_deinit@plt>
lea    0xc43(%rip),%rdi
movsd  0x10(%rsp),%xmm0
mov    $0x1,%al
call   1070 <printf@plt>
mov    (%rsp),%edi
mov    0x8(%rsp),%esi
lea    0xc40(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
mov    %r12,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [61 x i8] c"Out Of Memory: could not allocate space for the two arrays.\0A\00"
@RAND_MAX = global double 0.000000e+00
@CLOCK_MONOTONIC_RAW = global i32 0
@.str.1 = [24 x i8] c"Dot product result: %f\0A\00"
@.str.2 = [20 x i8] c"Double dot product.\00"
define i32 @double_dot_product(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double*
%6 = alloca double*
%7 = alloca double
%8 = alloca %struct.timespec
%9 = alloca %struct.timespec
store i32 %0, i32* %3
%10 = load i32, i32* %3
%11 = zext i32 %10 to i64
%12 = mul i64 %11, 8
%13 = trunc i64 %12 to i32
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to double*
store double* %15, double** %5
%16 = load i32, i32* %3
%17 = zext i32 %16 to i64
%18 = mul i64 %17, 8
%19 = trunc i64 %18 to i32
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to double*
store double* %21, double** %6
store double 0.000000e+00, double* %7
%22 = load double*, double** %5
%23 = icmp eq double* %22, null
br i1 %23, label %27, label %24
24: ; preds = %1
%25 = load double*, double** %6
%26 = icmp eq double* %25, null
br i1 %26, label %27, label %29
27: ; preds = %24, %1
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %100
29: ; preds = %24
%30 = call i64 @time(i32* null)
%31 = trunc i64 %30 to i32
%32 = call i32 @srand(i32 %31)
store i32 0, i32* %4
br label %33
33: ; preds = %56, %29
%34 = load i32, i32* %4
%35 = load i32, i32* %3
%36 = icmp ult i32 %34, %35
br i1 %36, label %37, label %59
37: ; preds = %33
%38 = call i64 (...) @rand()
%39 = sitofp i64 %38 to double
%40 = load double, double* @RAND_MAX
%41 = fdiv double %40, 1.000000e+01
%42 = fdiv double %39, %41
%43 = load double*, double** %5
%44 = load i32, i32* %4
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds double, double* %43, i64 %45
store double %42, double* %46
%47 = call i64 (...) @rand()
%48 = sitofp i64 %47 to double
%49 = load double, double* @RAND_MAX
%50 = fdiv double %49, 1.000000e+01
%51 = fdiv double %48, %50
%52 = load double*, double** %6
%53 = load i32, i32* %4
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds double, double* %52, i64 %54
store double %51, double* %55
br label %56
56: ; preds = %37
%57 = load i32, i32* %4
%58 = add nsw i32 %57, 1
store i32 %58, i32* %4
br label %33
59: ; preds = %33
%60 = call i32 (...) @rapl_init()
%61 = call i32 (...) @rapl_start()
%62 = load i32, i32* @CLOCK_MONOTONIC_RAW
%63 = call i32 @clock_gettime(i32 %62, %struct.timespec* %8)
store i32 0, i32* %4
br label %64
64: ; preds = %81, %59
%65 = load i32, i32* %4
%66 = load i32, i32* %3
%67 = icmp ult i32 %65, %66
br i1 %67, label %68, label %84
68: ; preds = %64
%69 = load double, double* %7
%70 = load double*, double** %5
%71 = load i32, i32* %4
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds double, double* %70, i64 %72
%74 = load double, double* %73
%75 = load double*, double** %6
%76 = load i32, i32* %4
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds double, double* %75, i64 %77
%79 = load double, double* %78
%80 = call double @llvm.fmuladd.f64(double %74, double %79, double %69)
store double %80, double* %7
br label %81
81: ; preds = %68
%82 = load i32, i32* %4
%83 = add nsw i32 %82, 1
store i32 %83, i32* %4
br label %64
84: ; preds = %64
%85 = load i32, i32* @CLOCK_MONOTONIC_RAW
%86 = call i32 @clock_gettime(i32 %85, %struct.timespec* %9)
%87 = call i32 (...) @rapl_end()
%88 = call i32 (...) @rapl_deinit()
%89 = load double, double* %7
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), double %89)
%91 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%92 = load i32, i32* %91
%93 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%94 = load i32, i32* %93
%95 = call i32 @elapsed_time_hr(i32 %92, i32 %94, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%96 = load double*, double** %5
%97 = call i32 @free(double* %96)
%98 = load double*, double** %6
%99 = call i32 @free(double* %98)
store i32 0, i32* %2
br label %100
100: ; preds = %84, %27
%101 = load i32, i32* %2
ret i32 %101
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@RAND_MAX = global double 0.000000e+00
@CLOCK_MONOTONIC_RAW = global i32 0
@.str.1 = [24 x i8] c"Dot product result: %f\0A\00"
@.str.2 = [20 x i8] c"Double dot product.\00"
@str = [60 x i8] c"Out Of Memory: could not allocate space for the two arrays.\00"
define i32 @double_dot_product(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = shl i32 %0, 3
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to double*
%7 = tail call i64 @malloc(i32 %4)
%8 = inttoptr i64 %7 to double*
%9 = icmp eq i64 %5, 0
%10 = icmp eq i64 %7, 0
%11 = select i1 %9, i1 true, i1 %10
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str, i64 0, i64 0))
br label %116
14: ; preds = %1
%15 = tail call i64 @time(i32* null)
%16 = trunc i64 %15 to i32
%17 = tail call i32 @srand(i32 %16)
%18 = bitcast %struct.timespec* %2 to i8*
%19 = bitcast %struct.timespec* %3 to i8*
%20 = icmp eq i32 %0, 0
br i1 %20, label %80, label %21
21: ; preds = %14
%22 = zext i32 %0 to i64
br label %23
23: ; preds = %21, %23
%24 = phi i64 [ 0, %21 ], [ %37, %23 ]
%25 = tail call i64 (...) @rand()
%26 = sitofp i64 %25 to double
%27 = load double, double* @RAND_MAX
%28 = fdiv double %27, 1.000000e+01
%29 = fdiv double %26, %28
%30 = getelementptr inbounds double, double* %6, i64 %24
store double %29, double* %30
%31 = tail call i64 (...) @rand()
%32 = sitofp i64 %31 to double
%33 = load double, double* @RAND_MAX
%34 = fdiv double %33, 1.000000e+01
%35 = fdiv double %32, %34
%36 = getelementptr inbounds double, double* %8, i64 %24
store double %35, double* %36
%37 = add nuw nsw i64 %24, 1
%38 = icmp eq i64 %37, %22
br i1 %38, label %39, label %23
39: ; preds = %23
%40 = tail call i32 (...) @rapl_init()
%41 = tail call i32 (...) @rapl_start()
%42 = load i32, i32* @CLOCK_MONOTONIC_RAW
%43 = call i32 @clock_gettime(i32 %42, %struct.timespec* %2)
br i1 %20, label %102, label %44
44: ; preds = %39
%45 = add nsw i64 %22, -1
%46 = and i64 %22, 3
%47 = icmp ult i64 %45, 3
br i1 %47, label %85, label %48
48: ; preds = %44
%49 = and i64 %22, 4294967292
br label %50
50: ; preds = %50, %48
%51 = phi i64 [ 0, %48 ], [ %77, %50 ]
%52 = phi double [ 0.000000e+00, %48 ], [ %76, %50 ]
%53 = phi i64 [ 0, %48 ], [ %78, %50 ]
%54 = getelementptr inbounds double, double* %6, i64 %51
%55 = load double, double* %54
%56 = getelementptr inbounds double, double* %8, i64 %51
%57 = load double, double* %56
%58 = call double @llvm.fmuladd.f64(double %55, double %57, double %52)
%59 = or i64 %51, 1
%60 = getelementptr inbounds double, double* %6, i64 %59
%61 = load double, double* %60
%62 = getelementptr inbounds double, double* %8, i64 %59
%63 = load double, double* %62
%64 = call double @llvm.fmuladd.f64(double %61, double %63, double %58)
%65 = or i64 %51, 2
%66 = getelementptr inbounds double, double* %6, i64 %65
%67 = load double, double* %66
%68 = getelementptr inbounds double, double* %8, i64 %65
%69 = load double, double* %68
%70 = call double @llvm.fmuladd.f64(double %67, double %69, double %64)
%71 = or i64 %51, 3
%72 = getelementptr inbounds double, double* %6, i64 %71
%73 = load double, double* %72
%74 = getelementptr inbounds double, double* %8, i64 %71
%75 = load double, double* %74
%76 = call double @llvm.fmuladd.f64(double %73, double %75, double %70)
%77 = add nuw nsw i64 %51, 4
%78 = add i64 %53, 4
%79 = icmp eq i64 %78, %49
br i1 %79, label %85, label %50
80: ; preds = %14
%81 = tail call i32 (...) @rapl_init()
%82 = tail call i32 (...) @rapl_start()
%83 = load i32, i32* @CLOCK_MONOTONIC_RAW
%84 = call i32 @clock_gettime(i32 %83, %struct.timespec* %2)
br label %102
85: ; preds = %50, %44
%86 = phi double [ undef, %44 ], [ %76, %50 ]
%87 = phi i64 [ 0, %44 ], [ %77, %50 ]
%88 = phi double [ 0.000000e+00, %44 ], [ %76, %50 ]
%89 = icmp eq i64 %46, 0
br i1 %89, label %102, label %90
90: ; preds = %85, %90
%91 = phi i64 [ %99, %90 ], [ %87, %85 ]
%92 = phi double [ %98, %90 ], [ %88, %85 ]
%93 = phi i64 [ %100, %90 ], [ 0, %85 ]
%94 = getelementptr inbounds double, double* %6, i64 %91
%95 = load double, double* %94
%96 = getelementptr inbounds double, double* %8, i64 %91
%97 = load double, double* %96
%98 = call double @llvm.fmuladd.f64(double %95, double %97, double %92)
%99 = add nuw nsw i64 %91, 1
%100 = add i64 %93, 1
%101 = icmp eq i64 %100, %46
br i1 %101, label %102, label %90
102: ; preds = %85, %90, %80, %39
%103 = phi double [ 0.000000e+00, %39 ], [ 0.000000e+00, %80 ], [ %86, %85 ], [ %98, %90 ]
%104 = load i32, i32* @CLOCK_MONOTONIC_RAW
%105 = call i32 @clock_gettime(i32 %104, %struct.timespec* %3)
%106 = call i32 (...) @rapl_end()
%107 = call i32 (...) @rapl_deinit()
%108 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), double %103)
%109 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%110 = load i32, i32* %109
%111 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%112 = load i32, i32* %111
%113 = call i32 @elapsed_time_hr(i32 %110, i32 %112, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%114 = call i32 @free(double* %6)
%115 = call i32 @free(double* %8)
br label %116
116: ; preds = %102, %12
ret i32 0
}