#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*/ free(char *);
char *strdup(char const *);
char *strtok(char *, char const *);
int strsplit(const char *str, char *parts[], const char *delimiter) {
  char *pch;
  int i = 0;
  char *tmp = strdup(str);
  pch = strtok(tmp, delimiter);
  parts[i++] = strdup(pch);
  while (pch) {
    pch = strtok(NULL, delimiter);
    if (NULL == pch)
      break;
    parts[i++] = strdup(pch);
  }
  free(tmp);
  free(pch);
  return i;
}


x86 assembly (clang, -O0):
<strsplit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strtok@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
cmpq   $0x0,-0x20(%rbp)
je     11df <strsplit+0xaf>
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <strtok@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    11b8 <strsplit+0x88>
jmp    11df <strsplit+0xaf>
mov    -0x20(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    118b <strsplit+0x5b>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strsplit>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r15
call   1050 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <strtok@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <strdup@plt>
mov    %rax,(%r15)
mov    $0x1,%ebx
test   %r13,%r13
je     119f <strsplit+0x6f>
xor    %edi,%edi
mov    %r12,%rsi
call   1040 <strtok@plt>
test   %rax,%rax
je     119f <strsplit+0x6f>
mov    $0x1,%ebx
xchg   %ax,%ax
mov    %rax,%rdi
call   1050 <strdup@plt>
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r12,%rsi
call   1040 <strtok@plt>
test   %rax,%rax
jne    1180 <strsplit+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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