#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strtok(char *, char *);
unsigned int parse_console_line(const char *line, char ***argv) {
  int i = 0;
  char *tokens;
  char *token;
  unsigned int num_tokens = 0;
  unsigned int len = strlen(line);
  tokens = (char *)malloc(sizeof(char) * (len + 1));
  strcpy(tokens, line);
  token = strtok(tokens, " ");
  while (token != NULL) {
    num_tokens++;
    token = strtok(NULL, " ");
  }
  *argv = (char **)malloc(sizeof(char *) * num_tokens);
  strcpy(tokens, line);
  token = strtok(tokens, " ");
  while (token != NULL) {
    (*argv)[i] = (char *)malloc(sizeof(char) * (strlen(token) + 1));
    strcpy((*argv)[i], token);
    i++;
    token = strtok(NULL, " ");
  }
  free(tokens);
  return num_tokens;
}


x86 assembly (clang, -O0):
<parse_console_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11df <parse_console_line+0x8f>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe2f(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    11b2 <parse_console_line+0x62>
mov    -0x2c(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1288 <parse_console_line+0x138>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd86(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1218 <parse_console_line+0xc8>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_console_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
lea    0xe7d(%rip),%rsi
mov    %r14,%rdi
call   1070 <strtok@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     11b3 <parse_console_line+0x63>
lea    0xe66(%rip),%rbx
nopw   0x0(%rax,%rax,1)
add    $0x1,%r13d
xor    %edi,%edi
mov    %rbx,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
jne    11a0 <parse_console_line+0x50>
lea    0x0(,%r13,8),%edi
call   1060 <malloc@plt>
mov    %rax,(%r15)
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
lea    0xe2b(%rip),%rsi
mov    %r14,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
je     122c <parse_console_line+0xdc>
mov    %rax,%rbx
xor    %ebp,%ebp
lea    0xe12(%rip),%r12
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    (%r15),%rcx
mov    %rax,(%rcx,%rbp,1)
mov    (%r15),%rax
mov    (%rax,%rbp,1),%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   1070 <strtok@plt>
mov    %rax,%rbx
add    $0x8,%rbp
test   %rax,%rax
jne    11f0 <parse_console_line+0xa0>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
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 i32 @parse_console_line(i8* %0, i8*** %1) {
%3 = alloca i8*
%4 = alloca i8***
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8*** %1, i8**** %4
store i32 0, i32* %5
store i32 0, i32* %8
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = add i32 %12, 1
%14 = zext i32 %13 to i64
%15 = mul i64 1, %14
%16 = trunc i64 %15 to i32
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = load i8*, i8** %3
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = load i8*, i8** %6
%23 = call i8* @strtok(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %23, i8** %7
br label %24
24: ; preds = %27, %2
%25 = load i8*, i8** %7
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i32, i32* %8
%29 = add i32 %28, 1
store i32 %29, i32* %8
%30 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %30, i8** %7
br label %24
31: ; preds = %24
%32 = load i32, i32* %8
%33 = zext i32 %32 to i64
%34 = mul i64 8, %33
%35 = trunc i64 %34 to i32
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i8**
%38 = load i8***, i8**** %4
store i8** %37, i8*** %38
%39 = load i8*, i8** %6
%40 = load i8*, i8** %3
%41 = call i32 @strcpy(i8* %39, i8* %40)
%42 = load i8*, i8** %6
%43 = call i8* @strtok(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %43, i8** %7
br label %44
44: ; preds = %47, %31
%45 = load i8*, i8** %7
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %72
47: ; preds = %44
%48 = load i8*, i8** %7
%49 = call i32 @strlen(i8* %48)
%50 = add nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = mul i64 1, %51
%53 = trunc i64 %52 to i32
%54 = call i64 @malloc(i32 %53)
%55 = inttoptr i64 %54 to i8*
%56 = load i8***, i8**** %4
%57 = load i8**, i8*** %56
%58 = load i32, i32* %5
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8*, i8** %57, i64 %59
store i8* %55, i8** %60
%61 = load i8***, i8**** %4
%62 = load i8**, i8*** %61
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8*, i8** %62, i64 %64
%66 = load i8*, i8** %65
%67 = load i8*, i8** %7
%68 = call i32 @strcpy(i8* %66, i8* %67)
%69 = load i32, i32* %5
%70 = add nsw i32 %69, 1
store i32 %70, i32* %5
%71 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %71, i8** %7
br label %44
72: ; preds = %44
%73 = load i8*, i8** %6
%74 = call i32 @free(i8* %73)
%75 = load i32, i32* %8
ret i32 %75
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i32 @parse_console_line(i8* %0, i8*** nocapture %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add i32 %3, 1
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @strcpy(i8* %6, i8* %0)
%8 = tail call i8* @strtok(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %2, %10
%11 = phi i32 [ %12, %10 ], [ 0, %2 ]
%12 = add i32 %11, 1
%13 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %10
15: ; preds = %10, %2
%16 = phi i32 [ 0, %2 ], [ %12, %10 ]
%17 = shl i32 %16, 3
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8**
store i8** %19, i8*** %1
%20 = tail call i32 @strcpy(i8* %6, i8* %0)
%21 = tail call i8* @strtok(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %39, label %23
23: ; preds = %15, %23
%24 = phi i64 [ %36, %23 ], [ 0, %15 ]
%25 = phi i8* [ %37, %23 ], [ %21, %15 ]
%26 = tail call i32 @strlen(i8* %25)
%27 = add nsw i32 %26, 1
%28 = tail call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
%30 = load i8**, i8*** %1
%31 = getelementptr inbounds i8*, i8** %30, i64 %24
store i8* %29, i8** %31
%32 = load i8**, i8*** %1
%33 = getelementptr inbounds i8*, i8** %32, i64 %24
%34 = load i8*, i8** %33
%35 = tail call i32 @strcpy(i8* %34, i8* %25)
%36 = add nuw i64 %24, 1
%37 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %23
39: ; preds = %23, %15
%40 = tail call i32 @free(i8* %6)
ret i32 %16
}