#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 */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ gets(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ realloc(char *, int);
int strlen(char *);
int main() {
  char *input;
  int size;
  input = (char *)malloc(4069);
  if (input == (char *)NULL) {
    puts("Out of memory");
    return 0;
  }
  printf("Say something..: ");
  gets(input);
  size = strlen(input) + 1;
  if (!realloc(input, size)) {
    puts("Unable to reallocate memory");
    return 0;
  }
  puts("Memory reallocation successful");
  printf("String is: %s\n\n", input);
  free(input);
  puts("Memory has been released");
  printf("String now is %s", input);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xfe5,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b0 <main+0x40>
lea    0xe61(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    124f <main+0xdf>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <gets@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <realloc@plt>
cmp    $0x0,%eax
jne    1203 <main+0x93>
lea    0xe2e(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    124f <main+0xdf>
lea    0xe32(%rip),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rsi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xe3a(%rip),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0xfe5,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11f4 <main+0x84>
mov    %rax,%rbx
lea    0xe84(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1070 <gets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%esi
mov    %rbx,%rdi
call   1090 <realloc@plt>
test   %eax,%eax
je     11fd <main+0x8d>
lea    0xe85(%rip),%rdi
call   1040 <puts@plt>
lea    0xe98(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe8f(%rip),%rdi
call   1040 <puts@plt>
lea    0xe9c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1209 <main+0x99>
lea    0xe05(%rip),%rdi
jmp    1204 <main+0x94>
lea    0xe1c(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Out of memory\00"
@.str.1 = [18 x i8] c"Say something..: \00"
@.str.2 = [28 x i8] c"Unable to reallocate memory\00"
@.str.3 = [31 x i8] c"Memory reallocation successful\00"
@.str.4 = [16 x i8] c"String is: %s\0A\0A\00"
@.str.5 = [25 x i8] c"Memory has been released\00"
@.str.6 = [17 x i8] c"String now is %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
%4 = call i64 @malloc(i32 4069)
%5 = inttoptr i64 %4 to i8*
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %32
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%12 = load i8*, i8** %2
%13 = call i32 @gets(i8* %12)
%14 = load i8*, i8** %2
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
store i32 %16, i32* %3
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%19 = call i32 @realloc(i8* %17, i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %10
%22 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %32
23: ; preds = %10
%24 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%25 = load i8*, i8** %2
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %2
%28 = call i32 @free(i8* %27)
%29 = call i32 @puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%30 = load i8*, i8** %2
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8* %30)
store i32 0, i32* %1
br label %32
32: ; preds = %23, %21, %8
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [14 x i8] c"Out of memory\00"
@.str.1 = [18 x i8] c"Say something..: \00"
@.str.2 = [28 x i8] c"Unable to reallocate memory\00"
@.str.3 = [31 x i8] c"Memory reallocation successful\00"
@.str.4 = [16 x i8] c"String is: %s\0A\0A\00"
@.str.5 = [25 x i8] c"Memory has been released\00"
@.str.6 = [17 x i8] c"String now is %s\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 4069)
%2 = inttoptr i64 %1 to i8*
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %21
6: ; preds = %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @gets(i8* %2)
%9 = tail call i32 @strlen(i8* %2)
%10 = add nsw i32 %9, 1
%11 = tail call i32 @realloc(i8* %2, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %6
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %21
15: ; preds = %6
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%18 = tail call i32 @free(i8* %2)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8* %2)
br label %21
21: ; preds = %15, %13, %4
ret i32 0
}