#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*/ printf(char *, ...);
char *strtok(char *, char *);
void teststrtok2() {
  int in = 0;
  char buffer[] = "Fred,John,Ann";
  char *p[10];
  char *buf = buffer;
  while ((p[in] = strtok(buf, ",")) != NULL) {
    printf("%s\n", p[in]);
    in++;
    buf = NULL;
  }
  printf("Total results = %d\n", in);
}


x86 assembly (clang, -O0):
<teststrtok2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xec7(%rip),%rax
mov    %rax,-0x12(%rbp)
mov    0xec5(%rip),%eax
mov    %eax,-0xa(%rbp)
mov    0xebf(%rip),%ax
mov    %ax,-0x6(%rbp)
lea    -0x12(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rdi
lea    0xeaa(%rip),%rsi
call   1040 <strtok@plt>
movslq -0x4(%rbp),%rcx
mov    %rax,-0x70(%rbp,%rcx,8)
cmp    $0x0,%rax
je     11a9 <teststrtok2+0x89>
movslq -0x4(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rsi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
movq   $0x0,-0x78(%rbp)
jmp    1159 <teststrtok2+0x39>
mov    -0x4(%rbp),%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<teststrtok2>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movabs $0x6e6e412c6e686f,%rax
mov    %rax,0xe(%rsp)
movabs $0x686f4a2c64657246,%rax
4a 6f 68
mov    %rax,0x8(%rsp)
lea    0xeb2(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1050 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     119a <teststrtok2+0x6a>
lea    0xe9a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rax,%rdi
call   1030 <puts@plt>
add    $0x1,%ebx
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
jne    1180 <teststrtok2+0x50>
lea    0xe6f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.teststrtok2.buffer = [14 x i8] c"Fred,John,Ann\00"
@.str = [2 x i8] c",\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [20 x i8] c"Total results = %d\0A\00"
define void @teststrtok2() {
%1 = alloca i32
%2 = alloca [14 x i8]
%3 = alloca [10 x i8*]
%4 = alloca i8*
store i32 0, i32* %1
%5 = bitcast [14 x i8]* %2 to i8*
%6 = getelementptr inbounds [14 x i8], [14 x i8]* %2, i64 0, i64 0
store i8* %6, i8** %4
br label %7
7: ; preds = %14, %0
%8 = load i8*, i8** %4
%9 = call i8* @strtok(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %11
store i8* %9, i8** %12
%13 = icmp ne i8* %9, null
br i1 %13, label %14, label %22
14: ; preds = %7
%15 = load i32, i32* %1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %16
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %1
%21 = add nsw i32 %20, 1
store i32 %21, i32* %1
store i8* null, i8** %4
br label %7
22: ; preds = %7
%23 = load i32, i32* %1
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %23)
ret void
}

LLVM IR (-O2):
@__const.teststrtok2.buffer = [14 x i8] c"Fred,John,Ann\00"
@.str = [2 x i8] c",\00"
@.str.2 = [20 x i8] c"Total results = %d\0A\00"
define void @teststrtok2() {
%1 = alloca [14 x i8]
%2 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 0
%3 = call i8* @strtok(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %12, label %5
5: ; preds = %0, %5
%6 = phi i8* [ %10, %5 ], [ %3, %0 ]
%7 = phi i32 [ %9, %5 ], [ 0, %0 ]
%8 = call i32 @puts(i8* %6)
%9 = add nuw nsw i32 %7, 1
%10 = 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 %5
12: ; preds = %5, %0
%13 = phi i32 [ 0, %0 ], [ %9, %5 ]
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %13)
ret void
}