#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 *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strtok(char *, char *);
void demo_strtok(void) {
  char str[] = "This is a string";
  char copy[50];
  strcpy(copy, str);
  printf("str = %s\n", str);
  char *word1 = strtok(copy, " ");
  char *word2 = strtok(NULL, " ");
  char *word3 = strtok(NULL, " ");
  char *word4 = strtok(NULL, " ");
  char *word5 = strtok(NULL, " ");
  printf("word1 = %s\n", word1);
  printf("word2 = %s\n", word2);
  printf("word3 = %s\n", word3);
  printf("word4 = %s\n", word4);
  printf("word5 = %s\n", (word5 != NULL ? word5 : "NULL"));
}


x86 assembly (clang, -O0):
<demo_strtok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    0xebe(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xebb(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeb9(%rip),%al
mov    %al,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x20(%rbp),%rsi
lea    0xe9f(%rip),%rdi
call   1040 <printf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x68(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe85(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x70(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe71(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x78(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe5d(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x80(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe49(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x88(%rbp)
mov    -0x68(%rbp),%rsi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
mov    -0x70(%rbp),%rsi
lea    0xe30(%rip),%rdi
call   1040 <printf@plt>
mov    -0x78(%rbp),%rsi
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x80(%rbp),%rsi
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x88(%rbp)
je     123f <demo_strtok+0x10f>
mov    -0x88(%rbp),%rax
mov    %rax,-0x90(%rbp)
jmp    1252 <demo_strtok+0x122>
lea    0xe13(%rip),%rax
mov    %rax,-0x90(%rbp)
jmp    1252 <demo_strtok+0x122>
mov    -0x90(%rbp),%rsi
lea    0xded(%rip),%rdi
call   1040 <printf@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<demo_strtok>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x60,%rsp
movaps 0xebc(%rip),%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
lea    0x20(%rsp),%r14
mov    %rsp,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xeaa(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xea5(%rip),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <strtok@plt>
mov    %rax,%r14
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <strtok@plt>
mov    %rax,%r15
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <strtok@plt>
mov    %rax,%r12
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <strtok@plt>
mov    %rax,%r13
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <strtok@plt>
mov    %rax,%rbx
lea    0xe5e(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
lea    0xe5b(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
lea    0xe58(%rip),%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
lea    0xe55(%rip),%rdi
mov    %r13,%rsi
call   1040 <printf@plt>
test   %rbx,%rbx
lea    0xe5b(%rip),%rsi
cmovne %rbx,%rsi
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.demo_strtok.str = [17 x i8] c"This is a string\00"
@.str = [10 x i8] c"str = %s\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [12 x i8] c"word1 = %s\0A\00"
@.str.3 = [12 x i8] c"word2 = %s\0A\00"
@.str.4 = [12 x i8] c"word3 = %s\0A\00"
@.str.5 = [12 x i8] c"word4 = %s\0A\00"
@.str.6 = [12 x i8] c"word5 = %s\0A\00"
@.str.7 = [5 x i8] c"NULL\00"
define void @demo_strtok() {
%1 = alloca [17 x i8]
%2 = alloca [50 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = bitcast [17 x i8]* %1 to i8*
%9 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%11 = call i32 @strcpy(i8* %9, i8* %10)
%12 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%13 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%15 = call i8* @strtok(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %15, i8** %3
%16 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %4
%17 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %5
%18 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %18, i8** %6
%19 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %19, i8** %7
%20 = load i8*, i8** %3
%21 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %4
%23 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %5
%25 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %6
%27 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %0
%31 = load i8*, i8** %7
br label %33
32: ; preds = %0
br label %33
33: ; preds = %32, %30
%34 = phi i8* [ %31, %30 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), %32 ]
%35 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %34)
ret void
}

LLVM IR (-O2):
@__const.demo_strtok.str = [17 x i8] c"This is a string\00"
@.str = [10 x i8] c"str = %s\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [12 x i8] c"word1 = %s\0A\00"
@.str.3 = [12 x i8] c"word2 = %s\0A\00"
@.str.4 = [12 x i8] c"word3 = %s\0A\00"
@.str.5 = [12 x i8] c"word4 = %s\0A\00"
@.str.6 = [12 x i8] c"word5 = %s\0A\00"
@.str.7 = [5 x i8] c"NULL\00"
define void @demo_strtok() {
%1 = alloca [17 x i8]
%2 = alloca [50 x i8]
%3 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* %3)
%6 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %3)
%7 = call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%13 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%14 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%15 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %10)
%16 = icmp eq i8* %11, null
%17 = select i1 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* %11
%18 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i8* %17)
ret void
}