#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *realloc(char *, size_t);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *read_input(FILE *stream) {
  char buf[1000];
  char *buf_p = buf;
  size_t bytes_read;
  char *ret = NULL;
  size_t size = 0;
  char *tmp;
  while (1) {
    buf_p = fgets(buf, 1000, stream);
    if (buf_p == NULL) {
      return ret;
    }
    bytes_read = strlen(buf_p);
    size += bytes_read;
    tmp = realloc(ret, size + bytes_read + 1);
    if (tmp == NULL) {
      if (ret != NULL) {
        free(ret);
      }
      return NULL;
    } else {
      ret = tmp;
    }
    if (size == bytes_read) {
      strcpy(ret, buf_p);
    } else {
      strcat(ret, buf_p);
    }
    if (bytes_read < (1000 - 1)) {
      return ret;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<read_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x400(%rbp),%rax
mov    %rax,-0x408(%rbp)
movq   $0x0,-0x418(%rbp)
movq   $0x0,-0x420(%rbp)
lea    -0x400(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x3e8,%esi
call   1060 <fgets@plt>
mov    %rax,-0x408(%rbp)
cmpq   $0x0,-0x408(%rbp)
jne    11cd <read_input+0x6d>
mov    -0x418(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c2 <read_input+0x162>
mov    -0x408(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x410(%rbp)
mov    -0x410(%rbp),%rax
add    -0x420(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    -0x418(%rbp),%rdi
mov    -0x420(%rbp),%rsi
add    -0x410(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    124f <read_input+0xef>
cmpq   $0x0,-0x418(%rbp)
je     1242 <read_input+0xe2>
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c2 <read_input+0x162>
mov    -0x428(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x420(%rbp),%rax
cmp    -0x410(%rbp),%rax
jne    1289 <read_input+0x129>
mov    -0x418(%rbp),%rdi
mov    -0x408(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    129c <read_input+0x13c>
mov    -0x418(%rbp),%rdi
mov    -0x408(%rbp),%rsi
call   1080 <strcat@plt>
cmpq   $0x3e7,-0x410(%rbp)
jae    12bd <read_input+0x15d>
mov    -0x418(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c2 <read_input+0x162>
jmp    1193 <read_input+0x33>
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_input>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3e8,%rsp
mov    %rdi,%r14
xor    %r12d,%r12d
xor    %r15d,%r15d
jmp    1191 <read_input+0x31>
nopl   0x0(%rax)
call   1080 <strcat@plt>
mov    %rbp,%r15
cmp    $0x3e6,%r13
jbe    11f2 <read_input+0x92>
mov    %rsp,%rdi
mov    $0x3e8,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11f2 <read_input+0x92>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%r15,1),%rbp
lea    (%rax,%rbp,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     11e2 <read_input+0x82>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
test   %r15,%r15
jne    1180 <read_input+0x20>
call   1040 <strcpy@plt>
jmp    1185 <read_input+0x25>
test   %r12,%r12
je     11ef <read_input+0x8f>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x3e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @read_input(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [1000 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i32* %0, i32** %3
%10 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
store i8* %10, i8** %5
store i8* null, i8** %7
store i64 0, i64* %8
br label %11
11: ; preds = %1, %59
%12 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%13 = load i32*, i32** %3
%14 = call i8* @fgets(i8* %12, i32 1000, i32* %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i8*, i8** %7
store i8* %18, i8** %2
br label %60
19: ; preds = %11
%20 = load i8*, i8** %5
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = load i64, i64* %8
%24 = add i64 %23, %22
store i64 %24, i64* %8
%25 = load i8*, i8** %7
%26 = load i64, i64* %8
%27 = load i64, i64* %6
%28 = add i64 %26, %27
%29 = add i64 %28, 1
%30 = call i8* @realloc(i8* %25, i64 %29)
store i8* %30, i8** %9
%31 = load i8*, i8** %9
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %40
33: ; preds = %19
%34 = load i8*, i8** %7
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
br label %39
39: ; preds = %36, %33
store i8* null, i8** %2
br label %60
40: ; preds = %19
%41 = load i8*, i8** %9
store i8* %41, i8** %7
br label %42
42: ; preds = %40
%43 = load i64, i64* %8
%44 = load i64, i64* %6
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = load i8*, i8** %7
%48 = load i8*, i8** %5
%49 = call i32 @strcpy(i8* %47, i8* %48)
br label %54
50: ; preds = %42
%51 = load i8*, i8** %7
%52 = load i8*, i8** %5
%53 = call i32 @strcat(i8* %51, i8* %52)
br label %54
54: ; preds = %50, %46
%55 = load i64, i64* %6
%56 = icmp ult i64 %55, 999
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = load i8*, i8** %7
store i8* %58, i8** %2
br label %60
59: ; preds = %54
br label %11
60: ; preds = %57, %39, %17
%61 = load i8*, i8** %2
ret i8* %61
}

LLVM IR (-O2):
define i8* @read_input(i32* nocapture %0) {
%2 = alloca [1000 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %26, %1
%5 = phi i8* [ null, %1 ], [ %14, %26 ]
%6 = phi i64 [ 0, %1 ], [ %11, %26 ]
%7 = call i8* @fgets(i8* %3, i32 1000, i32* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %28, label %9
9: ; preds = %4
%10 = call i64 @strlen(i8* %7)
%11 = add i64 %10, %6
%12 = add i64 %10, 1
%13 = add i64 %12, %11
%14 = call i8* @realloc(i8* %5, i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = icmp eq i8* %5, null
br i1 %17, label %28, label %18
18: ; preds = %16
%19 = call i32 @free(i8* %5)
br label %28
20: ; preds = %9
%21 = icmp eq i64 %6, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = call i32 @strcpy(i8* %14, i8* %7)
br label %26
24: ; preds = %20
%25 = call i32 @strcat(i8* %14, i8* %7)
br label %26
26: ; preds = %24, %22
%27 = icmp ult i64 %10, 999
br i1 %27, label %28, label %4
28: ; preds = %26, %4, %16, %18
%29 = phi i8* [ null, %18 ], [ null, %16 ], [ %14, %26 ], [ %5, %4 ]
ret i8* %29
}