#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 **calloc(int, int);
int count_tokens(char *, char);
char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
char *strtok(char *, char *);
char **build_tokenlist(char *string, int *num) {
  char **strarray = NULL;
  char *current = NULL;
  char *currcpy = NULL;
  int index = 0;
  int count = count_tokens(string, ' ');
  if (count) {
    strarray = calloc(count + 1, sizeof(char *));
    current = strtok(string, " ");
    while (current) {
      currcpy = malloc(strlen(current));
      strcpy(currcpy, current);
      strarray[index] = currcpy;
      index++;
      current = strtok(NULL, " ");
    }
    *num = count;
  }
  return strarray;
}


x86 assembly (clang, -O0):
<build_tokenlist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x20,%esi
call   1060 <count_tokens@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1239 <build_tokenlist+0xd9>
mov    -0x30(%rbp),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1230 <build_tokenlist+0xd0>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdde(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    11d2 <build_tokenlist+0x72>
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_tokenlist>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    $0x20,%esi
call   1060 <count_tokens@plt>
test   %eax,%eax
je     11ff <build_tokenlist+0x9f>
mov    %eax,%r12d
mov    %rbp,(%rsp)
mov    %eax,%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strtok@plt>
test   %rax,%rax
je     11f6 <build_tokenlist+0x96>
mov    %rax,%rbp
lea    0xe46(%rip),%r13
mov    %r14,%r15
nopl   (%rax)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %rbx,(%r15)
xor    %edi,%edi
mov    %r13,%rsi
call   1080 <strtok@plt>
mov    %rax,%rbp
add    $0x8,%r15
test   %rax,%rax
jne    11c0 <build_tokenlist+0x60>
mov    (%rsp),%rax
mov    %r12d,(%rax)
jmp    1202 <build_tokenlist+0xa2>
xor    %r14d,%r14d
mov    %r14,%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** @build_tokenlist(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i8** null, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i32 0, i32* %8
%10 = load i8*, i8** %3
%11 = call i32 @count_tokens(i8* %10, i8 signext 32)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %41
14: ; preds = %2
%15 = load i32, i32* %9
%16 = add nsw i32 %15, 1
%17 = call i8** @calloc(i32 %16, i32 8)
store i8** %17, i8*** %5
%18 = load i8*, i8** %3
%19 = call i8* @strtok(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %6
br label %20
20: ; preds = %23, %14
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %38
23: ; preds = %20
%24 = load i8*, i8** %6
%25 = call i32 @strlen(i8* %24)
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = load i8*, i8** %6
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i8*, i8** %7
%31 = load i8**, i8*** %5
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
store i8* %30, i8** %34
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %37, i8** %6
br label %20
38: ; preds = %20
%39 = load i32, i32* %9
%40 = load i32*, i32** %4
store i32 %39, i32* %40
br label %41
41: ; preds = %38, %2
%42 = load i8**, i8*** %5
ret i8** %42
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define noalias i8** @build_tokenlist(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @count_tokens(i8* %0, i8 signext 32)
%4 = icmp eq i32 %3, 0
br i1 %4, label %21, label %5
5: ; preds = %2
%6 = add nsw i32 %3, 1
%7 = tail call i8** @calloc(i32 %6, i32 8)
%8 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %20, label %10
10: ; preds = %5, %10
%11 = phi i64 [ %17, %10 ], [ 0, %5 ]
%12 = phi i8* [ %18, %10 ], [ %8, %5 ]
%13 = tail call i32 @strlen(i8* %12)
%14 = tail call i8* @malloc(i32 %13)
%15 = tail call i32 @strcpy(i8* %14, i8* %12)
%16 = getelementptr inbounds i8*, i8** %7, i64 %11
store i8* %14, i8** %16
%17 = add nuw i64 %11, 1
%18 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %10
20: ; preds = %10, %5
store i32 %3, i32* %1
br label %21
21: ; preds = %20, %2
%22 = phi i8** [ %7, %20 ], [ null, %2 ]
ret i8** %22
}