#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 my_strcspn(char *, char *);
char *my_strdup(char *);
char *my_strndup(char *, int);
int /*<<< orphan*/ my_strspn(char *, char *);
char *my_strtok(char *str, char *delim) {
  static char *buf = NULL;
  char *tmp;
  int dec;
  if (str != NULL)
    buf = my_strdup(str);
  buf += my_strspn(buf, delim);
  if (buf != NULL) {
    dec = my_strcspn(buf, delim);
    if (dec == 0)
      return (NULL);
    tmp = my_strndup(buf, dec);
    buf += dec;
    return (tmp);
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<my_strtok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116b <my_strtok+0x2b>
mov    -0x10(%rbp),%rdi
call   1040 <my_strdup@plt>
mov    %rax,0x2edd(%rip)
mov    0x2ed6(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <my_strspn@plt>
mov    %eax,%ecx
mov    0x2ec4(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2eb7(%rip)
cmpq   $0x0,0x2eaf(%rip)
je     1200 <my_strtok+0xc0>
mov    0x2ea2(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <my_strcspn@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11c9 <my_strtok+0x89>
movq   $0x0,-0x8(%rbp)
jmp    1208 <my_strtok+0xc8>
mov    0x2e78(%rip),%rdi
mov    -0x24(%rbp),%esi
call   1030 <my_strndup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    0x2e62(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e55(%rip)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1208 <my_strtok+0xc8>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strtok>:
push   %rbx
mov    %rsi,%rbx
test   %rdi,%rdi
je     115a <my_strtok+0x1a>
call   1040 <my_strdup@plt>
mov    %rax,%rdi
mov    %rax,0x2ef0(%rip)
jmp    1161 <my_strtok+0x21>
mov    0x2ee7(%rip),%rdi
mov    %rbx,%rsi
call   1060 <my_strspn@plt>
mov    0x2ed8(%rip),%rcx
movslq %eax,%rdi
add    %rcx,%rdi
mov    %rdi,0x2ecb(%rip)
test   %rcx,%rcx
je     11aa <my_strtok+0x6a>
mov    %rbx,%rsi
call   1050 <my_strcspn@plt>
test   %eax,%eax
je     11aa <my_strtok+0x6a>
mov    %eax,%ebx
mov    0x2eb1(%rip),%rdi
mov    %eax,%esi
call   1030 <my_strndup@plt>
movslq %ebx,%rcx
add    %rcx,0x2ea0(%rip)
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@my_strtok.buf = internal unnamed_addr global i8* null
define i8* @my_strtok(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i8*, i8** @my_strtok.buf
br label %8
6: ; preds = %2
%7 = tail call i8* @my_strdup(i8* %0)
store i8* %7, i8** @my_strtok.buf
br label %8
8: ; preds = %4, %6
%9 = phi i8* [ %5, %4 ], [ %7, %6 ]
%10 = tail call i32 @my_strspn(i8* %9, i8* %1)
%11 = load i8*, i8** @my_strtok.buf
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds i8, i8* %11, i64 %12
store i8* %13, i8** @my_strtok.buf
%14 = icmp eq i8* %11, null
br i1 %14, label %24, label %15
15: ; preds = %8
%16 = tail call i32 @my_strcspn(i8* %13, i8* %1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %15
%19 = load i8*, i8** @my_strtok.buf
%20 = tail call i8* @my_strndup(i8* %19, i32 %16)
%21 = load i8*, i8** @my_strtok.buf
%22 = sext i32 %16 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8* %23, i8** @my_strtok.buf
br label %24
24: ; preds = %8, %15, %18
%25 = phi i8* [ %20, %18 ], [ null, %15 ], [ null, %8 ]
ret i8* %25
}