#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 *);
int /*<<< orphan*/ printf(char *);
char *strdup(char *);
char *strtok(char *, char *);
int count_tokens_in_str(char *str, char *delimiters) {
  char *str_copy;
  char *token;
  int count;
  count = 0;
  str_copy = strdup(str);
  if (str_copy == NULL) {
    printf("Error duplicating string.\n");
    return -1;
  }
  token = strtok(str_copy, delimiters);
  while (token != NULL) {
    token = strtok(NULL, delimiters);
    count++;
  }
  free(str_copy);
  str_copy = NULL;
  return count;
}


x86 assembly (clang, -O0):
<count_tokens_in_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1187 <count_tokens_in_str+0x47>
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <count_tokens_in_str+0x99>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11c2 <count_tokens_in_str+0x82>
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1198 <count_tokens_in_str+0x58>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_tokens_in_str>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
call   1060 <strdup@plt>
test   %rax,%rax
je     118c <count_tokens_in_str+0x4c>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1182 <count_tokens_in_str+0x42>
nopw   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r15,%rsi
call   1050 <strtok@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    1170 <count_tokens_in_str+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    119d <count_tokens_in_str+0x5d>
lea    0xe6d(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Error duplicating string.\0A\00"
define i32 @count_tokens_in_str(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %31
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = load i8*, i8** %5
%18 = call i8* @strtok(i8* %16, i8* %17)
store i8* %18, i8** %7
br label %19
19: ; preds = %22, %15
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = call i8* @strtok(i8* null, i8* %23)
store i8* %24, i8** %7
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
br label %19
27: ; preds = %19
%28 = load i8*, i8** %6
%29 = call i32 @free(i8* %28)
store i8* null, i8** %6
%30 = load i32, i32* %8
store i32 %30, i32* %3
br label %31
31: ; preds = %27, %13
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@str = [26 x i8] c"Error duplicating string.\00"
define i32 @count_tokens_in_str(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %18
7: ; preds = %2
%8 = tail call i8* @strtok(i8* %3, i8* %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %7, %10
%11 = phi i32 [ %13, %10 ], [ 0, %7 ]
%12 = tail call i8* @strtok(i8* null, i8* %1)
%13 = add nuw nsw i32 %11, 1
%14 = icmp eq i8* %12, null
br i1 %14, label %15, label %10
15: ; preds = %10, %7
%16 = phi i32 [ 0, %7 ], [ %13, %10 ]
%17 = tail call i32 @free(i8* %3)
br label %18
18: ; preds = %15, %5
%19 = phi i32 [ -1, %5 ], [ %16, %15 ]
ret i32 %19
}