#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 *string_duplicate(char *);
char *strtok_r(char *, char *, char **);
char **string_split(char *text, char *separator) {
  char **substrings = NULL;
  int size = 0;
  char *text_to_iterate = string_duplicate(text);
  char *token = NULL, *next = NULL;
  token = strtok_r(text_to_iterate, separator, &next);
  while (token != NULL) {
    size++;
    substrings = realloc(substrings, sizeof(char *) * size);
    substrings[size - 1] = string_duplicate(token);
    token = strtok_r(NULL, separator, &next);
  }
  size++;
  substrings = realloc(substrings, sizeof(char *) * size);
  substrings[size - 1] = NULL;
  free(text_to_iterate);
  return substrings;
}


x86 assembly (clang, -O0):
<string_split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <string_duplicate@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11f3 <string_split+0xb3>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <string_duplicate@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x38(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
jmp    1191 <string_split+0x51>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

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

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

LLVM IR (-O2):
define i8** @string_split(i8* %0, i8* nocapture %1) {
%3 = alloca i8*
%4 = tail call i8* @string_duplicate(i8* %0)
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = call i8* @strtok_r(i8* %4, i8* %1, i8** %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %22, label %8
8: ; preds = %2, %8
%9 = phi i64 [ %12, %8 ], [ 0, %2 ]
%10 = phi i8* [ %18, %8 ], [ %6, %2 ]
%11 = phi i8** [ %15, %8 ], [ null, %2 ]
%12 = add nuw i64 %9, 1
%13 = trunc i64 %12 to i32
%14 = shl i32 %13, 3
%15 = call i8** @realloc(i8** %11, i32 %14)
%16 = call i8* @string_duplicate(i8* %10)
%17 = getelementptr inbounds i8*, i8** %15, i64 %9
store i8* %16, i8** %17
%18 = call i8* @strtok_r(i8* null, i8* %1, i8** %3)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %8
20: ; preds = %8
%21 = trunc i64 %12 to i32
br label %22
22: ; preds = %20, %2
%23 = phi i32 [ 0, %2 ], [ %21, %20 ]
%24 = phi i8** [ null, %2 ], [ %15, %20 ]
%25 = shl i32 %23, 3
%26 = add i32 %25, 8
%27 = call i8** @realloc(i8** %24, i32 %26)
%28 = zext i32 %23 to i64
%29 = getelementptr inbounds i8*, i8** %27, i64 %28
store i8* null, i8** %29
%30 = call i32 @free(i8* %4)
ret i8** %27
}