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

/* Variables and functions */
char *MAP_FAILED;
int MAP_FILE;
int MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_READ;
int PROT_WRITE;
scalar_t__ close(int);
int /*<<< orphan*/ emit(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *stdout;
int do_stream(char *file_name) {
  FILE *output_stream = stdout;
  int fd = open(file_name, O_RDONLY);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  struct stat stat;
  if (fstat(fd, &stat) < 0) {
    perror("fstat");
    exit(1);
  }
  char *sof = mmap(NULL, (size_t)stat.st_size, PROT_READ | PROT_WRITE,
                   MAP_FILE | MAP_PRIVATE, fd, (off_t)0);
  if (sof == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  char *eof = sof + stat.st_size;
  char *sol = sof;
  char *eol;
  char *p = sof;
  while (p < eof) {
    if (*p == '\n') {
      *p = 0;
      eol = p;
      emit(sol, eol, output_stream);
      p++;
      sol = p;
    } else {
      p++;
    }
  }
  if (close(fd) < 0) {
    perror("close");
    exit(1);
  }
  return 1;
}


x86 assembly (clang, -O0):
<do_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11bf <do_stream+0x4f>
lea    0xe50(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    11eb <do_stream+0x7b>
lea    0xe29(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movslq -0x18(%rbp),%rsi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    0x2dc9(%rip),%rax
or     (%rax),%edx
mov    0x2dd0(%rip),%rax
mov    (%rax),%ecx
mov    0x2da7(%rip),%rax
or     (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d7e(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <do_stream+0xe1>
lea    0xdc9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    12d8 <do_stream+0x168>
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    12c7 <do_stream+0x157>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1070 <emit@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    12d3 <do_stream+0x163>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1270 <do_stream+0x100>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1300 <do_stream+0x190>
lea    0xd1f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_stream>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e42(%rip),%rax
mov    (%rax),%r15
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1245 <do_stream+0xd5>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     124e <do_stream+0xde>
movslq (%rsp),%rbp
mov    0x2e40(%rip),%rax
mov    0x2e09(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0e(%rip),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dc1(%rip),%rax
cmp    (%rax),%rbx
je     1257 <do_stream+0xe7>
test   %ebp,%ebp
jle    1228 <do_stream+0xb8>
add    %rbx,%rbp
mov    %rbx,%rdi
jmp    1209 <do_stream+0x99>
add    $0x1,%rbx
cmp    %rbp,%rbx
jae    1228 <do_stream+0xb8>
cmpb   $0xa,(%rbx)
jne    1200 <do_stream+0x90>
movb   $0x0,(%rbx)
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <emit@plt>
add    $0x1,%rbx
mov    %rbx,%rdi
cmp    %rbp,%rbx
jb     1209 <do_stream+0x99>
mov    %r14d,%edi
call   1040 <close@plt>
test   %rax,%rax
js     1260 <do_stream+0xf0>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb4(%rip),%rdi
jmp    1267 <do_stream+0xf7>
lea    0xdb0(%rip),%rdi
jmp    1267 <do_stream+0xf7>
lea    0xdad(%rip),%rdi
jmp    1267 <do_stream+0xf7>
lea    0xda9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@stdout = global i32* null
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FILE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [6 x i8] c"close\00"
define i32 @do_stream(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = load i32*, i32** @stdout
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = bitcast %struct.stat* %2 to i8*
%12 = call i64 @fstat(i32 %5, %struct.stat* %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = sext i32 %19 to i64
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %22, %21
%24 = load i32, i32* @MAP_FILE
%25 = load i32, i32* @MAP_PRIVATE
%26 = or i32 %25, %24
%27 = tail call i8* @mmap(i32* null, i64 %20, i32 %23, i32 %26, i32 %5, i32 0)
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %33
30: ; preds = %17
%31 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %17
%34 = getelementptr inbounds i8, i8* %27, i64 %20
%35 = icmp sgt i32 %19, 0
br i1 %35, label %36, label %50
36: ; preds = %33, %46
%37 = phi i8* [ %48, %46 ], [ %27, %33 ]
%38 = phi i8* [ %47, %46 ], [ %27, %33 ]
%39 = load i8, i8* %37
%40 = icmp eq i8 %39, 10
br i1 %40, label %41, label %44
41: ; preds = %36
store i8 0, i8* %37
%42 = tail call i32 @emit(i8* %38, i8* %37, i32* %3)
%43 = getelementptr inbounds i8, i8* %37, i64 1
br label %46
44: ; preds = %36
%45 = getelementptr inbounds i8, i8* %37, i64 1
br label %46
46: ; preds = %44, %41
%47 = phi i8* [ %43, %41 ], [ %38, %44 ]
%48 = phi i8* [ %43, %41 ], [ %45, %44 ]
%49 = icmp ult i8* %48, %34
br i1 %49, label %36, label %50
50: ; preds = %46, %33
%51 = tail call i64 @close(i32 %5)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%55 = tail call i32 @exit(i32 1)
unreachable
56: ; preds = %50
ret i32 1
}