#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_r(char *, char *, char **);
void teststrtok_r() {
  char *str1, *str2, *token, *subtoken;
  char *saveptr1, *saveptr2;
  int j;
  char arg1[] = "a/bbb///cc;xxx:yyy:";
  char arg2[] = ";:";
  char arg3[] = "/";
  for (j = 1, str1 = arg1;; j++, str1 = NULL) {
    token = strtok_r(str1, arg2, &saveptr1);
    if (token == NULL)
      break;
    printf("%d: %s\n", j, token);
    for (str2 = token;; str2 = NULL) {
      subtoken = strtok_r(str2, arg3, &saveptr2);
      if (subtoken == NULL)
        break;
      printf(" --> %s\n", subtoken);
    }
  }
}


x86 assembly (clang, -O0):
<teststrtok_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0xed1(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xece(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xecc(%rip),%eax
mov    %eax,-0x40(%rbp)
mov    0xec6(%rip),%ax
mov    %ax,-0x53(%rbp)
mov    0xebe(%rip),%al
mov    %al,-0x51(%rbp)
mov    0xeb5(%rip),%ax
mov    %ax,-0x55(%rbp)
movl   $0x1,-0x34(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x53(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119a <teststrtok_r+0x7a>
jmp    1216 <teststrtok_r+0xf6>
mov    -0x34(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x55(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dc <teststrtok_r+0xbc>
jmp    11fb <teststrtok_r+0xdb>
mov    -0x20(%rbp),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x10(%rbp)
jmp    11b7 <teststrtok_r+0x97>
jmp    1200 <teststrtok_r+0xe0>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
movq   $0x0,-0x8(%rbp)
jmp    1175 <teststrtok_r+0x55>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<teststrtok_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movaps 0xecb(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x3a7979,0x20(%rsp)
movb   $0x0,0x4(%rsp)
movw   $0x3a3b,0x2(%rsp)
movw   $0x2f,0x6(%rsp)
lea    0x10(%rsp),%rdi
lea    0x2(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1040 <strtok_r@plt>
test   %rax,%rax
je     1201 <teststrtok_r+0xe1>
mov    %rax,%r12
mov    $0x1,%r15d
lea    0x6(%rsp),%rbx
lea    0x30(%rsp),%rbp
lea    0x8(%rsp),%r13
lea    0xe8e(%rip),%r14
jmp    11bb <teststrtok_r+0x9b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r15d
xor    %edi,%edi
lea    0x2(%rsp),%rsi
mov    %r13,%rdx
call   1040 <strtok_r@plt>
mov    %rax,%r12
test   %rax,%rax
je     1201 <teststrtok_r+0xe1>
lea    0xe55(%rip),%rdi
mov    %r15d,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r12,%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <strtok_r@plt>
test   %rax,%rax
je     11a0 <teststrtok_r+0x80>
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %edi,%edi
jmp    11e0 <teststrtok_r+0xc0>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.teststrtok_r.arg1 = [20 x i8] c"a/bbb///cc;xxx:yyy:\00"
@__const.teststrtok_r.arg2 = [3 x i8] c";:\00"
@__const.teststrtok_r.arg3 = [2 x i8] c"/\00"
@.str = [8 x i8] c"%d: %s\0A\00"
@.str.1 = [9 x i8] c" --> %s\0A\00"
define void @teststrtok_r() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [20 x i8]
%9 = alloca [3 x i8]
%10 = alloca [2 x i8]
%11 = bitcast [20 x i8]* %8 to i8*
%12 = bitcast [3 x i8]* %9 to i8*
%13 = bitcast [2 x i8]* %10 to i8*
store i32 1, i32* %7
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
store i8* %14, i8** %1
br label %15
15: ; preds = %39, %0
%16 = load i8*, i8** %1
%17 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%18 = call i8* @strtok_r(i8* %16, i8* %17, i8** %5)
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %15
br label %42
22: ; preds = %15
%23 = load i32, i32* %7
%24 = load i8*, i8** %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %23, i8* %24)
%26 = load i8*, i8** %3
store i8* %26, i8** %2
br label %27
27: ; preds = %37, %22
%28 = load i8*, i8** %2
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%30 = call i8* @strtok_r(i8* %28, i8* %29, i8** %6)
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %27
br label %38
34: ; preds = %27
%35 = load i8*, i8** %4
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %34
store i8* null, i8** %2
br label %27
38: ; preds = %33
br label %39
39: ; preds = %38
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
store i8* null, i8** %1
br label %15
42: ; preds = %21
ret void
}

LLVM IR (-O2):
@__const.teststrtok_r.arg1 = [20 x i8] c"a/bbb///cc;xxx:yyy:\00"
@__const.teststrtok_r.arg2 = [3 x i8] c";:\00"
@.str = [8 x i8] c"%d: %s\0A\00"
@.str.1 = [9 x i8] c" --> %s\0A\00"
define void @teststrtok_r() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca [20 x i8]
%4 = alloca [3 x i8]
%5 = alloca i16
%6 = bitcast i8** %1 to i8*
%7 = bitcast i8** %2 to i8*
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%10 = bitcast i16* %5 to i8*
store i16 47, i16* %5
%11 = call i8* @strtok_r(i8* %8, i8* %9, i8** %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %28, label %13
13: ; preds = %0, %24
%14 = phi i8* [ %26, %24 ], [ %11, %0 ]
%15 = phi i32 [ %25, %24 ], [ 1, %0 ]
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %14)
%17 = call i8* @strtok_r(i8* %14, i8* %10, i8** %2)
%18 = icmp eq i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %13, %19
%20 = phi i8* [ %22, %19 ], [ %17, %13 ]
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i8* @strtok_r(i8* null, i8* %10, i8** %2)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %19
24: ; preds = %19, %13
%25 = add nuw nsw i32 %15, 1
%26 = call i8* @strtok_r(i8* null, i8* %9, i8** %1)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %13
28: ; preds = %24, %0
ret void
}