#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 */

/* Variables and functions */
scalar_t__ isalpha(char const);
unsigned int strlen(char const *);
int str_word_count(const char *str) {
  int count;
  bool in_word;
  unsigned int i;
  count = 0;
  in_word = false;
  for (i = 0; i < strlen(str); i++) {
    if (isalpha(str[i]) && !in_word) {
      count++;
      in_word = true;
    } else if (!isalpha(str[i]) && in_word) {
      in_word = false;
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<str_word_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jae    11d9 <str_word_count+0xb9>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalpha@plt>
cmp    $0x0,%rax
je     1196 <str_word_count+0x76>
cmpl   $0x0,-0x10(%rbp)
jne    1196 <str_word_count+0x76>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
jmp    11c6 <str_word_count+0xa6>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalpha@plt>
cmp    $0x0,%rax
jne    11c1 <str_word_count+0xa1>
cmpl   $0x0,-0x10(%rbp)
je     11c1 <str_word_count+0xa1>
movl   $0x0,-0x10(%rbp)
jmp    11c6 <str_word_count+0xa6>
jmp    11cb <str_word_count+0xab>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1141 <str_word_count+0x21>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_word_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
test   %eax,%eax
je     1194 <str_word_count+0x74>
xor    %ebp,%ebp
xor    %ebx,%ebx
xor    %r14d,%r14d
jmp    1159 <str_word_count+0x39>
nopl   0x0(%rax)
test   %rax,%rax
cmove  %ecx,%ebx
add    $0x1,%rbp
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbp
jae    1197 <str_word_count+0x77>
movsbl (%r15,%rbp,1),%edi
call   1040 <isalpha@plt>
test   %rax,%rax
je     1180 <str_word_count+0x60>
test   %ebx,%ebx
jne    1180 <str_word_count+0x60>
add    $0x1,%r14d
mov    $0x1,%ebx
jmp    1146 <str_word_count+0x26>
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbp,1),%edi
call   1040 <isalpha@plt>
xor    %ecx,%ecx
test   %ebx,%ebx
jne    1140 <str_word_count+0x20>
mov    %ebx,%ecx
jmp    1140 <str_word_count+0x20>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @str_word_count(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %29, label %4
4: ; preds = %1, %22
%5 = phi i64 [ %25, %22 ], [ 0, %1 ]
%6 = phi i32 [ %24, %22 ], [ 0, %1 ]
%7 = phi i32 [ %23, %22 ], [ 0, %1 ]
%8 = getelementptr inbounds i8, i8* %0, i64 %5
%9 = load i8, i8* %8
%10 = tail call i64 @isalpha(i8 signext %9)
%11 = icmp eq i64 %10, 0
%12 = icmp ne i32 %6, 0
%13 = select i1 %11, i1 true, i1 %12
br i1 %13, label %16, label %14
14: ; preds = %4
%15 = add nsw i32 %7, 1
br label %22
16: ; preds = %4
%17 = load i8, i8* %8
%18 = tail call i64 @isalpha(i8 signext %17)
%19 = icmp eq i64 %18, 0
%20 = select i1 %19, i1 %12, i1 false
%21 = select i1 %20, i32 0, i32 %6
br label %22
22: ; preds = %16, %14
%23 = phi i32 [ %7, %16 ], [ %15, %14 ]
%24 = phi i32 [ %21, %16 ], [ 1, %14 ]
%25 = add nuw nsw i64 %5, 1
%26 = tail call i32 @strlen(i8* %0)
%27 = zext i32 %26 to i64
%28 = icmp ult i64 %25, %27
br i1 %28, label %4, label %29
29: ; preds = %22, %1
%30 = phi i32 [ 0, %1 ], [ %23, %22 ]
ret i32 %30
}