#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;
scalar_t__ clock();
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *, double);
int main() {
  clock_t begin, end;
  double time_spent;
  begin = clock();
  int i = 0;
  while (i++ < 10000000) {
  }
  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("On stack %.3lf seconds\n", time_spent);
  begin = clock();
  int *ptr = malloc(1 * sizeof(int));
  while ((*ptr) < 10000000) {
    (*ptr) += 1;
  }
  free(ptr);
  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("On heap %.3lf seconds\n", time_spent);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
cmp    $0x989680,%eax
jge    117c <main+0x3c>
jmp    1161 <main+0x21>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2e4d(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
lea    0xe50(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x10(%rbp)
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpl   $0x989680,(%rax)
jge    11ee <main+0xae>
mov    -0x30(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    11ce <main+0x8e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2dd2(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
lea    0xded(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <clock@plt>
sub    %rbx,%rax
cvtsi2sd %rax,%xmm0
mov    0x2e84(%rip),%r14
divsd  (%r14),%xmm0
lea    0xe90(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%rbx
mov    $0x4,%edi
call   1060 <malloc@plt>
cmpl   $0x98967f,(%rax)
jg     1197 <main+0x57>
movl   $0x989680,(%rax)
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1040 <clock@plt>
sub    %rbx,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r14),%xmm0
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str = [24 x i8] c"On stack %.3lf seconds\0A\00"
@.str.1 = [23 x i8] c"On heap %.3lf seconds\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca double
%5 = alloca i32
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i64 (...) @clock()
store i64 %7, i64* %2
store i32 0, i32* %5
br label %8
8: ; preds = %12, %0
%9 = load i32, i32* %5
%10 = add nsw i32 %9, 1
store i32 %10, i32* %5
%11 = icmp slt i32 %9, 10000000
br i1 %11, label %12, label %13
12: ; preds = %8
br label %8
13: ; preds = %8
%14 = call i64 (...) @clock()
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = load i64, i64* %2
%17 = sub nsw i64 %15, %16
%18 = sitofp i64 %17 to double
%19 = load double, double* @CLOCKS_PER_SEC
%20 = fdiv double %18, %19
store double %20, double* %4
%21 = load double, double* %4
%22 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), double %21)
%23 = call i64 (...) @clock()
store i64 %23, i64* %2
%24 = call i32* @malloc(i32 4)
store i32* %24, i32** %6
br label %25
25: ; preds = %29, %13
%26 = load i32*, i32** %6
%27 = load i32, i32* %26
%28 = icmp slt i32 %27, 10000000
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32*, i32** %6
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 1
store i32 %32, i32* %30
br label %25
33: ; preds = %25
%34 = load i32*, i32** %6
%35 = call i32 @free(i32* %34)
%36 = call i64 (...) @clock()
store i64 %36, i64* %3
%37 = load i64, i64* %3
%38 = load i64, i64* %2
%39 = sub nsw i64 %37, %38
%40 = sitofp i64 %39 to double
%41 = load double, double* @CLOCKS_PER_SEC
%42 = fdiv double %40, %41
store double %42, double* %4
%43 = load double, double* %4
%44 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), double %43)
ret i32 0
}

LLVM IR (-O2):
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str = [24 x i8] c"On stack %.3lf seconds\0A\00"
@.str.1 = [23 x i8] c"On heap %.3lf seconds\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @clock()
%2 = tail call i64 (...) @clock()
%3 = sub nsw i64 %2, %1
%4 = sitofp i64 %3 to double
%5 = load double, double* @CLOCKS_PER_SEC
%6 = fdiv double %4, %5
%7 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), double %6)
%8 = tail call i64 (...) @clock()
%9 = tail call i32* @malloc(i32 4)
%10 = load i32, i32* %9
%11 = icmp slt i32 %10, 10000000
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 10000000, i32* %9
br label %13
13: ; preds = %12, %0
%14 = tail call i32 @free(i32* %9)
%15 = tail call i64 (...) @clock()
%16 = sub nsw i64 %15, %8
%17 = sitofp i64 %16 to double
%18 = load double, double* @CLOCKS_PER_SEC
%19 = fdiv double %17, %18
%20 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), double %19)
ret i32 0
}