#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 */
char EOF;
char fgetc(int /*<<< orphan*/ *);
scalar_t__ isspace(char);
int countword(FILE *in) {
  int count = 0;
  char ch;
  int state = 0;
  while ((ch = fgetc(in)) != EOF) {
    if (isspace(ch)) {
      state = 0;
    } else if (state == 0) {
      state = 1;
      count++;
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<countword>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,-0xd(%rbp)
movsbl %al,%eax
mov    0x2e98(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     119e <countword+0x7e>
movsbl -0xd(%rbp),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     117a <countword+0x5a>
movl   $0x0,-0x14(%rbp)
jmp    1199 <countword+0x79>
cmpl   $0x0,-0x14(%rbp)
jne    1194 <countword+0x74>
movl   $0x1,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1199 <countword+0x79>
jmp    113a <countword+0x1a>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countword>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1030 <fgetc@plt>
mov    0x2eae(%rip),%r14
xor    %r13d,%r13d
cmp    (%r14),%al
je     117a <countword+0x5a>
xor    %r15d,%r15d
xor    %ebx,%ebx
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1040 <isspace@plt>
xor    $0x1,%ebx
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
cmovne %r15d,%ebx
add    %ebx,%r13d
mov    %r12,%rdi
call   1030 <fgetc@plt>
mov    %ebp,%ebx
cmp    (%r14),%al
jne    1150 <countword+0x30>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i8 0
define i32 @countword(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
store i32* %0, i32** %2
store i32 0, i32* %3
store i32 0, i32* %5
br label %6
6: ; preds = %25, %1
%7 = load i32*, i32** %2
%8 = call signext i8 @fgetc(i32* %7)
store i8 %8, i8* %4
%9 = sext i8 %8 to i32
%10 = load i8, i8* @EOF
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %9, %11
br i1 %12, label %13, label %26
13: ; preds = %6
%14 = load i8, i8* %4
%15 = call i64 @isspace(i8 signext %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 0, i32* %5
br label %25
18: ; preds = %13
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
store i32 1, i32* %5
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %24
24: ; preds = %21, %18
br label %25
25: ; preds = %24, %17
br label %6
26: ; preds = %6
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@EOF = global i8 0
define i32 @countword(i32* nocapture %0) {
%2 = tail call signext i8 @fgetc(i32* %0)
%3 = load i8, i8* @EOF
%4 = icmp eq i8 %2, %3
br i1 %4, label %18, label %5
5: ; preds = %1, %5
%6 = phi i8 [ %15, %5 ], [ %2, %1 ]
%7 = phi i32 [ %14, %5 ], [ 0, %1 ]
%8 = phi i32 [ %13, %5 ], [ 0, %1 ]
%9 = tail call i64 @isspace(i8 signext %6)
%10 = icmp eq i64 %9, 0
%11 = xor i32 %7, 1
%12 = select i1 %10, i32 %11, i32 0
%13 = add nuw nsw i32 %12, %8
%14 = zext i1 %10 to i32
%15 = tail call signext i8 @fgetc(i32* %0)
%16 = load i8, i8* @EOF
%17 = icmp eq i8 %15, %16
br i1 %17, label %18, label %5
18: ; preds = %5, %1
%19 = phi i32 [ 0, %1 ], [ %13, %5 ]
ret i32 %19
}