#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 */
scalar_t__ RAND_MAX;
scalar_t__ calloc(int, int);
double omp_get_wtime();
int /*<<< orphan*/ printf(char *, int, double);
double rand();
int /*<<< orphan*/ srand(int);
int main(void) {
  double *A, media = 0.0f;
  int N = 20000000;
  double t0, t1, tdelta;
  int i;
  A = (double *)calloc(20000000, sizeof(double));
  srand(1313);
  for (i = 0; i < N; i++)
    A[i] = rand() / (double)RAND_MAX;
  t0 = omp_get_wtime();
#pragma omp parallel for reduction(+ : media)
  for (i = 0; i < N; i++)
    media += A[i];
  media = media / N;
  t1 = omp_get_wtime();
  tdelta = t1 - t0;
  printf("Media de %d numeros em tempo: %.4f segundos.\n", N, tdelta);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x1312d00,-0x1c(%rbp)
mov    $0x1312d00,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x521,%edi
call   1050 <srand@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d0 <main+0x80>
mov    $0x0,%al
call   1070 <rand@plt>
mov    0x2e3c(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1192 <main+0x42>
mov    $0x0,%al
call   1030 <omp_get_wtime@plt>
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1214 <main+0xc4>
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11e3 <main+0x93>
movsd  -0x18(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <omp_get_wtime@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x1c(%rbp),%esi
movsd  -0x38(%rbp),%xmm0
lea    0xdaf(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x1312d00,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,%r15
mov    $0x521,%edi
call   1050 <srand@plt>
mov    0x2e6c(%rip),%r14
xor    %ebx,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   1070 <rand@plt>
xorps  %xmm1,%xmm1
cvtsi2sdq (%r14),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,(%r15,%rbx,8)
add    $0x1,%rbx
cmp    $0x1312d00,%rbx
jne    1180 <main+0x30>
xor    %eax,%eax
call   1030 <omp_get_wtime@plt>
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1030 <omp_get_wtime@plt>
subsd  0x8(%rsp),%xmm0
lea    0xe39(%rip),%rdi
mov    $0x1312d00,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
@.str = [46 x i8] c"Media de %d numeros em tempo: %.4f segundos.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double*
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32
store i32 0, i32* %1
store double 0.000000e+00, double* %3
store i32 20000000, i32* %4
%9 = call i64 @calloc(i32 20000000, i32 8)
%10 = inttoptr i64 %9 to double*
store double* %10, double** %2
%11 = call i32 @srand(i32 1313)
store i32 0, i32* %8
br label %12
12: ; preds = %25, %0
%13 = load i32, i32* %8
%14 = load i32, i32* %4
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %28
16: ; preds = %12
%17 = call double (...) @rand()
%18 = load i64, i64* @RAND_MAX
%19 = sitofp i64 %18 to double
%20 = fdiv double %17, %19
%21 = load double*, double** %2
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds double, double* %21, i64 %23
store double %20, double* %24
br label %25
25: ; preds = %16
%26 = load i32, i32* %8
%27 = add nsw i32 %26, 1
store i32 %27, i32* %8
br label %12
28: ; preds = %12
%29 = call double (...) @omp_get_wtime()
store double %29, double* %5
store i32 0, i32* %8
br label %30
30: ; preds = %42, %28
%31 = load i32, i32* %8
%32 = load i32, i32* %4
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %45
34: ; preds = %30
%35 = load double*, double** %2
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds double, double* %35, i64 %37
%39 = load double, double* %38
%40 = load double, double* %3
%41 = fadd double %40, %39
store double %41, double* %3
br label %42
42: ; preds = %34
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %30
45: ; preds = %30
%46 = load double, double* %3
%47 = load i32, i32* %4
%48 = sitofp i32 %47 to double
%49 = fdiv double %46, %48
store double %49, double* %3
%50 = call double (...) @omp_get_wtime()
store double %50, double* %6
%51 = load double, double* %6
%52 = load double, double* %5
%53 = fsub double %51, %52
store double %53, double* %7
%54 = load i32, i32* %4
%55 = load double, double* %7
%56 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %54, double %55)
ret i32 0
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
@.str = [46 x i8] c"Media de %d numeros em tempo: %.4f segundos.\0A\00"
define i32 @main() {
%1 = tail call i64 @calloc(i32 20000000, i32 8)
%2 = inttoptr i64 %1 to double*
%3 = tail call i32 @srand(i32 1313)
br label %4
4: ; preds = %0, %4
%5 = phi i64 [ 0, %0 ], [ %11, %4 ]
%6 = tail call double (...) @rand()
%7 = load i64, i64* @RAND_MAX
%8 = sitofp i64 %7 to double
%9 = fdiv double %6, %8
%10 = getelementptr inbounds double, double* %2, i64 %5
store double %9, double* %10
%11 = add nuw nsw i64 %5, 1
%12 = icmp eq i64 %11, 20000000
br i1 %12, label %13, label %4
13: ; preds = %4
%14 = tail call double (...) @omp_get_wtime()
%15 = tail call double (...) @omp_get_wtime()
%16 = fsub double %15, %14
%17 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 20000000, double %16)
ret i32 0
}