#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 strcspn(char *, char const *);
int /*<<< orphan*/ strspn(char *, char const *);
char *strtok(char *str, const char *delims) {
  static char *save = "";
  char **lasts = &save;
  char *sbegin, *send;
  sbegin = str ? str : *lasts;
  sbegin += strspn(sbegin, delims);
  if (*sbegin == '\0') {
    *lasts = "";
    return NULL;
  }
  send = sbegin + strcspn(sbegin, delims);
  if (*send != '\0') {
    *send++ = '\0';
  }
  *lasts = send;
  return sbegin;
}


x86 assembly (clang, -O0):
<strtok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0x2ef9(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1153 <strtok+0x33>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    115e <strtok+0x3e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strspn@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ae <strtok+0x8e>
mov    -0x20(%rbp),%rax
lea    0xe62(%rip),%rcx
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1208 <strtok+0xe8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcspn@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f5 <strtok+0xd5>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtok>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1136 <strtok+0x16>
mov    0x2efa(%rip),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strspn@plt>
cltq
cmpb   $0x0,(%rbx,%rax,1)
je     116d <strtok+0x4d>
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcspn@plt>
movslq %eax,%rcx
lea    (%rbx,%rcx,1),%rax
cmpb   $0x0,(%rbx,%rcx,1)
je     1176 <strtok+0x56>
movb   $0x0,(%rax)
add    $0x1,%rax
jmp    1176 <strtok+0x56>
lea    0xe8c(%rip),%rax
xor    %ebx,%ebx
mov    %rax,0x2eb3(%rip)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@strtok.save = internal global i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0)
@.str = [1 x i8] zeroinitializer
define i8* @strtok(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8** @strtok.save, i8*** %6
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i8*, i8** %4
br label %16
13: ; preds = %2
%14 = load i8**, i8*** %6
%15 = load i8*, i8** %14
br label %16
16: ; preds = %13, %11
%17 = phi i8* [ %12, %11 ], [ %15, %13 ]
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = load i8*, i8** %5
%20 = call i32 @strspn(i8* %18, i8* %19)
%21 = load i8*, i8** %7
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = load i8**, i8*** %6
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %29
store i8* null, i8** %3
br label %48
30: ; preds = %16
%31 = load i8*, i8** %7
%32 = load i8*, i8** %7
%33 = load i8*, i8** %5
%34 = call i32 @strcspn(i8* %32, i8* %33)
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %31, i64 %35
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = load i8*, i8** %8
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %8
store i8 0, i8* %42
br label %44
44: ; preds = %41, %30
%45 = load i8*, i8** %8
%46 = load i8**, i8*** %6
store i8* %45, i8** %46
%47 = load i8*, i8** %7
store i8* %47, i8** %3
br label %48
48: ; preds = %44, %28
%49 = load i8*, i8** %3
ret i8* %49
}

LLVM IR (-O2):
@strtok.save = internal unnamed_addr global i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0)
@.str = [1 x i8] zeroinitializer
define i8* @strtok(i8* %0, i8* nocapture %1) {
%3 = icmp eq i8* %0, null
%4 = load i8*, i8** @strtok.save
%5 = select i1 %3, i8* %4, i8* %0
%6 = tail call i32 @strspn(i8* %5, i8* %1)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %5, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %2
%12 = tail call i32 @strcspn(i8* %8, i8* %1)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %8, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %11
%18 = getelementptr inbounds i8, i8* %14, i64 1
store i8 0, i8* %14
br label %19
19: ; preds = %11, %17, %2
%20 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %2 ], [ %18, %17 ], [ %14, %11 ]
%21 = phi i8* [ null, %2 ], [ %8, %17 ], [ %8, %11 ]
store i8* %20, i8** @strtok.save
ret i8* %21
}