#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 *);
char *strtok_r(char *, char *, char **);
int main(void) {
  char str[] = "Hello world.";
  char *token = strtok(str, " ");
  while (token != NULL) {
    printf("%s\n", token);
    token = strtok(NULL, " ");
  }
  printf("\n");
  printf("Note that the original string is changed by strtok:\n");
  printf("%s\n", str);
  printf("If you want to parse multiple strings concurrently with strtok(), "
         "use strtok_r()\n");
  char str1[] = "Foo bar";
  char str2[] = "Michigan Tech";
  char *saveptr1;
  char *saveptr2;
  char *tok1 = strtok_r(str1, " ", &saveptr1);
  while (tok1 != NULL) {
    char *tok2 = strtok_r(str2, " ", &saveptr2);
    while (tok2 != NULL) {
      printf("%s\n", tok2);
      tok2 = strtok_r(NULL, " ", &saveptr2);
    }
    printf("%s\n", tok1);
    tok1 = strtok_r(NULL, " ", &saveptr1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeba(%rip),%rax
mov    %rax,-0x11(%rbp)
mov    0xeb8(%rip),%eax
mov    %eax,-0x9(%rbp)
mov    0xeb3(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0x11(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a6 <main+0x76>
mov    -0x20(%rbp),%rsi
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe75(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    1170 <main+0x40>
lea    0xeeb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x11(%rbp),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0xeb1(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xeae(%rip),%rax
mov    %rax,-0x36(%rbp)
mov    0xeac(%rip),%eax
mov    %eax,-0x2e(%rbp)
mov    0xea6(%rip),%ax
mov    %ax,-0x2a(%rbp)
lea    -0x28(%rbp),%rdi
lea    0xdf6(%rip),%rsi
lea    -0x40(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12b0 <main+0x180>
lea    -0x36(%rbp),%rdi
lea    0xdd3(%rip),%rsi
lea    -0x48(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1281 <main+0x151>
mov    -0x58(%rbp),%rsi
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd9e(%rip),%rsi
lea    -0x48(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x58(%rbp)
jmp    1247 <main+0x117>
mov    -0x50(%rbp),%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd6f(%rip),%rsi
lea    -0x40(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x50(%rbp)
jmp    1224 <main+0xf4>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40,%rsp
movabs $0x2e646c726f7720,%rax
mov    %rax,0xd(%rsp)
movabs $0x6f77206f6c6c6548,%rax
20 77 6f
mov    %rax,0x8(%rsp)
lea    0xe9b(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     11a7 <main+0x67>
lea    0xe85(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
call   1040 <puts@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
jne    1190 <main+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe65(%rip),%rdi
call   1040 <puts@plt>
lea    0x8(%rsp),%rdi
call   1040 <puts@plt>
lea    0xe83(%rip),%rdi
call   1040 <puts@plt>
movabs $0x726162206f6f46,%rax
mov    %rax,0x20(%rsp)
movabs $0x68636554206e61,%rax
mov    %rax,0x2e(%rsp)
movabs $0x6e6167696863694d,%rax
67 61 6e
mov    %rax,0x28(%rsp)
lea    0xe06(%rip),%rsi
lea    0x20(%rsp),%rdi
lea    0x18(%rsp),%rdx
call   1050 <strtok_r@plt>
test   %rax,%rax
je     127c <main+0x13c>
mov    %rax,%r12
lea    0xde8(%rip),%rbx
lea    0x28(%rsp),%r14
lea    0x38(%rsp),%r15
lea    0x18(%rsp),%r13
jmp    125d <main+0x11d>
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <puts@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1050 <strtok_r@plt>
mov    %rax,%r12
test   %rax,%rax
je     127c <main+0x13c>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <strtok_r@plt>
test   %rax,%rax
je     1240 <main+0x100>
mov    %rax,%rdi
call   1040 <puts@plt>
xor    %edi,%edi
jmp    1260 <main+0x120>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.str = [13 x i8] c"Hello world.\00"
@.str = [2 x i8] c" \00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [53 x i8] c"Note that the original string is changed by strtok:\0A\00"
@.str.4 = [82 x i8] c"If you want to parse multiple strings concurrently with strtok(), use strtok_r()\0A\00"
@__const.main.str1 = [8 x i8] c"Foo bar\00"
@__const.main.str2 = [14 x i8] c"Michigan Tech\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [13 x i8]
%3 = alloca i8*
%4 = alloca [8 x i8]
%5 = alloca [14 x i8]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %1
%10 = bitcast [13 x i8]* %2 to i8*
%11 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%12 = call i8* @strtok(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %3
br label %13
13: ; preds = %16, %0
%14 = load i8*, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i8*, i8** %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %3
br label %13
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
%23 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.4, i64 0, i64 0))
%26 = bitcast [8 x i8]* %4 to i8*
%27 = bitcast [14 x i8]* %5 to i8*
%28 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%29 = call i8* @strtok_r(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6)
store i8* %29, i8** %8
br label %30
30: ; preds = %43, %20
%31 = load i8*, i8** %8
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %47
33: ; preds = %30
%34 = getelementptr inbounds [14 x i8], [14 x i8]* %5, i64 0, i64 0
%35 = call i8* @strtok_r(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7)
store i8* %35, i8** %9
br label %36
36: ; preds = %39, %33
%37 = load i8*, i8** %9
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i8*, i8** %9
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7)
store i8* %42, i8** %9
br label %36
43: ; preds = %36
%44 = load i8*, i8** %8
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %44)
%46 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6)
store i8* %46, i8** %8
br label %30
47: ; preds = %30
ret i32 0
}

LLVM IR (-O2):
@__const.main.str = [13 x i8] c"Hello world.\00"
@.str = [2 x i8] c" \00"
@__const.main.str2 = [14 x i8] c"Michigan Tech\00"
@str = [52 x i8] c"Note that the original string is changed by strtok:\00"
@str.5 = [81 x i8] c"If you want to parse multiple strings concurrently with strtok(), use strtok_r()\00"
define i32 @main() {
%1 = alloca [13 x i8]
%2 = alloca i64
%3 = alloca [14 x i8]
%4 = alloca i8*
%5 = alloca i8*
%6 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%7 = call i8* @strtok(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %0, %9
%10 = phi i8* [ %12, %9 ], [ %7, %0 ]
%11 = call i32 @puts(i8* %10)
%12 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %9
14: ; preds = %9, %0
%15 = call i32 @putchar(i32 10)
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%17 = call i32 @puts(i8* %6)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @str.5, i64 0, i64 0))
%19 = bitcast i64* %2 to i8*
store i64 32195221423877958, i64* %2
%20 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%21 = bitcast i8** %4 to i8*
%22 = bitcast i8** %5 to i8*
%23 = call i8* @strtok_r(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%24 = icmp eq i8* %23, null
br i1 %24, label %38, label %25
25: ; preds = %14, %34
%26 = phi i8* [ %36, %34 ], [ %23, %14 ]
%27 = call i8* @strtok_r(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %5)
%28 = icmp eq i8* %27, null
br i1 %28, label %34, label %29
29: ; preds = %25, %29
%30 = phi i8* [ %32, %29 ], [ %27, %25 ]
%31 = call i32 @puts(i8* %30)
%32 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %5)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %29
34: ; preds = %29, %25
%35 = call i32 @puts(i8* %26)
%36 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %25
38: ; preds = %34, %14
ret i32 0
}