#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 */
char EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int word_count = 0, line_count = 0;
  char read_char;
  FILE *fp = fopen("/etc/passwd", "r");
  if (fp == NULL) {
    perror("An error occured: ");
    exit(1);
  }
  printf("Reading file: \n");
  do {
    read_char = fgetc(fp);
    if (read_char == '\n') {
      line_count++;
    }
    word_count++;
  } while (read_char != EOF);
  word_count--;
  printf("word_count=%d, line_count=%d \n", word_count, line_count);
  fclose(fp);
  return 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,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xe75(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bc <main+0x5c>
lea    0xe61(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    11ec <main+0x8c>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movsbl -0x19(%rbp),%eax
mov    0x2de8(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11ca <main+0x6a>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe82(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f0 <main+0x80>
mov    %rax,%r14
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    $0xffffffff,%ebx
mov    0x2e3c(%rip),%r15
nopl   0x0(%rax)
mov    %r14,%rdi
call   1060 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
add    %ecx,%ebp
add    $0x1,%ebx
cmp    (%r15),%al
jne    11b0 <main+0x40>
lea    0xe51(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"An error occured: \00"
@.str.3 = [16 x i8] c"Reading file: \0A\00"
@EOF = global i8 0
@.str.4 = [31 x i8] c"word_count=%d, line_count=%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* %6
store i32 0, i32* %7
%10 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %9
%11 = load i32*, i32** %9
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %30, %16
%19 = load i32*, i32** %9
%20 = call signext i8 @fgetc(i32* %19)
store i8 %20, i8* %8
%21 = load i8, i8* %8
%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* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %27
27: ; preds = %24, %18
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %30
30: ; preds = %27
%31 = load i8, i8* %8
%32 = sext i8 %31 to i32
%33 = load i8, i8* @EOF
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %32, %34
br i1 %35, label %18, label %36
36: ; preds = %30
%37 = load i32, i32* %6
%38 = add nsw i32 %37, -1
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = load i32, i32* %7
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %39, i32 %40)
%42 = load i32*, i32** %9
%43 = call i32 @fclose(i32* %42)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"An error occured: \00"
@EOF = global i8 0
@.str.4 = [31 x i8] c"word_count=%d, line_count=%d \0A\00"
@str = [15 x i8] c"Reading file: \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %10, %8
%11 = phi i32 [ 0, %8 ], [ %16, %10 ]
%12 = phi i32 [ 0, %8 ], [ %17, %10 ]
%13 = tail call signext i8 @fgetc(i32* %3)
%14 = icmp eq i8 %13, 10
%15 = zext i1 %14 to i32
%16 = add nuw nsw i32 %11, %15
%17 = add nuw nsw i32 %12, 1
%18 = load i8, i8* @EOF
%19 = icmp eq i8 %13, %18
br i1 %19, label %20, label %10
20: ; preds = %10
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %12, i32 %16)
%22 = tail call i32 @fclose(i32* %3)
ret i32 0
}