#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int main(int argc, char **argv) {
  FILE *fp = NULL;
  char *fname = NULL;
  int word_count = 0;
  char buf[64 * 1024] = {
      0,
  };
  if (argc != 2) {
    printf("Usage: %s <text-file-name>\n", argv[0]);
    return 1;
  }
  fname = argv[1];
  fp = fopen(fname, "r");
  if (fp == NULL) {
    printf("Failed to open file %s for reading. err = %d(%s)\n", fname, errno,
           strerror(errno));
    goto out;
  }
  while (fscanf(fp, "%s", buf) == 1) {
    word_count++;
  }
  printf("word: %d\n", word_count);
out:
  if (fp) {
    fclose(fp);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x10030(%rbp),%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
je     11ce <main+0x6e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129c <main+0x13c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1244 <main+0xe4>
mov    -0x20(%rbp),%rax
mov    %rax,-0x10040(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10034(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x10040(%rbp),%rsi
mov    -0x10034(%rbp),%edx
mov    %rax,%rcx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1288 <main+0x128>
jmp    1249 <main+0xe9>
mov    -0x18(%rbp),%rdi
lea    -0x10030(%rbp),%rdx
lea    0xdf5(%rip),%rsi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    1277 <main+0x117>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1249 <main+0xe9>
mov    -0x24(%rbp),%esi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
je     129c <main+0x13c>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x10008,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rdi
mov    $0x10000,%edx
xor    %esi,%esi
call   1050 <memset@plt>
cmp    $0x2,%ebp
jne    11e3 <main+0x83>
mov    0x8(%rbx),%rbp
lea    0xe8b(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fc <main+0x9c>
mov    %rax,%rbx
mov    $0xffffffff,%ebp
lea    0xea3(%rip),%r14
mov    %rsp,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <fscanf@plt>
add    $0x1,%ebp
cmp    $0x1,%eax
je     11b0 <main+0x50>
lea    0xe86(%rip),%rdi
xor    %r14d,%r14d
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1225 <main+0xc5>
mov    (%rbx),%rsi
lea    0xe13(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%r14d
jmp    1225 <main+0xc5>
mov    0x2ddd(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe0b(%rip),%rdi
xor    %r14d,%r14d
mov    %rbp,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x10008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Usage: %s <text-file-name>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [50 x i8] c"Failed to open file %s for reading. err = %d(%s)\0A\00"
@errno = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [10 x i8] c"word: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [65536 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
store i8* null, i8** %7
store i32 0, i32* %8
%10 = bitcast [65536 x i8]* %9 to i8*
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %50
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = load i8*, i8** %7
%28 = load i32, i32* @errno
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %27, i32 %28, i8* %30)
br label %44
32: ; preds = %18
br label %33
33: ; preds = %38, %32
%34 = load i32*, i32** %6
%35 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%36 = call i32 @fscanf(i32* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
br label %33
41: ; preds = %33
%42 = load i32, i32* %8
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %41, %26
%45 = load i32*, i32** %6
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
br label %50
50: ; preds = %13, %47, %44
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [28 x i8] c"Usage: %s <text-file-name>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [50 x i8] c"Failed to open file %s for reading. err = %d(%s)\0A\00"
@errno = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [10 x i8] c"word: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [65536 x i8]
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %26
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %11, i32 %15, i8* %16)
br label %26
18: ; preds = %9, %18
%19 = phi i32 [ %22, %18 ], [ 0, %9 ]
%20 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%21 = icmp eq i32 %20, 1
%22 = add nuw nsw i32 %19, 1
br i1 %21, label %18, label %23
23: ; preds = %18
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%25 = call i32 @fclose(i32* %12)
br label %26
26: ; preds = %14, %23, %6
%27 = phi i32 [ 1, %6 ], [ 0, %23 ], [ 0, %14 ]
ret i32 %27
}