#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 cnt_tok(char *, char *);
char **gbgc_malloc(int /*<<< orphan*/ *, int);
char *my_strdup(char *);
int my_strlen(char *);
char **my_strtokloop(char **, char *, int);
int /*<<< orphan*/ my_tokcut(char *, char *);
char **my_strtok(char *str, char *del) {
  int len;
  int n;
  char **out;
  char *cpy;
  if (str == NULL || del == NULL || (len = my_strlen(str)) <= 0 ||
      (cpy = my_strdup(str)) == NULL || (n = cnt_tok(str, del)) < 1 ||
      (out = gbgc_malloc(NULL, sizeof(char *) * (n + 1))) == NULL)
    return (NULL);
  out[0] = cpy;
  my_tokcut(cpy, del);
  return (my_strtokloop(out, cpy, len));
}


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     11f0 <my_strtok+0x90>
cmpq   $0x0,-0x18(%rbp)
je     11f0 <my_strtok+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <my_strlen@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    11f0 <my_strtok+0x90>
mov    -0x10(%rbp),%rdi
call   1060 <my_strdup@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11f0 <my_strtok+0x90>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <cnt_tok@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x1,%eax
jl     11f0 <my_strtok+0x90>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <gbgc_malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11fd <my_strtok+0x9d>
movq   $0x0,-0x8(%rbp)
jmp    1229 <my_strtok+0xc9>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <my_tokcut@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <my_strtokloop@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strtok>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11de <my_strtok+0x7e>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11de <my_strtok+0x7e>
mov    %rdi,%rbp
call   1030 <my_strlen@plt>
test   %eax,%eax
jle    11de <my_strtok+0x7e>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1060 <my_strdup@plt>
test   %rax,%rax
je     11de <my_strtok+0x7e>
mov    %rax,%r14
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <cnt_tok@plt>
test   %eax,%eax
jle    11de <my_strtok+0x7e>
lea    0x8(,%rax,8),%esi
xor    %edi,%edi
call   1040 <gbgc_malloc@plt>
test   %rax,%rax
je     11de <my_strtok+0x7e>
mov    %rax,%rbp
mov    %r14,(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <my_tokcut@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <my_strtokloop@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8** @my_strtok(i8* %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %36, label %12
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %36, label %15
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = call i32 @my_strlen(i8* %16)
store i32 %17, i32* %6
%18 = icmp sle i32 %17, 0
br i1 %18, label %36, label %19
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = call i8* @my_strdup(i8* %20)
store i8* %21, i8** %9
%22 = icmp eq i8* %21, null
br i1 %22, label %36, label %23
23: ; preds = %19
%24 = load i8*, i8** %4
%25 = load i8*, i8** %5
%26 = call i32 @cnt_tok(i8* %24, i8* %25)
store i32 %26, i32* %7
%27 = icmp slt i32 %26, 1
br i1 %27, label %36, label %28
28: ; preds = %23
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = mul i64 8, %31
%33 = trunc i64 %32 to i32
%34 = call i8** @gbgc_malloc(i32* null, i32 %33)
store i8** %34, i8*** %8
%35 = icmp eq i8** %34, null
br i1 %35, label %36, label %37
36: ; preds = %28, %23, %19, %15, %12, %2
store i8** null, i8*** %3
br label %48
37: ; preds = %28
%38 = load i8*, i8** %9
%39 = load i8**, i8*** %8
%40 = getelementptr inbounds i8*, i8** %39, i64 0
store i8* %38, i8** %40
%41 = load i8*, i8** %9
%42 = load i8*, i8** %5
%43 = call i32 @my_tokcut(i8* %41, i8* %42)
%44 = load i8**, i8*** %8
%45 = load i8*, i8** %9
%46 = load i32, i32* %6
%47 = call i8** @my_strtokloop(i8** %44, i8* %45, i32 %46)
store i8** %47, i8*** %3
br label %48
48: ; preds = %37, %36
%49 = load i8**, i8*** %3
ret i8** %49
}

LLVM IR (-O2):
define i8** @my_strtok(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %23, label %6
6: ; preds = %2
%7 = tail call i32 @my_strlen(i8* %0)
%8 = icmp slt i32 %7, 1
br i1 %8, label %23, label %9
9: ; preds = %6
%10 = tail call i8* @my_strdup(i8* %0)
%11 = icmp eq i8* %10, null
br i1 %11, label %23, label %12
12: ; preds = %9
%13 = tail call i32 @cnt_tok(i8* %0, i8* %1)
%14 = icmp slt i32 %13, 1
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = shl i32 %13, 3
%17 = add i32 %16, 8
%18 = tail call i8** @gbgc_malloc(i32* null, i32 %17)
%19 = icmp eq i8** %18, null
br i1 %19, label %23, label %20
20: ; preds = %15
store i8* %10, i8** %18
%21 = tail call i32 @my_tokcut(i8* %10, i8* %1)
%22 = tail call i8** @my_strtokloop(i8** %18, i8* %10, i32 %7)
br label %23
23: ; preds = %2, %6, %9, %12, %15, %20
%24 = phi i8** [ %22, %20 ], [ null, %15 ], [ null, %12 ], [ null, %9 ], [ null, %6 ], [ null, %2 ]
ret i8** %24
}