#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 */
typedef int uint8_t;
typedef int uint32_t;

/* Variables and functions */
int count_entries(char *, char);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int strcspn(char *, char *);
int strlen(char *);
char **split(char *str, char c) {
  char **result, **temp, *new_string;
  uint8_t entries = count_entries(str, c);
  uint8_t i;
  uint32_t substr, length;
  length = strlen(str);
  new_string = (char *)malloc(sizeof(char) * length);
  memcpy(new_string, str, length);
  result = (char **)malloc(sizeof(char *) * entries);
  temp = result;
  for (i = 0; i < entries; ++i) {
    substr = strcspn(new_string, ";");
    *(temp + i) = (char *)malloc(sizeof(char) * substr);
    memcpy(*(temp + i), new_string, substr);
    *(*(temp + i) + substr + 1) = '\0';
    memmove(new_string, new_string + (substr + 1), (length - substr));
  }
  free(new_string);
  return result;
}


x86 assembly (clang, -O0):
<split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1040 <count_entries@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x38(%rbp)
movslq -0x38(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1070 <memcpy@plt>
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1279 <split+0x109>
mov    -0x28(%rbp),%rdi
lea    0xe06(%rip),%rsi
call   1060 <strcspn@plt>
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rsi
mov    -0x38(%rbp),%edx
sub    -0x34(%rbp),%edx
call   1090 <memmove@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11e3 <split+0x73>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
call   1040 <count_entries@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1070 <memcpy@plt>
lea    0x0(,%r15,8),%edi
call   1080 <malloc@plt>
mov    %rax,%r12
test   %r15d,%r15d
jle    1221 <split+0xb1>
mov    %r15d,%r13d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
lea    0xe26(%rip),%rsi
call   1060 <strcspn@plt>
movslq %eax,%rbp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,(%r12,%r15,8)
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
mov    (%r12,%r15,8),%rax
movb   $0x0,0x1(%rax,%rbp,1)
lea    (%rbx,%rbp,1),%rsi
add    $0x1,%rsi
mov    %r14d,%edx
sub    %ebp,%edx
mov    %rbx,%rdi
call   1090 <memmove@plt>
add    $0x1,%r15
cmp    %r15,%r13
jne    11d0 <split+0x60>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c";\00"
define i8** @split(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i8 %1, i8* %4
%12 = load i8*, i8** %3
%13 = load i8, i8* %4
%14 = call i32 @count_entries(i8* %12, i8 signext %13)
store i32 %14, i32* %8
%15 = load i8*, i8** %3
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %11
%17 = load i32, i32* %11
%18 = sext i32 %17 to i64
%19 = mul i64 1, %18
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = load i8*, i8** %3
%25 = load i32, i32* %11
%26 = call i32 @memcpy(i8* %23, i8* %24, i32 %25)
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = mul i64 8, %28
%30 = trunc i64 %29 to i32
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i8**
store i8** %32, i8*** %5
%33 = load i8**, i8*** %5
store i8** %33, i8*** %6
store i32 0, i32* %9
br label %34
34: ; preds = %78, %2
%35 = load i32, i32* %9
%36 = load i32, i32* %8
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %81
38: ; preds = %34
%39 = load i8*, i8** %7
%40 = call i32 @strcspn(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = mul i64 1, %42
%44 = trunc i64 %43 to i32
%45 = call i64 @malloc(i32 %44)
%46 = inttoptr i64 %45 to i8*
%47 = load i8**, i8*** %6
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8*, i8** %47, i64 %49
store i8* %46, i8** %50
%51 = load i8**, i8*** %6
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8*, i8** %51, i64 %53
%55 = load i8*, i8** %54
%56 = load i8*, i8** %7
%57 = load i32, i32* %10
%58 = call i32 @memcpy(i8* %55, i8* %56, i32 %57)
%59 = load i8**, i8*** %6
%60 = load i32, i32* %9
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8*, i8** %59, i64 %61
%63 = load i8*, i8** %62
%64 = load i32, i32* %10
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = getelementptr inbounds i8, i8* %66, i64 1
store i8 0, i8* %67
%68 = load i8*, i8** %7
%69 = load i8*, i8** %7
%70 = load i32, i32* %10
%71 = add nsw i32 %70, 1
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %69, i64 %72
%74 = load i32, i32* %11
%75 = load i32, i32* %10
%76 = sub nsw i32 %74, %75
%77 = call i32 @memmove(i8* %68, i8* %73, i32 %76)
br label %78
78: ; preds = %38
%79 = load i32, i32* %9
%80 = add nsw i32 %79, 1
store i32 %80, i32* %9
br label %34
81: ; preds = %34
%82 = load i8*, i8** %7
%83 = call i32 @free(i8* %82)
%84 = load i8**, i8*** %5
ret i8** %84
}

LLVM IR (-O2):
@.str = [2 x i8] c";\00"
define i8** @split(i8* %0, i8 signext %1) {
%3 = tail call i32 @count_entries(i8* %0, i8 signext %1)
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @memcpy(i8* %6, i8* %0, i32 %4)
%8 = shl i32 %3, 3
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8**
%11 = icmp sgt i32 %3, 0
br i1 %11, label %12, label %32
12: ; preds = %2
%13 = zext i32 %3 to i64
br label %14
14: ; preds = %12, %14
%15 = phi i64 [ 0, %12 ], [ %30, %14 ]
%16 = tail call i32 @strcspn(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = sext i32 %16 to i64
%18 = tail call i64 @malloc(i32 %16)
%19 = inttoptr i64 %18 to i8*
%20 = getelementptr inbounds i8*, i8** %10, i64 %15
store i8* %19, i8** %20
%21 = tail call i32 @memcpy(i8* %19, i8* %6, i32 %16)
%22 = load i8*, i8** %20
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = getelementptr inbounds i8, i8* %23, i64 %17
store i8 0, i8* %24
%25 = add nsw i32 %16, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %6, i64 %26
%28 = sub nsw i32 %4, %16
%29 = tail call i32 @memmove(i8* %6, i8* %27, i32 %28)
%30 = add nuw nsw i64 %15, 1
%31 = icmp eq i64 %30, %13
br i1 %31, label %32, label %14
32: ; preds = %14, %2
%33 = tail call i32 @free(i8* %6)
ret i8** %10
}