#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 *s, const char *delim) {
  char *tmp = NULL;
  char *t;
  static char *ptr;
  if (s)
    ptr = s;
  if (!ptr && !s)
    return NULL;
  ptr += strspn(ptr, delim);
  t = strpbrk(ptr, delim);
  if (t) {
    *t++ = 0;
    tmp = ptr;
    ptr = t;
  } else {
    tmp = ptr;
    ptr = NULL;
  }
  return tmp;
}


x86 assembly (clang, -O0):
<strtok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114e <strtok+0x2e>
mov    -0x10(%rbp),%rax
mov    %rax,0x2eea(%rip)
cmpq   $0x0,0x2ee2(%rip)
jne    1174 <strtok+0x54>
cmpq   $0x0,-0x10(%rbp)
jne    1174 <strtok+0x54>
movq   $0x0,-0x8(%rbp)
jmp    1204 <strtok+0xe4>
mov    0x2ebd(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strspn@plt>
mov    %eax,%ecx
mov    0x2eab(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e9e(%rip)
mov    0x2e97(%rip),%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    0x2e66(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,0x2e57(%rip)
jmp    11fc <strtok+0xdc>
mov    0x2e4b(%rip),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,0x2e3c(%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   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
jne    113c <strtok+0x1c>
mov    0x2f01(%rip),%rbx
test   %rbx,%rbx
je     117b <strtok+0x5b>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strspn@plt>
movslq %eax,%r15
add    %rbx,%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strpbrk@plt>
test   %rax,%rax
je     1169 <strtok+0x49>
mov    %rax,%rcx
add    $0x1,%rcx
movb   $0x0,(%rax)
jmp    116b <strtok+0x4b>
xor    %ecx,%ecx
mov    %rcx,0x2ec6(%rip)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %r15d,%r15d
jmp    1172 <strtok+0x52>

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

LLVM IR (-O2):
@strtok.ptr = 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.ptr
%6 = icmp eq i8* %5, null
br i1 %6, label %18, label %7
7: ; preds = %2, %4
%8 = phi i8* [ %5, %4 ], [ %0, %2 ]
%9 = tail call i32 @strspn(i8* %8, i8* %1)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = tail call i8* @strpbrk(i8* %11, i8* %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %7
%15 = getelementptr inbounds i8, i8* %12, i64 1
store i8 0, i8* %12
br label %16
16: ; preds = %7, %14
%17 = phi i8* [ %15, %14 ], [ null, %7 ]
store i8* %17, i8** @strtok.ptr
br label %18
18: ; preds = %4, %16
%19 = phi i8* [ %11, %16 ], [ null, %4 ]
ret i8* %19
}