#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_SUCCESS;
int atoi(char *);
int /*<<< orphan*/ free(float *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int recursiveReduce(float *, int);
double seconds();
int main(int argc, char **argv) {
  int size = 1 << 24;
  printf("%s starting reduction with array size %d\n", argv[0], size);
  int blocksize = 512;
  if (argc > 1) {
    blocksize = atoi(argv[1]);
  }
  size_t bytes = size * sizeof(float);
  float *h_idata = (float *)malloc(bytes);
  for (int i = 0; i < size; i++) {
    h_idata[i] = (float)(rand() & 0xFF);
  }
  double iStart = seconds();
  int cpu_sum = recursiveReduce(h_idata, size);
  double iElaps = seconds() - iStart;
  printf("cpu reduce elapsed %f sec cpu_sum: %d\n", iElaps, cpu_sum);
  free(h_idata);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1000000,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x200,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c6 <main+0x56>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    121d <main+0xad>
mov    $0x0,%al
call   1090 <rand@plt>
and    $0xff,%eax
cvtsi2ss %eax,%xmm0
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e6 <main+0x76>
mov    $0x0,%al
call   1040 <seconds@plt>
movsd  %xmm0,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <recursiveReduce@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1040 <seconds@plt>
subsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
mov    -0x3c(%rbp),%esi
lea    0xdd2(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    (%rsi),%rsi
lea    0xe93(%rip),%rdi
mov    $0x1000000,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x4000000,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <rand@plt>
movzbl %al,%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
movss  %xmm0,(%rbx,%rbp,4)
add    $0x1,%rbp
cmp    $0x1000000,%rbp
jne    1190 <main+0x30>
xor    %eax,%eax
call   1040 <seconds@plt>
movsd  %xmm0,(%rsp)
mov    %rbx,%rdi
mov    $0x1000000,%esi
call   1070 <recursiveReduce@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <seconds@plt>
subsd  (%rsp),%xmm0
lea    0xe49(%rip),%rdi
mov    %ebp,%esi
mov    $0x1,%al
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"%s starting reduction with array size %d\0A\00"
@.str.1 = [39 x i8] c"cpu reduce elapsed %f sec cpu_sum: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca float*
%10 = alloca i32
%11 = alloca double
%12 = alloca i32
%13 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 16777216, i32* %6
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = load i32, i32* %6
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %17)
store i32 512, i32* %7
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 1
br i1 %20, label %21, label %26
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %7
br label %26
26: ; preds = %21, %2
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 4
store i64 %29, i64* %8
%30 = load i64, i64* %8
%31 = call i64 @malloc(i64 %30)
%32 = inttoptr i64 %31 to float*
store float* %32, float** %9
store i32 0, i32* %10
br label %33
33: ; preds = %45, %26
%34 = load i32, i32* %10
%35 = load i32, i32* %6
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %48
37: ; preds = %33
%38 = call i32 (...) @rand()
%39 = and i32 %38, 255
%40 = sitofp i32 %39 to float
%41 = load float*, float** %9
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds float, float* %41, i64 %43
store float %40, float* %44
br label %45
45: ; preds = %37
%46 = load i32, i32* %10
%47 = add nsw i32 %46, 1
store i32 %47, i32* %10
br label %33
48: ; preds = %33
%49 = call double (...) @seconds()
store double %49, double* %11
%50 = load float*, float** %9
%51 = load i32, i32* %6
%52 = call i32 @recursiveReduce(float* %50, i32 %51)
store i32 %52, i32* %12
%53 = call double (...) @seconds()
%54 = load double, double* %11
%55 = fsub double %53, %54
store double %55, double* %13
%56 = load double, double* %13
%57 = load i32, i32* %12
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), double %56, i32 %57)
%59 = load float*, float** %9
%60 = call i32 @free(float* %59)
%61 = load i32, i32* @EXIT_SUCCESS
ret i32 %61
}

LLVM IR (-O2):
@.str = [42 x i8] c"%s starting reduction with array size %d\0A\00"
@.str.1 = [39 x i8] c"cpu reduce elapsed %f sec cpu_sum: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %3, i32 16777216)
%5 = tail call i64 @malloc(i64 67108864)
%6 = inttoptr i64 %5 to float*
br label %15
7: ; preds = %15
%8 = tail call double (...) @seconds()
%9 = tail call i32 @recursiveReduce(float* %6, i32 16777216)
%10 = tail call double (...) @seconds()
%11 = fsub double %10, %8
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), double %11, i32 %9)
%13 = tail call i32 @free(float* %6)
%14 = load i32, i32* @EXIT_SUCCESS
ret i32 %14
15: ; preds = %2, %15
%16 = phi i64 [ 0, %2 ], [ %21, %15 ]
%17 = tail call i32 (...) @rand()
%18 = and i32 %17, 255
%19 = sitofp i32 %18 to float
%20 = getelementptr inbounds float, float* %6, i64 %16
store float %19, float* %20
%21 = add nuw nsw i64 %16, 1
%22 = icmp eq i64 %21, 16777216
br i1 %22, label %7, label %15
}