#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ isspace(char);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
size_t read(int, char *, int);
size_t wc_get_word_count(int fd) {
  char buf[4096];
  size_t num = 0;
  ssize_t n;
  lseek(fd, 0, SEEK_SET);
  while ((n = read(fd, buf, 4096)) > 0) {
    for (size_t i = 1; i < n; i++) {
      if (!isspace(buf[i - 1]) && isspace(buf[i])) {
        num++;
      }
    }
  }
  return num;
}


x86 assembly (clang, -O0):
<wc_get_word_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x1018(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e8d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x1020(%rbp)
cmp    $0x0,%rax
jbe    1213 <wc_get_word_count+0xe3>
movq   $0x1,-0x1028(%rbp)
mov    -0x1028(%rbp),%rax
cmp    -0x1020(%rbp),%rax
jae    120e <wc_get_word_count+0xde>
mov    -0x1028(%rbp),%rax
sub    $0x1,%rax
movsbl -0x1010(%rbp,%rax,1),%edi
ff
call   1040 <isspace@plt>
cmp    $0x0,%rax
jne    11f2 <wc_get_word_count+0xc2>
mov    -0x1028(%rbp),%rax
movsbl -0x1010(%rbp,%rax,1),%edi
ff
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     11f2 <wc_get_word_count+0xc2>
mov    -0x1018(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1018(%rbp)
jmp    11f7 <wc_get_word_count+0xc7>
mov    -0x1028(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1028(%rbp)
jmp    118c <wc_get_word_count+0x5c>
jmp    115c <wc_get_word_count+0x2c>
mov    -0x1018(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wc_get_word_count>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %edi,%r15d
mov    0x2e97(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11db <wc_get_word_count+0xab>
mov    %rax,%rbp
xor    %r12d,%r12d
mov    %rsp,%r14
jmp    1198 <wc_get_word_count+0x68>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11db <wc_get_word_count+0xab>
cmp    $0x2,%rbp
jb     1180 <wc_get_word_count+0x50>
mov    $0x1,%ebx
jmp    11b9 <wc_get_word_count+0x89>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbx
cmp    %rbx,%rbp
je     1180 <wc_get_word_count+0x50>
movsbl -0x1(%rsp,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
jne    11b0 <wc_get_word_count+0x80>
movsbl (%rsp,%rbx,1),%edi
call   1040 <isspace@plt>
cmp    $0x1,%rax
sbb    $0xffffffffffffffff,%r12
jmp    11b0 <wc_get_word_count+0x80>
mov    %r12,%rax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i64 @wc_get_word_count(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %2
store i64 0, i64* %4
%7 = load i32, i32* %2
%8 = load i32, i32* @SEEK_SET
%9 = call i32 @lseek(i32 %7, i32 0, i32 %8)
br label %10
10: ; preds = %40, %1
%11 = load i32, i32* %2
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%13 = call i64 @read(i32 %11, i8* %12, i32 4096)
store i64 %13, i64* %5
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %41
15: ; preds = %10
store i64 1, i64* %6
br label %16
16: ; preds = %37, %15
%17 = load i64, i64* %6
%18 = load i64, i64* %5
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %40
20: ; preds = %16
%21 = load i64, i64* %6
%22 = sub i64 %21, 1
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = call i64 @isspace(i8 signext %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %20
%28 = load i64, i64* %6
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = call i64 @isspace(i8 signext %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i64, i64* %4
%35 = add i64 %34, 1
store i64 %35, i64* %4
br label %36
36: ; preds = %33, %27, %20
br label %37
37: ; preds = %36
%38 = load i64, i64* %6
%39 = add i64 %38, 1
store i64 %39, i64* %6
br label %16
40: ; preds = %16
br label %10
41: ; preds = %10
%42 = load i64, i64* %4
ret i64 %42
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i64 @wc_get_word_count(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @lseek(i32 %0, i32 0, i32 %4)
%6 = call i64 @read(i32 %0, i8* %3, i32 4096)
%7 = icmp eq i64 %6, 0
br i1 %7, label %35, label %12
8: ; preds = %31, %12
%9 = phi i64 [ %14, %12 ], [ %32, %31 ]
%10 = call i64 @read(i32 %0, i8* %3, i32 4096)
%11 = icmp eq i64 %10, 0
br i1 %11, label %35, label %12
12: ; preds = %1, %8
%13 = phi i64 [ %10, %8 ], [ %6, %1 ]
%14 = phi i64 [ %9, %8 ], [ 0, %1 ]
%15 = icmp ugt i64 %13, 1
br i1 %15, label %16, label %8
16: ; preds = %12, %31
%17 = phi i64 [ %33, %31 ], [ 1, %12 ]
%18 = phi i64 [ %32, %31 ], [ %14, %12 ]
%19 = add i64 %17, -1
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = tail call i64 @isspace(i8 signext %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %16
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %17
%26 = load i8, i8* %25
%27 = tail call i64 @isspace(i8 signext %26)
%28 = icmp ne i64 %27, 0
%29 = zext i1 %28 to i64
%30 = add i64 %18, %29
br label %31
31: ; preds = %24, %16
%32 = phi i64 [ %18, %16 ], [ %30, %24 ]
%33 = add nuw i64 %17, 1
%34 = icmp eq i64 %33, %13
br i1 %34, label %8, label %16
35: ; preds = %8, %1
%36 = phi i64 [ 0, %1 ], [ %9, %8 ]
ret i64 %36
}