#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 */
int test_word(int, int, char *);
int wait_for_word(int, char *);
int word_length(int, char *);
int my_count_words(char *str) {
  char res;
  int index;
  int length;
  index = 0;
  res = 0;
  while ((*(str + index) != '\0')) {
    index = wait_for_word(index, str);
    length = word_length(index, str);
    if ((test_word(index, length, str) == 1) && (length > 0))
      res++;
    index = index + length;
  }
  return (res);
}


x86 assembly (clang, -O0):
<my_count_words>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movb   $0x0,-0x9(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11b2 <my_count_words+0x82>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1050 <wait_for_word@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1040 <word_length@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1030 <test_word@plt>
cmp    $0x1,%eax
jne    11a4 <my_count_words+0x74>
cmpl   $0x0,-0x14(%rbp)
jle    11a4 <my_count_words+0x74>
mov    -0x9(%rbp),%al
add    $0x1,%al
mov    %al,-0x9(%rbp)
mov    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1147 <my_count_words+0x17>
movsbl -0x9(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_count_words>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,(%rdi)
je     1197 <my_count_words+0x67>
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <wait_for_word@plt>
mov    %eax,%r15d
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <word_length@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %eax,%esi
mov    %r14,%rdx
call   1030 <test_word@plt>
cmp    $0x1,%eax
sete   %al
test   %ebp,%ebp
setg   %cl
and    %al,%cl
add    %cl,%bl
add    %r15d,%ebp
movslq %ebp,%rax
cmpb   $0x0,(%r14,%rax,1)
jne    1150 <my_count_words+0x20>
movsbl %bl,%eax
jmp    1199 <my_count_words+0x69>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @my_count_words(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %22, label %4
4: ; preds = %1, %4
%5 = phi i8 [ %14, %4 ], [ 0, %1 ]
%6 = phi i32 [ %15, %4 ], [ 0, %1 ]
%7 = tail call i32 @wait_for_word(i32 %6, i8* %0)
%8 = tail call i32 @word_length(i32 %7, i8* %0)
%9 = tail call i32 @test_word(i32 %7, i32 %8, i8* %0)
%10 = icmp eq i32 %9, 1
%11 = icmp sgt i32 %8, 0
%12 = and i1 %11, %10
%13 = zext i1 %12 to i8
%14 = add i8 %5, %13
%15 = add nsw i32 %8, %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %4
20: ; preds = %4
%21 = sext i8 %14 to i32
br label %22
22: ; preds = %20, %1
%23 = phi i32 [ 0, %1 ], [ %21, %20 ]
ret i32 %23
}