#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__ strlen(char *);
char *strtok(char *, char *);
int countTokens(char *str, char *limit) {
  int count = 0;
  char *token;
  if (str != NULL && strlen(str)) {
    token = strtok(str, limit);
    while (token != NULL) {
      count++;
      token = strtok(NULL, limit);
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<countTokens>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1195 <countTokens+0x75>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
je     1195 <countTokens+0x75>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1190 <countTokens+0x70>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    1166 <countTokens+0x46>
jmp    1195 <countTokens+0x75>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countTokens>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     1142 <countTokens+0x32>
cmpb   $0x0,(%rdi)
je     1142 <countTokens+0x32>
mov    %rsi,%r14
call   1030 <strtok@plt>
test   %rax,%rax
je     1142 <countTokens+0x32>
xor    %ebx,%ebx
nop
add    $0x1,%ebx
xor    %edi,%edi
mov    %r14,%rsi
call   1030 <strtok@plt>
test   %rax,%rax
jne    1130 <countTokens+0x20>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32 @countTokens(i8* %0, i8* nocapture %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %15, label %4
4: ; preds = %2
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %4
%8 = tail call i8* @strtok(i8* %0, i8* %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %7, %10
%11 = phi i32 [ %12, %10 ], [ 0, %7 ]
%12 = add nuw nsw i32 %11, 1
%13 = tail call i8* @strtok(i8* null, i8* %1)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %10
15: ; preds = %10, %7, %4, %2
%16 = phi i32 [ 0, %4 ], [ 0, %2 ], [ 0, %7 ], [ %12, %10 ]
ret i32 %16
}