#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 */
typedef scalar_t__ clock_t;

/* Variables and functions */
double CLOCKS_PER_SEC;
int atoi(char *);
scalar_t__ clock();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ prefix_sum_chal(int *, int *, int const);
int /*<<< orphan*/ prefix_sum_goal(int *, int *, int const);
int /*<<< orphan*/ prefix_sum_init(int *, int const);
int /*<<< orphan*/ printf(char *, int const);
int /*<<< orphan*/ stderr;
int sum_array(int *, int const);
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Syntax: %s <array_size> <kernel>\n", argv[0]);
    return 1;
  } else {
    clock_t begin, end;
    double time_spent;
    const int N = atoi(argv[1]);
    const int K = atoi(argv[2]);
    int *v = (int *)malloc(2 * N * sizeof(int));
    prefix_sum_init(v, N);
    begin = clock();
    printf("Kernel %d\n", K);
    switch (K) {
    case 1:
      prefix_sum_chal(v, v + N, N);
      break;
    case 2:
      prefix_sum_goal(v, v + N, N);
    }
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("Time spent = %lf\n", time_spent);
    printf("Sum = %d\n", sum_array(v, N));
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d8 <main+0x48>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x157>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
shl    $0x3,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   10a0 <prefix_sum_init@plt>
xor    %eax,%eax
call   1050 <clock@plt>
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%esi
lea    0xdfa(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x1,%eax
je     1252 <main+0xc2>
jmp    1241 <main+0xb1>
mov    -0x3c(%rbp),%eax
sub    $0x2,%eax
je     1272 <main+0xe2>
jmp    128d <main+0xfd>
mov    -0x38(%rbp),%rdi
mov    -0x38(%rbp),%rsi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1030 <prefix_sum_chal@plt>
jmp    128d <main+0xfd>
mov    -0x38(%rbp),%rdi
mov    -0x38(%rbp),%rsi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1080 <prefix_sum_goal@plt>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2d3c(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
cvttsd2si -0x28(%rbp),%esi
lea    0xd6c(%rip),%rdi
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <sum_array@plt>
mov    %eax,%esi
lea    0xd64(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x1,%edi
jg     11c2 <main+0x32>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xe4d(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%ebp
jmp    127b <main+0xeb>
mov    0x8(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
lea    0x0(,%rbx,8),%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
call   10a0 <prefix_sum_init@plt>
xor    %eax,%eax
call   1050 <clock@plt>
mov    %rax,%r15
lea    0xe20(%rip),%rdi
mov    %ebp,%esi
call   1060 <printf@plt>
cmp    $0x2,%ebp
je     1226 <main+0x96>
cmp    $0x1,%ebp
jne    1237 <main+0xa7>
movslq %ebx,%rax
lea    (%r14,%rax,4),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1030 <prefix_sum_chal@plt>
jmp    1237 <main+0xa7>
movslq %ebx,%rax
lea    (%r14,%rax,4),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1080 <prefix_sum_goal@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <clock@plt>
sub    %r15,%rax
cvtsi2sd %rax,%xmm0
mov    0x2d99(%rip),%rax
divsd  (%rax),%xmm0
cvttsd2si %xmm0,%esi
lea    0xdcf(%rip),%rdi
call   1060 <printf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <sum_array@plt>
lea    0xdcb(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"Syntax: %s <array_size> <kernel>\0A\00"
@.str.1 = [11 x i8] c"Kernel %d\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.2 = [18 x i8] c"Time spent = %lf\0A\00"
@.str.3 = [10 x i8] c"Sum = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 1, i32* %3
br label %74
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %9
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %10
%29 = load i32, i32* %9
%30 = mul nsw i32 2, %29
%31 = sext i32 %30 to i64
%32 = mul i64 %31, 4
%33 = trunc i64 %32 to i32
%34 = call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i32*
store i32* %35, i32** %11
%36 = load i32*, i32** %11
%37 = load i32, i32* %9
%38 = call i32 @prefix_sum_init(i32* %36, i32 %37)
%39 = call i64 (...) @clock()
store i64 %39, i64* %6
%40 = load i32, i32* %10
%41 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %10
switch i32 %42, label %59 [
i32 1, label %43
i32 2, label %51
]
43: ; preds = %20
%44 = load i32*, i32** %11
%45 = load i32*, i32** %11
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = load i32, i32* %9
%50 = call i32 @prefix_sum_chal(i32* %44, i32* %48, i32 %49)
br label %59
51: ; preds = %20
%52 = load i32*, i32** %11
%53 = load i32*, i32** %11
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %53, i64 %55
%57 = load i32, i32* %9
%58 = call i32 @prefix_sum_goal(i32* %52, i32* %56, i32 %57)
br label %59
59: ; preds = %51, %20, %43
%60 = call i64 (...) @clock()
store i64 %60, i64* %7
%61 = load i64, i64* %7
%62 = load i64, i64* %6
%63 = sub nsw i64 %61, %62
%64 = sitofp i64 %63 to double
%65 = load double, double* @CLOCKS_PER_SEC
%66 = fdiv double %64, %65
store double %66, double* %8
%67 = load double, double* %8
%68 = fptosi double %67 to i32
%69 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %68)
%70 = load i32*, i32** %11
%71 = load i32, i32* %9
%72 = call i32 @sum_array(i32* %70, i32 %71)
%73 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %72)
store i32 0, i32* %3
br label %74
74: ; preds = %59, %14
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"Syntax: %s <array_size> <kernel>\0A\00"
@.str.1 = [11 x i8] c"Kernel %d\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.2 = [18 x i8] c"Time spent = %lf\0A\00"
@.str.3 = [10 x i8] c"Sum = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %39
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = shl i32 %11, 3
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
%18 = tail call i32 @prefix_sum_init(i32* %17, i32 %11)
%19 = tail call i64 (...) @clock()
%20 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %14)
switch i32 %14, label %29 [
i32 1, label %21
i32 2, label %25
]
21: ; preds = %8
%22 = sext i32 %11 to i64
%23 = getelementptr inbounds i32, i32* %17, i64 %22
%24 = tail call i32 @prefix_sum_chal(i32* %17, i32* %23, i32 %11)
br label %29
25: ; preds = %8
%26 = sext i32 %11 to i64
%27 = getelementptr inbounds i32, i32* %17, i64 %26
%28 = tail call i32 @prefix_sum_goal(i32* %17, i32* %27, i32 %11)
br label %29
29: ; preds = %25, %8, %21
%30 = tail call i64 (...) @clock()
%31 = sub nsw i64 %30, %19
%32 = sitofp i64 %31 to double
%33 = load double, double* @CLOCKS_PER_SEC
%34 = fdiv double %32, %33
%35 = fptosi double %34 to i32
%36 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = tail call i32 @sum_array(i32* %17, i32 %11)
%38 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %29, %4
%40 = phi i32 [ 1, %4 ], [ 0, %29 ]
ret i32 %40
}