#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 **malloc(int);
char *strdup(char *);
int strlen(char *);
char *strtok(char *, char const *);
char **str_split(char *buffer, char const *separator, int *length) {
  char **result = malloc(sizeof(char *) * strlen(buffer));
  char *poly = strtok(buffer, separator);
  *length = 0;
  while (poly) {
    *(result + (*length)++) = strdup(poly);
    poly = strtok(0, separator);
  }
  return result;
}


x86 assembly (clang, -O0):
<str_split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     11ce <str_split+0x8e>
mov    -0x28(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rsi
mov    (%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,(%rsi)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1189 <str_split+0x49>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_split>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
lea    0x0(,%rax,8),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <strtok@plt>
movl   $0x0,0x0(%r13)
test   %rax,%rax
je     11c8 <str_split+0x88>
mov    %rax,%rdi
call   1060 <strdup@plt>
movl   $0x1,0x0(%r13)
mov    %rax,(%r15)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
je     11c8 <str_split+0x88>
movslq 0x0(%r13),%rbx
mov    %rax,%rdi
call   1060 <strdup@plt>
lea    0x1(%rbx),%ecx
mov    %ecx,0x0(%r13)
mov    %rax,(%r15,%rbx,8)
jmp    11a0 <str_split+0x60>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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