#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*/ *fgets(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
char *read_stdin() {
  size_t bsize = 1024;
  size_t size = 1;
  char buf[bsize];
  char *res = (char *)malloc(sizeof(char) * bsize);
  char *tmp = NULL;
  if (NULL == res) {
    return NULL;
  }
  res[0] = '\0';
  if (NULL != fgets(buf, bsize, stdin)) {
    tmp = res;
    size += (size_t)strlen(buf);
    res = (char *)realloc(res, size);
    if (NULL == res) {
      free(tmp);
      return NULL;
    }
    strcat(res, buf);
    return res;
  }
  free(res);
  return NULL;
}


x86 assembly (clang, -O0):
<read_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movq   $0x400,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    11da <read_stdin+0x7a>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1291 <read_stdin+0x131>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rsi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1279 <read_stdin+0x119>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
call   1040 <strlen@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    1258 <read_stdin+0xf8>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1291 <read_stdin+0x131>
mov    -0x48(%rbp),%rsi
mov    -0x30(%rbp),%rdi
call   1080 <strcat@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1291 <read_stdin+0x131>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_stdin>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    $0x400,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11cb <read_stdin+0x6b>
mov    %rax,%r14
movb   $0x0,(%rax)
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11c3 <read_stdin+0x63>
mov    %rsp,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %r14,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     11c3 <read_stdin+0x63>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1080 <strcat@plt>
jmp    11cd <read_stdin+0x6d>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stdin = global i32 0
define i8* @read_stdin() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i64 1024, i64* %2
store i64 1, i64* %3
%9 = load i64, i64* %2
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i64, i64* %2
%13 = mul i64 1, %12
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %6
store i8* null, i8** %7
%17 = load i8*, i8** %6
%18 = icmp eq i8* null, %17
br i1 %18, label %19, label %20
19: ; preds = %0
store i8* null, i8** %1
store i32 1, i32* %8
br label %48
20: ; preds = %0
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 0, i8* %22
%23 = load i64, i64* %2
%24 = load i32, i32* @stdin
%25 = call i32* @fgets(i8* %11, i64 %23, i32 %24)
%26 = icmp ne i32* null, %25
br i1 %26, label %27, label %45
27: ; preds = %20
%28 = load i8*, i8** %6
store i8* %28, i8** %7
%29 = call i64 @strlen(i8* %11)
%30 = load i64, i64* %3
%31 = add i64 %30, %29
store i64 %31, i64* %3
%32 = load i8*, i8** %6
%33 = load i64, i64* %3
%34 = call i64 @realloc(i8* %32, i64 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %6
%36 = load i8*, i8** %6
%37 = icmp eq i8* null, %36
br i1 %37, label %38, label %41
38: ; preds = %27
%39 = load i8*, i8** %7
%40 = call i32 @free(i8* %39)
store i8* null, i8** %1
store i32 1, i32* %8
br label %48
41: ; preds = %27
%42 = load i8*, i8** %6
%43 = call i32 @strcat(i8* %42, i8* %11)
%44 = load i8*, i8** %6
store i8* %44, i8** %1
store i32 1, i32* %8
br label %48
45: ; preds = %20
%46 = load i8*, i8** %6
%47 = call i32 @free(i8* %46)
store i8* null, i8** %1
store i32 1, i32* %8
br label %48
48: ; preds = %45, %41, %38, %19
%49 = load i8*, i8** %4
%50 = load i8*, i8** %1
ret i8* %50
}

LLVM IR (-O2):
@stdin = global i32 0
define i8* @read_stdin() {
%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 %22, label %6
6: ; preds = %0
store i8 0, i8* %4
%7 = load i32, i32* @stdin
%8 = call i32* @fgets(i8* %2, i64 1024, i32 %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %20, label %10
10: ; preds = %6
%11 = call i64 @strlen(i8* %2)
%12 = add i64 %11, 1
%13 = call i64 @realloc(i8* %4, i64 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @free(i8* %4)
br label %22
18: ; preds = %10
%19 = call i32 @strcat(i8* %14, i8* %2)
br label %22
20: ; preds = %6
%21 = call i32 @free(i8* %4)
br label %22
22: ; preds = %0, %20, %18, %16
%23 = phi i8* [ null, %16 ], [ %14, %18 ], [ null, %20 ], [ null, %0 ]
ret i8* %23
}