#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 string_to_tokens(char *arg_string, char *arg_array[], int array_size,
                     char *separator) {
  int num_toks = 0;
  char *strtok();
  if (arg_array == NULL || array_size <= 1 || separator == NULL)
    return -1;
  if ((arg_array[num_toks] = strtok(arg_string, separator)) == NULL) {
    return 0;
  }
  for (num_toks = 1; num_toks < array_size; num_toks++) {
    if ((arg_array[num_toks] = strtok(NULL, separator)) == NULL)
      break;
  }
  if (num_toks == array_size)
    arg_array[num_toks] = NULL;
  return (num_toks);
}


x86 assembly (clang, -O0):
<string_to_tokens>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     114e <string_to_tokens+0x3e>
cmpl   $0x1,-0x1c(%rbp)
jle    114e <string_to_tokens+0x3e>
cmpq   $0x0,-0x28(%rbp)
jne    115a <string_to_tokens+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <string_to_tokens+0xe9>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strtok@plt>
mov    -0x18(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
mov    %rax,(%rcx,%rdx,8)
cmp    $0x0,%rax
jne    1189 <string_to_tokens+0x79>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <string_to_tokens+0xe9>
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d7 <string_to_tokens+0xc7>
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1030 <strtok@plt>
mov    -0x18(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
mov    %rax,(%rcx,%rdx,8)
cmp    $0x0,%rax
jne    11c4 <string_to_tokens+0xb4>
jmp    11d7 <string_to_tokens+0xc7>
jmp    11c9 <string_to_tokens+0xb9>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1190 <string_to_tokens+0x80>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    11f3 <string_to_tokens+0xe3>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_to_tokens>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rsi,%rsi
je     1185 <string_to_tokens+0x75>
mov    %edx,%ebp
cmp    $0x2,%edx
jl     1185 <string_to_tokens+0x75>
mov    %rcx,%rbx
test   %rcx,%rcx
je     1185 <string_to_tokens+0x75>
mov    %rsi,%r15
mov    %rbx,%rsi
call   1030 <strtok@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     116e <string_to_tokens+0x5e>
mov    %ebp,%r14d
mov    $0x1,%ebp
nopl   0x0(%rax)
xor    %edi,%edi
mov    %rbx,%rsi
call   1030 <strtok@plt>
mov    %rax,(%r15,%rbp,8)
test   %rax,%rax
je     1173 <string_to_tokens+0x63>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1150 <string_to_tokens+0x40>
jmp    1178 <string_to_tokens+0x68>
xor    %r14d,%r14d
jmp    1185 <string_to_tokens+0x75>
cmp    %ebp,%r14d
jne    1182 <string_to_tokens+0x72>
movq   $0x0,(%r15,%r14,8)
jmp    1185 <string_to_tokens+0x75>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @string_to_tokens(i8* %0, i8** %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %10
%11 = load i8**, i8*** %7
%12 = icmp eq i8** %11, null
br i1 %12, label %19, label %13
13: ; preds = %4
%14 = load i32, i32* %8
%15 = icmp sle i32 %14, 1
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i8*, i8** %9
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %16, %13, %4
store i32 -1, i32* %5
br label %59
20: ; preds = %16
%21 = load i8*, i8** %6
%22 = load i8*, i8** %9
%23 = call i8* @strtok(i8* %21, i8* %22)
%24 = load i8**, i8*** %7
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
store i8* %23, i8** %27
%28 = icmp eq i8* %23, null
br i1 %28, label %29, label %30
29: ; preds = %20
store i32 0, i32* %5
br label %59
30: ; preds = %20
store i32 1, i32* %10
br label %31
31: ; preds = %45, %30
%32 = load i32, i32* %10
%33 = load i32, i32* %8
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %48
35: ; preds = %31
%36 = load i8*, i8** %9
%37 = call i8* @strtok(i8* null, i8* %36)
%38 = load i8**, i8*** %7
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
store i8* %37, i8** %41
%42 = icmp eq i8* %37, null
br i1 %42, label %43, label %44
43: ; preds = %35
br label %48
44: ; preds = %35
br label %45
45: ; preds = %44
%46 = load i32, i32* %10
%47 = add nsw i32 %46, 1
store i32 %47, i32* %10
br label %31
48: ; preds = %43, %31
%49 = load i32, i32* %10
%50 = load i32, i32* %8
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = load i8**, i8*** %7
%54 = load i32, i32* %10
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
store i8* null, i8** %56
br label %57
57: ; preds = %52, %48
%58 = load i32, i32* %10
store i32 %58, i32* %5
br label %59
59: ; preds = %57, %29, %19
%60 = load i32, i32* %5
ret i32 %60
}

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