#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 double time_t;

/* Variables and functions */
double CLOCKS_PER_SEC;
double clock();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ stderr;
void *malloc_sample(void *tid) {
  time_t t = clock();
  char *ptr = malloc(1000 * sizeof(char));
  if (!ptr) {
    fprintf(stderr, "Can't allocate memory!");
    exit(1);
  }
  free(ptr);
  printf("malloc %lf\n", (double)((double)clock() - t) / CLOCKS_PER_SEC);
  return NULL;
}


x86 assembly (clang, -O0):
<malloc_sample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <clock@plt>
movsd  %xmm0,-0x10(%rbp)
mov    $0x3e8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b0 <malloc_sample+0x50>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1040 <clock@plt>
subsd  -0x10(%rbp),%xmm0
mov    0x2e1c(%rip),%rax
divsd  (%rax),%xmm0
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc_sample>:
push   %rax
xor    %eax,%eax
call   1040 <clock@plt>
movsd  %xmm0,(%rsp)
mov    $0x3e8,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11ab <malloc_sample+0x4b>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1040 <clock@plt>
subsd  (%rsp),%xmm0
mov    0x2e51(%rip),%rax
divsd  (%rax),%xmm0
lea    0xe75(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"Can't allocate memory!\00"
@.str.1 = [12 x i8] c"malloc %lf\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define i8* @malloc_sample(i8* %0) {
%2 = alloca i8*
%3 = alloca double
%4 = alloca i8*
store i8* %0, i8** %2
%5 = call double (...) @clock()
store double %5, double* %3
%6 = call i8* @malloc(i32 1000)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i32 @free(i8* %14)
%16 = call double (...) @clock()
%17 = load double, double* %3
%18 = fsub double %16, %17
%19 = load double, double* @CLOCKS_PER_SEC
%20 = fdiv double %18, %19
%21 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), double %20)
ret i8* null
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"Can't allocate memory!\00"
@.str.1 = [12 x i8] c"malloc %lf\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define noalias i8* @malloc_sample(i8* nocapture readnone %0) {
%2 = tail call double (...) @clock()
%3 = tail call dereferenceable_or_null(1000) i8* @malloc(i32 1000)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = tail call i32 @free(i8* %3)
%11 = tail call double (...) @clock()
%12 = fsub double %11, %2
%13 = load double, double* @CLOCKS_PER_SEC
%14 = fdiv double %12, %13
%15 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), double %14)
ret i8* null
}