#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *nextword(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  printf("Program that separates text to words.\n");
  if (argc < 2) {
    printf("Usage: towords filename\n");
    exit(1);
  }
  char *fileName = argv[1];
  FILE *fd = fopen(fileName, "r");
  if (fd == NULL) {
    printf("Could not open file %s\n", fileName);
    exit(1);
  }
  char *w;
  int wordCount = 0;
  while ((w = nextword(fd)) != NULL) {
    printf("%d: %s\n", wordCount, w);
    wordCount++;
  }
  printf("words total = %d\n", wordCount);
  fclose(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jge    1196 <main+0x46>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe93(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dd <main+0x8d>
mov    -0x18(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <nextword@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     121e <main+0xce>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e4 <main+0x94>
mov    -0x2c(%rbp),%esi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x1,%ebp
jg     1190 <main+0x30>
lea    0xed9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x8(%rbx),%rbp
lea    0xe65(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
jne    11c3 <main+0x63>
lea    0xe53(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <nextword@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11ff <main+0x9f>
lea    0xe3e(%rip),%r14
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1050 <nextword@plt>
test   %rax,%rax
jne    11e0 <main+0x80>
lea    0xe1c(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [39 x i8] c"Program that separates text to words.\0A\00"
@.str.1 = [25 x i8] c"Usage: towords filename\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [24 x i8] c"Could not open file %s\0A\00"
@.str.4 = [8 x i8] c"%d: %s\0A\00"
@.str.5 = [18 x i8] c"words total = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %7
%22 = load i32*, i32** %7
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = load i8*, i8** %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
store i32 0, i32* %9
br label %29
29: ; preds = %33, %28
%30 = load i32*, i32** %7
%31 = call i8* @nextword(i32* %30)
store i8* %31, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32, i32* %9
%35 = load i8*, i8** %8
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %34, i8* %35)
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
store i32 %38, i32* %9
br label %29
39: ; preds = %29
%40 = load i32, i32* %9
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
%44 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [24 x i8] c"Could not open file %s\0A\00"
@.str.4 = [8 x i8] c"%d: %s\0A\00"
@.str.5 = [18 x i8] c"words total = %d\0A\00"
@str = [38 x i8] c"Program that separates text to words.\00"
@str.6 = [24 x i8] c"Usage: towords filename\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.6, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = tail call i8* @nextword(i32* %11)
%15 = icmp eq i8* %14, null
br i1 %15, label %26, label %19
16: ; preds = %8
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13, %19
%20 = phi i8* [ %24, %19 ], [ %14, %13 ]
%21 = phi i32 [ %23, %19 ], [ 0, %13 ]
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %21, i8* %20)
%23 = add nuw nsw i32 %21, 1
%24 = tail call i8* @nextword(i32* %11)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %19
26: ; preds = %19, %13
%27 = phi i32 [ 0, %13 ], [ %23, %19 ]
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = tail call i32 @fclose(i32* %11)
%30 = tail call i32 @exit(i32 0)
unreachable
}