#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 */
scalar_t__ malloc(int);
scalar_t__ realloc(char **, int);
char *strdup(char *);
char *strtok(char *, char *);
char **split_by_char(char *str, char *delimeter, int *num) {
  char **final = NULL;
  char *token;
  char *input = str;
  int n = 0;
  input = strdup(str);
  final = (char **)malloc(sizeof(char *));
  if (input != NULL) {
    token = strtok(input, delimeter);
    while (token != NULL) {
      final = (char **)realloc(final, (sizeof(char *) * (n + 1)));
      final[n] = token;
      n++;
      token = strtok(NULL, delimeter);
    }
  }
  if (num != NULL)
    *num = n;
  return final;
}


x86 assembly (clang, -O0):
<split_by_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    $0x8,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11fc <split_by_char+0xbc>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f7 <split_by_char+0xb7>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    11a2 <split_by_char+0x62>
jmp    11fc <split_by_char+0xbc>
cmpq   $0x0,-0x18(%rbp)
je     1210 <split_by_char+0xd0>
mov    -0x34(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_by_char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%r15
call   1060 <strdup@plt>
mov    %rax,%rbx
mov    $0x8,%edi
call   1030 <malloc@plt>
mov    %rax,%r12
xor    %r14d,%r14d
test   %rbx,%rbx
je     11c1 <split_by_char+0x81>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
je     11c1 <split_by_char+0x81>
mov    %rax,%rbx
mov    %rbp,(%rsp)
mov    $0x8,%ebp
xor    %r14d,%r14d
nopl   (%rax)
mov    %r14,%r13
add    $0x1,%r14
mov    %r12,%rdi
mov    %ebp,%esi
call   1040 <realloc@plt>
mov    %rax,%r12
mov    %rbx,(%rax,%r13,8)
xor    %edi,%edi
mov    %r15,%rsi
call   1050 <strtok@plt>
mov    %rax,%rbx
add    $0x8,%ebp
test   %rax,%rax
jne    1190 <split_by_char+0x50>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     11ca <split_by_char+0x8a>
mov    %r14d,0x0(%rbp)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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