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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ find_count_of_words(char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int, int);
int txt_file_number_of_words(FILE *fp) {
  int words = 0;
  char buffer[1024] = {0x00};
  size_t bytes = 0;
  fseek(fp, 0L, SEEK_SET);
  do {
    memset(buffer, 0x00, sizeof(buffer));
    bytes = fread(buffer, sizeof(char), sizeof(buffer) - 1, fp);
    if (bytes > 0) {
      words += find_count_of_words(buffer);
    }
  } while (bytes > 0);
  return words;
}


x86 assembly (clang, -O0):
<txt_file_number_of_words>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
movq   $0x0,-0x418(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <fseek@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3ff,%edx
call   1030 <fread@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jbe    11e5 <txt_file_number_of_words+0xa5>
lea    -0x410(%rbp),%rdi
call   1060 <find_count_of_words@plt>
mov    %rax,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
jmp    11ea <txt_file_number_of_words+0xaa>
cmpq   $0x0,-0x418(%rbp)
ja     118a <txt_file_number_of_words+0x4a>
mov    -0xc(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<txt_file_number_of_words>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%r14
mov    %rsp,%r15
xor    %ebx,%ebx
mov    $0x400,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x3ff,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11e3 <txt_file_number_of_words+0xa3>
xor    %ebx,%ebx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   1060 <find_count_of_words@plt>
add    %eax,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x3ff,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11b0 <txt_file_number_of_words+0x70>
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32 @txt_file_number_of_words(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i64
store i32* %0, i32** %2
store i32 0, i32* %3
%6 = bitcast [1024 x i8]* %4 to i8*
store i64 0, i64* %5
%7 = load i32*, i32** %2
%8 = load i32, i32* @SEEK_SET
%9 = call i32 @fseek(i32* %7, i64 0, i32 %8)
br label %10
10: ; preds = %26, %1
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 1024)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = load i32*, i32** %2
%15 = call i64 @fread(i8* %13, i32 1, i32 1023, i32* %14)
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %10
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = call i64 @find_count_of_words(i8* %19)
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = add nsw i64 %22, %20
%24 = trunc i64 %23 to i32
store i32 %24, i32* %3
br label %25
25: ; preds = %18, %10
br label %26
26: ; preds = %25
%27 = load i64, i64* %5
%28 = icmp ugt i64 %27, 0
br i1 %28, label %10, label %29
29: ; preds = %26
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32 @txt_file_number_of_words(i32* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @fseek(i32* %0, i64 0, i32 %4)
%6 = call i32 @memset(i8* %3, i32 0, i32 1024)
%7 = call i64 @fread(i8* %3, i32 1, i32 1023, i32* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %1, %9
%10 = phi i32 [ %13, %9 ], [ 0, %1 ]
%11 = call i64 @find_count_of_words(i8* %3)
%12 = trunc i64 %11 to i32
%13 = add i32 %10, %12
%14 = call i32 @memset(i8* %3, i32 0, i32 1024)
%15 = call i64 @fread(i8* %3, i32 1, i32 1023, i32* %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %9
17: ; preds = %9, %1
%18 = phi i32 [ 0, %1 ], [ %13, %9 ]
ret i32 %18
}