#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 *);
int /*<<< orphan*/ free(char *);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
char *strtok_r(void *, char *, char **);
void Check_strtok_r(void) {
  char *str, *ptr, *last;
  ptr = strdup("12,abc");
  str = ptr;
  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");
  free(str);
}


x86 assembly (clang, -O0):
<Check_strtok_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8c(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119f <Check_strtok_r+0x4f>
lea    0xe6f(%rip),%rdi
call   1060 <Panic@plt>
mov    -0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11c5 <Check_strtok_r+0x75>
lea    0xe5f(%rip),%rdi
call   1060 <Panic@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe37(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f4 <Check_strtok_r+0xa4>
lea    0xe42(%rip),%rdi
call   1060 <Panic@plt>
mov    -0x10(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     121a <Check_strtok_r+0xca>
lea    0xe2f(%rip),%rdi
call   1060 <Panic@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xde2(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1249 <Check_strtok_r+0xf9>
lea    0xe12(%rip),%rdi
call   1060 <Panic@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Check_strtok_r>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,%r14
lea    0xe9d(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1040 <strtok_r@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1189 <Check_strtok_r+0x39>
lea    0xe85(%rip),%rdi
call   1060 <Panic@plt>
lea    0xe8c(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11a9 <Check_strtok_r+0x59>
lea    0xe7b(%rip),%rdi
call   1060 <Panic@plt>
lea    0xe57(%rip),%rsi
mov    %rsp,%rdx
xor    %edi,%edi
call   1040 <strtok_r@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11ce <Check_strtok_r+0x7e>
lea    0xe68(%rip),%rdi
call   1060 <Panic@plt>
lea    0xe2e(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11ee <Check_strtok_r+0x9e>
lea    0xe5b(%rip),%rdi
call   1060 <Panic@plt>
lea    0xe12(%rip),%rsi
mov    %rsp,%rdx
xor    %edi,%edi
call   1040 <strtok_r@plt>
test   %rax,%rax
je     1210 <Check_strtok_r+0xc0>
lea    0xe4b(%rip),%rdi
call   1060 <Panic@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
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 = alloca i8*
%4 = call i8* @strdup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %2
%5 = load i8*, i8** %2
store i8* %5, i8** %1
%6 = load i8*, i8** %2
%7 = call i8* @strtok_r(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %3)
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = icmp ne i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @Panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i8*, i8** %2
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @Panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %3)
store i8* %19, i8** %2
%20 = load i8*, i8** %2
%21 = icmp ne i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @Panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i8*, i8** %2
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @Panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %3)
store i8* %31, i8** %2
%32 = load i8*, i8** %2
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @Panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = load i8*, i8** %1
%38 = call i32 @free(i8* %37)
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
%29 = call i32 @free(i8* %3)
ret void
}