#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 */
scalar_t__ __likely(char);
int /*<<< orphan*/ strcspn(char *, char const *);
int /*<<< orphan*/ strspn(char *, char const *);
char *strtok_r(char *s, const char *delim, char **ptrptr) {
  char *tmp = 0;
  if (s == 0)
    s = *ptrptr;
  s += strspn(s, delim);
  if (__likely(*s)) {
    tmp = s;
    s += strcspn(s, delim);
    if (__likely(*s))
      *s++ = 0;
  }
  *ptrptr = s;
  return tmp;
}


x86 assembly (clang, -O0):
<strtok_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1162 <strtok_r+0x32>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strspn@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1050 <__likely@plt>
cmp    $0x0,%rax
je     11e7 <strtok_r+0xb7>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcspn@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1050 <__likely@plt>
cmp    $0x0,%rax
je     11e2 <strtok_r+0xb2>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x0,(%rax)
jmp    11e7 <strtok_r+0xb7>
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtok_r>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1149 <strtok_r+0x19>
mov    (%r14),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strspn@plt>
cltq
lea    (%rbx,%rax,1),%r12
movsbl (%rbx,%rax,1),%edi
call   1050 <__likely@plt>
test   %rax,%rax
je     1191 <strtok_r+0x61>
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <strcspn@plt>
cltq
lea    (%r12,%rax,1),%rbx
movsbl (%r12,%rax,1),%edi
call   1050 <__likely@plt>
test   %rax,%rax
je     1197 <strtok_r+0x67>
movb   $0x0,(%rbx)
add    $0x1,%rbx
jmp    1197 <strtok_r+0x67>
mov    %r12,%rbx
xor    %r12d,%r12d
mov    %rbx,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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