#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 */
scalar_t__ realloc(char **, int);
char *strtok_r(char *, char *, char **);
int tokenize_cmd(char *cmd, char ***argv, int *argc) {
  char *saveptr = NULL;
  for (*argc = 0, *argv = NULL;; (*argc)++, cmd = NULL) {
    int will_break = 0;
    char *token = strtok_r(cmd, " ", &saveptr);
    if (token == NULL)
      will_break = 1;
    *argv = (char **)realloc(*argv, ((*argc) + 1) * sizeof(char **));
    (*argv)[*argc] = token;
    if (will_break)
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tokenize_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe9d(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1182 <tokenize_cmd+0x62>
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
mov    %rdx,(%rax,%rcx,8)
cmpl   $0x0,-0x24(%rbp)
je     11ce <tokenize_cmd+0xae>
jmp    11eb <tokenize_cmd+0xcb>
jmp    11d3 <tokenize_cmd+0xb3>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1151 <tokenize_cmd+0x31>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tokenize_cmd>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r13
mov    %rsi,%r14
movq   $0x0,0x8(%rsp)
movl   $0x0,(%rdx)
movq   $0x0,(%rsi)
lea    0xeb0(%rip),%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <strtok_r@plt>
mov    %rax,%rbx
mov    (%r14),%rdi
mov    0x0(%r13),%eax
lea    0x8(,%rax,8),%esi
call   1040 <realloc@plt>
mov    %rax,(%r14)
movslq 0x0(%r13),%rcx
mov    %rbx,(%rax,%rcx,8)
test   %rbx,%rbx
je     119c <tokenize_cmd+0x7c>
add    $0x1,%ecx
mov    %ecx,0x0(%r13)
xor    %edi,%edi
jmp    1160 <tokenize_cmd+0x40>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i32 @tokenize_cmd(i8* %0, i8*** nocapture %1, i32* nocapture %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i8* null, i8** %4
store i32 0, i32* %2
store i8** null, i8*** %1
br label %6
6: ; preds = %19, %3
%7 = phi i8* [ %0, %3 ], [ null, %19 ]
%8 = call i8* @strtok_r(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%9 = icmp eq i8* %8, null
%10 = load i8**, i8*** %1
%11 = load i32, i32* %2
%12 = shl i32 %11, 3
%13 = add i32 %12, 8
%14 = call i64 @realloc(i8** %10, i32 %13)
%15 = inttoptr i64 %14 to i8**
store i8** %15, i8*** %1
%16 = load i32, i32* %2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8*, i8** %15, i64 %17
store i8* %8, i8** %18
br i1 %9, label %21, label %19
19: ; preds = %6
%20 = add nsw i32 %16, 1
store i32 %20, i32* %2
br label %6
21: ; preds = %6
ret i32 0
}