#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 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_head(char *arg) {
  char memory;
  char *buff = &memory;
  int fd = open(arg, O_RDONLY);
  if (fd == -1) {
    perror("Error while opening");
  }
  write_title(arg);
  ssize_t read_status, write_status;
  int nl_count = 0;
  do {
    read_status = read(fd, buff, 1);
    if (read_status == -1) {
      perror("Error while reading");
    }
    if (*buff == '\n') {
      nl_count++;
    }
    if ((nl_count <= 10) && (read_status != 0)) {
      write_status = write(1, buff, 1);
      if (write_status == -1) {
        perror("Error while writing");
      }
    }
  } while ((read_status != 0) && (nl_count != 10));
}


x86 assembly (clang, -O0):
<write_head>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x9(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    118f <write_head+0x3f>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%rdi
call   1050 <write_title@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c9 <write_head+0x79>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11e2 <write_head+0x92>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0xa,-0x28(%rbp)
jg     1224 <write_head+0xd4>
cmpl   $0x0,-0x20(%rbp)
je     1224 <write_head+0xd4>
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
mov    %edx,%edi
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    121f <write_head+0xcf>
lea    0xe0e(%rip),%rdi
call   1070 <perror@plt>
jmp    1224 <write_head+0xd4>
jmp    1229 <write_head+0xd9>
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x29(%rbp)
je     1242 <write_head+0xf2>
cmpl   $0xa,-0x28(%rbp)
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    119f <write_head+0x4f>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_head>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     1213 <write_head+0xc3>
mov    %rbp,%rdi
call   1050 <write_title@plt>
xor    %ebx,%ebx
lea    0x7(%rsp),%r14
lea    0xe86(%rip),%r15
lea    0xe93(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11ee <write_head+0x9e>
xor    %eax,%eax
cmpb   $0xa,0x7(%rsp)
sete   %al
add    %eax,%ebx
test   %ebp,%ebp
je     11e3 <write_head+0x93>
cmp    $0xa,%ebx
ja     11e3 <write_head+0x93>
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11f8 <write_head+0xa8>
test   %ebp,%ebp
je     1204 <write_head+0xb4>
cmp    $0xa,%ebx
jne    11a0 <write_head+0x50>
jmp    1204 <write_head+0xb4>
mov    %r15,%rdi
call   1070 <perror@plt>
jmp    11b7 <write_head+0x67>
mov    %r12,%rdi
call   1070 <perror@plt>
test   %ebp,%ebp
jne    11e7 <write_head+0x97>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde6(%rip),%rdi
call   1070 <perror@plt>
jmp    1178 <write_head+0x28>

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"
@.str.2 = [20 x i8] c"Error while writing\00"
define void @write_head(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
store i8* %3, i8** %4
%9 = load i8*, i8** %2
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = load i8*, i8** %2
%18 = call i32 @write_title(i8* %17)
store i32 0, i32* %8
br label %19
19: ; preds = %56, %16
%20 = load i32, i32* %5
%21 = load i8*, i8** %4
%22 = call i32 @read(i32 %20, i8* %21, i32 1)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %19
%28 = load i8*, i8** %4
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 10
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* %8
%34 = add nsw i32 %33, 1
store i32 %34, i32* %8
br label %35
35: ; preds = %32, %27
%36 = load i32, i32* %8
%37 = icmp sle i32 %36, 10
br i1 %37, label %38, label %49
38: ; preds = %35
%39 = load i32, i32* %6
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %38
%42 = load i8*, i8** %4
%43 = call i32 @write(i32 1, i8* %42, i32 1)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %41
%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, %41
br label %49
49: ; preds = %48, %38, %35
br label %50
50: ; preds = %49
%51 = load i32, i32* %6
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32, i32* %8
%55 = icmp ne i32 %54, 10
br label %56
56: ; preds = %53, %50
%57 = phi i1 [ false, %50 ], [ %55, %53 ]
br i1 %57, label %19, label %58
58: ; preds = %56
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"
@.str.2 = [20 x i8] c"Error while writing\00"
define void @write_head(i8* %0) {
%2 = alloca i8
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = tail call i32 @write_title(i8* %0)
br label %10
10: ; preds = %29, %8
%11 = phi i32 [ 0, %8 ], [ %20, %29 ]
%12 = call i32 @read(i32 %4, i8* %2, i32 1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = load i8, i8* %2
%18 = icmp eq i8 %17, 10
%19 = zext i1 %18 to i32
%20 = add nuw nsw i32 %11, %19
%21 = icmp ult i32 %20, 11
%22 = icmp ne i32 %12, 0
%23 = and i1 %22, %21
br i1 %23, label %24, label %29
24: ; preds = %16
%25 = call i32 @write(i32 1, i8* %2, i32 1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %16, %27, %24
%30 = icmp ne i32 %20, 10
%31 = select i1 %22, i1 %30, i1 false
br i1 %31, label %10, label %32
32: ; preds = %29
ret void
}