#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void *realloc(void *, int);
int main(int argc, char *argv[]) {
  printf("Trying to malloc 4 bytes.\n");
  void *byte4 = malloc(4);
  printf("Value of byte4: %p\n\n", byte4);
  printf("Trying to malloc 64 more bytes.\n");
  void *byte64 = malloc(64);
  printf("Value of byte64: %p\n\n", byte64);
  printf("Trying to malloc 128 more bytes.\n");
  void *byte128 = malloc(128);
  printf("Value of byte128: %p\n\n", byte128);
  printf("Trying to malloc 1024 more bytes.\n");
  void *byte1024 = malloc(1024);
  printf("Value of byte1024: %p\n\n", byte1024);
  printf("Freeing byte4 then allocating 32 bytes\n");
  printf("The address of a should be the same as the address of byte4\n");
  free(byte4);
  void *byte32 = malloc(32);
  printf("Value of byte32: %p\n\n", byte32);
  printf("Trying to calloc a 16 element array of 8 bytes\n");
  void *array16 = calloc(16, 8);
  printf("Value of array16: %p\n\n", array16);
  printf("Trying to realloc byte64 to 5000 bytes\n");
  ((int *)byte64)[0] = 314159;
  printf("Value of byte64 before the realloc: %d\n", ((int *)byte64)[0]);
  printf("Location of byte64 before the realloc: %p\n", byte64);
  byte64 = realloc(byte64, 5000);
  printf("Value of byte64 after the realloc: %d\n", ((int *)byte64)[0]);
  printf("Location of byte64 after the realloc: %p\n\n", byte64);
  printf("Trying to allocate 64 bytes\n");
  printf("This should have the same address as byte64\n");
  void *c = malloc(64);
  printf("Value of c: %p\n", c);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x40,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x80,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xed4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xedc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x10,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xee7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xef0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movl   $0x4cb2f,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xefa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xf10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1388,%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xf15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xf2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x40,%edi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
lea    0xf63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xfc1(%rip),%rdi
call   1040 <puts@plt>
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe6c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xfb1(%rip),%rdi
call   1040 <puts@plt>
mov    $0x40,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe57(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xfa7(%rip),%rdi
call   1040 <puts@plt>
mov    $0x80,%edi
call   1070 <malloc@plt>
lea    0xe46(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xfa1(%rip),%rdi
call   1040 <puts@plt>
mov    $0x400,%edi
call   1070 <malloc@plt>
lea    0xe36(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf9c(%rip),%rdi
call   1040 <puts@plt>
lea    0xfb7(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x20,%edi
call   1070 <malloc@plt>
lea    0xe13(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xfc4(%rip),%rdi
call   1040 <puts@plt>
mov    $0x10,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
lea    0xdfd(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xfc7(%rip),%rdi
call   1040 <puts@plt>
movl   $0x4cb2f,(%rbx)
lea    0xdf1(%rip),%rdi
mov    $0x4cb2f,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe06(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x1388,%esi
call   1080 <realloc@plt>
mov    %rax,%rbx
mov    (%rax),%esi
lea    0xe0e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe27(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf87(%rip),%rdi
call   1040 <puts@plt>
lea    0xf97(%rip),%rdi
call   1040 <puts@plt>
mov    $0x40,%edi
call   1070 <malloc@plt>
lea    0xe1f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Trying to malloc 4 bytes.\0A\00"
@.str.1 = [21 x i8] c"Value of byte4: %p\0A\0A\00"
@.str.2 = [33 x i8] c"Trying to malloc 64 more bytes.\0A\00"
@.str.3 = [22 x i8] c"Value of byte64: %p\0A\0A\00"
@.str.4 = [34 x i8] c"Trying to malloc 128 more bytes.\0A\00"
@.str.5 = [23 x i8] c"Value of byte128: %p\0A\0A\00"
@.str.6 = [35 x i8] c"Trying to malloc 1024 more bytes.\0A\00"
@.str.7 = [24 x i8] c"Value of byte1024: %p\0A\0A\00"
@.str.8 = [40 x i8] c"Freeing byte4 then allocating 32 bytes\0A\00"
@.str.9 = [61 x i8] c"The address of a should be the same as the address of byte4\0A\00"
@.str.10 = [22 x i8] c"Value of byte32: %p\0A\0A\00"
@.str.11 = [48 x i8] c"Trying to calloc a 16 element array of 8 bytes\0A\00"
@.str.12 = [23 x i8] c"Value of array16: %p\0A\0A\00"
@.str.13 = [40 x i8] c"Trying to realloc byte64 to 5000 bytes\0A\00"
@.str.14 = [40 x i8] c"Value of byte64 before the realloc: %d\0A\00"
@.str.15 = [43 x i8] c"Location of byte64 before the realloc: %p\0A\00"
@.str.16 = [39 x i8] c"Value of byte64 after the realloc: %d\0A\00"
@.str.17 = [43 x i8] c"Location of byte64 after the realloc: %p\0A\0A\00"
@.str.18 = [29 x i8] c"Trying to allocate 64 bytes\0A\00"
@.str.19 = [45 x i8] c"This should have the same address as byte64\0A\00"
@.str.20 = [16 x i8] c"Value of c: %p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%14 = call i8* @malloc(i32 4)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%18 = call i8* @malloc(i32 64)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%22 = call i8* @malloc(i32 128)
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%26 = call i8* @malloc(i32 1024)
store i8* %26, i8** %9
%27 = load i8*, i8** %9
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0))
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.9, i64 0, i64 0))
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
%33 = call i8* @malloc(i32 32)
store i8* %33, i8** %10
%34 = load i8*, i8** %10
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), i8* %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.11, i64 0, i64 0))
%37 = call i8* @calloc(i32 16, i32 8)
store i8* %37, i8** %11
%38 = load i8*, i8** %11
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.12, i64 0, i64 0), i8* %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.13, i64 0, i64 0))
%41 = load i8*, i8** %7
%42 = bitcast i8* %41 to i32*
%43 = getelementptr inbounds i32, i32* %42, i64 0
store i32 314159, i32* %43
%44 = load i8*, i8** %7
%45 = bitcast i8* %44 to i32*
%46 = getelementptr inbounds i32, i32* %45, i64 0
%47 = load i32, i32* %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.14, i64 0, i64 0), i32 %47)
%49 = load i8*, i8** %7
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.15, i64 0, i64 0), i8* %49)
%51 = load i8*, i8** %7
%52 = call i8* @realloc(i8* %51, i32 5000)
store i8* %52, i8** %7
%53 = load i8*, i8** %7
%54 = bitcast i8* %53 to i32*
%55 = getelementptr inbounds i32, i32* %54, i64 0
%56 = load i32, i32* %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.16, i64 0, i64 0), i32 %56)
%58 = load i8*, i8** %7
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.17, i64 0, i64 0), i8* %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.18, i64 0, i64 0))
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.19, i64 0, i64 0))
%62 = call i8* @malloc(i32 64)
store i8* %62, i8** %12
%63 = load i8*, i8** %12
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.20, i64 0, i64 0), i8* %63)
%65 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c"Value of byte4: %p\0A\0A\00"
@.str.3 = [22 x i8] c"Value of byte64: %p\0A\0A\00"
@.str.5 = [23 x i8] c"Value of byte128: %p\0A\0A\00"
@.str.7 = [24 x i8] c"Value of byte1024: %p\0A\0A\00"
@.str.10 = [22 x i8] c"Value of byte32: %p\0A\0A\00"
@.str.12 = [23 x i8] c"Value of array16: %p\0A\0A\00"
@.str.14 = [40 x i8] c"Value of byte64 before the realloc: %d\0A\00"
@.str.15 = [43 x i8] c"Location of byte64 before the realloc: %p\0A\00"
@.str.16 = [39 x i8] c"Value of byte64 after the realloc: %d\0A\00"
@.str.17 = [43 x i8] c"Location of byte64 after the realloc: %p\0A\0A\00"
@.str.20 = [16 x i8] c"Value of c: %p\0A\00"
@str = [26 x i8] c"Trying to malloc 4 bytes.\00"
@str.21 = [32 x i8] c"Trying to malloc 64 more bytes.\00"
@str.22 = [33 x i8] c"Trying to malloc 128 more bytes.\00"
@str.23 = [34 x i8] c"Trying to malloc 1024 more bytes.\00"
@str.24 = [39 x i8] c"Freeing byte4 then allocating 32 bytes\00"
@str.25 = [60 x i8] c"The address of a should be the same as the address of byte4\00"
@str.26 = [47 x i8] c"Trying to calloc a 16 element array of 8 bytes\00"
@str.27 = [39 x i8] c"Trying to realloc byte64 to 5000 bytes\00"
@str.28 = [28 x i8] c"Trying to allocate 64 bytes\00"
@str.29 = [44 x i8] c"This should have the same address as byte64\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%4 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.21, i64 0, i64 0))
%7 = tail call dereferenceable_or_null(64) i8* @malloc(i32 64)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.22, i64 0, i64 0))
%10 = tail call dereferenceable_or_null(128) i8* @malloc(i32 128)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.23, i64 0, i64 0))
%13 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %13)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.24, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str.25, i64 0, i64 0))
%17 = tail call i32 @free(i8* %4)
%18 = tail call dereferenceable_or_null(32) i8* @malloc(i32 32)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), i8* %18)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.26, i64 0, i64 0))
%21 = tail call dereferenceable_or_null(128) i8* @calloc(i32 16, i32 8)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.12, i64 0, i64 0), i8* %21)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.27, i64 0, i64 0))
%24 = bitcast i8* %7 to i32*
store i32 314159, i32* %24
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.14, i64 0, i64 0), i32 314159)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.15, i64 0, i64 0), i8* %7)
%27 = tail call i8* @realloc(i8* %7, i32 5000)
%28 = bitcast i8* %27 to i32*
%29 = load i32, i32* %28
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.16, i64 0, i64 0), i32 %29)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.17, i64 0, i64 0), i8* %27)
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.28, i64 0, i64 0))
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.29, i64 0, i64 0))
%34 = tail call dereferenceable_or_null(64) i8* @malloc(i32 64)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.20, i64 0, i64 0), i8* %34)
%36 = tail call i32 @exit(i32 0)
unreachable
}