#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 */
size_t strlen(char const *);
int /*<<< orphan*/ strncmp(char *, char const *, size_t);
char *strtok(char *str, const char *delim) {
  static char *last_ptr;
  if (!str) {
    last_ptr = str;
  }
  size_t delim_len = strlen(delim);
  while (*last_ptr != '\0' && !strncmp(last_ptr, delim, delim_len)) {
    last_ptr++;
  }
  if (*last_ptr != '\0') {
    last_ptr++;
    return last_ptr - 1;
  } else {
    return NULL;
  }
}


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)
jne    1146 <strtok+0x26>
mov    -0x10(%rbp),%rax
mov    %rax,0x2ef2(%rip)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2ede(%rip),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     118a <strtok+0x6a>
mov    0x2ec6(%rip),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    119a <strtok+0x7a>
jmp    11b1 <strtok+0x91>
mov    0x2e97(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2e8c(%rip)
jmp    1153 <strtok+0x33>
mov    0x2e80(%rip),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ea <strtok+0xca>
mov    0x2e6d(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2e62(%rip)
mov    0x2e5b(%rip),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    11f2 <strtok+0xd2>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtok>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2f06(%rip),%r12
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmpb   $0x0,(%r12)
je     117e <strtok+0x5e>
mov    %rax,%r15
add    $0x1,%r12
nopl   0x0(%rax,%rax,1)
mov    %r12,%rbx
add    $0xffffffffffffffff,%r12
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1175 <strtok+0x55>
lea    0x1(%rbx),%r12
cmpb   $0x0,(%rbx)
jne    1150 <strtok+0x30>
xor    %r12d,%r12d
mov    %rbx,0x2ebc(%rip)
jmp    1181 <strtok+0x61>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@strtok.last_ptr = internal unnamed_addr global i8* null
define i8* @strtok(i8* readnone %0, i8* nocapture %1) {
%3 = icmp ne i8* %0, null
%4 = load i8*, i8** @strtok.last_ptr
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = load i8, i8* %4
%7 = icmp eq i8 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %2, %13
%9 = phi i8* [ %12, %13 ], [ %4, %2 ]
%10 = tail call i32 @strncmp(i8* %9, i8* %1, i64 %5)
%11 = icmp eq i32 %10, 0
%12 = getelementptr inbounds i8, i8* %9, i64 1
br i1 %11, label %13, label %16
13: ; preds = %8
%14 = load i8, i8* %12
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %8
16: ; preds = %8, %13
%17 = phi i8* [ null, %13 ], [ %9, %8 ]
store i8* %12, i8** @strtok.last_ptr
br label %18
18: ; preds = %16, %2
%19 = phi i8* [ null, %2 ], [ %17, %16 ]
ret i8* %19
}