#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 /*<<< orphan*/ free(char *);
char **malloc(int);
char **realloc(char **, int);
char *strdup(char const *);
char *strtok(char *, char *);
char **tokenize(const char *input) {
  char *str = strdup(input);
  int count = 0;
  int capacity = 10;
  char **result = malloc(capacity * sizeof(*result));
  char *tok = strtok(str, " ");
  while (1) {
    if (count >= capacity)
      result = realloc(result, (capacity *= 2) * sizeof(*result));
    result[count++] = tok ? strdup(tok) : tok;
    if (!tok)
      break;
    tok = strtok(NULL, " ");
  }
  free(str);
  return result;
}


x86 assembly (clang, -O0):
<tokenize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xa,-0x18(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     11c8 <tokenize+0x78>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e5 <tokenize+0x95>
mov    -0x28(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x30(%rbp)
jmp    11ed <tokenize+0x9d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
cmpq   $0x0,-0x28(%rbp)
jne    1217 <tokenize+0xc7>
jmp    1230 <tokenize+0xe0>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdde(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    119e <tokenize+0x4e>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tokenize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    $0x50,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
mov    $0xa,%r13d
xor    %ebp,%ebp
lea    0xe81(%rip),%r15
mov    %rbx,(%rsp)
mov    %rbx,%rdi
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rsi
call   1060 <strtok@plt>
mov    %rax,%rbx
movslq %r13d,%rax
cmp    %rax,%rbp
jl     11bd <tokenize+0x6d>
lea    0x0(%r13,%r13,1),%r14d
shl    $0x4,%r13d
mov    %r12,%rdi
mov    %r13d,%esi
call   1050 <realloc@plt>
mov    %rax,%r12
mov    %r14d,%r13d
test   %rbx,%rbx
je     11d6 <tokenize+0x86>
mov    %rbx,%rdi
call   1070 <strdup@plt>
mov    %rax,(%r12,%rbp,8)
add    $0x1,%rbp
xor    %edi,%edi
jmp    1190 <tokenize+0x40>
mov    %ebp,%eax
movq   $0x0,(%r12,%rax,8)
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r12,%rax
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 i8** @tokenize(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i8* @strdup(i8* %8)
store i8* %9, i8** %3
store i32 0, i32* %4
store i32 10, i32* %5
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 8
%13 = trunc i64 %12 to i32
%14 = call i8** @malloc(i32 %13)
store i8** %14, i8*** %6
%15 = load i8*, i8** %3
%16 = call i8* @strtok(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %7
br label %17
17: ; preds = %1, %47
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = icmp sge i32 %18, %19
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = load i8**, i8*** %6
%23 = load i32, i32* %5
%24 = mul nsw i32 %23, 2
store i32 %24, i32* %5
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 8
%27 = trunc i64 %26 to i32
%28 = call i8** @realloc(i8** %22, i32 %27)
store i8** %28, i8*** %6
br label %29
29: ; preds = %21, %17
%30 = load i8*, i8** %7
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %7
%34 = call i8* @strdup(i8* %33)
br label %37
35: ; preds = %29
%36 = load i8*, i8** %7
br label %37
37: ; preds = %35, %32
%38 = phi i8* [ %34, %32 ], [ %36, %35 ]
%39 = load i8**, i8*** %6
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8*, i8** %39, i64 %42
store i8* %38, i8** %43
%44 = load i8*, i8** %7
%45 = icmp ne i8* %44, null
br i1 %45, label %47, label %46
46: ; preds = %37
br label %49
47: ; preds = %37
%48 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %48, i8** %7
br label %17
49: ; preds = %46
%50 = load i8*, i8** %3
%51 = call i32 @free(i8* %50)
%52 = load i8**, i8*** %6
ret i8** %52
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i8** @tokenize(i8* nocapture %0) {
%2 = tail call i8* @strdup(i8* %0)
%3 = tail call i8** @malloc(i32 80)
br label %4
4: ; preds = %20, %1
%5 = phi i8* [ null, %20 ], [ %2, %1 ]
%6 = phi i64 [ %23, %20 ], [ 0, %1 ]
%7 = phi i32 [ %17, %20 ], [ 10, %1 ]
%8 = phi i8** [ %18, %20 ], [ %3, %1 ]
%9 = tail call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = sext i32 %7 to i64
%11 = icmp slt i64 %6, %10
br i1 %11, label %16, label %12
12: ; preds = %4
%13 = shl nsw i32 %7, 1
%14 = shl i32 %7, 4
%15 = tail call i8** @realloc(i8** %8, i32 %14)
br label %16
16: ; preds = %12, %4
%17 = phi i32 [ %13, %12 ], [ %7, %4 ]
%18 = phi i8** [ %15, %12 ], [ %8, %4 ]
%19 = icmp eq i8* %9, null
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = tail call i8* @strdup(i8* %9)
%22 = getelementptr inbounds i8*, i8** %18, i64 %6
store i8* %21, i8** %22
%23 = add nuw i64 %6, 1
br label %4
24: ; preds = %16
%25 = and i64 %6, 4294967295
%26 = getelementptr inbounds i8*, i8** %18, i64 %25
store i8* null, i8** %26
%27 = tail call i32 @free(i8* %2)
ret i8** %18
}