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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11a5 <main+0x55>
lea    0xe73(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11aa <main+0x5a>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    11e2 <main+0x92>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    11dd <main+0x8d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11aa <main+0x5a>
mov    -0xc(%rbp),%esi
lea    0xe24(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11c8 <main+0x78>
mov    %eax,%ebx
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %ebp,%ebp
test   %eax,%eax
jle    11af <main+0x5f>
lea    0xf(%rsp),%r14
nop
xor    %eax,%eax
cmpb   $0xa,0xf(%rsp)
sete   %al
add    %eax,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jg     1190 <main+0x40>
lea    0xe5a(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <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 i32
%5 = alloca i8
%6 = 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** %6
%7 = load i8*, i8** %6
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
br label %15
15: ; preds = %26, %14
%16 = load i32, i32* %2
%17 = call i32 @read(i32 %16, i8* %5, i32 1)
store i32 %17, i32* %4
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i8, i8* %5
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %26
26: ; preds = %23, %19
br label %15
27: ; preds = %15
%28 = load i32, i32* %3
%29 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %28)
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 i32 @read(i32 %3, i8* %1, i32 1)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %11, label %19
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 [ %16, %11 ], [ 0, %5 ]
%13 = load i8, i8* %1
%14 = icmp eq i8 %13, 10
%15 = zext i1 %14 to i32
%16 = add nuw nsw i32 %12, %15
%17 = call i32 @read(i32 %3, i8* %1, i32 1)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %11, label %19
19: ; preds = %11, %5
%20 = phi i32 [ 0, %5 ], [ %16, %11 ]
%21 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %20)
ret i32 0
}