#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 *);
char *strtok(char *, char *);
int strsplit(char *str, char *parts[], char *delimiter) {
  char *pch;
  int i = 0;
  pch = strtok(str, delimiter);
  parts[i++] = strdup(pch);
  while (pch) {
    pch = strtok(NULL, delimiter);
    if (NULL == pch)
      break;
    parts[i++] = strdup(pch);
  }
  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
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     11d2 <strsplit+0xa2>
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    11ab <strsplit+0x7b>
jmp    11d2 <strsplit+0xa2>
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    117e <strsplit+0x4e>
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   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdx,%rsi
call   1040 <strtok@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <strdup@plt>
mov    %rax,(%r14)
mov    $0x1,%ebx
test   %r12,%r12
je     119f <strsplit+0x6f>
xor    %edi,%edi
mov    %r15,%rsi
call   1040 <strtok@plt>
test   %rax,%rax
je     119f <strsplit+0x6f>
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rdi
call   1050 <strdup@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r15,%rsi
call   1040 <strtok@plt>
test   %rax,%rax
jne    1180 <strsplit+0x50>
xor    %edi,%edi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
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
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8* %2, i8** %6
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = load i8*, i8** %6
%11 = call i8* @strtok(i8* %9, i8* %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = call i8* @strdup(i8* %12)
%14 = load i8**, i8*** %5
%15 = load i32, i32* %8
%16 = add nsw i32 %15, 1
store i32 %16, i32* %8
%17 = sext i32 %15 to i64
%18 = getelementptr inbounds i8*, i8** %14, i64 %17
store i8* %13, i8** %18
br label %19
19: ; preds = %28, %3
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = load i8*, i8** %6
%24 = call i8* @strtok(i8* null, i8* %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = icmp eq i8* null, %25
br i1 %26, label %27, label %28
27: ; preds = %22
br label %36
28: ; preds = %22
%29 = load i8*, i8** %7
%30 = call i8* @strdup(i8* %29)
%31 = load i8**, i8*** %5
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8*, i8** %31, i64 %34
store i8* %30, i8** %35
br label %19
36: ; preds = %27, %19
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
%39 = load i32, i32* %8
ret i32 %39
}

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