#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close_file(int *);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int /*<<< orphan*/ write_title(char *);
void write_lines(char *arg) {
  int fd;
  if (*arg == '-') {
    fd = 0;
  } else {
    fd = open(arg, O_RDONLY);
  }
  if (fd < 0) {
    perror("Error while opening");
  }
  ssize_t read_status, seek_status, write_status;
  char memory;
  char *buffer = &memory;
  int nl_count = 0;
  seek_status = lseek(fd, 0, SEEK_END);
  if (seek_status == -1) {
    perror("Error while seeking");
  }
  do {
    seek_status = lseek(fd, -1, SEEK_CUR);
    if (seek_status == -1) {
      break;
    }
    read_status = read(fd, buffer, 1);
    if (read_status == -1) {
      perror("Error while reading");
    }
    seek_status = lseek(fd, -1, SEEK_CUR);
    if (*buffer == '\n') {
      nl_count++;
    }
  } while (nl_count != 11);
  write_title(arg);
  if (seek_status != -1) {
    lseek(fd, 1, SEEK_CUR);
  }
  do {
    read_status = read(fd, buffer, 1);
    if (read_status == -1) {
      perror("Error while reading");
    }
    if (read_status == 0) {
      break;
    }
    write_status = write(1, buffer, 1);
    if (write_status == -1) {
      perror("Error while writing");
    }
  } while ((read_status > 0) && (write_status != -1));
  close_file(&fd);
}


x86 assembly (clang, -O0):
<write_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    1198 <write_lines+0x28>
movl   $0x0,-0xc(%rbp)
jmp    11ad <write_lines+0x3d>
mov    -0x8(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c3 <write_lines+0x53>
lea    0xe42(%rip),%rdi
call   1090 <perror@plt>
lea    -0x19(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fe <write_lines+0x8e>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
jmp    1203 <write_lines+0x93>
mov    -0xc(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    122b <write_lines+0xbb>
jmp    1296 <write_lines+0x126>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1255 <write_lines+0xe5>
lea    0xdd8(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1287 <write_lines+0x117>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    128c <write_lines+0x11c>
cmpl   $0xb,-0x2c(%rbp)
jne    1203 <write_lines+0x93>
mov    -0x8(%rbp),%rdi
call   1070 <write_title@plt>
cmpl   $0xffffffff,-0x14(%rbp)
je     12bf <write_lines+0x14f>
mov    -0xc(%rbp),%edi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1050 <lseek@plt>
jmp    12c4 <write_lines+0x154>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12ee <write_lines+0x17e>
lea    0xd3f(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0x0,-0x10(%rbp)
jne    12fd <write_lines+0x18d>
jmp    134f <write_lines+0x1df>
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
mov    %edx,%edi
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1326 <write_lines+0x1b6>
lea    0xd1b(%rip),%rdi
call   1090 <perror@plt>
jmp    132b <write_lines+0x1bb>
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x2d(%rbp)
jle    1344 <write_lines+0x1d4>
cmpl   $0xffffffff,-0x18(%rbp)
setne  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    12c4 <write_lines+0x154>
lea    -0xc(%rbp),%rdi
call   1030 <close_file@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_lines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
cmpb   $0x2d,(%rdi)
jne    1190 <write_lines+0x20>
movl   $0x0,0x4(%rsp)
xor    %r13d,%r13d
jmp    11b0 <write_lines+0x40>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
mov    %eax,%r13d
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     12d0 <write_lines+0x160>
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r13d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     12bf <write_lines+0x14f>
mov    0x2e0b(%rip),%r12
lea    0x3(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %r13d,%edi
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     123d <write_lines+0xcd>
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     122f <write_lines+0xbf>
mov    (%r12),%edx
mov    %r13d,%edi
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
xor    %ecx,%ecx
cmpb   $0xa,0x3(%rsp)
sete   %cl
add    %ecx,%ebx
cmp    $0xb,%ebx
jne    11e0 <write_lines+0x70>
jmp    1247 <write_lines+0xd7>
lea    0xdf2(%rip),%rdi
call   1090 <perror@plt>
jmp    120b <write_lines+0x9b>
mov    %r14,%rdi
call   1070 <write_title@plt>
jmp    1267 <write_lines+0xf7>
mov    %eax,%ebp
mov    %r14,%rdi
call   1070 <write_title@plt>
cmp    $0xffffffff,%ebp
je     1267 <write_lines+0xf7>
mov    (%r12),%edx
mov    %r13d,%edi
mov    $0x1,%esi
call   1050 <lseek@plt>
lea    0x3(%rsp),%r14
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
je     12a6 <write_lines+0x136>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12e1 <write_lines+0x171>
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1306 <write_lines+0x196>
test   %ebp,%ebp
jg     1270 <write_lines+0x100>
lea    0x4(%rsp),%rdi
call   1030 <close_file@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4e(%rip),%rdi
call   1090 <perror@plt>
jmp    11ce <write_lines+0x5e>
lea    0xd29(%rip),%rdi
call   1090 <perror@plt>
jmp    11b0 <write_lines+0x40>
lea    0xd40(%rip),%rdi
call   1090 <perror@plt>
lea    0x3(%rsp),%rsi
mov    $0x1,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    12a6 <write_lines+0x136>
lea    0xd2f(%rip),%rdi
call   1090 <perror@plt>
jmp    12a6 <write_lines+0x136>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"Error while opening\00"
@SEEK_END = global i32 0
@.str.1 = [20 x i8] c"Error while seeking\00"
@SEEK_CUR = global i32 0
@.str.2 = [20 x i8] c"Error while reading\00"
@.str.3 = [20 x i8] c"Error while writing\00"
define void @write_lines(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 45
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 0, i32* %3
br label %19
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %3
br label %19
19: ; preds = %15, %14
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
store i8* %7, i8** %8
store i32 0, i32* %9
%25 = load i32, i32* %3
%26 = load i32, i32* @SEEK_END
%27 = call i32 @lseek(i32 %25, i32 0, i32 %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
br label %33
33: ; preds = %60, %32
%34 = load i32, i32* %3
%35 = load i32, i32* @SEEK_CUR
%36 = call i32 @lseek(i32 %34, i32 -1, i32 %35)
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %33
br label %63
40: ; preds = %33
%41 = load i32, i32* %3
%42 = load i8*, i8** %8
%43 = call i32 @read(i32 %41, i8* %42, i32 1)
store i32 %43, i32* %4
%44 = load i32, i32* %4
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %40
%49 = load i32, i32* %3
%50 = load i32, i32* @SEEK_CUR
%51 = call i32 @lseek(i32 %49, i32 -1, i32 %50)
store i32 %51, i32* %5
%52 = load i8*, i8** %8
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 10
br i1 %55, label %56, label %59
56: ; preds = %48
%57 = load i32, i32* %9
%58 = add nsw i32 %57, 1
store i32 %58, i32* %9
br label %59
59: ; preds = %56, %48
br label %60
60: ; preds = %59
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 11
br i1 %62, label %33, label %63
63: ; preds = %60, %39
%64 = load i8*, i8** %2
%65 = call i32 @write_title(i8* %64)
%66 = load i32, i32* %5
%67 = icmp ne i32 %66, -1
br i1 %67, label %68, label %72
68: ; preds = %63
%69 = load i32, i32* %3
%70 = load i32, i32* @SEEK_CUR
%71 = call i32 @lseek(i32 %69, i32 1, i32 %70)
br label %72
72: ; preds = %68, %63
br label %73
73: ; preds = %99, %72
%74 = load i32, i32* %3
%75 = load i8*, i8** %8
%76 = call i32 @read(i32 %74, i8* %75, i32 1)
store i32 %76, i32* %4
%77 = load i32, i32* %4
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %81
79: ; preds = %73
%80 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %81
81: ; preds = %79, %73
%82 = load i32, i32* %4
%83 = icmp eq i32 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %81
br label %101
85: ; preds = %81
%86 = load i8*, i8** %8
%87 = call i32 @write(i32 1, i8* %86, i32 1)
store i32 %87, i32* %6
%88 = load i32, i32* %6
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %92
90: ; preds = %85
%91 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %92
92: ; preds = %90, %85
br label %93
93: ; preds = %92
%94 = load i32, i32* %4
%95 = icmp sgt i32 %94, 0
br i1 %95, label %96, label %99
96: ; preds = %93
%97 = load i32, i32* %6
%98 = icmp ne i32 %97, -1
br label %99
99: ; preds = %96, %93
%100 = phi i1 [ false, %93 ], [ %98, %96 ]
br i1 %100, label %73, label %101
101: ; preds = %99, %84
%102 = call i32 @close_file(i32* %3)
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"Error while opening\00"
@SEEK_END = global i32 0
@.str.1 = [20 x i8] c"Error while seeking\00"
@SEEK_CUR = global i32 0
@.str.2 = [20 x i8] c"Error while reading\00"
@.str.3 = [20 x i8] c"Error while writing\00"
define void @write_lines(i8* %0) {
%2 = alloca i32
%3 = alloca i8
%4 = bitcast i32* %2 to i8*
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 45
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 0, i32* %2
br label %14
8: ; preds = %1
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %0, i32 %9)
store i32 %10, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %7, %12, %8
%15 = phi i32 [ 0, %7 ], [ %10, %12 ], [ %10, %8 ]
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @lseek(i32 %15, i32 0, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
br label %22
22: ; preds = %21, %34
%23 = phi i32 [ %40, %34 ], [ 0, %21 ]
%24 = load i32, i32* @SEEK_CUR
%25 = tail call i32 @lseek(i32 %15, i32 -1, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = tail call i32 @write_title(i8* %0)
br label %48
29: ; preds = %22
%30 = call i32 @read(i32 %15, i8* %3, i32 1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* @SEEK_CUR
%36 = tail call i32 @lseek(i32 %15, i32 -1, i32 %35)
%37 = load i8, i8* %3
%38 = icmp eq i8 %37, 10
%39 = zext i1 %38 to i32
%40 = add nuw nsw i32 %23, %39
%41 = icmp eq i32 %40, 11
br i1 %41, label %42, label %22
42: ; preds = %34
%43 = tail call i32 @write_title(i8* %0)
%44 = icmp eq i32 %36, -1
br i1 %44, label %48, label %45
45: ; preds = %42
%46 = load i32, i32* @SEEK_CUR
%47 = tail call i32 @lseek(i32 %15, i32 1, i32 %46)
br label %48
48: ; preds = %27, %45, %42
br label %49
49: ; preds = %48, %60
%50 = call i32 @read(i32 %15, i8* %3, i32 1)
switch i32 %50, label %51 [
i32 -1, label %54
i32 0, label %62
]
51: ; preds = %49
%52 = call i32 @write(i32 1, i8* %3, i32 1)
%53 = icmp eq i32 %52, -1
br i1 %53, label %58, label %60
54: ; preds = %49
%55 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @write(i32 1, i8* %3, i32 1)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %62
58: ; preds = %51, %54
%59 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %62
60: ; preds = %51
%61 = icmp sgt i32 %50, 0
br i1 %61, label %49, label %62
62: ; preds = %49, %60, %54, %58
%63 = call i32 @close_file(i32* %2)
ret void
}