#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char **, int);
int /*<<< orphan*/ stderr;
char *strtok_r(char *, char *, char **);
char **rshell_parse_cmd(char *cmd) {
  int bufsize = 1024;
  int position = 0;
  char **tokens = (char **)malloc(bufsize * sizeof(char *));
  char *token;
  char *saveptr;
  if (!tokens) {
    fprintf(stderr, "rshell: allocation error\n");
    exit(EXIT_FAILURE);
  }
  token = strtok_r(cmd, " ", &saveptr);
  while (token != NULL) {
    tokens[position++] = token;
    if (position >= bufsize) {
      bufsize += 1024;
      tokens = (char **)realloc(tokens, bufsize * sizeof(char *));
      if (!tokens) {
        fprintf(stderr, "rshell: allocation error\n");
        exit(EXIT_FAILURE);
      }
    }
    token = strtok_r(NULL, " ", &saveptr);
  }
  tokens[position] = NULL;
  return tokens;
}


x86 assembly (clang, -O0):
<rshell_parse_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x400,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ab <rshell_parse_cmd+0x5b>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe64(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1030 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1266 <rshell_parse_cmd+0x116>
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     1249 <rshell_parse_cmd+0xf9>
mov    -0xc(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1244 <rshell_parse_cmd+0xf4>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1249 <rshell_parse_cmd+0xf9>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdc6(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1030 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
jmp    11c3 <rshell_parse_cmd+0x73>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rshell_parse_cmd>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    $0x2000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11ee <rshell_parse_cmd+0x9e>
mov    %rax,%r12
lea    0xea1(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
call   1030 <strtok_r@plt>
test   %rax,%rax
je     1215 <rshell_parse_cmd+0xc5>
mov    $0x400,%r13d
xor    %ebx,%ebx
lea    0xe7c(%rip),%r14
lea    0x8(%rsp),%r15
jmp    11cc <rshell_parse_cmd+0x7c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%r12
add    $0x400,%r13d
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strtok_r@plt>
test   %rax,%rax
je     1211 <rshell_parse_cmd+0xc1>
mov    %rax,(%r12,%rbx,8)
add    $0x1,%rbx
cmp    %ebx,%r13d
jg     11ba <rshell_parse_cmd+0x6a>
lea    0x2000(,%r13,8),%esi
mov    %r12,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
jne    11b0 <rshell_parse_cmd+0x60>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %ebx,%eax
jmp    1217 <rshell_parse_cmd+0xc7>
xor    %eax,%eax
movq   $0x0,(%r12,%rax,8)
mov    %r12,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"rshell: allocation error\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c" \00"
define i8** @rshell_parse_cmd(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %2
store i32 1024, i32* %3
store i32 0, i32* %4
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 8
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8**
store i8** %13, i8*** %5
%14 = load i8**, i8*** %5
%15 = icmp ne i8** %14, null
br i1 %15, label %21, label %16
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %1
%22 = load i8*, i8** %2
%23 = call i8* @strtok_r(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7)
store i8* %23, i8** %6
br label %24
24: ; preds = %55, %21
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %57
27: ; preds = %24
%28 = load i8*, i8** %6
%29 = load i8**, i8*** %5
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8*, i8** %29, i64 %32
store i8* %28, i8** %33
%34 = load i32, i32* %4
%35 = load i32, i32* %3
%36 = icmp sge i32 %34, %35
br i1 %36, label %37, label %55
37: ; preds = %27
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1024
store i32 %39, i32* %3
%40 = load i8**, i8*** %5
%41 = load i32, i32* %3
%42 = sext i32 %41 to i64
%43 = mul i64 %42, 8
%44 = trunc i64 %43 to i32
%45 = call i64 @realloc(i8** %40, i32 %44)
%46 = inttoptr i64 %45 to i8**
store i8** %46, i8*** %5
%47 = load i8**, i8*** %5
%48 = icmp ne i8** %47, null
br i1 %48, label %54, label %49
49: ; preds = %37
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %37
br label %55
55: ; preds = %54, %27
%56 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7)
store i8* %56, i8** %6
br label %24
57: ; preds = %24
%58 = load i8**, i8*** %5
%59 = load i32, i32* %4
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
store i8* null, i8** %61
%62 = load i8**, i8*** %5
ret i8** %62
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"rshell: allocation error\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c" \00"
define i8** @rshell_parse_cmd(i8* %0) {
%2 = alloca i8*
%3 = tail call i64 @malloc(i32 8192)
%4 = bitcast i8** %2 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = inttoptr i64 %3 to i8**
%13 = call i8* @strtok_r(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2)
%14 = icmp eq i8* %13, null
br i1 %14, label %42, label %15
15: ; preds = %11, %35
%16 = phi i64 [ %20, %35 ], [ 0, %11 ]
%17 = phi i8* [ %38, %35 ], [ %13, %11 ]
%18 = phi i8** [ %37, %35 ], [ %12, %11 ]
%19 = phi i32 [ %36, %35 ], [ 1024, %11 ]
%20 = add nuw i64 %16, 1
%21 = getelementptr inbounds i8*, i8** %18, i64 %16
store i8* %17, i8** %21
%22 = trunc i64 %20 to i32
%23 = icmp sgt i32 %19, %22
br i1 %23, label %35, label %24
24: ; preds = %15
%25 = add nsw i32 %19, 1024
%26 = shl i32 %25, 3
%27 = call i64 @realloc(i8** %18, i32 %26)
%28 = inttoptr i64 %27 to i8**
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %24, %15
%36 = phi i32 [ %25, %24 ], [ %19, %15 ]
%37 = phi i8** [ %28, %24 ], [ %18, %15 ]
%38 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %15
40: ; preds = %35
%41 = and i64 %20, 4294967295
br label %42
42: ; preds = %40, %11
%43 = phi i64 [ 0, %11 ], [ %41, %40 ]
%44 = phi i8** [ %12, %11 ], [ %37, %40 ]
%45 = getelementptr inbounds i8*, i8** %44, i64 %43
store i8* null, i8** %45
ret i8** %44
}