#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int c;
  int lines = 0;
  char *fileName = argv[1];
  FILE *fd = fopen(fileName, "r");
  printf("Program that counts lines.\n");
  if (fd == NULL) {
    printf("Could not open file.\n");
    exit(1);
  }
  while ((c = fgetc(fd)) != -1) {
    if (c == 10) {
      lines++;
    }
  }
  fclose(fd);
  printf("Total lines: %d\n", lines);
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11be <main+0x6e>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11c3 <main+0x73>
mov    -0x28(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     11f0 <main+0xa0>
cmpl   $0xa,-0x14(%rbp)
jne    11eb <main+0x9b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c3 <main+0x73>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
test   %rbx,%rbx
jne    119d <main+0x3d>
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %ebp,%ebp
jmp    11b0 <main+0x50>
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    $0xa,%eax
je     11a1 <main+0x41>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x50>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe31(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [28 x i8] c"Program that counts lines.\0A\00"
@.str.2 = [22 x i8] c"Could not open file.\0A\00"
@.str.3 = [17 x i8] c"Total lines: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %9
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32*, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
br label %22
22: ; preds = %32, %21
%23 = load i32*, i32** %9
%24 = call i32 @fgetc(i32* %23)
store i32 %24, i32* %6
%25 = icmp ne i32 %24, -1
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %32
32: ; preds = %29, %26
br label %22
33: ; preds = %22
%34 = load i32*, i32** %9
%35 = call i32 @fclose(i32* %34)
%36 = load i32, i32* %7
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.3 = [17 x i8] c"Total lines: %d\0A\00"
@str = [27 x i8] c"Program that counts lines.\00"
@str.4 = [21 x i8] c"Could not open file.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%7 = icmp eq i32* %5, null
br i1 %7, label %8, label %15
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.4, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %15, %11
%12 = tail call i32 @fgetc(i32* %5)
switch i32 %12, label %11 [
i32 -1, label %17
i32 10, label %13
]
13: ; preds = %11
%14 = add nuw nsw i32 %16, 1
br label %15
15: ; preds = %2, %13
%16 = phi i32 [ %14, %13 ], [ 0, %2 ]
br label %11
17: ; preds = %11
%18 = tail call i32 @fclose(i32* %5)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%20 = tail call i32 @exit(i32 1)
unreachable
}