#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 cgc_strlen(char *);
int /*<<< orphan*/ *strchr(char *, char);
size_t count_words(char *str) {
  if (!str)
    return 0;
  int i, len = cgc_strlen(str);
  size_t words = 0;
  for (i = 0; i < len; i++) {
    if (strchr(" \t\r\n", str[i]) == NULL)
      words++;
  }
  words = len ? words + 1 : words;
  return words;
}


x86 assembly (clang, -O0):
<count_words>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1144 <count_words+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11df <count_words+0xbf>
mov    -0x10(%rbp),%rdi
call   1040 <cgc_strlen@plt>
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11ac <count_words+0x8c>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
lea    0xe86(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
jne    1199 <count_words+0x79>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    119e <count_words+0x7e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115f <count_words+0x3f>
cmpl   $0x0,-0x18(%rbp)
je     11c7 <count_words+0xa7>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11cf <count_words+0xaf>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_words>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1170 <count_words+0x50>
mov    %rdi,%r12
call   1040 <cgc_strlen@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    1174 <count_words+0x54>
mov    %r14d,%r13d
xor    %ebx,%ebx
lea    0xeb5(%rip),%r15
xor    %ebp,%ebp
nopl   (%rax)
movsbl (%r12,%rbx,1),%esi
mov    %r15,%rdi
call   1030 <strchr@plt>
cmp    $0x1,%rax
adc    $0x0,%rbp
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1150 <count_words+0x30>
jmp    1176 <count_words+0x56>
xor    %ebp,%ebp
jmp    117e <count_words+0x5e>
xor    %ebp,%ebp
cmp    $0x1,%r14d
sbb    $0xffffffffffffffff,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c" \09\0D\0A\00"
define i64 @count_words(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i64 0, i64* %2
br label %43
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @cgc_strlen(i8* %11)
store i32 %12, i32* %5
store i64 0, i64* %6
store i32 0, i32* %4
br label %13
13: ; preds = %29, %10
%14 = load i32, i32* %4
%15 = load i32, i32* %5
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %32
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = call i32* @strchr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %22)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load i64, i64* %6
%27 = add i64 %26, 1
store i64 %27, i64* %6
br label %28
28: ; preds = %25, %17
br label %29
29: ; preds = %28
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %13
32: ; preds = %13
%33 = load i32, i32* %5
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i64, i64* %6
%37 = add i64 %36, 1
br label %40
38: ; preds = %32
%39 = load i64, i64* %6
br label %40
40: ; preds = %38, %35
%41 = phi i64 [ %37, %35 ], [ %39, %38 ]
store i64 %41, i64* %6
%42 = load i64, i64* %6
store i64 %42, i64* %2
br label %43
43: ; preds = %40, %9
%44 = load i64, i64* %2
ret i64 %44
}

LLVM IR (-O2):
@.str = [5 x i8] c" \09\0D\0A\00"
define i64 @count_words(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %24, label %3
3: ; preds = %1
%4 = tail call i32 @cgc_strlen(i8* %0)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %3
%7 = zext i32 %4 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %17, %8 ]
%10 = phi i64 [ 0, %6 ], [ %16, %8 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %9
%12 = load i8, i8* %11
%13 = tail call i32* @strchr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %12)
%14 = icmp eq i32* %13, null
%15 = zext i1 %14 to i64
%16 = add i64 %10, %15
%17 = add nuw nsw i64 %9, 1
%18 = icmp eq i64 %17, %7
br i1 %18, label %19, label %8
19: ; preds = %8, %3
%20 = phi i64 [ 0, %3 ], [ %16, %8 ]
%21 = icmp ne i32 %4, 0
%22 = zext i1 %21 to i64
%23 = add i64 %20, %22
br label %24
24: ; preds = %1, %19
%25 = phi i64 [ %23, %19 ], [ 0, %1 ]
ret i64 %25
}