#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 */
size_t strcspn(char *, char const *);
size_t strspn(char *, char const *);
char *strtok(char *str, const char *delimiters) {
  static char *last = NULL;
  char *res;
  size_t start, end;
  if (str)
    last = str;
  if (last == NULL)
    return NULL;
  start = strspn(last, delimiters);
  last += start;
  if (*last == '\0') {
    last = NULL;
    return NULL;
  }
  res = last;
  end = strcspn(last, delimiters);
  if (last[end] == '\0')
    last = NULL;
  else {
    last[end] = '\0';
    last += end + 1;
  }
  return res;
}


x86 assembly (clang, -O0):
<strtok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <strtok+0x26>
mov    -0x10(%rbp),%rax
mov    %rax,0x2ef2(%rip)
cmpq   $0x0,0x2eea(%rip)
jne    1161 <strtok+0x41>
movq   $0x0,-0x8(%rbp)
jmp    1226 <strtok+0x106>
mov    0x2ed0(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strspn@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    0x2eb8(%rip),%rax
mov    %rax,0x2eb1(%rip)
mov    0x2eaa(%rip),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b2 <strtok+0x92>
movq   $0x0,0x2e93(%rip)
movq   $0x0,-0x8(%rbp)
jmp    1226 <strtok+0x106>
mov    0x2e7f(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e74(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcspn@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e60(%rip),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11f9 <strtok+0xd9>
movq   $0x0,0x2e44(%rip)
jmp    121e <strtok+0xfe>
mov    0x2e38(%rip),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
add    0x2e21(%rip),%rax
mov    %rax,0x2e1a(%rip)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%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    113b <strtok+0x1b>
mov    0x2f02(%rip),%rbx
test   %rbx,%rbx
je     1187 <strtok+0x67>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strspn@plt>
cmpb   $0x0,(%rbx,%rax,1)
je     116d <strtok+0x4d>
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcspn@plt>
cmpb   $0x0,(%rbx,%rax,1)
je     1173 <strtok+0x53>
movb   $0x0,(%rbx,%rax,1)
add    %rbx,%rax
add    $0x1,%rax
jmp    1175 <strtok+0x55>
xor    %eax,%eax
xor    %ebx,%ebx
jmp    1175 <strtok+0x55>
xor    %eax,%eax
mov    %rax,0x2ebc(%rip)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %ebx,%ebx
jmp    117c <strtok+0x5c>

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

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