#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 */
struct stat {
  scalar_t__ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fgets_unlocked(char *, scalar_t__, int /*<<< orphan*/);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, int);
int /*<<< orphan*/ stdin;
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
char *read_stdin(int *stdin_size) {
  struct stat stat;
  if (fstat(0, &stat) < 0) {
    perror("fstat");
    exit(1);
  }
  char *result = malloc(stat.st_size);
  do {
    fgets_unlocked(result, stat.st_size, stdin);
  } while (strncmp(result, ">THREE", 6));
  int read = 0;
  while (fgets_unlocked(result + read, stat.st_size - read, stdin)) {
    int len = strlen(result + read);
    if (len == 0 || result[read] == '>') {
      break;
    }
    read += len;
    if (result[read - 1] == '\n') {
      read--;
    }
  }
  result[read++] = '>';
  result = realloc(result, read);
  *stdin_size = read;
  return result;
}


x86 assembly (clang, -O0):
<read_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
jge    11b7 <read_stdin+0x37>
lea    0xe58(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets_unlocked@plt>
mov    -0x18(%rbp),%rdi
lea    0xe21(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11c4 <read_stdin+0x44>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
sub    %rax,%rsi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets_unlocked@plt>
cmp    $0x0,%rax
je     1296 <read_stdin+0x116>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1260 <read_stdin+0xe0>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3e,%eax
jne    1265 <read_stdin+0xe5>
jmp    1296 <read_stdin+0x116>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1291 <read_stdin+0x111>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1200 <read_stdin+0x80>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x3e,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_stdin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rsi
xor    %edi,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
js     1269 <read_stdin+0xe9>
mov    (%rsp),%r15
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    0x2e31(%rip),%rbp
lea    0xe48(%rip),%rbx
xchg   %ax,%ax
mov    0x0(%rbp),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <fgets_unlocked@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11c0 <read_stdin+0x40>
mov    0x0(%rbp),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <fgets_unlocked@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1242 <read_stdin+0xc2>
mov    %r12,%r13
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     1242 <read_stdin+0xc2>
cmpb   $0x3e,0x0(%r13)
je     1242 <read_stdin+0xc2>
add    %ebx,%eax
movslq %eax,%rbx
lea    -0x1(%rbx),%rax
cmpb   $0xa,-0x1(%r12,%rbx,1)
cmove  %eax,%ebx
movslq %ebx,%rax
lea    (%r12,%rax,1),%r13
mov    %r15,%rsi
sub    %rax,%rsi
mov    0x0(%rbp),%edx
mov    %r13,%rdi
call   1050 <fgets_unlocked@plt>
test   %rax,%rax
jne    1200 <read_stdin+0x80>
movslq %ebx,%rax
add    $0x1,%ebx
movb   $0x3e,(%r12,%rax,1)
mov    %r12,%rdi
mov    %ebx,%esi
call   1070 <realloc@plt>
mov    %ebx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd90(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [6 x i8] c"fstat\00"
@stdin = global i32 0
@.str.1 = [7 x i8] c">THREE\00"
define i8* @read_stdin(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %2
%7 = call i64 @fstat(i32 0, %struct.stat* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%14 = load i64, i64* %13
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %4
br label %16
16: ; preds = %22, %12
%17 = load i8*, i8** %4
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%19 = load i64, i64* %18
%20 = load i32, i32* @stdin
%21 = call i64 @fgets_unlocked(i8* %17, i64 %19, i32 %20)
br label %22
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = call i64 @strncmp(i8* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%25 = icmp ne i64 %24, 0
br i1 %25, label %16, label %26
26: ; preds = %22
store i32 0, i32* %5
br label %27
27: ; preds = %72, %26
%28 = load i8*, i8** %4
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = sub nsw i64 %33, %35
%37 = load i32, i32* @stdin
%38 = call i64 @fgets_unlocked(i8* %31, i64 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %73
40: ; preds = %27
%41 = load i8*, i8** %4
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = call i32 @strlen(i8* %44)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp eq i32 %46, 0
br i1 %47, label %56, label %48
48: ; preds = %40
%49 = load i8*, i8** %4
%50 = load i32, i32* %5
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 62
br i1 %55, label %56, label %57
56: ; preds = %48, %40
br label %73
57: ; preds = %48
%58 = load i32, i32* %6
%59 = load i32, i32* %5
%60 = add nsw i32 %59, %58
store i32 %60, i32* %5
%61 = load i8*, i8** %4
%62 = load i32, i32* %5
%63 = sub nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %61, i64 %64
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 10
br i1 %68, label %69, label %72
69: ; preds = %57
%70 = load i32, i32* %5
%71 = add nsw i32 %70, -1
store i32 %71, i32* %5
br label %72
72: ; preds = %69, %57
br label %27
73: ; preds = %56, %27
%74 = load i8*, i8** %4
%75 = load i32, i32* %5
%76 = add nsw i32 %75, 1
store i32 %76, i32* %5
%77 = sext i32 %75 to i64
%78 = getelementptr inbounds i8, i8* %74, i64 %77
store i8 62, i8* %78
%79 = load i8*, i8** %4
%80 = load i32, i32* %5
%81 = call i8* @realloc(i8* %79, i32 %80)
store i8* %81, i8** %4
%82 = load i32, i32* %5
%83 = load i32*, i32** %2
store i32 %82, i32* %83
%84 = load i8*, i8** %4
ret i8* %84
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [6 x i8] c"fstat\00"
@stdin = global i32 0
@.str.1 = [7 x i8] c">THREE\00"
define i8* @read_stdin(i32* nocapture writeonly %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @fstat(i32 0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i64, i64* %10
%12 = tail call i8* @malloc(i64 %11)
br label %13
13: ; preds = %13, %9
%14 = load i32, i32* @stdin
%15 = tail call i64 @fgets_unlocked(i8* %12, i64 %11, i32 %14)
%16 = tail call i64 @strncmp(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %13
18: ; preds = %13
%19 = load i32, i32* @stdin
%20 = tail call i64 @fgets_unlocked(i8* %12, i64 %11, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %44, label %22
22: ; preds = %18, %30
%23 = phi i8* [ %39, %30 ], [ %12, %18 ]
%24 = phi i32 [ %37, %30 ], [ 0, %18 ]
%25 = tail call i32 @strlen(i8* %23)
%26 = icmp eq i32 %25, 0
br i1 %26, label %44, label %27
27: ; preds = %22
%28 = load i8, i8* %23
%29 = icmp eq i8 %28, 62
br i1 %29, label %44, label %30
30: ; preds = %27
%31 = add nsw i32 %25, %24
%32 = add nsw i32 %31, -1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %12, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 10
%37 = select i1 %36, i32 %32, i32 %31
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %12, i64 %38
%40 = sub nsw i64 %11, %38
%41 = load i32, i32* @stdin
%42 = tail call i64 @fgets_unlocked(i8* %39, i64 %40, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %22
44: ; preds = %30, %27, %22, %18
%45 = phi i32 [ 0, %18 ], [ %24, %22 ], [ %24, %27 ], [ %37, %30 ]
%46 = add nsw i32 %45, 1
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %12, i64 %47
store i8 62, i8* %48
%49 = tail call i8* @realloc(i8* %12, i32 %46)
store i32 %46, i32* %0
ret i8* %49
}