#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 **allocateMemory(int);
char **reallocateMemory(char **, unsigned int);
char *strtok(char *, char const *);
void splitStringOnDelimiter(char *string, const char *delimiter,
                            char ***tokenTarget, unsigned *numTokens) {
  *numTokens = 0;
  *tokenTarget = allocateMemory(sizeof(char *));
  char *token = strtok(string, delimiter);
  while (token != NULL) {
    *tokenTarget =
        reallocateMemory(*tokenTarget, ((*numTokens) + 1) * sizeof(char *));
    (*tokenTarget)[*numTokens] = token;
    ++(*numTokens);
    token = strtok(NULL, delimiter);
  }
}


x86 assembly (clang, -O0):
<splitStringOnDelimiter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    $0x8,%edi
call   1050 <allocateMemory@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11df <splitStringOnDelimiter+0xaf>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x3,%rax
mov    %eax,%esi
call   1040 <reallocateMemory@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1030 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1177 <splitStringOnDelimiter+0x47>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<splitStringOnDelimiter>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movl   $0x0,(%rcx)
mov    $0x8,%edi
call   1050 <allocateMemory@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strtok@plt>
test   %rax,%rax
je     11a7 <splitStringOnDelimiter+0x77>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r14),%rdi
mov    (%r12),%eax
lea    0x8(,%rax,8),%esi
call   1040 <reallocateMemory@plt>
mov    %rax,(%r14)
mov    (%r12),%ecx
mov    %rbx,(%rax,%rcx,8)
lea    0x1(%rcx),%eax
mov    %eax,(%r12)
xor    %edi,%edi
mov    %r15,%rsi
call   1030 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <splitStringOnDelimiter+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @splitStringOnDelimiter(i8* %0, i8* %1, i8*** %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8***
%8 = alloca i32*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8*** %2, i8**** %7
store i32* %3, i32** %8
%10 = load i32*, i32** %8
store i32 0, i32* %10
%11 = call i8** @allocateMemory(i32 8)
%12 = load i8***, i8**** %7
store i8** %11, i8*** %12
%13 = load i8*, i8** %5
%14 = load i8*, i8** %6
%15 = call i8* @strtok(i8* %13, i8* %14)
store i8* %15, i8** %9
br label %16
16: ; preds = %19, %4
%17 = load i8*, i8** %9
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %42
19: ; preds = %16
%20 = load i8***, i8**** %7
%21 = load i8**, i8*** %20
%22 = load i32*, i32** %8
%23 = load i32, i32* %22
%24 = add i32 %23, 1
%25 = zext i32 %24 to i64
%26 = mul i64 %25, 8
%27 = trunc i64 %26 to i32
%28 = call i8** @reallocateMemory(i8** %21, i32 %27)
%29 = load i8***, i8**** %7
store i8** %28, i8*** %29
%30 = load i8*, i8** %9
%31 = load i8***, i8**** %7
%32 = load i8**, i8*** %31
%33 = load i32*, i32** %8
%34 = load i32, i32* %33
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %32, i64 %35
store i8* %30, i8** %36
%37 = load i32*, i32** %8
%38 = load i32, i32* %37
%39 = add i32 %38, 1
store i32 %39, i32* %37
%40 = load i8*, i8** %6
%41 = call i8* @strtok(i8* null, i8* %40)
store i8* %41, i8** %9
br label %16
42: ; preds = %16
ret void
}

LLVM IR (-O2):
define void @splitStringOnDelimiter(i8* %0, i8* nocapture %1, i8*** nocapture %2, i32* nocapture %3) {
store i32 0, i32* %3
%5 = tail call i8** @allocateMemory(i32 8)
store i8** %5, i8*** %2
%6 = tail call i8* @strtok(i8* %0, i8* %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %21, label %8
8: ; preds = %4, %8
%9 = phi i8* [ %19, %8 ], [ %6, %4 ]
%10 = load i8**, i8*** %2
%11 = load i32, i32* %3
%12 = shl i32 %11, 3
%13 = add i32 %12, 8
%14 = tail call i8** @reallocateMemory(i8** %10, i32 %13)
store i8** %14, i8*** %2
%15 = load i32, i32* %3
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8*, i8** %14, i64 %16
store i8* %9, i8** %17
%18 = add i32 %15, 1
store i32 %18, i32* %3
%19 = tail call i8* @strtok(i8* null, i8* %1)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %8
21: ; preds = %8, %4
ret void
}