#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 */
char *LeslieStrtok(char *, char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *);
int main(int argc, char *argv[]) {
  char str[80] = {'\0'};
  char delim[3] = ",|";
  char *token = NULL;
  if (argv[1])
    snprintf(str, 80, argv[1]);
  else
    snprintf(str, 80, "hello|world,leslie,zhai");
  printf("strtok %s with %s\n", str, delim);
  token = LeslieStrtok(str, delim);
  while (token != NULL) {
    printf("%s\n", token);
    token = LeslieStrtok(NULL, delim);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
mov    0xe93(%rip),%ax
mov    %ax,-0x63(%rbp)
mov    0xe8b(%rip),%al
mov    %al,-0x61(%rbp)
movq   $0x0,-0x70(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11ac <main+0x6c>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    $0x50,%esi
call   1040 <snprintf@plt>
jmp    11c1 <main+0x81>
lea    -0x60(%rbp),%rdi
mov    $0x50,%esi
lea    0xe47(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rsi
lea    -0x63(%rbp),%rdx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x60(%rbp),%rdi
lea    -0x63(%rbp),%rsi
call   1060 <LeslieStrtok@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     121b <main+0xdb>
mov    -0x70(%rbp),%rsi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x63(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1060 <LeslieStrtok@plt>
mov    %rax,-0x70(%rbp)
jmp    11e8 <main+0xa8>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movb   $0x0,0xe(%rsp)
movw   $0x7c2c,0xc(%rsp)
mov    0x8(%rsi),%rdx
test   %rdx,%rdx
jne    116f <main+0x3f>
lea    0xe94(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x50,%esi
call   1040 <snprintf@plt>
lea    0xe96(%rip),%rdi
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r14
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <LeslieStrtok@plt>
test   %rax,%rax
je     11dc <main+0xac>
lea    0xe77(%rip),%r14
lea    0xc(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <LeslieStrtok@plt>
test   %rax,%rax
jne    11c0 <main+0x90>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.delim = [3 x i8] c",|\00"
@.str = [24 x i8] c"hello|world,leslie,zhai\00"
@.str.1 = [19 x i8] c"strtok %s with %s\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [80 x i8]
%7 = alloca [3 x i8]
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [80 x i8]* %6 to i8*
%10 = bitcast [3 x i8]* %7 to i8*
store i8* null, i8** %8
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @snprintf(i8* %16, i32 80, i8* %19)
br label %24
21: ; preds = %2
%22 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%23 = call i32 @snprintf(i8* %22, i32 80, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %21, %15
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%27 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* %26)
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%30 = call i8* @LeslieStrtok(i8* %28, i8* %29)
store i8* %30, i8** %8
br label %31
31: ; preds = %34, %24
%32 = load i8*, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i8*, i8** %8
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%38 = call i8* @LeslieStrtok(i8* null, i8* %37)
store i8* %38, i8** %8
br label %31
39: ; preds = %31
ret i32 0
}

LLVM IR (-O2):
@__const.main.delim = [3 x i8] c",|\00"
@.str = [24 x i8] c"hello|world,leslie,zhai\00"
@.str.1 = [19 x i8] c"strtok %s with %s\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = alloca [3 x i8]
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @snprintf(i8* %5, i32 80, i8* %8)
br label %14
12: ; preds = %2
%13 = call i32 @snprintf(i8* %5, i32 80, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %10
%15 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %6)
%16 = call i8* @LeslieStrtok(i8* %5, i8* %6)
%17 = icmp eq i8* %16, null
br i1 %17, label %23, label %18
18: ; preds = %14, %18
%19 = phi i8* [ %21, %18 ], [ %16, %14 ]
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = call i8* @LeslieStrtok(i8* null, i8* %6)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %18
23: ; preds = %18, %14
ret i32 0
}