#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 *strpbrk(char *, char const *);
int /*<<< orphan*/ strspn(char *, char const *);
char *strtok(char *s1, const char *s2) {
  char *sbegin, *send;
  static char *next;
  sbegin = (s1 != NULL) ? s1 : next;
  if (sbegin == NULL)
    return NULL;
  sbegin += strspn(sbegin, s2);
  if (*sbegin == '\0') {
    next = NULL;
    return NULL;
  }
  send = strpbrk(sbegin, s2);
  if (send != NULL)
    *send++ = '\0';
  next = send;
  return sbegin;
}


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     1148 <strtok+0x28>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1153 <strtok+0x33>
mov    0x2ee9(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1173 <strtok+0x53>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <strtok+0xd9>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strspn@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b8 <strtok+0x98>
movq   $0x0,0x2e8d(%rip)
movq   $0x0,-0x8(%rbp)
jmp    11f9 <strtok+0xd9>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strpbrk@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e6 <strtok+0xc6>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,0x2e47(%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    1136 <strtok+0x16>
mov    0x2f02(%rip),%rbx
test   %rbx,%rbx
je     116d <strtok+0x4d>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strspn@plt>
cltq
cmpb   $0x0,(%rbx,%rax,1)
je     1171 <strtok+0x51>
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strpbrk@plt>
test   %rax,%rax
je     1177 <strtok+0x57>
mov    %rax,%rcx
add    $0x1,%rcx
movb   $0x0,(%rax)
jmp    1179 <strtok+0x59>
xor    %ebx,%ebx
jmp    1180 <strtok+0x60>
xor    %ecx,%ecx
xor    %ebx,%ebx
jmp    1179 <strtok+0x59>
xor    %ecx,%ecx
mov    %rcx,0x2eb8(%rip)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@strtok.next = 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 i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i8*, i8** %4
br label %14
12: ; preds = %2
%13 = load i8*, i8** @strtok.next
br label %14
14: ; preds = %12, %10
%15 = phi i8* [ %11, %10 ], [ %13, %12 ]
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i8* null, i8** %3
br label %43
19: ; preds = %14
%20 = load i8*, i8** %6
%21 = load i8*, i8** %5
%22 = call i32 @strspn(i8* %20, i8* %21)
%23 = load i8*, i8** %6
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %19
store i8* null, i8** @strtok.next
store i8* null, i8** %3
br label %43
31: ; preds = %19
%32 = load i8*, i8** %6
%33 = load i8*, i8** %5
%34 = call i8* @strpbrk(i8* %32, i8* %33)
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i8*, i8** %7
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %7
store i8 0, i8* %38
br label %40
40: ; preds = %37, %31
%41 = load i8*, i8** %7
store i8* %41, i8** @strtok.next
%42 = load i8*, i8** %6
store i8* %42, i8** %3
br label %43
43: ; preds = %40, %30, %18
%44 = load i8*, i8** %3
ret i8* %44
}

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