#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 ntokenize(int maxtoks, char *string, char **toks, char *sep) {
  char *str = string;
  int j;
  char *token;
  char *saveptr;
  int num_tokens = 0;
  for (j = 0; num_tokens < maxtoks; j++) {
    token = strtok_r(str, sep, &saveptr);
    if (token == NULL) {
      break;
    } else {
      toks[j] = token;
      num_tokens++;
    }
    str = NULL;
  }
  return num_tokens;
}


x86 assembly (clang, -O0):
<ntokenize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    119d <ntokenize+0x8d>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1030 <strtok_r@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    116e <ntokenize+0x5e>
jmp    119d <ntokenize+0x8d>
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    113d <ntokenize+0x2d>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ntokenize>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
jle    116a <ntokenize+0x5a>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %edi,%r13d
xor    %ebx,%ebx
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rsi,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <strtok_r@plt>
test   %rax,%rax
je     116c <ntokenize+0x5c>
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
mov    $0x0,%esi
cmp    %rbx,%r13
jne    1140 <ntokenize+0x30>
mov    %r13d,%ebx
jmp    116c <ntokenize+0x5c>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @ntokenize(i32 %0, i8* %1, i8** %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i8* %3, i8** %8
%14 = load i8*, i8** %6
store i8* %14, i8** %9
store i32 0, i32* %13
store i32 0, i32* %10
br label %15
15: ; preds = %35, %4
%16 = load i32, i32* %13
%17 = load i32, i32* %5
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %38
19: ; preds = %15
%20 = load i8*, i8** %9
%21 = load i8*, i8** %8
%22 = call i8* @strtok_r(i8* %20, i8* %21, i8** %12)
store i8* %22, i8** %11
%23 = load i8*, i8** %11
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %19
br label %38
26: ; preds = %19
%27 = load i8*, i8** %11
%28 = load i8**, i8*** %7
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
store i8* %27, i8** %31
%32 = load i32, i32* %13
%33 = add nsw i32 %32, 1
store i32 %33, i32* %13
br label %34
34: ; preds = %26
store i8* null, i8** %9
br label %35
35: ; preds = %34
%36 = load i32, i32* %10
%37 = add nsw i32 %36, 1
store i32 %37, i32* %10
br label %15
38: ; preds = %25, %15
%39 = load i32, i32* %13
ret i32 %39
}

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