#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int main() {
  FILE *fp;
  int count = 0;
  char filename[40];
  char sampleChar;
  printf("Enter file name: ");
  scanf("%s", filename);
  fp = fopen(filename, "r");
  if (fp == NULL) {
    printf("Could not open file %s", filename);
    return 0;
  }
  for (sampleChar = getc(fp); sampleChar != EOF; sampleChar = getc(fp)) {
    if (sampleChar == '\n') {
      count = count + 1;
    }
  }
  fclose(fp);
  printf("The file %s has %d lines\n", filename, count);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x40(%rbp),%rsi
lea    0xea9(%rip),%rdi
call   1050 <scanf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c1 <main+0x71>
lea    -0x40(%rbp),%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1234 <main+0xe4>
mov    -0x10(%rbp),%rdi
call   1070 <getc@plt>
mov    %al,-0x41(%rbp)
movsbl -0x41(%rbp),%eax
mov    0x2e10(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     120f <main+0xbf>
movsbl -0x41(%rbp),%eax
cmp    $0xa,%eax
jne    11f9 <main+0xa9>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11fe <main+0xae>
mov    -0x10(%rbp),%rdi
call   1070 <getc@plt>
mov    %al,-0x41(%rbp)
jmp    11cd <main+0x7d>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x40(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
lea    0xea0(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb8(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
call   1050 <scanf@plt>
lea    0xe90(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e3 <main+0x93>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <getc@plt>
mov    0x2e47(%rip),%r15
cmp    (%r15),%al
je     11c6 <main+0x76>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
add    %ecx,%ebx
mov    %r14,%rdi
call   1070 <getc@plt>
cmp    (%r15),%al
jne    11b0 <main+0x60>
mov    %r14,%rdi
call   1030 <fclose@plt>
lea    0xe56(%rip),%rdi
mov    %rsp,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11f4 <main+0xa4>
lea    0xe2a(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Enter file name: \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [23 x i8] c"Could not open file %s\00"
@EOF = global i8 0
@.str.4 = [26 x i8] c"The file %s has %d lines\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [40 x i8]
%5 = alloca i8
store i32 0, i32* %1
store i32 0, i32* %3
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %10, i32** %2
%11 = load i32*, i32** %2
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %14)
store i32 0, i32* %1
br label %42
16: ; preds = %0
%17 = load i32*, i32** %2
%18 = call signext i8 @getc(i32* %17)
store i8 %18, i8* %5
br label %19
19: ; preds = %33, %16
%20 = load i8, i8* %5
%21 = sext i8 %20 to i32
%22 = load i8, i8* @EOF
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %21, %23
br i1 %24, label %25, label %36
25: ; preds = %19
%26 = load i8, i8* %5
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %3
%31 = add nsw i32 %30, 1
store i32 %31, i32* %3
br label %32
32: ; preds = %29, %25
br label %33
33: ; preds = %32
%34 = load i32*, i32** %2
%35 = call signext i8 @getc(i32* %34)
store i8 %35, i8* %5
br label %19
36: ; preds = %19
%37 = load i32*, i32** %2
%38 = call i32 @fclose(i32* %37)
%39 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%40 = load i32, i32* %3
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %39, i32 %40)
store i32 0, i32* %1
br label %42
42: ; preds = %36, %13
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@.str = [18 x i8] c"Enter file name: \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [23 x i8] c"Could not open file %s\00"
@EOF = global i8 0
@.str.4 = [26 x i8] c"The file %s has %d lines\0A\00"
define i32 @main() {
%1 = alloca [40 x i8]
%2 = getelementptr inbounds [40 x i8], [40 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%5 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %2)
br label %26
9: ; preds = %0
%10 = call signext i8 @getc(i32* %5)
%11 = load i8, i8* @EOF
%12 = icmp eq i8 %10, %11
br i1 %12, label %22, label %13
13: ; preds = %9, %13
%14 = phi i8 [ %19, %13 ], [ %10, %9 ]
%15 = phi i32 [ %18, %13 ], [ 0, %9 ]
%16 = icmp eq i8 %14, 10
%17 = zext i1 %16 to i32
%18 = add nuw nsw i32 %15, %17
%19 = call signext i8 @getc(i32* %5)
%20 = load i8, i8* @EOF
%21 = icmp eq i8 %19, %20
br i1 %21, label %22, label %13
22: ; preds = %13, %9
%23 = phi i32 [ 0, %9 ], [ %18, %13 ]
%24 = call i32 @fclose(i32* %5)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %2, i32 %23)
br label %26
26: ; preds = %22, %7
ret i32 0
}