#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 *);
char **malloc(int);
char **realloc(char **, int);
int /*<<< orphan*/ stderr;
char *strtok(char *, char *);
char **mshell_split_line_into_arguments(char *line) {
  int bufsize = 64, position = 0;
  char **tokens = malloc(bufsize * sizeof(char *));
  char *token;
  if (!tokens) {
    fprintf(stderr, "lsh: allocation error\n");
    exit(EXIT_FAILURE);
  }
  token = strtok(line, " \t\r\n\a");
  while (token != NULL) {
    tokens[position] = token;
    position++;
    if (position >= bufsize) {
      bufsize += 64;
      tokens = realloc(tokens, bufsize * sizeof(char *));
      if (!tokens) {
        fprintf(stderr, "lsh: allocation error\n");
        exit(EXIT_FAILURE);
      }
    }
    token = strtok(NULL, " \t\r\n\a");
  }
  tokens[position] = NULL;
  return tokens;
}


x86 assembly (clang, -O0):
<mshell_split_line_into_arguments>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x40,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ab <mshell_split_line_into_arguments+0x5b>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     125b <mshell_split_line_into_arguments+0x10b>
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     1242 <mshell_split_line_into_arguments+0xf2>
mov    -0xc(%rbp),%eax
add    $0x40,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123d <mshell_split_line_into_arguments+0xed>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1242 <mshell_split_line_into_arguments+0xf2>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdca(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    11bf <mshell_split_line_into_arguments+0x6f>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mshell_split_line_into_arguments>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x200,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11d8 <mshell_split_line_into_arguments+0x88>
mov    %rax,%r15
lea    0xea3(%rip),%rsi
mov    %r14,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     11ff <mshell_split_line_into_arguments+0xaf>
mov    $0x40,%r12d
xor    %ebx,%ebx
lea    0xe87(%rip),%r14
jmp    11b6 <mshell_split_line_into_arguments+0x66>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%r15
add    $0x40,%r12d
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
je     11fb <mshell_split_line_into_arguments+0xab>
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
cmp    %ebx,%r12d
jg     11a7 <mshell_split_line_into_arguments+0x57>
lea    0x200(,%r12,8),%esi
mov    %r15,%rdi
call   1050 <realloc@plt>
test   %rax,%rax
jne    11a0 <mshell_split_line_into_arguments+0x50>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %ebx,%eax
jmp    1201 <mshell_split_line_into_arguments+0xb1>
xor    %eax,%eax
movq   $0x0,(%r15,%rax,8)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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