#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*/ getchar();
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(void) {
  char *input;
  int size;
  input = (char *)malloc(4069);
  if (input == NULL) {
    puts("ERROR : Not enough memory");
    return 0;
  }
  printf("Enter some text %c\n", 25);
  gets(input);
  size = strlen(input) + 1;
  if (!realloc(input, size)) {
    puts("Unable to reallocate memory");
    return 0;
  }
  puts("Memory reallocation successfull");
  printf("String is:\n\"%s\"\n", input);
  free(input);
  puts("Memory has been released!");
  printf("String now is \n\"%s\"\n", input);
  getchar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xfe5,%edi
call   1090 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c0 <main+0x40>
lea    0xe51(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    126b <main+0xeb>
lea    0xe53(%rip),%rdi
mov    $0x19,%esi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <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   10a0 <realloc@plt>
cmp    $0x0,%eax
jne    1218 <main+0x98>
lea    0xe27(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    126b <main+0xeb>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xe35(%rip),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rsi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@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   1090 <malloc@plt>
test   %rax,%rax
je     1214 <main+0x94>
mov    %rax,%rbx
lea    0xe7c(%rip),%rdi
mov    $0x19,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1080 <gets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%esi
mov    %rbx,%rdi
call   10a0 <realloc@plt>
test   %eax,%eax
je     121d <main+0x9d>
lea    0xe7a(%rip),%rdi
call   1040 <puts@plt>
lea    0xe8e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
lea    0xe94(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
jmp    1229 <main+0xa9>
lea    0xde5(%rip),%rdi
jmp    1224 <main+0xa4>
lea    0xe0a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"ERROR : Not enough memory\00"
@.str.1 = [20 x i8] c"Enter some text %c\0A\00"
@.str.2 = [28 x i8] c"Unable to reallocate memory\00"
@.str.3 = [32 x i8] c"Memory reallocation successfull\00"
@.str.4 = [17 x i8] c"String is:\0A\22%s\22\0A\00"
@.str.5 = [26 x i8] c"Memory has been released!\00"
@.str.6 = [21 x i8] c"String now is \0A\22%s\22\0A\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 ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %33
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 25)
%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 %33
23: ; preds = %10
%24 = call i32 @puts(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%25 = load i8*, i8** %2
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 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 ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%30 = load i8*, i8** %2
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %30)
%32 = call i32 (...) @getchar()
store i32 0, i32* %1
br label %33
33: ; preds = %23, %21, %8
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [26 x i8] c"ERROR : Not enough memory\00"
@.str.1 = [20 x i8] c"Enter some text %c\0A\00"
@.str.2 = [28 x i8] c"Unable to reallocate memory\00"
@.str.3 = [32 x i8] c"Memory reallocation successfull\00"
@.str.4 = [17 x i8] c"String is:\0A\22%s\22\0A\00"
@.str.5 = [26 x i8] c"Memory has been released!\00"
@.str.6 = [21 x i8] c"String now is \0A\22%s\22\0A\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 ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %22
6: ; preds = %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 25)
%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 %22
15: ; preds = %6
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 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 ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%21 = tail call i32 (...) @getchar()
br label %22
22: ; preds = %15, %13, %4
ret i32 0
}