#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*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ putchar(char);
scalar_t__ read(int, char *, int);
int main(void) {
  int fd, linecnt = 0;
  char c;
  char *fname = "test.txt";
  if ((fd = open(fname, O_RDONLY)) < 0) {
    perror("open()");
    exit(-1);
  }
  while (read(fd, &c, 1) > 0) {
    putchar(c);
    if (c == '\n')
      linecnt++;
  }
  printf("Total line: %d\n", linecnt);
  read(0, &c, 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11b5 <main+0x55>
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    11ba <main+0x5a>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jle    11f9 <main+0x99>
movsbl -0xd(%rbp),%edi
call   1030 <putchar@plt>
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
jne    11f4 <main+0x94>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ba <main+0x5a>
mov    -0xc(%rbp),%esi
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11f5 <main+0x95>
mov    %eax,%r15d
lea    0x7(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
xor    %ebp,%ebp
test   %rax,%rax
jle    11c9 <main+0x69>
lea    0x7(%rsp),%r14
movsbl 0x7(%rsp),%edi
xor    %ebx,%ebx
cmp    $0xa,%edi
sete   %bl
call   1030 <putchar@plt>
add    %ebx,%ebp
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jg     11a0 <main+0x40>
lea    0xe40(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
lea    0x7(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <read@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [16 x i8] c"Total line: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %3
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
br label %14
14: ; preds = %27, %13
%15 = load i32, i32* %2
%16 = call i64 @read(i32 %15, i8* %4, i32 1)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = load i8, i8* %4
%20 = call i32 @putchar(i8 signext %19)
%21 = load i8, i8* %4
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %27
27: ; preds = %24, %18
br label %14
28: ; preds = %14
%29 = load i32, i32* %3
%30 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = call i64 @read(i32 0, i8* %4, i32 1)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [16 x i8] c"Total line: %d\0A\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i64 @read(i32 %3, i8* %1, i32 1)
%7 = icmp sgt i64 %6, 0
br i1 %7, label %11, label %20
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %5, %11
%12 = phi i32 [ %17, %11 ], [ 0, %5 ]
%13 = load i8, i8* %1
%14 = tail call i32 @putchar(i8 signext %13)
%15 = icmp eq i8 %13, 10
%16 = zext i1 %15 to i32
%17 = add nuw nsw i32 %12, %16
%18 = call i64 @read(i32 %3, i8* %1, i32 1)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %11, label %20
20: ; preds = %11, %5
%21 = phi i32 [ 0, %5 ], [ %17, %11 ]
%22 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i64 @read(i32 0, i8* %1, i32 1)
ret i32 0
}