#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 *strstrtok(char *str, char *delim) {
  static char *prev;
  if (!str)
    str = prev;
  if (str) {
    char *end = strstr(str, delim);
    if (end) {
      prev = end + strlen(delim);
      *end = 0;
    } else {
      prev = 0;
    }
  }
  return str;
}


x86 assembly (clang, -O0):
<strstrtok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1146 <strstrtok+0x26>
mov    0x2ef6(%rip),%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ad <strstrtok+0x8d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119d <strstrtok+0x7d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2ea7(%rip)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11a8 <strstrtok+0x88>
movq   $0x0,0x2e90(%rip)
jmp    11ad <strstrtok+0x8d>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstrtok>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
jne    1137 <strstrtok+0x17>
mov    0x2f01(%rip),%rbx
test   %rbx,%rbx
je     1174 <strstrtok+0x54>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1169 <strstrtok+0x49>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
add    %r15,%rax
mov    %rax,0x2ed5(%rip)
movb   $0x0,(%r15)
jmp    1174 <strstrtok+0x54>
movq   $0x0,0x2ec4(%rip)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@strstrtok.prev = internal unnamed_addr global i8* null
define i8* @strstrtok(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = load i8*, i8** @strstrtok.prev
%5 = select i1 %3, i8* %4, i8* %0
%6 = icmp eq i8* %5, null
br i1 %6, label %15, 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
%11 = tail call i32 @strlen(i8* %1)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
store i8* %13, i8** @strstrtok.prev
store i8 0, i8* %8
br label %15
14: ; preds = %7
store i8* null, i8** @strstrtok.prev
br label %15
15: ; preds = %10, %14, %2
ret i8* %5
}