#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(char *, char *);
int split_str(char *str, char *tokens[]) {
  int num_tokens = 0;
  char *token;
  token = strtok(str, " ");
  if (token != NULL) {
    tokens[num_tokens++] = token;
  }
  while (token != NULL) {
    token = strtok(NULL, " ");
    if (token != NULL) {
      tokens[num_tokens++] = token;
    }
  }
  return num_tokens;
}


x86 assembly (clang, -O0):
<split_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xece(%rip),%rsi
call   1030 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1160 <split_str+0x50>
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1165 <split_str+0x55>
cmpq   $0x0,-0x20(%rbp)
je     11ae <split_str+0x9e>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe85(%rip),%rsi
call   1030 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a9 <split_str+0x99>
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1165 <split_str+0x55>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_str>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xee1(%rip),%rsi
call   1030 <strtok@plt>
test   %rax,%rax
je     1169 <split_str+0x59>
mov    %rax,(%r14)
lea    0xecd(%rip),%rsi
xor    %edi,%edi
call   1030 <strtok@plt>
test   %rax,%rax
je     116d <split_str+0x5d>
mov    $0x1,%ebx
lea    0xeb5(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rax,(%r14,%rbx,8)
xor    %edi,%edi
mov    %r15,%rsi
call   1030 <strtok@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1150 <split_str+0x40>
jmp    1172 <split_str+0x62>
xor    %ebx,%ebx
jmp    1172 <split_str+0x62>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
define i32 @split_str(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i32 0, i32* %5
%7 = load i8*, i8** %3
%8 = call i8* @strtok(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i8*, i8** %6
%13 = load i8**, i8*** %4
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
store i32 %15, i32* %5
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds i8*, i8** %13, i64 %16
store i8* %12, i8** %17
br label %18
18: ; preds = %11, %2
br label %19
19: ; preds = %33, %18
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %34
22: ; preds = %19
%23 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = load i8**, i8*** %4
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
store i8* %27, i8** %32
br label %33
33: ; preds = %26, %22
br label %19
34: ; preds = %19
%35 = load i32, i32* %5
ret i32 %35
}

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