#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_SET;
int /*<<< orphan*/ close_file(int *);
int lseek(int, unsigned long, 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_last_10_lines(int argc, char *arg) {
  char memory;
  char *buffer = &memory;
  long unsigned int new_lines[11];
  long unsigned int write_point;
  long unsigned int read_bytes_count = 0;
  int count_nl_found = -1;
  int is_hole_file = 1;
  int fd = open(arg, O_RDONLY);
  ssize_t read_return_value, write_return_value, seek_status;
  if (fd < 0) {
    perror("Error while opening");
  }
  do {
    read_return_value = read(fd, buffer, 1);
    if (read_return_value <= 0) {
      if (read_return_value < 0) {
        perror("Error while reading");
      }
      break;
    } else {
      read_bytes_count++;
      if (*buffer == '\n') {
        new_lines[count_nl_found] = read_bytes_count;
        count_nl_found++;
        if (count_nl_found == 11) {
          count_nl_found = 0;
          is_hole_file = 0;
        }
      }
    }
  } while ((read_return_value != 0) && (read_return_value != -1));
  if (is_hole_file == 0) {
    write_point = new_lines[count_nl_found];
  } else if (is_hole_file == 1) {
    write_point = 0;
  }
  write_title(arg);
  do {
    seek_status = lseek(fd, write_point, SEEK_SET);
    if (seek_status < 0) {
      perror("Error while seeking");
    }
    write_point++;
    read_return_value = read(fd, buffer, 1);
    if (read_return_value < 0) {
      perror("Error while reading");
    }
    write_return_value = write(1, buffer, 1);
    if (write_return_value < 0) {
      perror("Error while writing");
    }
  } while ((write_return_value > 0) && (write_point != read_bytes_count) &&
           (read_return_value > 0));
  close_file(&fd);
}


x86 assembly (clang, -O0):
<write_last_10_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x11(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x90(%rbp)
movl   $0xffffffff,-0x94(%rbp)
ff ff ff
movl   $0x1,-0x98(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x9c(%rbp)
cmpl   $0x0,-0x9c(%rbp)
jge    11da <write_last_10_lines+0x6a>
lea    0xe2b(%rip),%rdi
call   1090 <perror@plt>
jmp    11df <write_last_10_lines+0x6f>
mov    -0x9c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0xa0(%rbp)
cmpl   $0x0,-0xa0(%rbp)
jg     1224 <write_last_10_lines+0xb4>
cmpl   $0x0,-0xa0(%rbp)
jge    121f <write_last_10_lines+0xaf>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
jmp    12cb <write_last_10_lines+0x15b>
mov    -0x90(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x90(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    128e <write_last_10_lines+0x11e>
mov    -0x90(%rbp),%rcx
movslq -0x94(%rbp),%rax
mov    %rcx,-0x80(%rbp,%rax,8)
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
cmpl   $0xb,-0x94(%rbp)
jne    1289 <write_last_10_lines+0x119>
movl   $0x0,-0x94(%rbp)
movl   $0x0,-0x98(%rbp)
jmp    128e <write_last_10_lines+0x11e>
jmp    1293 <write_last_10_lines+0x123>
jmp    1298 <write_last_10_lines+0x128>
xor    %eax,%eax
cmpl   $0x0,-0xa0(%rbp)
mov    %al,-0xa9(%rbp)
je     12bd <write_last_10_lines+0x14d>
cmpl   $0xffffffff,-0xa0(%rbp)
setne  %al
mov    %al,-0xa9(%rbp)
mov    -0xa9(%rbp),%al
test   $0x1,%al
jne    11df <write_last_10_lines+0x6f>
cmpl   $0x0,-0x98(%rbp)
jne    12f0 <write_last_10_lines+0x180>
movslq -0x94(%rbp),%rax
mov    -0x80(%rbp,%rax,8),%rax
mov    %rax,-0x88(%rbp)
jmp    130d <write_last_10_lines+0x19d>
cmpl   $0x1,-0x98(%rbp)
jne    1308 <write_last_10_lines+0x198>
movq   $0x0,-0x88(%rbp)
jmp    130d <write_last_10_lines+0x19d>
mov    -0x10(%rbp),%rdi
call   1070 <write_title@plt>
mov    -0x9c(%rbp),%edi
mov    -0x88(%rbp),%rsi
mov    0x2cae(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0xa8(%rbp)
cmpl   $0x0,-0xa8(%rbp)
jge    1350 <write_last_10_lines+0x1e0>
lea    0xcdd(%rip),%rdi
call   1090 <perror@plt>
mov    -0x88(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x88(%rbp)
mov    -0x9c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0xa0(%rbp)
cmpl   $0x0,-0xa0(%rbp)
jge    1395 <write_last_10_lines+0x225>
lea    0xc84(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
mov    %edx,%edi
call   1040 <write@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x0,-0xa4(%rbp)
jge    13c4 <write_last_10_lines+0x254>
lea    0xc7d(%rip),%rdi
call   1090 <perror@plt>
jmp    13c9 <write_last_10_lines+0x259>
xor    %eax,%eax
cmpl   $0x0,-0xa4(%rbp)
mov    %al,-0xaa(%rbp)
jle    140a <write_last_10_lines+0x29a>
mov    -0x88(%rbp),%rcx
xor    %eax,%eax
cmp    -0x90(%rbp),%rcx
mov    %al,-0xaa(%rbp)
je     140a <write_last_10_lines+0x29a>
cmpl   $0x0,-0xa0(%rbp)
setg   %al
mov    %al,-0xaa(%rbp)
mov    -0xaa(%rbp),%al
test   $0x1,%al
jne    1316 <write_last_10_lines+0x1a6>
lea    -0x9c(%rbp),%rdi
call   1030 <close_file@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_last_10_lines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%r15
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <open@plt>
mov    %eax,%r13d
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     12db <write_last_10_lines+0x16b>
lea    0x3(%rsp),%rsi
mov    $0x1,%ebp
mov    %r13d,%edi
mov    $0x1,%edx
call   1060 <read@plt>
xor    %r14d,%r14d
test   %eax,%eax
jle    1228 <write_last_10_lines+0xb8>
mov    %r15,0x8(%rsp)
mov    $0xffffffff,%ebx
mov    $0x1,%ebp
lea    0x3(%rsp),%r15
xor    %r12d,%r12d
jmp    11f4 <write_last_10_lines+0x84>
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1217 <write_last_10_lines+0xa7>
add    $0x1,%r12
cmpb   $0xa,0x3(%rsp)
jne    11e0 <write_last_10_lines+0x70>
movslq %ebx,%rbx
mov    %r12,0x10(%rsp,%rbx,8)
add    $0x1,%ebx
cmp    $0xb,%ebx
cmove  %r14d,%ebx
cmove  %r14d,%ebp
jmp    11e0 <write_last_10_lines+0x70>
mov    0x8(%rsp),%r15
test   %eax,%eax
js     1234 <write_last_10_lines+0xc4>
test   %ebp,%ebp
je     1244 <write_last_10_lines+0xd4>
xor    %ebx,%ebx
jmp    124c <write_last_10_lines+0xdc>
mov    $0xffffffff,%ebx
xor    %r12d,%r12d
test   %eax,%eax
jns    1220 <write_last_10_lines+0xb0>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
test   %ebp,%ebp
jne    1224 <write_last_10_lines+0xb4>
movslq %ebx,%rax
mov    0x10(%rsp,%rax,8),%rbx
mov    %r15,%rdi
call   1070 <write_title@plt>
add    $0xffffffffffffffff,%r12
mov    0x2d79(%rip),%r14
lea    0x3(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14),%edx
mov    %r13d,%edi
mov    %rbx,%rsi
call   1050 <lseek@plt>
test   %eax,%eax
js     12bf <write_last_10_lines+0x14f>
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12cd <write_last_10_lines+0x15d>
mov    $0x1,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
test   %eax,%eax
js     12ec <write_last_10_lines+0x17c>
je     12f8 <write_last_10_lines+0x188>
cmp    %rbx,%r12
je     12f8 <write_last_10_lines+0x188>
add    $0x1,%rbx
test   %ebp,%ebp
jg     1270 <write_last_10_lines+0x100>
jmp    12f8 <write_last_10_lines+0x188>
lea    0xd62(%rip),%rdi
call   1090 <perror@plt>
jmp    1282 <write_last_10_lines+0x112>
lea    0xd40(%rip),%rdi
call   1090 <perror@plt>
jmp    1298 <write_last_10_lines+0x128>
lea    0xd1e(%rip),%rdi
call   1090 <perror@plt>
jmp    11a1 <write_last_10_lines+0x31>
lea    0xd49(%rip),%rdi
call   1090 <perror@plt>
lea    0x4(%rsp),%rdi
call   1030 <close_file@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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