#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
scalar_t__ strlen(char *);
int CountRowsInFile(char *fileName) {
  FILE *file;
  char *buffer;
  int numberOfRows = 0;
  file = fopen(fileName, "r");
  if (file == NULL)
    return -1;
  while (!feof(file)) {
    buffer = (char *)malloc(sizeof(char) * 50);
    fgets(buffer, 50, file);
    if (strlen(buffer) > 0)
      numberOfRows++;
    free(buffer);
  }
  fclose(file);
  return numberOfRows;
}


x86 assembly (clang, -O0):
<CountRowsInFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <CountRowsInFile+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <CountRowsInFile+0xba>
jmp    11b3 <CountRowsInFile+0x43>
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d1 <CountRowsInFile+0x61>
jmp    121b <CountRowsInFile+0xab>
mov    $0x32,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x32,%esi
call   1060 <fgets@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jle    120d <CountRowsInFile+0x9d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    11b3 <CountRowsInFile+0x43>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CountRowsInFile>:
push   %rbp
push   %r14
push   %rbx
lea    0xe85(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11ed <CountRowsInFile+0x7d>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <feof@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    11e3 <CountRowsInFile+0x73>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    $0x32,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x32,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
xor    %ecx,%ecx
test   %rax,%rax
setg   %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11a0 <CountRowsInFile+0x30>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    11f2 <CountRowsInFile+0x82>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @CountRowsInFile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %37
12: ; preds = %1
br label %13
13: ; preds = %30, %12
%14 = load i32*, i32** %4
%15 = call i32 @feof(i32* %14)
%16 = icmp ne i32 %15, 0
%17 = xor i1 %16, true
br i1 %17, label %18, label %33
18: ; preds = %13
%19 = call i64 @malloc(i32 50)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = load i32*, i32** %4
%23 = call i32 @fgets(i8* %21, i32 50, i32* %22)
%24 = load i8*, i8** %5
%25 = call i64 @strlen(i8* %24)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %30
30: ; preds = %27, %18
%31 = load i8*, i8** %5
%32 = call i32 @free(i8* %31)
br label %13
33: ; preds = %13
%34 = load i32*, i32** %4
%35 = call i32 @fclose(i32* %34)
%36 = load i32, i32* %6
store i32 %36, i32* %2
br label %37
37: ; preds = %33, %11
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @CountRowsInFile(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = tail call i32 @feof(i32* %2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %19
7: ; preds = %4, %7
%8 = phi i32 [ %15, %7 ], [ 0, %4 ]
%9 = tail call i64 @malloc(i32 50)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @fgets(i8* %10, i32 50, i32* %2)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %10)
%13 = icmp sgt i64 %12, 0
%14 = zext i1 %13 to i32
%15 = add nuw nsw i32 %8, %14
%16 = tail call i32 @free(i8* %10)
%17 = tail call i32 @feof(i32* %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %7, label %19
19: ; preds = %7, %4
%20 = phi i32 [ 0, %4 ], [ %15, %7 ]
%21 = tail call i32 @fclose(i32* %2)
br label %22
22: ; preds = %1, %19
%23 = phi i32 [ %20, %19 ], [ -1, %1 ]
ret i32 %23
}