#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*/ free(char *);
scalar_t__ malloc(unsigned int);
scalar_t__ read(int /*<<< orphan*/, char *, int);
char *realloc(char *, int);
char *read_redirected() {
  int size;
  char *p;
  char *line;
  char *end;
  for (size = 64,
      p = line = ((char *)malloc((unsigned int)(sizeof(char) * (size)))),
      end = p + size;
       ; p++) {
    if (p == end) {
      size += 64;
      p = line = realloc(line, size);
      end = p + size;
    }
    if (read(0, p, 1) <= 0) {
      free(line);
      return NULL;
    }
    if (*p == '\n')
      break;
  }
  *p = '\0';
  return line;
}


x86 assembly (clang, -O0):
<read_redirected>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x40,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11af <read_redirected+0x6f>
mov    -0xc(%rbp),%eax
add    $0x40,%eax
mov    %eax,-0xc(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jg     11df <read_redirected+0x9f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1219 <read_redirected+0xd9>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11f4 <read_redirected+0xb4>
jmp    120a <read_redirected+0xca>
jmp    11f9 <read_redirected+0xb9>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1175 <read_redirected+0x35>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_redirected>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x40,%r15d
mov    $0x40,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%r12
add    $0x40,%r12
mov    %rax,%r14
cmp    %r12,%rbx
jne    1192 <read_redirected+0x52>
nopw   0x0(%rax,%rax,1)
lea    0x40(%r15),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%rbx
movslq %r15d,%rax
lea    (%rax,%rbx,1),%r12
add    $0x40,%r12
mov    %ebp,%r15d
mov    %rbx,%r14
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11b6 <read_redirected+0x76>
cmpb   $0xa,(%rbx)
je     11c3 <read_redirected+0x83>
add    $0x1,%rbx
cmp    %r12,%rbx
jne    1192 <read_redirected+0x52>
jmp    1170 <read_redirected+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    11c6 <read_redirected+0x86>
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @read_redirected() {
%1 = tail call i64 @malloc(i32 64)
%2 = inttoptr i64 %1 to i8*
%3 = getelementptr inbounds i8, i8* %2, i64 64
br label %4
4: ; preds = %27, %0
%5 = phi i32 [ 64, %0 ], [ %16, %27 ]
%6 = phi i8* [ %2, %0 ], [ %28, %27 ]
%7 = phi i8* [ %2, %0 ], [ %18, %27 ]
%8 = phi i8* [ %3, %0 ], [ %19, %27 ]
%9 = icmp eq i8* %6, %8
br i1 %9, label %10, label %15
10: ; preds = %4
%11 = add nsw i32 %5, 64
%12 = tail call i8* @realloc(i8* %7, i32 %11)
%13 = sext i32 %11 to i64
%14 = getelementptr inbounds i8, i8* %12, i64 %13
br label %15
15: ; preds = %10, %4
%16 = phi i32 [ %11, %10 ], [ %5, %4 ]
%17 = phi i8* [ %12, %10 ], [ %6, %4 ]
%18 = phi i8* [ %12, %10 ], [ %7, %4 ]
%19 = phi i8* [ %14, %10 ], [ %8, %4 ]
%20 = tail call i64 @read(i32 0, i8* %17, i32 1)
%21 = icmp slt i64 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = tail call i32 @free(i8* %18)
br label %30
24: ; preds = %15
%25 = load i8, i8* %17
%26 = icmp eq i8 %25, 10
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = getelementptr inbounds i8, i8* %17, i64 1
br label %4
29: ; preds = %24
store i8 0, i8* %17
br label %30
30: ; preds = %29, %22
%31 = phi i8* [ null, %22 ], [ %18, %29 ]
ret i8* %31
}