#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*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int count_towns(char *file_name) {
  FILE *ifp;
  char buf[100];
  int i = 0;
  ifp = fopen(file_name, "r");
  while (fscanf(ifp, " BY %[^\n]", buf) == 1)
    i++;
  fclose(ifp);
  return i;
}


x86 assembly (clang, -O0):
<count_towns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x84(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeac(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    0xe96(%rip),%rsi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
jne    118e <count_towns+0x5e>
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    115d <count_towns+0x2d>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x84(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_towns>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x68,%rsp
lea    0xebe(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
mov    $0xffffffff,%ebx
lea    0xeac(%rip),%r15
mov    %rsp,%r12
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <fscanf@plt>
add    $0x1,%ebx
cmp    $0x1,%eax
je     1160 <count_towns+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [10 x i8] c" BY %[^\0A]\00"
define i32 @count_towns(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [100 x i8]
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
br label %8
8: ; preds = %13, %1
%9 = load i32*, i32** %3
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%11 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
store i32 %15, i32* %5
br label %8
16: ; preds = %8
%17 = load i32*, i32** %3
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* %5
ret i32 %19
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [10 x i8] c" BY %[^\0A]\00"
define i32 @count_towns(i8* nocapture %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 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))
br label %5
5: ; preds = %5, %1
%6 = phi i32 [ 0, %1 ], [ %9, %5 ]
%7 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%8 = icmp eq i32 %7, 1
%9 = add nuw nsw i32 %6, 1
br i1 %8, label %5, label %10
10: ; preds = %5
%11 = call i32 @fclose(i32* %4)
ret i32 %6
}