#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 BUFSIZ;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ yap_sysquit(char *, char *);
unsigned countntot(char *fname) {
  unsigned ntot = 0;
  char buf[BUFSIZ];
  FILE *fp = fopen(fname, "r");
  if (!fp)
    yap_sysquit("Cannot open file '%s' for read\n", fname);
  while (fgets(buf, BUFSIZ, fp))
    ntot++;
  fclose(fp);
  assert(ntot > 0);
  return ntot;
}


x86 assembly (clang, -O0):
<countntot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11be <countntot+0x6e>
mov    -0x8(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   1070 <yap_sysquit@plt>
jmp    11c3 <countntot+0x73>
mov    -0x30(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11f1 <countntot+0xa1>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c3 <countntot+0x73>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0xc(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countntot>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
mov    0x2e7b(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe7f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    119d <countntot+0x4d>
lea    0xe6d(%rip),%rdi
mov    %rbx,%rsi
call   1070 <yap_sysquit@plt>
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12),%esi
mov    %r14,%rdi
mov    %r15,%rdx
call   1040 <fgets@plt>
add    $0xffffffff,%ebx
test   %rax,%rax
jne    11b0 <countntot+0x60>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
test   %ebx,%ebx
setne  %dil
call   1050 <assert@plt>
neg    %ebx
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [32 x i8] c"Cannot open file '%s' for read\0A\00"
define i32 @countntot(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
store i8* %0, i8** %2
store i32 0, i32* %3
%7 = load i32, i32* @BUFSIZ
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
%11 = load i8*, i8** %2
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = call i32 @yap_sysquit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %1
br label %19
19: ; preds = %24, %18
%20 = load i32, i32* @BUFSIZ
%21 = load i32*, i32** %6
%22 = call i64 @fgets(i8* %10, i32 %20, i32* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* %3
%26 = add i32 %25, 1
store i32 %26, i32* %3
br label %19
27: ; preds = %19
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* %3
%31 = icmp ugt i32 %30, 0
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %3
%35 = load i8*, i8** %4
ret i32 %34
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [32 x i8] c"Cannot open file '%s' for read\0A\00"
define i32 @countntot(i8* %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @yap_sysquit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %1
br label %10
10: ; preds = %9, %10
%11 = phi i32 [ %15, %10 ], [ 0, %9 ]
%12 = load i32, i32* @BUFSIZ
%13 = call i64 @fgets(i8* %4, i32 %12, i32* %5)
%14 = icmp eq i64 %13, 0
%15 = add i32 %11, 1
br i1 %14, label %16, label %10
16: ; preds = %10
%17 = call i32 @fclose(i32* %5)
%18 = icmp ne i32 %11, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
ret i32 %11
}