#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 fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int c;
  int lines = 0;
  printf("Program that counts lines.\n");
  if (argc < 2) {
    printf("Usage: countlines filename\n");
    exit(1);
  }
  char *fileName = argv[1];
  FILE *fd = fopen(fileName, "r");
  if (fd == NULL) {
    printf("Could not open file %s\n", fileName);
    exit(1);
  }
  while ((c = fgetc(fd)) != -1) {
    if (c == '\n') {
      lines++;
    }
  }
  printf("Total lines: %d\n", lines);
  exit(0);
}


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)
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jge    118d <main+0x4d>
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d4 <main+0x94>
mov    -0x20(%rbp),%rsi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11d9 <main+0x99>
mov    -0x28(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1206 <main+0xc6>
cmpl   $0xa,-0x14(%rbp)
jne    1201 <main+0xc1>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11d9 <main+0x99>
mov    -0x18(%rbp),%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x1,%ebp
jg     117f <main+0x2f>
lea    0xed6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x8(%rbx),%rbp
lea    0xe76(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
jne    11b2 <main+0x62>
lea    0xe64(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
jmp    11c0 <main+0x70>
add    $0x1,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    $0xa,%eax
je     11b9 <main+0x69>
cmp    $0xffffffff,%eax
jne    11c0 <main+0x70>
lea    0xe41(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

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