#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 */
void *calloc(size_t, int);
int /*<<< orphan*/ free(char *);
char **realloc(char **, size_t);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strtok(char *, char *);
char **strutil_split(char *string, char *delimiter, size_t *amount) {
  char *local = calloc(strlen(string) + 1, sizeof(char));
  strncpy(local, string, strlen(string) + 1);
  char **tokens = calloc(1, sizeof(char *));
  char *tok = strtok(local, delimiter);
  for (size_t i = 0; tok != NULL; i++) {
    size_t length = strlen(tok) + 1;
    char *token = calloc(length, sizeof(char));
    strncpy(token, tok, length);
    tokens[i] = token;
    (*amount)++;
    tok = strtok(NULL, delimiter);
    if (tok != NULL) {
      tokens = realloc(tokens, ((*amount) + 1) * sizeof(char *));
    }
  }
  free(local);
  return tokens;
}


x86 assembly (clang, -O0):
<strutil_split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
add    $0x1,%eax
movslq %eax,%rdx
call   1040 <strncpy@plt>
mov    $0x1,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1295 <strutil_split+0x135>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1080 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     127f <strutil_split+0x11f>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
shl    $0x3,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
jmp    1284 <strutil_split+0x124>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11e9 <strutil_split+0x89>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strutil_split>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rdx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <strncpy@plt>
mov    $0x1,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,%r13
mov    %rbp,%rdi
mov    %r14,0x10(%rsp)
mov    %r14,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     124c <strutil_split+0xec>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <strncpy@plt>
mov    %r15,0x0(%r13,%r14,1)
addq   $0x1,(%r12)
xor    %edi,%edi
mov    0x10(%rsp),%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     1247 <strutil_split+0xe7>
mov    %rax,%rbx
mov    (%r12),%rax
lea    0x8(,%rax,8),%rsi
mov    %r13,%rdi
call   1070 <realloc@plt>
mov    %rax,%r13
add    $0x8,%r14
jmp    11e0 <strutil_split+0x80>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8** @strutil_split(i8* %0, i8* %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64* %2, i64** %6
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = call i8* @calloc(i64 %16, i32 1)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = load i8*, i8** %4
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = call i32 @strncpy(i8* %18, i8* %19, i64 %23)
%25 = call i8* @calloc(i64 1, i32 8)
%26 = bitcast i8* %25 to i8**
store i8** %26, i8*** %8
%27 = load i8*, i8** %7
%28 = load i8*, i8** %5
%29 = call i8* @strtok(i8* %27, i8* %28)
store i8* %29, i8** %9
store i64 0, i64* %10
br label %30
30: ; preds = %63, %3
%31 = load i8*, i8** %9
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %66
33: ; preds = %30
%34 = load i8*, i8** %9
%35 = call i32 @strlen(i8* %34)
%36 = add nsw i32 %35, 1
%37 = sext i32 %36 to i64
store i64 %37, i64* %11
%38 = load i64, i64* %11
%39 = call i8* @calloc(i64 %38, i32 1)
store i8* %39, i8** %12
%40 = load i8*, i8** %12
%41 = load i8*, i8** %9
%42 = load i64, i64* %11
%43 = call i32 @strncpy(i8* %40, i8* %41, i64 %42)
%44 = load i8*, i8** %12
%45 = load i8**, i8*** %8
%46 = load i64, i64* %10
%47 = getelementptr inbounds i8*, i8** %45, i64 %46
store i8* %44, i8** %47
%48 = load i64*, i64** %6
%49 = load i64, i64* %48
%50 = add i64 %49, 1
store i64 %50, i64* %48
%51 = load i8*, i8** %5
%52 = call i8* @strtok(i8* null, i8* %51)
store i8* %52, i8** %9
%53 = load i8*, i8** %9
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %62
55: ; preds = %33
%56 = load i8**, i8*** %8
%57 = load i64*, i64** %6
%58 = load i64, i64* %57
%59 = add i64 %58, 1
%60 = mul i64 %59, 8
%61 = call i8** @realloc(i8** %56, i64 %60)
store i8** %61, i8*** %8
br label %62
62: ; preds = %55, %33
br label %63
63: ; preds = %62
%64 = load i64, i64* %10
%65 = add i64 %64, 1
store i64 %65, i64* %10
br label %30
66: ; preds = %30
%67 = load i8*, i8** %7
%68 = call i32 @free(i8* %67)
%69 = load i8**, i8*** %8
ret i8** %69
}

LLVM IR (-O2):
define i8** @strutil_split(i8* %0, i8* nocapture %1, i64* nocapture %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = tail call i8* @calloc(i64 %6, i32 1)
%8 = tail call i32 @strlen(i8* %0)
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = tail call i32 @strncpy(i8* %7, i8* %0, i64 %10)
%12 = tail call i8* @calloc(i64 1, i32 8)
%13 = bitcast i8* %12 to i8**
%14 = tail call i8* @strtok(i8* %7, i8* %1)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %19, %3
%17 = phi i8** [ %13, %3 ], [ %20, %19 ]
%18 = tail call i32 @free(i8* %7)
ret i8** %17
19: ; preds = %3, %33
%20 = phi i8** [ %37, %33 ], [ %13, %3 ]
%21 = phi i8* [ %31, %33 ], [ %14, %3 ]
%22 = phi i64 [ %38, %33 ], [ 0, %3 ]
%23 = tail call i32 @strlen(i8* %21)
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = tail call i8* @calloc(i64 %25, i32 1)
%27 = tail call i32 @strncpy(i8* %26, i8* %21, i64 %25)
%28 = getelementptr inbounds i8*, i8** %20, i64 %22
store i8* %26, i8** %28
%29 = load i64, i64* %2
%30 = add i64 %29, 1
store i64 %30, i64* %2
%31 = tail call i8* @strtok(i8* null, i8* %1)
%32 = icmp eq i8* %31, null
br i1 %32, label %16, label %33
33: ; preds = %19
%34 = load i64, i64* %2
%35 = shl i64 %34, 3
%36 = add i64 %35, 8
%37 = tail call i8** @realloc(i8** %20, i64 %36)
%38 = add i64 %22, 1
br label %19
}