#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;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ elapsed_time_hr(struct timespec, struct timespec, char *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ printf(char *, ...);
int mem_calloc(unsigned int size) {
  struct timespec start, end;
  int nbytes = size * 1048576;
  int nelements = nbytes / sizeof(int);
  clock_gettime(CLOCK_MONOTONIC, &start);
  int *array = (int *)calloc(nelements, sizeof(int));
  clock_gettime(CLOCK_MONOTONIC, &end);
  if (array == NULL) {
    printf("Out Of Memory: could not allocate space for the array.\n");
    return 0;
  }
  elapsed_time_hr(start, end, "Allocate memory");
  printf("Finished callocating %d MB memory.\n\n", size);
  free(array);
  return 0;
}


x86 assembly (clang, -O0):
<mem_calloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x14,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shr    $0x2,%rax
mov    %eax,-0x20(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x20(%rbp),%edi
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <clock_gettime@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11c9 <mem_calloc+0x79>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fc <mem_calloc+0xac>
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    0xe62(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
mov    -0x8(%rbp),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_calloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    %edi,%ebx
shl    $0x14,%ebx
sar    $0x2,%ebx
mov    0x2e6e(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0x8(%rsp),%rsi
call   1060 <clock_gettime@plt>
mov    %ebx,%edi
mov    $0x4,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    0x0(%rbp),%edi
mov    %rsp,%rsi
call   1060 <clock_gettime@plt>
test   %rbx,%rbx
je     11d4 <mem_calloc+0x74>
mov    0x8(%rsp),%edi
mov    (%rsp),%esi
lea    0xe4c(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
lea    0xe50(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
jmp    11e0 <mem_calloc+0x80>
lea    0xe5a(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [56 x i8] c"Out Of Memory: could not allocate space for the array.\0A\00"
@.str.1 = [16 x i8] c"Allocate memory\00"
@.str.2 = [37 x i8] c"Finished callocating %d MB memory.\0A\0A\00"
define i32 @mem_calloc(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = mul i32 %9, 1048576
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = udiv i64 %12, 4
%14 = trunc i64 %13 to i32
store i32 %14, i32* %7
%15 = load i32, i32* @CLOCK_MONOTONIC
%16 = call i32 @clock_gettime(i32 %15, %struct.timespec* %4)
%17 = load i32, i32* %7
%18 = call i64 @calloc(i32 %17, i32 4)
%19 = inttoptr i64 %18 to i32*
store i32* %19, i32** %8
%20 = load i32, i32* @CLOCK_MONOTONIC
%21 = call i32 @clock_gettime(i32 %20, %struct.timespec* %5)
%22 = load i32*, i32** %8
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %1
%25 = 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 %36
26: ; preds = %1
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @elapsed_time_hr(i32 %28, i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32*, i32** %8
%35 = call i32 @free(i32* %34)
store i32 0, i32* %2
br label %36
36: ; preds = %26, %24
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [16 x i8] c"Allocate memory\00"
@.str.2 = [37 x i8] c"Finished callocating %d MB memory.\0A\0A\00"
@str = [55 x i8] c"Out Of Memory: could not allocate space for the array.\00"
define i32 @mem_calloc(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = shl i32 %0, 20
%7 = ashr exact i32 %6, 2
%8 = load i32, i32* @CLOCK_MONOTONIC
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %2)
%10 = call i64 @calloc(i32 %7, i32 4)
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i32 @clock_gettime(i32 %11, %struct.timespec* %3)
%13 = icmp eq i64 %10, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
br label %25
16: ; preds = %1
%17 = inttoptr i64 %10 to i32*
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @elapsed_time_hr(i32 %19, i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%24 = call i32 @free(i32* %17)
br label %25
25: ; preds = %16, %14
ret i32 0
}