#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*/ Panic(char *);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
char *strtok_r(void *, char *, char **);
void Check_strtok_r(void) {
  char *ptr, *last;
  ptr = strdup("12,abc");
  ptr = strtok_r(ptr, ",", &last);
  if (!ptr)
    Panic("strtok_r result #1");
  if (strcmp(ptr, "12") != 0)
    Panic("strtok_r token #1");
  ptr = strtok_r((void *)0, ",", &last);
  if (!ptr)
    Panic("strtok_r result #2");
  if (strcmp(ptr, "abc") != 0)
    Panic("strtok_r token #2");
  ptr = strtok_r((void *)0, ",", &last);
  if (ptr)
    Panic("strtok_r result #3");
}


x86 assembly (clang, -O0):
<Check_strtok_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea4(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1030 <strtok_r@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1187 <Check_strtok_r+0x47>
lea    0xe87(%rip),%rdi
call   1050 <Panic@plt>
mov    -0x8(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11ad <Check_strtok_r+0x6d>
lea    0xe77(%rip),%rdi
call   1050 <Panic@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe4f(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1030 <strtok_r@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11dc <Check_strtok_r+0x9c>
lea    0xe5a(%rip),%rdi
call   1050 <Panic@plt>
mov    -0x8(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     1202 <Check_strtok_r+0xc2>
lea    0xe47(%rip),%rdi
call   1050 <Panic@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdfa(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1030 <strtok_r@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1231 <Check_strtok_r+0xf1>
lea    0xe2a(%rip),%rdi
call   1050 <Panic@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Check_strtok_r>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rdi
call   1060 <strdup@plt>
lea    0xeaf(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1030 <strtok_r@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1179 <Check_strtok_r+0x39>
lea    0xe95(%rip),%rdi
call   1050 <Panic@plt>
lea    0xe9c(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1199 <Check_strtok_r+0x59>
lea    0xe8b(%rip),%rdi
call   1050 <Panic@plt>
lea    0xe67(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1030 <strtok_r@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c0 <Check_strtok_r+0x80>
lea    0xe76(%rip),%rdi
call   1050 <Panic@plt>
lea    0xe3c(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11e0 <Check_strtok_r+0xa0>
lea    0xe69(%rip),%rdi
call   1050 <Panic@plt>
lea    0xe20(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1030 <strtok_r@plt>
test   %rax,%rax
je     1204 <Check_strtok_r+0xc4>
lea    0xe57(%rip),%rdi
call   1050 <Panic@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [7 x i8] c"12,abc\00"
@.str.1 = [2 x i8] c",\00"
@.str.2 = [19 x i8] c"strtok_r result\00"
@.str.3 = [3 x i8] c"12\00"
@.str.4 = [18 x i8] c"strtok_r token\00"
@.str.5 = [19 x i8] c"strtok_r result\00"
@.str.6 = [4 x i8] c"abc\00"
@.str.7 = [18 x i8] c"strtok_r token\00"
@.str.8 = [19 x i8] c"strtok_r result\00"
define void @Check_strtok_r() {
%1 = alloca i8*
%2 = alloca i8*
%3 = call i8* @strdup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %3, i8** %1
%4 = load i8*, i8** %1
%5 = call i8* @strtok_r(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2)
store i8* %5, i8** %1
%6 = load i8*, i8** %1
%7 = icmp ne i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = call i32 @Panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i8*, i8** %1
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @Panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2)
store i8* %17, i8** %1
%18 = load i8*, i8** %1
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 @Panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i8*, i8** %1
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @Panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2)
store i8* %29, i8** %1
%30 = load i8*, i8** %1
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @Panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"12,abc\00"
@.str.1 = [2 x i8] c",\00"
@.str.2 = [19 x i8] c"strtok_r result\00"
@.str.3 = [3 x i8] c"12\00"
@.str.4 = [18 x i8] c"strtok_r token\00"
@.str.5 = [19 x i8] c"strtok_r result\00"
@.str.6 = [4 x i8] c"abc\00"
@.str.7 = [18 x i8] c"strtok_r token\00"
@.str.8 = [19 x i8] c"strtok_r result\00"
define void @Check_strtok_r() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = tail call dereferenceable_or_null(7) i8* @strdup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = call i8* @strtok_r(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @Panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = call i32 @Panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %1)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @Panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @Panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %1)
%25 = icmp eq i8* %24, null
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @Panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
ret void
}