#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 */
char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
char *strtok_r(char *, char const *, char **);
char *tokenizer(char *str, const char *delim, char **saveptr) {
  char *token;
  if (str != NULL) {
    char *copied_str = malloc(strlen(str + 1));
    strcpy(copied_str, str);
    token = strtok_r(copied_str, delim, saveptr);
  } else {
    token = strtok_r(NULL, delim, saveptr);
  }
  return token;
}


x86 assembly (clang, -O0):
<tokenizer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     119e <tokenizer+0x5e>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
jmp    11b3 <tokenizer+0x73>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tokenizer>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
test   %rdi,%rdi
je     1176 <tokenizer+0x36>
mov    %rdi,%rbx
add    $0x1,%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    1179 <tokenizer+0x39>
xor    %r12d,%r12d
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <strtok_r@plt>

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

LLVM IR (-O2):
define i8* @tokenizer(i8* %0, i8* nocapture %1, i8** %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %10, label %5
5: ; preds = %3
%6 = getelementptr inbounds i8, i8* %0, i64 1
%7 = tail call i32 @strlen(i8* %6)
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i32 @strcpy(i8* %8, i8* %0)
br label %10
10: ; preds = %3, %5
%11 = phi i8* [ %8, %5 ], [ null, %3 ]
%12 = tail call i8* @strtok_r(i8* %11, i8* %1, i8** %2)
ret i8* %12
}