#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 */
scalar_t__ fgets_unlocked(char *, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
char *malloc(scalar_t__);
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;
  fstat(0, &stat);
  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   1080 <fstat@plt>
mov    -0x10(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets_unlocked@plt>
mov    -0x18(%rbp),%rdi
lea    0xe5b(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1184 <read_stdin+0x24>
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    0x2e0b(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets_unlocked@plt>
cmp    $0x0,%rax
je     1256 <read_stdin+0xf6>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1220 <read_stdin+0xc0>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3e,%eax
jne    1225 <read_stdin+0xc5>
jmp    1256 <read_stdin+0xf6>
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    1251 <read_stdin+0xf1>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c0 <read_stdin+0x60>
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   1080 <fstat@plt>
mov    (%rsp),%r15
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    0x2e5a(%rip),%rbp
lea    0xe6b(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
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    11a0 <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     1222 <read_stdin+0xc2>
mov    %r12,%r13
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     1222 <read_stdin+0xc2>
cmpb   $0x3e,0x0(%r13)
je     1222 <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    11e0 <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

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@stdin = global i32 0
@.str = [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 i32 @fstat(i32 0, %struct.stat* %2)
%5 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%6 = load i64, i64* %5
%7 = tail call i8* @malloc(i64 %6)
br label %8
8: ; preds = %8, %1
%9 = load i32, i32* @stdin
%10 = tail call i64 @fgets_unlocked(i8* %7, i64 %6, i32 %9)
%11 = tail call i64 @strncmp(i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %8
13: ; preds = %8
%14 = load i32, i32* @stdin
%15 = tail call i64 @fgets_unlocked(i8* %7, i64 %6, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %39, label %17
17: ; preds = %13, %25
%18 = phi i8* [ %34, %25 ], [ %7, %13 ]
%19 = phi i32 [ %32, %25 ], [ 0, %13 ]
%20 = tail call i32 @strlen(i8* %18)
%21 = icmp eq i32 %20, 0
br i1 %21, label %39, label %22
22: ; preds = %17
%23 = load i8, i8* %18
%24 = icmp eq i8 %23, 62
br i1 %24, label %39, label %25
25: ; preds = %22
%26 = add nsw i32 %20, %19
%27 = add nsw i32 %26, -1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %7, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 10
%32 = select i1 %31, i32 %27, i32 %26
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %7, i64 %33
%35 = sub nsw i64 %6, %33
%36 = load i32, i32* @stdin
%37 = tail call i64 @fgets_unlocked(i8* %34, i64 %35, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %17
39: ; preds = %25, %22, %17, %13
%40 = phi i32 [ 0, %13 ], [ %19, %17 ], [ %19, %22 ], [ %32, %25 ]
%41 = add nsw i32 %40, 1
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %7, i64 %42
store i8 62, i8* %43
%44 = tail call i8* @realloc(i8* %7, i32 %41)
store i32 %41, i32* %0
ret i8* %44
}