#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 *);
int /*<<< orphan*/ free(char **);
char **malloc(int);
char **realloc(char **, int);
int /*<<< orphan*/ stderr;
char *strtok(char *, char *);
char **lsh_split_line(char *line) {
  int bufsize = 64, position = 0;
  char **tokens = malloc(bufsize * sizeof(char *));
  char *token, **tokens_backup;
  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_backup = tokens;
      tokens = realloc(tokens, bufsize * sizeof(char *));
      if (!tokens) {
        free(tokens_backup);
        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):
<lsh_split_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x40,-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    11bb <lsh_split_line+0x5b>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     127c <lsh_split_line+0x11c>
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     1263 <lsh_split_line+0x103>
mov    -0xc(%rbp),%eax
add    $0x40,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%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    125e <lsh_split_line+0xfe>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1263 <lsh_split_line+0x103>
xor    %eax,%eax
mov    %eax,%edi
lea    0xda9(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    11cf <lsh_split_line+0x6f>
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):
<lsh_split_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x200,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     120f <lsh_split_line+0xaf>
mov    %rax,%r15
lea    0xe8f(%rip),%rsi
mov    %r14,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
je     11ee <lsh_split_line+0x8e>
mov    $0x40,%r12d
xor    %ebx,%ebx
lea    0xe73(%rip),%r14
jmp    11bf <lsh_split_line+0x5f>
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r14,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
je     11ea <lsh_split_line+0x8a>
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
cmp    %ebx,%r12d
jg     11b0 <lsh_split_line+0x50>
lea    0x200(,%r12,8),%esi
mov    %r15,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     1207 <lsh_split_line+0xa7>
add    $0x40,%r12d
mov    %rax,%r15
jmp    11b0 <lsh_split_line+0x50>
mov    %ebx,%eax
jmp    11f0 <lsh_split_line+0x90>
xor    %eax,%eax
movq   $0x0,(%r15,%rax,8)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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** @lsh_split_line(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 64, 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 i8** @malloc(i32 %11)
store i8** %12, i8*** %5
%13 = load i8**, i8*** %5
%14 = icmp ne i8** %13, null
br i1 %14, label %20, label %15
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %1
%21 = load i8*, i8** %2
%22 = call i8* @strtok(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* %22, i8** %6
br label %23
23: ; preds = %57, %20
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %59
26: ; preds = %23
%27 = load i8*, i8** %6
%28 = load i8**, i8*** %5
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
store i8* %27, i8** %31
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = load i32, i32* %3
%36 = icmp sge i32 %34, %35
br i1 %36, label %37, label %57
37: ; preds = %26
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 64
store i32 %39, i32* %3
%40 = load i8**, i8*** %5
store i8** %40, i8*** %7
%41 = load i8**, i8*** %5
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = mul i64 %43, 8
%45 = trunc i64 %44 to i32
%46 = call i8** @realloc(i8** %41, i32 %45)
store i8** %46, i8*** %5
%47 = load i8**, i8*** %5
%48 = icmp ne i8** %47, null
br i1 %48, label %56, label %49
49: ; preds = %37
%50 = load i8**, i8*** %7
%51 = call i32 @free(i8** %50)
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %37
br label %57
57: ; preds = %56, %26
%58 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* %58, i8** %6
br label %23
59: ; preds = %23
%60 = load i8**, i8*** %5
%61 = load i32, i32* %4
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8*, i8** %60, i64 %62
store i8* null, i8** %63
%64 = load i8**, i8*** %5
ret i8** %64
}

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** @lsh_split_line(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 %39, label %12
12: ; preds = %9, %32
%13 = phi i64 [ %18, %32 ], [ 0, %9 ]
%14 = phi i32 [ %34, %32 ], [ 64, %9 ]
%15 = phi i8* [ %35, %32 ], [ %10, %9 ]
%16 = phi i8** [ %33, %32 ], [ %2, %9 ]
%17 = getelementptr inbounds i8*, i8** %16, i64 %13
store i8* %15, i8** %17
%18 = add nuw i64 %13, 1
%19 = trunc i64 %18 to i32
%20 = icmp sgt i32 %14, %19
br i1 %20, label %32, label %21
21: ; preds = %12
%22 = add nsw i32 %14, 64
%23 = shl i32 %22, 3
%24 = tail call i8** @realloc(i8** %16, i32 %23)
%25 = icmp eq i8** %24, null
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = tail call i32 @free(i8** %16)
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %21, %12
%33 = phi i8** [ %24, %21 ], [ %16, %12 ]
%34 = phi i32 [ %22, %21 ], [ %14, %12 ]
%35 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %12
37: ; preds = %32
%38 = and i64 %18, 4294967295
br label %39
39: ; preds = %37, %9
%40 = phi i64 [ 0, %9 ], [ %38, %37 ]
%41 = phi i8** [ %2, %9 ], [ %33, %37 ]
%42 = getelementptr inbounds i8*, i8** %41, i64 %40
store i8* null, i8** %42
ret i8** %41
}