#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*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int /*<<< orphan*/, char *, int);
int main() {
  int result;
  int wresult;
  int written_bytes;
  int i, lines = 0;
  char buffer[100];
  char first_ten[100];
  int fd = open("a.txt", O_RDONLY);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  while (result = read(fd, buffer, 100)) {
    for (i = 0; i < result; i++) {
      if (buffer[i] == '\n')
        lines++;
      if (lines < 10) {
        first_ten[i] = buffer[i];
      } else {
        first_ten[i] = '\n';
        break;
      }
    }
    if (result < 0) {
      perror("read");
      return 2;
    }
    written_bytes = 0;
    while (written_bytes <= i) {
      wresult = write(STDOUT_FILENO, first_ten + written_bytes, 1);
      if (wresult < 0) {
        perror("write");
        return 3;
      }
      written_bytes += wresult;
    }
  }
  result = close(fd);
  if (result < 0) {
    perror("close");
    return 4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xf4(%rbp)
cmpl   $0x0,-0xf4(%rbp)
jge    11a9 <main+0x59>
lea    0xe6e(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1307 <main+0x1b7>
jmp    11ae <main+0x5e>
mov    -0xf4(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     12d0 <main+0x180>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1242 <main+0xf2>
movslq -0x14(%rbp),%rax
movsbl -0x80(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    11fc <main+0xac>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jge    121e <main+0xce>
movslq -0x14(%rbp),%rax
mov    -0x80(%rbp,%rax,1),%cl
movslq -0x14(%rbp),%rax
mov    %cl,-0xf0(%rbp,%rax,1)
jmp    122f <main+0xdf>
movslq -0x14(%rbp),%rax
movb   $0xa,-0xf0(%rbp,%rax,1)
0a
jmp    1242 <main+0xf2>
jmp    1234 <main+0xe4>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d5 <main+0x85>
cmpl   $0x0,-0x8(%rbp)
jge    1264 <main+0x114>
lea    0xdb8(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1307 <main+0x1b7>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     12cb <main+0x17b>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    -0xf0(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12bd <main+0x16d>
lea    0xd64(%rip),%rdi
call   1070 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1307 <main+0x1b7>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    126b <main+0x11b>
jmp    11ae <main+0x5e>
mov    -0xf4(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1300 <main+0x1b0>
lea    0xd27(%rip),%rdi
call   1070 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    1307 <main+0x1b7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1287 <main+0x137>
mov    %eax,%r13d
lea    0x70(%rsp),%rsi
mov    %eax,%edi
mov    $0x64,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1246 <main+0xf6>
xor    %r12d,%r12d
mov    0x2e4c(%rip),%rbx
lea    0x70(%rsp),%r15
nopl   0x0(%rax)
mov    $0x2,%ebp
test   %eax,%eax
jle    1295 <main+0x145>
mov    %eax,%eax
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edx
movzbl 0x70(%rsp,%r14,1),%ecx
xor    %r12d,%r12d
cmp    $0xa,%cl
sete   %r12b
add    %edx,%r12d
cmp    $0x9,%r12d
jg     1200 <main+0xb0>
mov    %cl,(%rsp,%r14,1)
add    $0x1,%r14
cmp    %r14,%rax
jne    11d0 <main+0x80>
mov    %eax,%r14d
jmp    1205 <main+0xb5>
xchg   %ax,%ax
movb   $0xa,(%rsp,%r14,1)
test   %eax,%eax
js     1295 <main+0x145>
xor    %ebp,%ebp
nop
mov    (%rbx),%edi
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1262 <main+0x112>
add    %eax,%ebp
cmp    %r14d,%ebp
jle    1210 <main+0xc0>
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x64,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    11b0 <main+0x60>
mov    %r13d,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
test   %eax,%eax
jns    1273 <main+0x123>
mov    $0x4,%ebp
lea    0xdb6(%rip),%rdi
jmp    126e <main+0x11e>
mov    $0x3,%ebp
lea    0xda2(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebp
lea    0xd73(%rip),%rdi
jmp    126e <main+0x11e>
lea    0xd6f(%rip),%rdi
jmp    126e <main+0x11e>

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

LLVM IR (-O2):
@.str = [6 x i8] c"a.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %53, label %8
8: ; preds = %0
%9 = call i32 @read(i32 %6, i8* %3, i32 100)
%10 = icmp eq i32 %9, 0
br i1 %10, label %50, label %14
11: ; preds = %40
%12 = call i32 @read(i32 %6, i8* %3, i32 100)
%13 = icmp eq i32 %12, 0
br i1 %13, label %50, label %14
14: ; preds = %8, %11
%15 = phi i32 [ %12, %11 ], [ %9, %8 ]
%16 = phi i32 [ %27, %11 ], [ 0, %8 ]
%17 = icmp sgt i32 %15, 0
br i1 %17, label %18, label %53
18: ; preds = %14
%19 = zext i32 %15 to i64
br label %20
20: ; preds = %18, %29
%21 = phi i64 [ 0, %18 ], [ %31, %29 ]
%22 = phi i32 [ %16, %18 ], [ %27, %29 ]
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %21
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 10
%26 = zext i1 %25 to i32
%27 = add nsw i32 %22, %26
%28 = icmp slt i32 %27, 10
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %21
store i8 %24, i8* %30
%31 = add nuw nsw i64 %21, 1
%32 = icmp eq i64 %31, %19
br i1 %32, label %37, label %20
33: ; preds = %20
%34 = trunc i64 %21 to i32
%35 = and i64 %21, 4294967295
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %35
store i8 10, i8* %36
br label %37
37: ; preds = %29, %33
%38 = phi i32 [ %34, %33 ], [ %15, %29 ]
%39 = icmp slt i32 %15, 0
br i1 %39, label %53, label %42
40: ; preds = %42
%41 = icmp sgt i32 %49, %38
br i1 %41, label %11, label %42
42: ; preds = %37, %40
%43 = phi i32 [ %49, %40 ], [ 0, %37 ]
%44 = load i32, i32* @STDOUT_FILENO
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %45
%47 = call i32 @write(i32 %44, i8* %46, i32 1)
%48 = icmp slt i32 %47, 0
%49 = add nsw i32 %47, %43
br i1 %48, label %53, label %40
50: ; preds = %11, %8
%51 = tail call i32 @close(i32 %6)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %37, %14, %42, %50, %0
%54 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), %50 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %42 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %14 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %37 ]
%55 = phi i32 [ 1, %0 ], [ 4, %50 ], [ 3, %42 ], [ 2, %14 ], [ 2, %37 ]
%56 = tail call i32 @perror(i8* %54)
br label %57
57: ; preds = %53, %50
%58 = phi i32 [ 0, %50 ], [ %55, %53 ]
ret i32 %58
}