#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*/ assert(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char **malloc(int);
size_t split_count(char *, char const);
int /*<<< orphan*/ stderr;
char *strdup(char *);
char *strtok(char *, char *);
char **str_split(char *a_str, const char a_delim) {
  char delim[2];
  delim[0] = a_delim;
  delim[1] = 0;
  size_t count = split_count(a_str, a_delim);
  char **result = malloc(sizeof(char *) * count);
  if (!result) {
    fprintf(stderr, "Failed to allocate memory when splitting string '%s'.\n",
            a_str);
    return NULL;
  }
  if (result) {
    size_t idx = 0;
    char *token = strtok(a_str, delim);
    while (token) {
      assert(idx < count);
      *(result + idx++) = strdup(token);
      token = strtok(0, delim);
    }
    assert(idx == count - 1);
    *(result + idx) = 0;
  }
  return result;
}


x86 assembly (clang, -O0):
<str_split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
mov    %al,-0x13(%rbp)
movb   $0x0,-0x12(%rbp)
mov    -0x10(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1070 <split_count@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d1 <str_split+0x71>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe41(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1282 <str_split+0x122>
cmpq   $0x0,-0x28(%rbp)
je     127a <str_split+0x11a>
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x13(%rbp),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     124e <str_split+0xee>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x38(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x30(%rbp)
mov    %rdx,(%rax,%rcx,8)
lea    -0x13(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1060 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    11f5 <str_split+0x95>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_split>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
mov    %sil,0xe(%rsp)
movb   $0x0,0xf(%rsp)
call   1070 <split_count@plt>
mov    %rax,%r12
lea    0x0(,%rax,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11f2 <str_split+0x92>
lea    0xe(%rsp),%rsi
mov    %r15,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     120c <str_split+0xac>
mov    %rax,%r13
xor    %ebx,%ebx
lea    0xe(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
cmp    %r12,%rbx
setb   %dil
call   1030 <assert@plt>
mov    %r13,%rdi
call   1080 <strdup@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r15,%rsi
call   1060 <strtok@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11c0 <str_split+0x60>
jmp    120e <str_split+0xae>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    %r15,%rdx
call   1040 <fprintf@plt>
jmp    1228 <str_split+0xc8>
xor    %ebx,%ebx
add    $0xffffffffffffffff,%r12
xor    %edi,%edi
cmp    %r12,%rbx
sete   %dil
call   1030 <assert@plt>
movq   $0x0,(%r14,%rbx,8)
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [55 x i8] c"Failed to allocate memory when splitting string '%s'.\0A\00"
define i8** @str_split(i8* %0, i8 signext %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i8
%6 = alloca [2 x i8]
%7 = alloca i64
%8 = alloca i8**
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %4
store i8 %1, i8* %5
%11 = load i8, i8* %5
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
store i8 %11, i8* %12
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 1
store i8 0, i8* %13
%14 = load i8*, i8** %4
%15 = load i8, i8* %5
%16 = call i64 @split_count(i8* %14, i8 signext %15)
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = mul i64 8, %17
%19 = trunc i64 %18 to i32
%20 = call i8** @malloc(i32 %19)
store i8** %20, i8*** %8
%21 = load i8**, i8*** %8
%22 = icmp ne i8** %21, null
br i1 %22, label %27, label %23
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %4
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i8* %25)
store i8** null, i8*** %3
br label %63
27: ; preds = %2
%28 = load i8**, i8*** %8
%29 = icmp ne i8** %28, null
br i1 %29, label %30, label %61
30: ; preds = %27
store i64 0, i64* %9
%31 = load i8*, i8** %4
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%33 = call i8* @strtok(i8* %31, i8* %32)
store i8* %33, i8** %10
br label %34
34: ; preds = %37, %30
%35 = load i8*, i8** %10
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %51
37: ; preds = %34
%38 = load i64, i64* %9
%39 = load i64, i64* %7
%40 = icmp ult i64 %38, %39
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = load i8*, i8** %10
%44 = call i8* @strdup(i8* %43)
%45 = load i8**, i8*** %8
%46 = load i64, i64* %9
%47 = add i64 %46, 1
store i64 %47, i64* %9
%48 = getelementptr inbounds i8*, i8** %45, i64 %46
store i8* %44, i8** %48
%49 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%50 = call i8* @strtok(i8* null, i8* %49)
store i8* %50, i8** %10
br label %34
51: ; preds = %34
%52 = load i64, i64* %9
%53 = load i64, i64* %7
%54 = sub i64 %53, 1
%55 = icmp eq i64 %52, %54
%56 = zext i1 %55 to i32
%57 = call i32 @assert(i32 %56)
%58 = load i8**, i8*** %8
%59 = load i64, i64* %9
%60 = getelementptr inbounds i8*, i8** %58, i64 %59
store i8* null, i8** %60
br label %61
61: ; preds = %51, %27
%62 = load i8**, i8*** %8
store i8** %62, i8*** %3
br label %63
63: ; preds = %61, %23
%64 = load i8**, i8*** %3
ret i8** %64
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [55 x i8] c"Failed to allocate memory when splitting string '%s'.\0A\00"
define noalias i8** @str_split(i8* %0, i8 signext %1) {
%3 = alloca [2 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
store i8 %1, i8* %4
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
store i8 0, i8* %5
%6 = tail call i64 @split_count(i8* %0, i8 signext %1)
%7 = trunc i64 %6 to i32
%8 = shl i32 %7, 3
%9 = tail call i8** @malloc(i32 %8)
%10 = icmp eq i8** %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %35
14: ; preds = %2
%15 = call i8* @strtok(i8* %0, i8* %4)
%16 = icmp eq i8* %15, null
br i1 %16, label %28, label %17
17: ; preds = %14, %17
%18 = phi i8* [ %26, %17 ], [ %15, %14 ]
%19 = phi i64 [ %24, %17 ], [ 0, %14 ]
%20 = icmp ult i64 %19, %6
%21 = zext i1 %20 to i32
%22 = tail call i32 @assert(i32 %21)
%23 = tail call i8* @strdup(i8* %18)
%24 = add i64 %19, 1
%25 = getelementptr inbounds i8*, i8** %9, i64 %19
store i8* %23, i8** %25
%26 = call i8* @strtok(i8* null, i8* %4)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %17
28: ; preds = %17, %14
%29 = phi i64 [ 0, %14 ], [ %24, %17 ]
%30 = add i64 %6, -1
%31 = icmp eq i64 %29, %30
%32 = zext i1 %31 to i32
%33 = tail call i32 @assert(i32 %32)
%34 = getelementptr inbounds i8*, i8** %9, i64 %29
store i8* null, i8** %34
br label %35
35: ; preds = %28, %11
ret i8** %9
}