#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*/ exit(int);
scalar_t__ ferror(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, size_t);
int /*<<< orphan*/ stdin;
char *str_trim(char *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
char *get_user_input() {
  char buffer[1024];
  size_t content_size = 1;
  char *content = (char *)malloc(1024);
  if (content == NULL) {
    perror("Fail to allocate content");
    exit(32);
  }
  content[0] = '\0';
  while (fgets(buffer, 1024, stdin)) {
    content_size += strlen(buffer);
    content = realloc(content, content_size);
    if (content == NULL) {
      perror("Failed to reallocate content while get input");
      free(content);
      exit(32);
    }
    strcat(content, buffer);
  }
  content = str_trim(content);
  if (ferror(stdin)) {
    free(content);
    perror("Error reading from stdin");
    exit(36);
  }
  return content;
}


x86 assembly (clang, -O0):
<get_user_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movq   $0x1,-0x408(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x410(%rbp)
cmpq   $0x0,-0x410(%rbp)
jne    11eb <get_user_input+0x4b>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
mov    $0x20,%edi
call   10b0 <exit@plt>
mov    -0x410(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x400(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1295 <get_user_input+0xf5>
lea    -0x400(%rbp),%rdi
call   1050 <strlen@plt>
add    -0x408(%rbp),%rax
mov    %rax,-0x408(%rbp)
mov    -0x410(%rbp),%rdi
mov    -0x408(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x410(%rbp)
cmpq   $0x0,-0x410(%rbp)
jne    127d <get_user_input+0xdd>
lea    0xdb7(%rip),%rdi
call   1090 <perror@plt>
mov    -0x410(%rbp),%rdi
call   1030 <free@plt>
mov    $0x20,%edi
call   10b0 <exit@plt>
mov    -0x410(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   10a0 <strcat@plt>
jmp    11f5 <get_user_input+0x55>
mov    -0x410(%rbp),%rdi
call   10c0 <str_trim@plt>
mov    %rax,-0x410(%rbp)
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12e2 <get_user_input+0x142>
mov    -0x410(%rbp),%rdi
call   1030 <free@plt>
lea    0xd73(%rip),%rdi
call   1090 <perror@plt>
mov    $0x24,%edi
call   10b0 <exit@plt>
mov    -0x410(%rbp),%rax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user_input>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    $0x400,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     127f <get_user_input+0xdf>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    0x2e18(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1235 <get_user_input+0x95>
mov    $0x1,%ebx
mov    %rsp,%r14
xchg   %ax,%ax
mov    %rbx,%r13
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
add    %r13,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
je     1262 <get_user_input+0xc2>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   10a0 <strcat@plt>
mov    (%r12),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
test   %rax,%rax
jne    11f0 <get_user_input+0x50>
mov    %r15,%rdi
call   10c0 <str_trim@plt>
mov    %rax,%rbx
mov    (%r12),%edi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1295 <get_user_input+0xf5>
mov    %rbx,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdb0(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   1030 <free@plt>
mov    $0x20,%edi
call   10b0 <exit@plt>
lea    0xd7a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x20,%edi
call   10b0 <exit@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xda2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x24,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Fail to allocate content\00"
@stdin = global i32 0
@.str.1 = [45 x i8] c"Failed to reallocate content while get input\00"
@.str.2 = [25 x i8] c"Error reading from stdin\00"
define i8* @get_user_input() {
%1 = alloca [1024 x i8]
%2 = alloca i64
%3 = alloca i8*
store i64 1, i64* %2
%4 = call i64 @malloc(i32 1024)
%5 = inttoptr i64 %4 to i8*
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 32)
unreachable
11: ; preds = %0
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
br label %14
14: ; preds = %34, %11
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%16 = load i32, i32* @stdin
%17 = call i64 @fgets(i8* %15, i32 1024, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %38
19: ; preds = %14
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%21 = call i64 @strlen(i8* %20)
%22 = load i64, i64* %2
%23 = add i64 %22, %21
store i64 %23, i64* %2
%24 = load i8*, i8** %3
%25 = load i64, i64* %2
%26 = call i8* @realloc(i8* %24, i64 %25)
store i8* %26, i8** %3
%27 = load i8*, i8** %3
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %34
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%31 = load i8*, i8** %3
%32 = call i32 @free(i8* %31)
%33 = call i32 @exit(i32 32)
unreachable
34: ; preds = %19
%35 = load i8*, i8** %3
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%37 = call i32 @strcat(i8* %35, i8* %36)
br label %14
38: ; preds = %14
%39 = load i8*, i8** %3
%40 = call i8* @str_trim(i8* %39)
store i8* %40, i8** %3
%41 = load i32, i32* @stdin
%42 = call i64 @ferror(i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = load i8*, i8** %3
%46 = call i32 @free(i8* %45)
%47 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 36)
unreachable
49: ; preds = %38
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@.str = [25 x i8] c"Fail to allocate content\00"
@stdin = global i32 0
@.str.1 = [45 x i8] c"Failed to reallocate content while get input\00"
@.str.2 = [25 x i8] c"Error reading from stdin\00"
define i8* @get_user_input() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i64 @malloc(i32 1024)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 32)
unreachable
9: ; preds = %0
store i8 0, i8* %4
%10 = load i32, i32* @stdin
%11 = call i64 @fgets(i8* %2, i32 1024, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %29, label %13
13: ; preds = %9, %24
%14 = phi i8* [ %18, %24 ], [ %4, %9 ]
%15 = phi i64 [ %17, %24 ], [ 1, %9 ]
%16 = call i64 @strlen(i8* %2)
%17 = add i64 %16, %15
%18 = call i8* @realloc(i8* %14, i64 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @free(i8* null)
%23 = call i32 @exit(i32 32)
unreachable
24: ; preds = %13
%25 = call i32 @strcat(i8* %18, i8* %2)
%26 = load i32, i32* @stdin
%27 = call i64 @fgets(i8* %2, i32 1024, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %13
29: ; preds = %24, %9
%30 = phi i8* [ %4, %9 ], [ %18, %24 ]
%31 = call i8* @str_trim(i8* %30)
%32 = load i32, i32* @stdin
%33 = call i64 @ferror(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %29
%36 = call i32 @free(i8* %31)
%37 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 36)
unreachable
39: ; preds = %29
ret i8* %31
}