#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(float *);
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();
float sqrt(float);
int /*<<< orphan*/ srand(int);
scalar_t__ time(int /*<<< orphan*/ *);
int float_norm(unsigned int size) {
  int i;
  float *v = (float *)malloc(size * sizeof(float));
  float sum = 0.0, norm = 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] = (float)rand() / (float)(RAND_MAX / 10.0);
  }
  rapl_init();
  rapl_start();
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  for (i = 0; i < size; i++) {
    sum = sum + (v[i] * v[i]);
  }
  norm = sqrt(sum);
  clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  rapl_end();
  rapl_deinit();
  elapsed_time_hr(start, end, "Float vector norm.");
  printf("Norm = %f\n", norm);
  free(v);
  return 0;
}


x86 assembly (clang, -O0):
<float_norm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1222 <float_norm+0x52>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1360 <float_norm+0x190>
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    128b <float_norm+0xbb>
mov    $0x0,%al
call   10f0 <rand@plt>
cvtsi2ss %rax,%xmm0
mov    0x2d90(%rip),%rax
movsd  (%rax),%xmm1
movsd  0xd9c(%rip),%xmm2
divsd  %xmm2,%xmm1
cvtsd2ss %xmm1,%xmm1
divss  %xmm1,%xmm0
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1239 <float_norm+0x69>
mov    $0x0,%al
call   10c0 <rapl_init@plt>
mov    $0x0,%al
call   1070 <rapl_start@plt>
mov    0x2d40(%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    12f8 <float_norm+0x128>
movss  -0x1c(%rbp),%xmm1
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12b2 <float_norm+0xe2>
movss  -0x1c(%rbp),%xmm0
call   10d0 <sqrt@plt>
movss  %xmm0,-0x20(%rbp)
mov    0x2cd2(%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   10e0 <rapl_deinit@plt>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%esi
lea    0xd0c(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
movss  -0x20(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xd0a(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@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):
<float_norm>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
lea    0x0(,%r14,4),%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12a2 <float_norm+0xc2>
mov    %rax,%r15
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
test   %r14d,%r14d
je     12b3 <float_norm+0xd3>
mov    %r14d,%r13d
mov    0x2dbe(%rip),%r12
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1100 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movsd  (%r12),%xmm1
divsd  0xdb3(%rip),%xmm1
cvtsd2ss %xmm1,%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,(%r15,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1230 <float_norm+0x50>
xor    %eax,%eax
call   10d0 <rapl_init@plt>
xor    %eax,%eax
call   1080 <rapl_start@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1060 <clock_gettime@plt>
test   %r14d,%r14d
je     12d2 <float_norm+0xf2>
lea    -0x1(%r13),%rcx
mov    %r13d,%eax
and    $0x3,%eax
cmp    $0x3,%rcx
jae    12d7 <float_norm+0xf7>
xorps  %xmm0,%xmm0
xor    %ecx,%ecx
jmp    1324 <float_norm+0x144>
lea    0xd7d(%rip),%rdi
call   1050 <puts@plt>
jmp    13b3 <float_norm+0x1d3>
xor    %eax,%eax
call   10d0 <rapl_init@plt>
xor    %eax,%eax
call   1080 <rapl_start@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1060 <clock_gettime@plt>
xorps  %xmm0,%xmm0
jmp    1346 <float_norm+0x166>
and    $0xfffffffc,%r13d
xorps  %xmm0,%xmm0
xor    %ecx,%ecx
movss  (%r15,%rcx,4),%xmm1
movss  0x4(%r15,%rcx,4),%xmm2
mulss  %xmm1,%xmm1
addss  %xmm0,%xmm1
mulss  %xmm2,%xmm2
addss  %xmm1,%xmm2
movss  0x8(%r15,%rcx,4),%xmm1
mulss  %xmm1,%xmm1
addss  %xmm2,%xmm1
movss  0xc(%r15,%rcx,4),%xmm0
mulss  %xmm0,%xmm0
addss  %xmm1,%xmm0
add    $0x4,%rcx
cmp    %rcx,%r13
jne    12e0 <float_norm+0x100>
test   %rax,%rax
je     1346 <float_norm+0x166>
lea    (%r15,%rcx,4),%rcx
xor    %edx,%edx
nop
movss  (%rcx,%rdx,4),%xmm1
mulss  %xmm1,%xmm1
addss  %xmm1,%xmm0
add    $0x1,%rdx
cmp    %rdx,%rax
jne    1330 <float_norm+0x150>
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jb     1354 <float_norm+0x174>
sqrtss %xmm0,%xmm0
jmp    1359 <float_norm+0x179>
call   10e0 <sqrt@plt>
movss  %xmm0,0x4(%rsp)
mov    0x2c7a(%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   10f0 <rapl_deinit@plt>
mov    (%rsp),%edi
mov    0x8(%rsp),%esi
lea    0xc7a(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
movss  0x4(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xc77(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
mov    %r15,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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 = [19 x i8] c"Float vector norm.\00"
@.str.2 = [11 x i8] c"Norm = %f\0A\00"
define i32 @float_norm(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca float*
%6 = alloca float
%7 = alloca float
%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, 4
%13 = trunc i64 %12 to i32
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to float*
store float* %15, float** %5
store float 0.000000e+00, float* %6
store float 0.000000e+00, float* %7
%16 = load float*, float** %5
%17 = icmp eq float* %16, null
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = 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 %84
20: ; preds = %1
%21 = call i64 @time(i32* null)
%22 = trunc i64 %21 to i32
%23 = call i32 @srand(i32 %22)
store i32 0, i32* %4
br label %24
24: ; preds = %39, %20
%25 = load i32, i32* %4
%26 = load i32, i32* %3
%27 = icmp ult i32 %25, %26
br i1 %27, label %28, label %42
28: ; preds = %24
%29 = call i64 (...) @rand()
%30 = sitofp i64 %29 to float
%31 = load double, double* @RAND_MAX
%32 = fdiv double %31, 1.000000e+01
%33 = fptrunc double %32 to float
%34 = fdiv float %30, %33
%35 = load float*, float** %5
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds float, float* %35, i64 %37
store float %34, float* %38
br label %39
39: ; preds = %28
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %24
42: ; preds = %24
%43 = call i32 (...) @rapl_init()
%44 = call i32 (...) @rapl_start()
%45 = load i32, i32* @CLOCK_MONOTONIC_RAW
%46 = call i32 @clock_gettime(i32 %45, %struct.timespec* %8)
store i32 0, i32* %4
br label %47
47: ; preds = %64, %42
%48 = load i32, i32* %4
%49 = load i32, i32* %3
%50 = icmp ult i32 %48, %49
br i1 %50, label %51, label %67
51: ; preds = %47
%52 = load float, float* %6
%53 = load float*, float** %5
%54 = load i32, i32* %4
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds float, float* %53, i64 %55
%57 = load float, float* %56
%58 = load float*, float** %5
%59 = load i32, i32* %4
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds float, float* %58, i64 %60
%62 = load float, float* %61
%63 = call float @llvm.fmuladd.f32(float %57, float %62, float %52)
store float %63, float* %6
br label %64
64: ; preds = %51
%65 = load i32, i32* %4
%66 = add nsw i32 %65, 1
store i32 %66, i32* %4
br label %47
67: ; preds = %47
%68 = load float, float* %6
%69 = call float @sqrt(float %68)
store float %69, float* %7
%70 = load i32, i32* @CLOCK_MONOTONIC_RAW
%71 = call i32 @clock_gettime(i32 %70, %struct.timespec* %9)
%72 = call i32 (...) @rapl_end()
%73 = call i32 (...) @rapl_deinit()
%74 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%75 = load i32, i32* %74
%76 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%77 = load i32, i32* %76
%78 = call i32 @elapsed_time_hr(i32 %75, i32 %77, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%79 = load float, float* %7
%80 = fpext float %79 to double
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), double %80)
%82 = load float*, float** %5
%83 = call i32 @free(float* %82)
store i32 0, i32* %2
br label %84
84: ; preds = %67, %18
%85 = load i32, i32* %2
ret i32 %85
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@RAND_MAX = global double 0.000000e+00
@CLOCK_MONOTONIC_RAW = global i32 0
@.str.1 = [19 x i8] c"Float vector norm.\00"
@.str.2 = [11 x i8] c"Norm = %f\0A\00"
@str = [55 x i8] c"Out Of Memory: could not allocate space for the array.\00"
define i32 @float_norm(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = shl i32 %0, 2
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to float*
%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 %98
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 %63, label %17
17: ; preds = %10
%18 = zext i32 %0 to i64
br label %19
19: ; preds = %17, %19
%20 = phi i64 [ 0, %17 ], [ %28, %19 ]
%21 = tail call i64 (...) @rand()
%22 = sitofp i64 %21 to float
%23 = load double, double* @RAND_MAX
%24 = fdiv double %23, 1.000000e+01
%25 = fptrunc double %24 to float
%26 = fdiv float %22, %25
%27 = getelementptr inbounds float, float* %6, i64 %20
store float %26, float* %27
%28 = add nuw nsw i64 %20, 1
%29 = icmp eq i64 %28, %18
br i1 %29, label %30, label %19
30: ; preds = %19
%31 = tail call i32 (...) @rapl_init()
%32 = tail call i32 (...) @rapl_start()
%33 = load i32, i32* @CLOCK_MONOTONIC_RAW
%34 = call i32 @clock_gettime(i32 %33, %struct.timespec* %2)
br i1 %16, label %83, label %35
35: ; preds = %30
%36 = add nsw i64 %18, -1
%37 = and i64 %18, 3
%38 = icmp ult i64 %36, 3
br i1 %38, label %68, label %39
39: ; preds = %35
%40 = and i64 %18, 4294967292
br label %41
41: ; preds = %41, %39
%42 = phi i64 [ 0, %39 ], [ %60, %41 ]
%43 = phi float [ 0.000000e+00, %39 ], [ %59, %41 ]
%44 = phi i64 [ 0, %39 ], [ %61, %41 ]
%45 = getelementptr inbounds float, float* %6, i64 %42
%46 = load float, float* %45
%47 = call float @llvm.fmuladd.f32(float %46, float %46, float %43)
%48 = or i64 %42, 1
%49 = getelementptr inbounds float, float* %6, i64 %48
%50 = load float, float* %49
%51 = call float @llvm.fmuladd.f32(float %50, float %50, float %47)
%52 = or i64 %42, 2
%53 = getelementptr inbounds float, float* %6, i64 %52
%54 = load float, float* %53
%55 = call float @llvm.fmuladd.f32(float %54, float %54, float %51)
%56 = or i64 %42, 3
%57 = getelementptr inbounds float, float* %6, i64 %56
%58 = load float, float* %57
%59 = call float @llvm.fmuladd.f32(float %58, float %58, float %55)
%60 = add nuw nsw i64 %42, 4
%61 = add i64 %44, 4
%62 = icmp eq i64 %61, %40
br i1 %62, label %68, label %41
63: ; preds = %10
%64 = tail call i32 (...) @rapl_init()
%65 = tail call i32 (...) @rapl_start()
%66 = load i32, i32* @CLOCK_MONOTONIC_RAW
%67 = call i32 @clock_gettime(i32 %66, %struct.timespec* %2)
br label %83
68: ; preds = %41, %35
%69 = phi float [ undef, %35 ], [ %59, %41 ]
%70 = phi i64 [ 0, %35 ], [ %60, %41 ]
%71 = phi float [ 0.000000e+00, %35 ], [ %59, %41 ]
%72 = icmp eq i64 %37, 0
br i1 %72, label %83, label %73
73: ; preds = %68, %73
%74 = phi i64 [ %80, %73 ], [ %70, %68 ]
%75 = phi float [ %79, %73 ], [ %71, %68 ]
%76 = phi i64 [ %81, %73 ], [ 0, %68 ]
%77 = getelementptr inbounds float, float* %6, i64 %74
%78 = load float, float* %77
%79 = call float @llvm.fmuladd.f32(float %78, float %78, float %75)
%80 = add nuw nsw i64 %74, 1
%81 = add i64 %76, 1
%82 = icmp eq i64 %81, %37
br i1 %82, label %83, label %73
83: ; preds = %68, %73, %63, %30
%84 = phi float [ 0.000000e+00, %30 ], [ 0.000000e+00, %63 ], [ %69, %68 ], [ %79, %73 ]
%85 = call float @sqrt(float %84)
%86 = load i32, i32* @CLOCK_MONOTONIC_RAW
%87 = call i32 @clock_gettime(i32 %86, %struct.timespec* %3)
%88 = call i32 (...) @rapl_end()
%89 = call i32 (...) @rapl_deinit()
%90 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%91 = load i32, i32* %90
%92 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%93 = load i32, i32* %92
%94 = call i32 @elapsed_time_hr(i32 %91, i32 %93, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%95 = fpext float %85 to double
%96 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), double %95)
%97 = call i32 @free(float* %6)
br label %98
98: ; preds = %83, %8
ret i32 0
}