#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 *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char const *, int /*<<< orphan*/);
char **realloc(char **, int);
int /*<<< orphan*/ strlen(char const *);
char *strtok(char *, char const *);
char **split_string(const char *string, const char *delim, size_t *len) {
  int n_spaces = 0;
  char **res = NULL;
  char *str = malloc(strlen(string));
  memcpy(str, string, strlen(string));
  char *part = strtok(str, delim);
  while (part) {
    res = realloc(res, sizeof(char *) * n_spaces);
    res[n_spaces] = part;
    n_spaces++;
    part = strtok(NULL, delim);
  }
  *len = n_spaces;
  free(str);
  return res;
}


x86 assembly (clang, -O0):
<split_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1221 <split_string+0xc1>
mov    -0x28(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1080 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    11d0 <split_string+0x70>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     11f4 <split_string+0x94>
mov    %rax,%rbp
mov    %r14,(%rsp)
xor    %ebx,%ebx
xor    %r14d,%r14d
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %ebx,%esi
call   1070 <realloc@plt>
mov    %rax,%r13
mov    %rbp,(%rax,%rbx,1)
add    $0x1,%r14
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <strtok@plt>
mov    %rax,%rbp
add    $0x8,%rbx
test   %rax,%rax
jne    11c0 <split_string+0x60>
mov    %r14d,%eax
mov    (%rsp),%r14
jmp    11f9 <split_string+0x99>
xor    %r13d,%r13d
xor    %eax,%eax
mov    %rax,(%r14)
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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