#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 */
typedef int wchar_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int *wcspbrk(int *, int const *);
int /*<<< orphan*/ wcsspn(int *, int const *);
wchar_t *wcstok(wchar_t *wcs, const wchar_t *delim, wchar_t **save_ptr) {
  wchar_t *result;
  if (wcs == NULL) {
    if (*save_ptr == NULL) {
      __set_errno(EINVAL);
      return NULL;
    } else
      wcs = *save_ptr;
  }
  wcs += wcsspn(wcs, delim);
  if (*wcs == L'\0') {
    *save_ptr = NULL;
    return NULL;
  }
  result = wcs;
  wcs = wcspbrk(result, delim);
  if (wcs == NULL)
    *save_ptr = NULL;
  else {
    *wcs = L'\0';
    *save_ptr = wcs + 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<wcstok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1188 <wcstok+0x58>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1178 <wcstok+0x48>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    1223 <wcstok+0xf3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    1188 <wcstok+0x58>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <wcsspn@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11ce <wcstok+0x9e>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1223 <wcstok+0xf3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <wcspbrk@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1202 <wcstok+0xd2>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
jmp    121b <wcstok+0xeb>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rcx
add    $0x4,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcstok>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
test   %rdi,%rdi
jne    114e <wcstok+0x1e>
mov    (%r14),%rbx
test   %rbx,%rbx
je     1191 <wcstok+0x61>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <wcsspn@plt>
cltq
cmpl   $0x0,(%rbx,%rax,4)
je     1181 <wcstok+0x51>
lea    (%rbx,%rax,4),%r15
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <wcspbrk@plt>
test   %rax,%rax
je     118a <wcstok+0x5a>
movl   $0x0,(%rax)
add    $0x4,%rax
jmp    118c <wcstok+0x5c>
movq   $0x0,(%r14)
jmp    119f <wcstok+0x6f>
xor    %eax,%eax
mov    %rax,(%r14)
jmp    11a2 <wcstok+0x72>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINVAL = global i32 0
define i32* @wcstok(i32* %0, i32* %1, i32** %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32**
%8 = alloca i32*
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32** %2, i32*** %7
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %22
11: ; preds = %3
%12 = load i32**, i32*** %7
%13 = load i32*, i32** %12
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @EINVAL
%17 = call i32 @__set_errno(i32 %16)
store i32* null, i32** %4
br label %50
18: ; preds = %11
%19 = load i32**, i32*** %7
%20 = load i32*, i32** %19
store i32* %20, i32** %5
br label %21
21: ; preds = %18
br label %22
22: ; preds = %21, %3
%23 = load i32*, i32** %5
%24 = load i32*, i32** %6
%25 = call i32 @wcsspn(i32* %23, i32* %24)
%26 = load i32*, i32** %5
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i32, i32* %26, i64 %27
store i32* %28, i32** %5
%29 = load i32*, i32** %5
%30 = load i32, i32* %29
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = load i32**, i32*** %7
store i32* null, i32** %33
store i32* null, i32** %4
br label %50
34: ; preds = %22
%35 = load i32*, i32** %5
store i32* %35, i32** %8
%36 = load i32*, i32** %8
%37 = load i32*, i32** %6
%38 = call i32* @wcspbrk(i32* %36, i32* %37)
store i32* %38, i32** %5
%39 = load i32*, i32** %5
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = load i32**, i32*** %7
store i32* null, i32** %42
br label %48
43: ; preds = %34
%44 = load i32*, i32** %5
store i32 0, i32* %44
%45 = load i32*, i32** %5
%46 = getelementptr inbounds i32, i32* %45, i64 1
%47 = load i32**, i32*** %7
store i32* %46, i32** %47
br label %48
48: ; preds = %43, %41
%49 = load i32*, i32** %8
store i32* %49, i32** %4
br label %50
50: ; preds = %48, %32, %15
%51 = load i32*, i32** %4
ret i32* %51
}

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