#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*/ EX_NOINPUT;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *const, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ stderr;
unsigned int countWords(const char *const filename) {
  FILE *f = fopen(filename, "r");
  if (f == NULL) {
    fprintf(stderr, "Cannot open file for counting words.\n");
    exit(EX_NOINPUT);
  }
  unsigned int n = 0;
  char word[30];
  while (!feof(f)) {
    fscanf(f, "%s", word);
    ++n;
  }
  fclose(f);
  return n - 1;
}


x86 assembly (clang, -O0):
<countWords>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ae <countWords+0x4e>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d3 <countWords+0x73>
jmp    11f5 <countWords+0x95>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xe46(%rip),%rsi
call   1040 <fscanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b5 <countWords+0x55>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countWords>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d2 <countWords+0x72>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <feof@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    11bd <countWords+0x5d>
mov    $0xffffffff,%ebp
lea    0xe8d(%rip),%r14
mov    %rsp,%r15
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
add    $0x1,%ebp
test   %eax,%eax
je     11a0 <countWords+0x40>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"Cannot open file for counting words.\0A\00"
@EX_NOINPUT = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @countWords(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [30 x i8]
store i8* %0, i8** %2
%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
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EX_NOINPUT
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
store i32 0, i32* %4
br label %16
16: ; preds = %21, %15
%17 = load i32*, i32** %3
%18 = call i32 @feof(i32* %17)
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i32*, i32** %3
%23 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%24 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32, i32* %4
%26 = add i32 %25, 1
store i32 %26, i32* %4
br label %16
27: ; preds = %16
%28 = load i32*, i32** %3
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* %4
%31 = sub i32 %30, 1
ret i32 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"Cannot open file for counting words.\0A\00"
@EX_NOINPUT = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @countWords(i8* nocapture %0) {
%2 = alloca [30 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @EX_NOINPUT
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %1
%11 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%12 = call i32 @feof(i32* %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10, %14
%15 = phi i32 [ %17, %14 ], [ 0, %10 ]
%16 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%17 = add i32 %15, 1
%18 = call i32 @feof(i32* %3)
%19 = icmp eq i32 %18, 0
br i1 %19, label %14, label %20
20: ; preds = %14, %10
%21 = phi i32 [ -1, %10 ], [ %15, %14 ]
%22 = call i32 @fclose(i32* %3)
ret i32 %21
}