#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;
char *calloc(int, int);
double clock();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, double);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
void *realoc_sample(void *tid) {
  char *ptr = calloc(1000, sizeof(char));
  time_t t = clock();
  ptr = realloc(ptr, 2000 * sizeof(char));
  if (!ptr) {
    fprintf(stderr, "Can't allocate memory!");
    exit(1);
  }
  free(ptr);
  printf("realoc %lf\n", (double)((double)clock() - t) / CLOCKS_PER_SEC);
  return NULL;
}


x86 assembly (clang, -O0):
<realoc_sample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x3e8,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <clock@plt>
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x7d0,%esi
call   1080 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d7 <realoc_sample+0x67>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1040 <clock@plt>
subsd  -0x18(%rbp),%xmm0
mov    0x2df5(%rip),%rax
divsd  (%rax),%xmm0
lea    0xe19(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realoc_sample>:
push   %rbx
sub    $0x10,%rsp
mov    $0x3e8,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <clock@plt>
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x7d0,%esi
call   1080 <realloc@plt>
test   %rax,%rax
je     11da <realoc_sample+0x6a>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1040 <clock@plt>
subsd  0x8(%rsp),%xmm0
mov    0x2e26(%rip),%rax
divsd  (%rax),%xmm0
lea    0xe4a(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <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"realoc %lf\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define i8* @realoc_sample(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca double
store i8* %0, i8** %2
%5 = call i8* @calloc(i32 1000, i32 1)
store i8* %5, i8** %3
%6 = call double (...) @clock()
store double %6, double* %4
%7 = load i8*, i8** %3
%8 = call i8* @realloc(i8* %7, i32 2000)
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 @free(i8* %16)
%18 = call double (...) @clock()
%19 = load double, double* %4
%20 = fsub double %18, %19
%21 = load double, double* @CLOCKS_PER_SEC
%22 = fdiv double %20, %21
%23 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), double %22)
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"realoc %lf\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define noalias i8* @realoc_sample(i8* nocapture readnone %0) {
%2 = tail call dereferenceable_or_null(1000) i8* @calloc(i32 1000, i32 1)
%3 = tail call double (...) @clock()
%4 = tail call i8* @realloc(i8* %2, i32 2000)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i32 @free(i8* %4)
%12 = tail call double (...) @clock()
%13 = fsub double %12, %3
%14 = load double, double* @CLOCKS_PER_SEC
%15 = fdiv double %13, %14
%16 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), double %15)
ret i8* null
}