#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 */
typedef int /*<<< orphan*/ token;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char **);
char **malloc(size_t);
int /*<<< orphan*/ printf(char *);
char **realloc(char **, size_t);
char *strtok(char *, char *);
char **parse_arguments(char *input) {
  unsigned int token_cnt = 0;
  size_t token_list_size = 64;
  char *token = NULL;
  char **token_list = malloc(token_list_size * sizeof(token));
  if (token_list == NULL) {
    printf("ERROR: Input buffer allocation failed! \n");
    exit(EXIT_FAILURE);
  }
  token = strtok(input, " \n\r\t");
  while (token != NULL) {
    token_list[token_cnt] = token;
    token_cnt++;
    if (token_cnt == token_list_size + 1) {
      token_list_size = token_list_size * 2;
      if (token_list_size > 1024) {
        printf("ERROR: Max argument size exceeded! \n");
        free(token_list);
        exit(EXIT_FAILURE);
      }
      token_list = realloc(token_list, token_list_size * sizeof(token));
      if (token_list == NULL) {
        printf("ERROR: Argument buffer re-allocation failed! \n");
        exit(EXIT_FAILURE);
      }
    }
    token = strtok(NULL, " \n\r\t");
  }
  token_list[token_cnt] = NULL;
  return token_list;
}


x86 assembly (clang, -O0):
<parse_arguments>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x40,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x3,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b9 <parse_arguments+0x59>
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1299 <parse_arguments+0x139>
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jne    1280 <parse_arguments+0x120>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x400,-0x18(%rbp)
jbe    1241 <parse_arguments+0xe1>
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x3,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    127b <parse_arguments+0x11b>
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1280 <parse_arguments+0x120>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd9e(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    11cd <parse_arguments+0x6d>
mov    -0x28(%rbp),%rax
mov    -0xc(%rbp),%ecx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_arguments>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x200,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1244 <parse_arguments+0xe4>
mov    %rax,%r15
lea    0xe78(%rip),%rsi
mov    %r14,%rdi
call   1070 <strtok@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     120b <parse_arguments+0xab>
mov    $0x40,%esi
lea    0xe5c(%rip),%r14
jmp    11c5 <parse_arguments+0x65>
cs nopw 0x0(%rax,%rax,1)
mov    %rsi,%rbx
xor    %edi,%edi
mov    %r14,%rsi
call   1070 <strtok@plt>
mov    %rbx,%rsi
test   %rax,%rax
je     1208 <parse_arguments+0xa8>
mov    %r12d,%ecx
mov    %rax,(%r15,%rcx,8)
add    $0x1,%r12d
lea    0x1(%rsi),%rax
cmp    %r12,%rax
jne    11b0 <parse_arguments+0x50>
lea    (%rsi,%rsi,1),%rbx
cmp    $0x401,%rbx
jae    1222 <parse_arguments+0xc2>
shl    $0x4,%rsi
mov    %r15,%rdi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b3 <parse_arguments+0x53>
lea    0xe04(%rip),%rdi
call   1040 <puts@plt>
jmp    1236 <parse_arguments+0xd6>
mov    %r12d,%r12d
movq   $0x0,(%r15,%r12,8)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe0a(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe0c(%rip),%rdi
jmp    1201 <parse_arguments+0xa1>

LLVM IR (-O0):
@.str = [41 x i8] c"ERROR: Input buffer allocation failed! \0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c" \0A\0D\09\00"
@.str.2 = [37 x i8] c"ERROR: Max argument size exceeded! \0A\00"
@.str.3 = [47 x i8] c"ERROR: Argument buffer re-allocation failed! \0A\00"
define i8** @parse_arguments(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8**
store i8* %0, i8** %2
store i32 0, i32* %3
store i64 64, i64* %4
store i8* null, i8** %5
%7 = load i64, i64* %4
%8 = mul i64 %7, 8
%9 = call i8** @malloc(i64 %8)
store i8** %9, i8*** %6
%10 = load i8**, i8*** %6
%11 = icmp eq i8** %10, null
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = load i8*, i8** %2
%18 = call i8* @strtok(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %18, i8** %5
br label %19
19: ; preds = %58, %16
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %60
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = load i8**, i8*** %6
%25 = load i32, i32* %3
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
store i8* %23, i8** %27
%28 = load i32, i32* %3
%29 = add i32 %28, 1
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = zext i32 %30 to i64
%32 = load i64, i64* %4
%33 = add i64 %32, 1
%34 = icmp eq i64 %31, %33
br i1 %34, label %35, label %58
35: ; preds = %22
%36 = load i64, i64* %4
%37 = mul i64 %36, 2
store i64 %37, i64* %4
%38 = load i64, i64* %4
%39 = icmp ugt i64 %38, 1024
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%42 = load i8**, i8*** %6
%43 = call i32 @free(i8** %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %35
%47 = load i8**, i8*** %6
%48 = load i64, i64* %4
%49 = mul i64 %48, 8
%50 = call i8** @realloc(i8** %47, i64 %49)
store i8** %50, i8*** %6
%51 = load i8**, i8*** %6
%52 = icmp eq i8** %51, null
br i1 %52, label %53, label %57
53: ; preds = %46
%54 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %46
br label %58
58: ; preds = %57, %22
%59 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %59, i8** %5
br label %19
60: ; preds = %19
%61 = load i8**, i8*** %6
%62 = load i32, i32* %3
%63 = zext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %61, i64 %63
store i8* null, i8** %64
%65 = load i8**, i8*** %6
ret i8** %65
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c" \0A\0D\09\00"
@str = [46 x i8] c"ERROR: Argument buffer re-allocation failed! \00"
@str.4 = [36 x i8] c"ERROR: Max argument size exceeded! \00"
@str.5 = [40 x i8] c"ERROR: Input buffer allocation failed! \00"
define noalias i8** @parse_arguments(i8* %0) {
%2 = tail call i8** @malloc(i64 512)
%3 = icmp eq i8** %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.5, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %1
%9 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %45, label %11
11: ; preds = %8, %38
%12 = phi i8** [ %40, %38 ], [ %2, %8 ]
%13 = phi i8* [ %41, %38 ], [ %9, %8 ]
%14 = phi i64 [ %39, %38 ], [ 64, %8 ]
%15 = phi i32 [ %18, %38 ], [ 0, %8 ]
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8*, i8** %12, i64 %16
store i8* %13, i8** %17
%18 = add i32 %15, 1
%19 = zext i32 %18 to i64
%20 = add i64 %14, 1
%21 = icmp eq i64 %20, %19
br i1 %21, label %22, label %38
22: ; preds = %11
%23 = shl i64 %14, 1
%24 = icmp ugt i64 %23, 1024
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.4, i64 0, i64 0))
%27 = tail call i32 @free(i8** %12)
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %22
%31 = shl i64 %14, 4
%32 = tail call i8** @realloc(i8** %12, i64 %31)
%33 = icmp eq i8** %32, null
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %30, %11
%39 = phi i64 [ %23, %30 ], [ %14, %11 ]
%40 = phi i8** [ %32, %30 ], [ %12, %11 ]
%41 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %11
43: ; preds = %38
%44 = zext i32 %18 to i64
br label %45
45: ; preds = %8, %43
%46 = phi i64 [ %44, %43 ], [ 0, %8 ]
%47 = phi i8** [ %40, %43 ], [ %2, %8 ]
%48 = getelementptr inbounds i8*, i8** %47, i64 %46
store i8* null, i8** %48
ret i8** %47
}