#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 EOF;
char *basename(char *);
int /*<<< orphan*/ exit(int);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ get_filename(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *, char *);
int countlines(FILE *fp) {
  int count = 0;
  int character = 0;
  while ((character = fgetc(fp)) != EOF) {
    if (character == '\n') {
      count++;
    }
  }
  if (count == 0) {
    char filename[0xFFF];
    get_filename(fp, filename);
    printf("%s doesn't contain any lines!\n", basename(filename));
    exit(1);
  }
  return count;
}


x86 assembly (clang, -O0):
<countlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%eax
je     11a0 <countlines+0x50>
cmpl   $0xa,-0x10(%rbp)
jne    119b <countlines+0x4b>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116d <countlines+0x1d>
cmpl   $0x0,-0xc(%rbp)
jne    11df <countlines+0x8f>
mov    -0x8(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
call   1030 <get_filename@plt>
lea    -0x1010(%rbp),%rdi
call   1060 <basename@plt>
mov    %rax,%rsi
lea    0xe30(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countlines>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rdi,%rbx
call   1050 <fgetc@plt>
mov    0x2e7e(%rip),%r14
cmp    (%r14),%eax
je     11a9 <countlines+0x59>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    (%r14),%eax
jne    1180 <countlines+0x30>
test   %ebp,%ebp
je     11a9 <countlines+0x59>
mov    %ebp,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <get_filename@plt>
mov    %rbp,%rdi
call   1060 <basename@plt>
lea    0xe3a(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@EOF = global i32 0
@.str = [31 x i8] c"%s doesn't contain any lines!\0A\00"
define i32 @countlines(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4095 x i8]
store i32* %0, i32** %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %6
6: ; preds = %17, %1
%7 = load i32*, i32** %2
%8 = call i32 @fgetc(i32* %7)
store i32 %8, i32* %4
%9 = load i32, i32* @EOF
%10 = icmp ne i32 %8, %9
br i1 %10, label %11, label %18
11: ; preds = %6
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 10
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
store i32 %16, i32* %3
br label %17
17: ; preds = %14, %11
br label %6
18: ; preds = %6
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = load i32*, i32** %2
%23 = getelementptr inbounds [4095 x i8], [4095 x i8]* %5, i64 0, i64 0
%24 = call i32 @get_filename(i32* %22, i8* %23)
%25 = getelementptr inbounds [4095 x i8], [4095 x i8]* %5, i64 0, i64 0
%26 = call i8* @basename(i8* %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %18
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@EOF = global i32 0
@.str = [31 x i8] c"%s doesn't contain any lines!\0A\00"
define i32 @countlines(i32* %0) {
%2 = alloca [4095 x i8]
%3 = tail call i32 @fgetc(i32* %0)
%4 = load i32, i32* @EOF
%5 = icmp eq i32 %3, %4
br i1 %5, label %17, label %6
6: ; preds = %1, %6
%7 = phi i32 [ %12, %6 ], [ %3, %1 ]
%8 = phi i32 [ %11, %6 ], [ 0, %1 ]
%9 = icmp eq i32 %7, 10
%10 = zext i1 %9 to i32
%11 = add nuw nsw i32 %8, %10
%12 = tail call i32 @fgetc(i32* %0)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %6
15: ; preds = %6
%16 = icmp eq i32 %11, 0
br i1 %16, label %17, label %23
17: ; preds = %1, %15
%18 = getelementptr inbounds [4095 x i8], [4095 x i8]* %2, i64 0, i64 0
%19 = call i32 @get_filename(i32* %0, i8* %18)
%20 = call i8* @basename(i8* %18)
%21 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
ret i32 %11
}