#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 strlen(char *);
char *strstr(char *, char *);
char *mbstrtok(char *str, char *delimiter) {
  static char *ptr = NULL;
  char *bdy = NULL;
  if (!str)
    str = ptr;
  if (!str)
    return NULL;
  if ((bdy = strstr(str, delimiter)) != NULL) {
    *bdy = '\0';
    ptr = bdy + strlen(delimiter);
  } else {
    ptr = NULL;
  }
  return str;
}


x86 assembly (clang, -O0):
<mbstrtok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114e <mbstrtok+0x2e>
mov    0x2eee(%rip),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1166 <mbstrtok+0x46>
movq   $0x0,-0x8(%rbp)
jmp    11c4 <mbstrtok+0xa4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11b1 <mbstrtok+0x91>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e8c(%rip)
jmp    11bc <mbstrtok+0x9c>
movq   $0x0,0x2e7c(%rip)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbstrtok>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1137 <mbstrtok+0x17>
mov    0x2f01(%rip),%rbx
test   %rbx,%rbx
je     116a <mbstrtok+0x4a>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1161 <mbstrtok+0x41>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
add    %r15,%rax
jmp    1163 <mbstrtok+0x43>
xor    %eax,%eax
mov    %rax,0x2ece(%rip)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@mbstrtok.ptr = internal global i8* null
define i8* @mbstrtok(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = load i8*, i8** @mbstrtok.ptr
store i8* %10, i8** %4
br label %11
11: ; preds = %9, %2
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %11
store i8* null, i8** %3
br label %30
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = load i8*, i8** %5
%18 = call i8* @strstr(i8* %16, i8* %17)
store i8* %18, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %27
20: ; preds = %15
%21 = load i8*, i8** %6
store i8 0, i8* %21
%22 = load i8*, i8** %6
%23 = load i8*, i8** %5
%24 = call i32 @strlen(i8* %23)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8* %26, i8** @mbstrtok.ptr
br label %28
27: ; preds = %15
store i8* null, i8** @mbstrtok.ptr
br label %28
28: ; preds = %27, %20
%29 = load i8*, i8** %4
store i8* %29, i8** %3
br label %30
30: ; preds = %28, %14
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
@mbstrtok.ptr = internal unnamed_addr global i8* null
define i8* @mbstrtok(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = load i8*, i8** @mbstrtok.ptr
%5 = select i1 %3, i8* %4, i8* %0
%6 = icmp eq i8* %5, null
br i1 %6, label %16, label %7
7: ; preds = %2
%8 = tail call i8* @strstr(i8* %5, i8* dereferenceable(1) %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %14, label %10
10: ; preds = %7
store i8 0, i8* %8
%11 = tail call i32 @strlen(i8* %1)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
br label %14
14: ; preds = %7, %10
%15 = phi i8* [ %13, %10 ], [ null, %7 ]
store i8* %15, i8** @mbstrtok.ptr
br label %16
16: ; preds = %2, %14
ret i8* %5
}