#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__ isspace(char const);
int words(const char *s) {
  unsigned int index = 0;
  const char *ch = s;
  int state = 0;
  while (*ch) {
    if (isspace(*ch))
      state = 0;
    else if (state == 0) {
      state = 1;
      ++index;
    }
    ++ch;
  }
  return index;
}


x86 assembly (clang, -O0):
<words>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1191 <words+0x81>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%rax
je     1161 <words+0x51>
movl   $0x0,-0x1c(%rbp)
jmp    1180 <words+0x70>
cmpl   $0x0,-0x1c(%rbp)
jne    117b <words+0x6b>
movl   $0x1,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1180 <words+0x70>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1132 <words+0x22>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<words>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%al
test   %al,%al
je     1159 <words+0x49>
mov    %rdi,%rbx
add    $0x1,%rbx
xor    %r14d,%r14d
xor    %ebp,%ebp
xor    %r15d,%r15d
nopl   0x0(%rax)
movsbl %al,%edi
call   1030 <isspace@plt>
xor    $0x1,%ebp
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
cmovne %r14d,%ebp
add    %ebp,%r15d
movzbl (%rbx),%eax
add    $0x1,%rbx
mov    %ecx,%ebp
test   %al,%al
jne    1130 <words+0x20>
jmp    115c <words+0x4c>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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