#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 count_words(char *, char);
char *strtok(char *, char *);
char **xmalloc(int);
char **split(char *str, char delim) {
  char **parts, *p;
  int word_count, i = 0;
  char delim_str[2];
  delim_str[0] = delim;
  delim_str[1] = '\0';
  word_count = count_words(str, delim);
  parts = xmalloc((word_count + 1) * sizeof(*parts));
  p = strtok(str, delim_str);
  while (p != NULL) {
    parts[i++] = p;
    p = strtok(NULL, delim_str);
  }
  parts[i] = NULL;
  return parts;
}


x86 assembly (clang, -O0):
<split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x2a(%rbp)
movb   $0x0,-0x29(%rbp)
mov    -0x8(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <count_words@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x2a(%rbp),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c6 <split+0x96>
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
lea    -0x2a(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    118b <split+0x5b>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    %sil,0xe(%rsp)
movb   $0x0,0xf(%rsp)
call   1030 <count_words@plt>
lea    0x8(,%rax,8),%edi
call   1040 <xmalloc@plt>
mov    %rax,%r15
lea    0xe(%rsp),%rsi
mov    %r14,%rdi
call   1050 <strtok@plt>
test   %rax,%rax
je     119b <split+0x6b>
xor    %ebx,%ebx
lea    0xe(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
jne    1180 <split+0x50>
mov    %ebx,%eax
jmp    119d <split+0x6d>
xor    %eax,%eax
movq   $0x0,(%r15,%rax,8)
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8** @split(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i8]
store i8* %0, i8** %3
store i8 %1, i8* %4
store i32 0, i32* %8
%10 = load i8, i8* %4
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
store i8 %10, i8* %11
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 1
store i8 0, i8* %12
%13 = load i8*, i8** %3
%14 = load i8, i8* %4
%15 = call i32 @count_words(i8* %13, i8 signext %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 8
%20 = trunc i64 %19 to i32
%21 = call i8** @xmalloc(i32 %20)
store i8** %21, i8*** %5
%22 = load i8*, i8** %3
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%24 = call i8* @strtok(i8* %22, i8* %23)
store i8* %24, i8** %6
br label %25
25: ; preds = %28, %2
%26 = load i8*, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load i8*, i8** %6
%30 = load i8**, i8*** %5
%31 = load i32, i32* %8
%32 = add nsw i32 %31, 1
store i32 %32, i32* %8
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8*, i8** %30, i64 %33
store i8* %29, i8** %34
%35 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%36 = call i8* @strtok(i8* null, i8* %35)
store i8* %36, i8** %6
br label %25
37: ; preds = %25
%38 = load i8**, i8*** %5
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
store i8* null, i8** %41
%42 = load i8**, i8*** %5
ret i8** %42
}

LLVM IR (-O2):
define i8** @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 i32 @count_words(i8* %0, i8 signext %1)
%7 = shl i32 %6, 3
%8 = add i32 %7, 8
%9 = tail call i8** @xmalloc(i32 %8)
%10 = call i8* @strtok(i8* %0, i8* %4)
%11 = icmp eq i8* %10, null
br i1 %11, label %21, label %12
12: ; preds = %2, %12
%13 = phi i64 [ %15, %12 ], [ 0, %2 ]
%14 = phi i8* [ %17, %12 ], [ %10, %2 ]
%15 = add nuw i64 %13, 1
%16 = getelementptr inbounds i8*, i8** %9, i64 %13
store i8* %14, i8** %16
%17 = call i8* @strtok(i8* null, i8* %4)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %12
19: ; preds = %12
%20 = and i64 %15, 4294967295
br label %21
21: ; preds = %19, %2
%22 = phi i64 [ 0, %2 ], [ %20, %19 ]
%23 = getelementptr inbounds i8*, i8** %9, i64 %22
store i8* null, i8** %23
ret i8** %9
}