#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);
char **malloc(int);
size_t split_count(char *, char const);
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) {
    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    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0xb(%rbp)
movb   $0x0,-0xa(%rbp)
mov    -0x8(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1060 <split_count@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1239 <str_split+0xe9>
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0xb(%rbp),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     120d <str_split+0xbd>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x30(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x28(%rbp)
mov    %rdx,(%rax,%rcx,8)
lea    -0xb(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok@plt>
mov    %rax,-0x30(%rbp)
jmp    11b4 <str_split+0x64>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
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
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %sil,0xe(%rsp)
movb   $0x0,0xf(%rsp)
call   1060 <split_count@plt>
mov    %rax,%r12
lea    0x0(,%rax,8),%edi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11fe <str_split+0xae>
lea    0xe(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <strtok@plt>
test   %rax,%rax
je     11e2 <str_split+0x92>
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   1070 <strdup@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r15,%rsi
call   1050 <strtok@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11b0 <str_split+0x60>
jmp    11e4 <str_split+0x94>
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):
define i8** @str_split(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca [2 x i8]
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %3
store i8 %1, i8* %4
%10 = load i8, i8* %4
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
store i8 %10, i8* %11
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 0, i8* %12
%13 = load i8*, i8** %3
%14 = load i8, i8* %4
%15 = call i64 @split_count(i8* %13, i8 signext %14)
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = mul i64 8, %16
%18 = trunc i64 %17 to i32
%19 = call i8** @malloc(i32 %18)
store i8** %19, i8*** %7
%20 = load i8**, i8*** %7
%21 = icmp ne i8** %20, null
br i1 %21, label %22, label %53
22: ; preds = %2
store i64 0, i64* %8
%23 = load i8*, i8** %3
%24 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%25 = call i8* @strtok(i8* %23, i8* %24)
store i8* %25, i8** %9
br label %26
26: ; preds = %29, %22
%27 = load i8*, i8** %9
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %43
29: ; preds = %26
%30 = load i64, i64* %8
%31 = load i64, i64* %6
%32 = icmp ult i64 %30, %31
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i8*, i8** %9
%36 = call i8* @strdup(i8* %35)
%37 = load i8**, i8*** %7
%38 = load i64, i64* %8
%39 = add i64 %38, 1
store i64 %39, i64* %8
%40 = getelementptr inbounds i8*, i8** %37, i64 %38
store i8* %36, i8** %40
%41 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%42 = call i8* @strtok(i8* null, i8* %41)
store i8* %42, i8** %9
br label %26
43: ; preds = %26
%44 = load i64, i64* %8
%45 = load i64, i64* %6
%46 = sub i64 %45, 1
%47 = icmp eq i64 %44, %46
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = load i8**, i8*** %7
%51 = load i64, i64* %8
%52 = getelementptr inbounds i8*, i8** %50, i64 %51
store i8* null, i8** %52
br label %53
53: ; preds = %43, %2
%54 = load i8**, i8*** %7
ret i8** %54
}

LLVM IR (-O2):
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 %32, label %11
11: ; preds = %2
%12 = call i8* @strtok(i8* %0, i8* %4)
%13 = icmp eq i8* %12, null
br i1 %13, label %25, label %14
14: ; preds = %11, %14
%15 = phi i8* [ %23, %14 ], [ %12, %11 ]
%16 = phi i64 [ %21, %14 ], [ 0, %11 ]
%17 = icmp ult i64 %16, %6
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i8* @strdup(i8* %15)
%21 = add i64 %16, 1
%22 = getelementptr inbounds i8*, i8** %9, i64 %16
store i8* %20, i8** %22
%23 = call i8* @strtok(i8* null, i8* %4)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %14
25: ; preds = %14, %11
%26 = phi i64 [ 0, %11 ], [ %21, %14 ]
%27 = add i64 %6, -1
%28 = icmp eq i64 %26, %27
%29 = zext i1 %28 to i32
%30 = tail call i32 @assert(i32 %29)
%31 = getelementptr inbounds i8*, i8** %9, i64 %26
store i8* null, i8** %31
br label %32
32: ; preds = %25, %2
ret i8** %9
}