#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 */
char *malloc(int);
char **realloc(char **, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
int _explode(char *delim, char *str, char ***r) {
  char **res = (char **)malloc(sizeof(char *) * strlen(str));
  char *p;
  int i = 0;
  while (p = strtok(str, delim)) {
    res[i] = malloc(strlen(p) + 1);
    strcpy(res[i], p);
    ++i;
    str = NULL;
  }
  res = realloc(res, sizeof(char *) * i);
  *r = res;
  return i;
}


x86 assembly (clang, -O0):
<_explode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11ed <_explode+0x9d>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movq   $0x0,-0x10(%rbp)
jmp    1185 <_explode+0x35>
mov    -0x20(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_explode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <strlen@plt>
lea    0x0(,%rax,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
je     11c7 <_explode+0x77>
mov    %rax,%rbx
xor    %r13d,%r13d
nop
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1050 <malloc@plt>
mov    %rax,(%r15,%r13,8)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
add    $0x1,%r13
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <_explode+0x40>
jmp    11ca <_explode+0x7a>
xor    %r13d,%r13d
lea    0x0(,%r13,8),%esi
mov    %r15,%rdi
call   1060 <realloc@plt>
mov    %rax,(%r14)
mov    %r13d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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