#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 */

/* Variables and functions */
void *calloc(int, int);
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ mhook_print_unfree();
int /*<<< orphan*/ printf(char *);
void *realloc(void *, int);
void *strdup(char *);
int main() {
  void *p;
  free(malloc(1));
  mhook_print_unfree();
  p = calloc(1, 2);
  mhook_print_unfree();
  p = realloc(p, 10);
  free(p);
  mhook_print_unfree();
  printf("realloc used as malloc()\n");
  p = realloc(NULL, 1);
  mhook_print_unfree();
  printf("realloc used as free()\n");
  p = realloc(p, 0);
  mhook_print_unfree();
  free(strdup("hello"));
  mhook_print_unfree();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1060 <mhook_print_unfree@plt>
mov    $0x1,%edi
mov    $0x2,%esi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1060 <mhook_print_unfree@plt>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1080 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1060 <mhook_print_unfree@plt>
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1,%esi
call   1080 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1060 <mhook_print_unfree@plt>
lea    0xe1a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1080 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1060 <mhook_print_unfree@plt>
lea    0xe10(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1060 <mhook_print_unfree@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1060 <mhook_print_unfree@plt>
mov    $0x1,%edi
mov    $0x2,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <mhook_print_unfree@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1080 <realloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1060 <mhook_print_unfree@plt>
lea    0xe40(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1080 <realloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <mhook_print_unfree@plt>
lea    0xe37(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <realloc@plt>
xor    %eax,%eax
call   1060 <mhook_print_unfree@plt>
lea    0xdfb(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1060 <mhook_print_unfree@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"realloc used as malloc()\0A\00"
@.str.1 = [24 x i8] c"realloc used as free()\0A\00"
@.str.2 = [6 x i8] c"hello\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
store i32 0, i32* %1
%3 = call i8* @malloc(i32 1)
%4 = call i32 @free(i8* %3)
%5 = call i32 (...) @mhook_print_unfree()
%6 = call i8* @calloc(i32 1, i32 2)
store i8* %6, i8** %2
%7 = call i32 (...) @mhook_print_unfree()
%8 = load i8*, i8** %2
%9 = call i8* @realloc(i8* %8, i32 10)
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = call i32 @free(i8* %10)
%12 = call i32 (...) @mhook_print_unfree()
%13 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%14 = call i8* @realloc(i8* null, i32 1)
store i8* %14, i8** %2
%15 = call i32 (...) @mhook_print_unfree()
%16 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%17 = load i8*, i8** %2
%18 = call i8* @realloc(i8* %17, i32 0)
store i8* %18, i8** %2
%19 = call i32 (...) @mhook_print_unfree()
%20 = call i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @free(i8* %20)
%22 = call i32 (...) @mhook_print_unfree()
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [6 x i8] c"hello\00"
@str = [25 x i8] c"realloc used as malloc()\00"
@str.3 = [23 x i8] c"realloc used as free()\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%2 = tail call i32 @free(i8* %1)
%3 = tail call i32 (...) @mhook_print_unfree()
%4 = tail call dereferenceable_or_null(2) i8* @calloc(i32 1, i32 2)
%5 = tail call i32 (...) @mhook_print_unfree()
%6 = tail call i8* @realloc(i8* %4, i32 10)
%7 = tail call i32 @free(i8* %6)
%8 = tail call i32 (...) @mhook_print_unfree()
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%10 = tail call i8* @realloc(i8* null, i32 1)
%11 = tail call i32 (...) @mhook_print_unfree()
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.3, i64 0, i64 0))
%13 = tail call i8* @realloc(i8* %10, i32 0)
%14 = tail call i32 (...) @mhook_print_unfree()
%15 = tail call dereferenceable_or_null(6) i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @free(i8* %15)
%17 = tail call i32 (...) @mhook_print_unfree()
ret i32 0
}