#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 *);
void *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
char **str_split(char *str, char *delimiter) {
  int str_len = strlen(str) + 1;
  char *tmp = malloc(sizeof(char) * str_len);
  strcpy(tmp, str);
  char *ptr = malloc(sizeof(char));
  ptr = strtok(str, delimiter);
  int count = 0;
  while (ptr != NULL) {
    count++;
    ptr = strtok(NULL, delimiter);
  }
  char **splitted = malloc(count * sizeof(char *));
  ptr = strtok(tmp, delimiter);
  count = 0;
  while (ptr != NULL) {
    splitted[count] = malloc(sizeof(char) * (strlen(ptr) + 1));
    strcpy(splitted[count], ptr);
    count++;
    ptr = strtok(NULL, delimiter);
  }
  free(ptr);
  return splitted;
}


x86 assembly (clang, -O0):
<str_split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11df <str_split+0x8f>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    11b5 <str_split+0x65>
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1271 <str_split+0x121>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    120a <str_split+0xba>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_split>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11a2 <str_split+0x52>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <strtok@plt>
add    $0x8,%ebx
test   %rax,%rax
jne    1190 <str_split+0x40>
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
je     1204 <str_split+0xb4>
mov    %rax,%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,(%rbx)
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <strtok@plt>
mov    %rax,%r15
add    $0x8,%rbx
test   %rax,%rax
jne    11d0 <str_split+0x80>
xor    %edi,%edi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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