#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 strspn(char *, char const *);
char *strtok(char *str, const char *delim) {
  static char *p = 0;
  if (str) {
    p = str;
  } else if (!p) {
    return 0;
  }
  str = p + strspn(p, delim);
  p = str + strcspn(str, delim);
  if (p == str) {
    return p = 0;
  }
  p = *p ? *p = 0, p + 1 : 0;
  return str;
}


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     114b <strtok+0x2b>
mov    -0x10(%rbp),%rax
mov    %rax,0x2ef2(%rip)
jmp    116b <strtok+0x4b>
cmpq   $0x0,0x2ee5(%rip)
jne    1166 <strtok+0x46>
movq   $0x0,-0x8(%rbp)
jmp    1236 <strtok+0x116>
jmp    116b <strtok+0x4b>
mov    0x2ec6(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2ebb(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strspn@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcspn@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e7a(%rip)
mov    0x2e73(%rip),%rax
cmp    -0x10(%rbp),%rax
jne    11e7 <strtok+0xc7>
movq   $0x0,0x2e5e(%rip)
movq   $0x0,-0x8(%rbp)
jmp    1236 <strtok+0x116>
mov    0x2e4a(%rip),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1218 <strtok+0xf8>
mov    0x2e37(%rip),%rax
movb   $0x0,(%rax)
mov    0x2e2d(%rip),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1223 <strtok+0x103>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    1223 <strtok+0x103>
mov    -0x30(%rbp),%rax
mov    %rax,0x2e0a(%rip)
mov    -0x10(%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     1188 <strtok+0x68>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strspn@plt>
movslq %eax,%r15
add    %rbx,%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <strcspn@plt>
test   %eax,%eax
je     116f <strtok+0x4f>
cltq
add    %r15,%rax
cmpb   $0x0,(%rax)
je     1176 <strtok+0x56>
movb   $0x0,(%rax)
add    $0x1,%rax
jmp    1178 <strtok+0x58>
xor    %eax,%eax
xor    %r15d,%r15d
jmp    1178 <strtok+0x58>
xor    %eax,%eax
mov    %rax,0x2eb9(%rip)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %r15d,%r15d
jmp    117f <strtok+0x5f>

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

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