#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 *strtok_r(char *, char *, char **);
int strsplit(char *str, char *parts[], int nparts) {
  char *tok;
  char *save = NULL;
  int i;
  for (i = 0; i < nparts; i++, str = NULL) {
    tok = strtok_r(str, " ", &save);
    if (tok == NULL)
      break;
    parts[i] = tok;
  }
  if (i == nparts)
    return -1;
  return i;
}


x86 assembly (clang, -O0):
<strsplit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    118c <strsplit+0x7c>
mov    -0x10(%rbp),%rdi
lea    0xeb7(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1030 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1166 <strsplit+0x56>
jmp    118c <strsplit+0x7c>
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
movq   $0x0,-0x10(%rbp)
jmp    1132 <strsplit+0x22>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    11a4 <strsplit+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <strsplit+0x9a>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strsplit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
movq   $0x0,(%rsp)
test   %edx,%edx
jle    1167 <strsplit+0x57>
mov    %rsi,%r13
mov    %r14d,%ebx
xor    %ebp,%ebp
lea    0xec7(%rip),%r15
mov    %rsp,%r12
nopl   0x0(%rax)
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <strtok_r@plt>
test   %rax,%rax
je     1169 <strsplit+0x59>
mov    %rax,0x0(%r13,%rbp,8)
add    $0x1,%rbp
mov    $0x0,%edi
cmp    %rbp,%rbx
jne    1140 <strsplit+0x30>
mov    %ebx,%ebp
jmp    1169 <strsplit+0x59>
xor    %ebp,%ebp
cmp    %r14d,%ebp
mov    $0xffffffff,%eax
cmovne %ebp,%eax
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 i32 @strsplit(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i8* null, i8** %9
store i32 0, i32* %10
br label %11
11: ; preds = %27, %3
%12 = load i32, i32* %10
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %30
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = call i8* @strtok_r(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %9)
store i8* %17, i8** %8
%18 = load i8*, i8** %8
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %15
br label %30
21: ; preds = %15
%22 = load i8*, i8** %8
%23 = load i8**, i8*** %6
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %23, i64 %25
store i8* %22, i8** %26
br label %27
27: ; preds = %21
%28 = load i32, i32* %10
%29 = add nsw i32 %28, 1
store i32 %29, i32* %10
store i8* null, i8** %5
br label %11
30: ; preds = %20, %11
%31 = load i32, i32* %10
%32 = load i32, i32* %7
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %4
br label %37
35: ; preds = %30
%36 = load i32, i32* %10
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %34
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i32 @strsplit(i8* %0, i8** nocapture writeonly %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %20
7: ; preds = %3
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %14
%10 = phi i64 [ 0, %7 ], [ %16, %14 ]
%11 = phi i8* [ %0, %7 ], [ null, %14 ]
%12 = call i8* @strtok_r(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%13 = icmp eq i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8*, i8** %1, i64 %10
store i8* %12, i8** %15
%16 = add nuw nsw i64 %10, 1
%17 = icmp eq i64 %16, %8
br i1 %17, label %20, label %9
18: ; preds = %9
%19 = trunc i64 %10 to i32
br label %20
20: ; preds = %14, %18, %3
%21 = phi i32 [ 0, %3 ], [ %19, %18 ], [ %2, %14 ]
%22 = icmp eq i32 %21, %2
%23 = select i1 %22, i32 -1, i32 %21
ret i32 %23
}