#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 *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strtok_r(char *, char *, char **);
int main() {
  char *save_ptr = NULL;
  char *tmp_str = NULL;
  char *str = malloc(100);
  snprintf(str, 100, "%s", "array[10].test");
  tmp_str = str;
  for (;; tmp_str = NULL) {
    char *k = strtok_r(tmp_str, "[].", &save_ptr);
    printf("k = %s\n", k ? k : "null");
    if (!k)
      break;
  }
  free(str);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x64,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x64,%esi
lea    0xe73(%rip),%rdx
lea    0xe6f(%rip),%rcx
call   1050 <snprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe66(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d1 <main+0x81>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11e1 <main+0x91>
lea    0xe46(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11e1 <main+0x91>
mov    -0x30(%rbp),%rsi
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1201 <main+0xb1>
jmp    1213 <main+0xc3>
jmp    1206 <main+0xb6>
movq   $0x0,-0x18(%rbp)
jmp    11a1 <main+0x51>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movq   $0x0,(%rsp)
mov    $0x64,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe89(%rip),%rdx
lea    0xe85(%rip),%rcx
mov    %rax,%rdi
mov    $0x64,%esi
call   1050 <snprintf@plt>
lea    0xe80(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1060 <strtok_r@plt>
mov    %rax,%rbx
test   %rax,%rax
lea    0xe74(%rip),%rbp
mov    %rax,%rsi
cmove  %rbp,%rsi
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
test   %rbx,%rbx
je     1207 <main+0xb7>
lea    0xe49(%rip),%r15
mov    %rsp,%r12
lea    0xe43(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edi,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <strtok_r@plt>
mov    %rax,%rbx
test   %rax,%rax
mov    %rax,%rsi
cmove  %rbp,%rsi
mov    %r13,%rdi
call   1040 <printf@plt>
test   %rbx,%rbx
jne    11e0 <main+0x90>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [15 x i8] c"array[10].test\00"
@.str.2 = [4 x i8] c"[].\00"
@.str.3 = [8 x i8] c"k = %s\0A\00"
@.str.4 = [5 x i8] c"null\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %2
store i8* null, i8** %3
%6 = call i8* @malloc(i32 100)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = call i32 @snprintf(i8* %7, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%9 = load i8*, i8** %4
store i8* %9, i8** %3
br label %10
10: ; preds = %25, %0
%11 = load i8*, i8** %3
%12 = call i8* @strtok_r(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %2)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i8*, i8** %5
br label %18
17: ; preds = %10
br label %18
18: ; preds = %17, %15
%19 = phi i8* [ %16, %15 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %17 ]
%20 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %18
br label %26
24: ; preds = %18
br label %25
25: ; preds = %24
store i8* null, i8** %3
br label %10
26: ; preds = %23
%27 = load i8*, i8** %4
%28 = call i32 @free(i8* %27)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [15 x i8] c"array[10].test\00"
@.str.2 = [4 x i8] c"[].\00"
@.str.3 = [8 x i8] c"k = %s\0A\00"
@.str.4 = [5 x i8] c"null\00"
define i32 @main() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%3 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%4 = tail call i32 @snprintf(i8* %3, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%5 = call i8* @strtok_r(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %1)
%6 = icmp eq i8* %5, null
%7 = select i1 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %5
%8 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br i1 %6, label %14, label %9
9: ; preds = %0, %9
%10 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %1)
%11 = icmp eq i8* %10, null
%12 = select i1 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %10
%13 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %12)
br i1 %11, label %14, label %9
14: ; preds = %9, %0
%15 = call i32 @free(i8* %3)
ret i32 0
}