#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 /*<<< orphan*/ free(char *);
char **realloc(char **, int);
char *strdup(char *);
char *strtok(char *, char const *);
tokenise(char *string, const char *delimiter, char ***tokens) {
  int count = 0;
  char *input = strdup(string);
  char *result = NULL;
  result = strtok(input, delimiter);
  while (result != NULL) {
    *tokens = realloc(*tokens, (count + 1) * sizeof(char *));
    (*tokens)[count++] = strdup(result);
    result = strtok(NULL, delimiter);
  }
  free(input);
  return count;
}


x86 assembly (clang, -O0):
<tokenise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11eb <tokenise+0xab>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok@plt>
mov    %rax,-0x30(%rbp)
jmp    1181 <tokenise+0x41>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tokenise>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
call   1060 <strdup@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
je     11d5 <tokenise+0x95>
mov    %rax,%rbx
mov    %rbp,(%rsp)
mov    $0x8,%ebp
mov    %r15,0x8(%rsp)
xor    %r13d,%r13d
mov    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%rdi
lea    0x1(%r13),%rax
mov    %rax,0x10(%rsp)
mov    %ebp,%esi
call   1040 <realloc@plt>
mov    %rax,%r14
mov    %rax,(%r15)
mov    %rbx,%rdi
call   1060 <strdup@plt>
mov    %rax,(%r14,%r13,8)
mov    0x10(%rsp),%r13
xor    %edi,%edi
mov    %r12,%rsi
call   1050 <strtok@plt>
mov    %rax,%rbx
add    $0x8,%ebp
test   %rax,%rax
jne    1190 <tokenise+0x50>
mov    (%rsp),%rbp
jmp    11d8 <tokenise+0x98>
xor    %r13d,%r13d
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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