#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(char *buf, char *tokens[], char *delimiters) {
  char *token;
  int ntokens = 0;
  token = strtok(buf, delimiters);
  while (token != NULL) {
    tokens[ntokens] = token;
    ntokens++;
    token = strtok(NULL, delimiters);
  }
  return ntokens;
}


x86 assembly (clang, -O0):
<split>:
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   1030 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1176 <split+0x66>
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1030 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    113c <split+0x2c>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdx,%rsi
call   1030 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1147 <split+0x37>
nopw   0x0(%rax,%rax,1)
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r15,%rsi
call   1030 <strtok@plt>
test   %rax,%rax
jne    1130 <split+0x20>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @split(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
br label %12
12: ; preds = %15, %3
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %25
15: ; preds = %12
%16 = load i8*, i8** %7
%17 = load i8**, i8*** %5
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8*, i8** %17, i64 %19
store i8* %16, i8** %20
%21 = load i32, i32* %8
%22 = add nsw i32 %21, 1
store i32 %22, i32* %8
%23 = load i8*, i8** %6
%24 = call i8* @strtok(i8* null, i8* %23)
store i8* %24, i8** %7
br label %12
25: ; preds = %12
%26 = load i32, i32* %8
ret i32 %26
}

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