#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
unsigned long query_file_count(char *lookup_file) {
  unsigned long count = 0;
  char *p, buf[32];
  FILE *fp;
  fp = fopen(lookup_file, "r");
  if (fp == NULL) {
    printf("warning: couldn't open file: %s\n", lookup_file);
    return 0;
  }
  while (1) {
    p = fgets(buf, sizeof(buf), fp);
    if (p == NULL)
      break;
    count++;
  }
  fclose(fp);
  return count;
}


x86 assembly (clang, -O0):
<query_file_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1190 <query_file_count+0x50>
mov    -0x10(%rbp),%rsi
lea    0xe84(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11dd <query_file_count+0x9d>
jmp    1195 <query_file_count+0x55>
lea    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rdx
mov    $0x20,%esi
call   1050 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bb <query_file_count+0x7b>
jmp    11cc <query_file_count+0x8c>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1195 <query_file_count+0x55>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<query_file_count>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
lea    0xead(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1193 <query_file_count+0x53>
mov    %rax,%r14
mov    $0xffffffffffffffff,%rbx
mov    %rsp,%r15
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x20,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1170 <query_file_count+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    11a4 <query_file_count+0x64>
lea    0xe68(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c"warning: couldn't open file: %s\0A\00"
define i64 @query_file_count(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca [32 x i8]
%7 = alloca i32*
store i8* %0, i8** %3
store i64 0, i64* %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i64 0, i64* %2
br label %30
15: ; preds = %1
br label %16
16: ; preds = %15, %23
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%18 = load i32*, i32** %7
%19 = call i8* @fgets(i8* %17, i32 32, i32* %18)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %16
br label %26
23: ; preds = %16
%24 = load i64, i64* %4
%25 = add i64 %24, 1
store i64 %25, i64* %4
br label %16
26: ; preds = %22
%27 = load i32*, i32** %7
%28 = call i32 @fclose(i32* %27)
%29 = load i64, i64* %4
store i64 %29, i64* %2
br label %30
30: ; preds = %26, %12
%31 = load i64, i64* %2
ret i64 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c"warning: couldn't open file: %s\0A\00"
define i64 @query_file_count(i8* %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %15
8: ; preds = %1, %8
%9 = phi i64 [ %12, %8 ], [ 0, %1 ]
%10 = call i8* @fgets(i8* %3, i32 32, i32* %4)
%11 = icmp eq i8* %10, null
%12 = add i64 %9, 1
br i1 %11, label %13, label %8
13: ; preds = %8
%14 = call i32 @fclose(i32* %4)
br label %15
15: ; preds = %13, %6
%16 = phi i64 [ 0, %6 ], [ %9, %13 ]
ret i64 %16
}