#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 /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strtok(char *, char *);
size_t countOfWordInString(const char *str) {
  char *temp = (char *)malloc(sizeof(char));
  strcpy(temp, str);
  unsigned count = 0;
  char *p;
  for (p = strtok(temp, " "); p; p = strtok(NULL, " ")) {
    count++;
  }
  free(temp);
  return count;
}


x86 assembly (clang, -O0):
<countOfWordInString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe87(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11af <countOfWordInString+0x6f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe5f(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    1182 <countOfWordInString+0x42>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countOfWordInString>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe99(%rip),%rsi
mov    %r14,%rdi
call   1060 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1194 <countOfWordInString+0x54>
lea    0xe83(%rip),%r15
nopl   (%rax)
add    $0x1,%ebx
xor    %edi,%edi
mov    %r15,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
jne    1180 <countOfWordInString+0x40>
mov    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
define i64 @countOfWordInString(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = call i64 @malloc(i32 1)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = load i8*, i8** %2
%10 = call i32 @strcpy(i8* %8, i8* %9)
store i32 0, i32* %4
%11 = load i8*, i8** %3
%12 = call i8* @strtok(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %5
br label %13
13: ; preds = %19, %1
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* %4
%18 = add i32 %17, 1
store i32 %18, i32* %4
br label %19
19: ; preds = %16
%20 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %20, i8** %5
br label %13
21: ; preds = %13
%22 = load i8*, i8** %3
%23 = call i32 @free(i8* %22)
%24 = load i32, i32* %4
%25 = zext i32 %24 to i64
ret i64 %25
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i64 @countOfWordInString(i8* %0) {
%2 = tail call i64 @malloc(i32 1)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @strcpy(i8* %3, i8* %0)
%5 = tail call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %14, label %7
7: ; preds = %1, %7
%8 = phi i32 [ %9, %7 ], [ 0, %1 ]
%9 = add i32 %8, 1
%10 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %7
12: ; preds = %7
%13 = zext i32 %9 to i64
br label %14
14: ; preds = %12, %1
%15 = phi i64 [ 0, %1 ], [ %13, %12 ]
%16 = tail call i32 @free(i8* %3)
ret i64 %15
}