#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_scalar_mult(unsigned int size) {
  int i;
  double *v = (double *)malloc(size * sizeof(double));
  double a = 0.0;
  if (v == NULL) {
    printf("Out Of Memory: could not allocate space for the array.\n");
    return 0;
  }
  srand((int)time(NULL));
  struct timespec start, end;
  for (i = 0; i < size; i++) {
    v[i] = (double)rand() / (double)(RAND_MAX / 10.0);
  }
  a = (double)rand() / (double)(RAND_MAX / 10.0);
  rapl_init();
  rapl_start();
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  for (i = 0; i < size; i++) {
    v[i] = a * v[i];
  }
  clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  rapl_end();
  rapl_deinit();
  printf("Scalar product result: %f\n", v[0]);
  elapsed_time_hr(start, end, "Double scalar multiplication.");
  free(v);
  return 0;
}


x86 assembly (clang, -O0):
<double_scalar_mult>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120a <double_scalar_mult+0x4a>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1353 <double_scalar_mult+0x193>
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    126f <double_scalar_mult+0xaf>
mov    $0x0,%al
call   10e0 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2da8(%rip),%rax
movsd  (%rax),%xmm1
movsd  0xdb4(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1221 <double_scalar_mult+0x61>
mov    $0x0,%al
call   10e0 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2d66(%rip),%rax
movsd  (%rax),%xmm1
movsd  0xd72(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   10c0 <rapl_init@plt>
mov    $0x0,%al
call   1070 <rapl_start@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1050 <clock_gettime@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    12fb <double_scalar_mult+0x13b>
movsd  -0x20(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12c2 <double_scalar_mult+0x102>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    $0x0,%al
call   10b0 <rapl_end@plt>
mov    $0x0,%al
call   10d0 <rapl_deinit@plt>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
lea    0xd16(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%esi
lea    0xd1d(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double_scalar_mult>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
lea    0x0(,%r14,8),%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12bd <double_scalar_mult+0xed>
mov    %rax,%rbp
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
mov    %r14d,%r12d
test   %r14d,%r14d
je     124f <double_scalar_mult+0x7f>
mov    0x2dd1(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   10f0 <rand@plt>
movsd  (%r15),%xmm0
divsd  0xdcc(%rip),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x0(%rbp,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1220 <double_scalar_mult+0x50>
xor    %eax,%eax
call   10f0 <rand@plt>
mov    %rax,%r15
mov    0x2d88(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,0x10(%rsp)
xor    %eax,%eax
call   10d0 <rapl_init@plt>
xor    %eax,%eax
call   1080 <rapl_start@plt>
mov    0x2d61(%rip),%r13
mov    0x0(%r13),%edi
lea    0x8(%rsp),%rsi
call   1060 <clock_gettime@plt>
test   %r14d,%r14d
je     1399 <double_scalar_mult+0x1c9>
xorps  %xmm0,%xmm0
cvtsi2sd %r15,%xmm0
movsd  0x10(%rsp),%xmm1
divsd  0xd54(%rip),%xmm1
divsd  %xmm1,%xmm0
cmp    $0x4,%r14d
jae    12ce <double_scalar_mult+0xfe>
xor    %eax,%eax
jmp    1380 <double_scalar_mult+0x1b0>
lea    0xd7d(%rip),%rdi
call   1050 <puts@plt>
jmp    13e1 <double_scalar_mult+0x211>
mov    %r12d,%eax
and    $0xfffffffc,%eax
movapd %xmm0,%xmm1
unpcklpd %xmm0,%xmm1
lea    -0x4(%rax),%rcx
mov    %rcx,%rdx
shr    $0x2,%rdx
add    $0x1,%rdx
test   %rcx,%rcx
je     13f2 <double_scalar_mult+0x222>
mov    %rdx,%rsi
and    $0xfffffffffffffffe,%rsi
xor    %ecx,%ecx
nopl   (%rax)
movupd 0x0(%rbp,%rcx,8),%xmm2
movupd 0x10(%rbp,%rcx,8),%xmm3
movupd 0x20(%rbp,%rcx,8),%xmm4
movupd 0x30(%rbp,%rcx,8),%xmm5
mulpd  %xmm1,%xmm2
mulpd  %xmm1,%xmm3
movupd %xmm2,0x0(%rbp,%rcx,8)
movupd %xmm3,0x10(%rbp,%rcx,8)
mulpd  %xmm1,%xmm4
mulpd  %xmm1,%xmm5
movupd %xmm4,0x20(%rbp,%rcx,8)
movupd %xmm5,0x30(%rbp,%rcx,8)
add    $0x8,%rcx
add    $0xfffffffffffffffe,%rsi
jne    1300 <double_scalar_mult+0x130>
test   $0x1,%dl
je     136f <double_scalar_mult+0x19f>
movupd 0x0(%rbp,%rcx,8),%xmm2
movupd 0x10(%rbp,%rcx,8),%xmm3
mulpd  %xmm1,%xmm2
mulpd  %xmm1,%xmm3
movupd %xmm2,0x0(%rbp,%rcx,8)
movupd %xmm3,0x10(%rbp,%rcx,8)
cmp    %r12,%rax
je     1399 <double_scalar_mult+0x1c9>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  0x0(%rbp,%rax,8),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x0(%rbp,%rax,8)
add    $0x1,%rax
cmp    %rax,%r12
jne    1380 <double_scalar_mult+0x1b0>
mov    0x0(%r13),%edi
mov    %rsp,%rsi
call   1060 <clock_gettime@plt>
xor    %eax,%eax
call   10c0 <rapl_end@plt>
xor    %eax,%eax
call   10e0 <rapl_deinit@plt>
movsd  0x0(%rbp),%xmm0
lea    0xc49(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
mov    0x8(%rsp),%edi
mov    (%rsp),%esi
lea    0xc4f(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ecx,%ecx
test   $0x1,%dl
jne    134f <double_scalar_mult+0x17f>
jmp    136f <double_scalar_mult+0x19f>

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

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