#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 count_params(char const *);
void *malloc(int);
char *strdup(char const *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strsep(char **, char *);
char **parse_cmdline(const char *cmdline) {
  int params_count = count_params(cmdline);
  char **parsed_params = malloc(sizeof(char *) * (params_count + 1));
  char *command = strdup(cmdline);
  char *token;
  int i = 0;
  while ((token = strsep(&command, " ")) != NULL && i < params_count) {
    if (i == params_count - 1 && strlen(token) > 0) {
      parsed_params[i] = strsep(&token, "\n");
    } else if (strlen(token) > 0) {
      int length = strlen(token);
      parsed_params[i] = malloc(length);
      strncpy(parsed_params[i], token, length);
      i++;
    }
  }
  parsed_params[params_count] = NULL;
  return parsed_params;
}


x86 assembly (clang, -O0):
<parse_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <count_params@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   1060 <strsep@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x31(%rbp)
je     11d5 <parse_cmdline+0x75>
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11e5 <parse_cmdline+0x85>
jmp    128c <parse_cmdline+0x12c>
mov    -0x2c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    122c <parse_cmdline+0xcc>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jle    122c <parse_cmdline+0xcc>
lea    -0x28(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1060 <strsep@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1287 <parse_cmdline+0x127>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jle    1282 <parse_cmdline+0x122>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1287 <parse_cmdline+0x127>
jmp    11a3 <parse_cmdline+0x43>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_cmdline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbp
call   1070 <count_params@plt>
mov    %eax,%r14d
lea    0x8(,%r14,8),%edi
call   1050 <malloc@plt>
mov    %rax,0x10(%rsp)
mov    %rbp,%rdi
call   1080 <strdup@plt>
mov    %rax,0x18(%rsp)
lea    0xe61(%rip),%rsi
lea    0x18(%rsp),%rdi
call   1060 <strsep@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1278 <parse_cmdline+0x118>
test   %r14d,%r14d
jle    1278 <parse_cmdline+0x118>
lea    -0x1(%r14),%r13d
movslq %r13d,%rcx
mov    %rcx,0x20(%rsp)
xor    %r12d,%r12d
lea    0xe2a(%rip),%rbp
lea    0x18(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
cmp    %r13d,%r12d
jne    121a <parse_cmdline+0xba>
mov    %rax,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1215 <parse_cmdline+0xb5>
lea    0x8(%rsp),%rdi
lea    0xe05(%rip),%rsi
call   1060 <strsep@plt>
mov    0x20(%rsp),%rcx
mov    0x10(%rsp),%rdx
mov    %rax,(%rdx,%rcx,8)
mov    %r13d,%r12d
jmp    125a <parse_cmdline+0xfa>
mov    0x8(%rsp),%rax
mov    %rax,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    125a <parse_cmdline+0xfa>
mov    0x8(%rsp),%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1050 <malloc@plt>
movslq %r12d,%rcx
mov    0x10(%rsp),%rdx
mov    %rax,(%rdx,%rcx,8)
mov    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    %r15d,%edx
call   1030 <strncpy@plt>
add    $0x1,%r12d
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <strsep@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1278 <parse_cmdline+0x118>
cmp    %r14d,%r12d
jl     11e0 <parse_cmdline+0x80>
movslq %r14d,%rcx
mov    0x10(%rsp),%rax
movq   $0x0,(%rax,%rcx,8)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [2 x i8] c"\0A\00"
define i8** @parse_cmdline(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @count_params(i8* %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 8, %13
%15 = trunc i64 %14 to i32
%16 = call i8* @malloc(i32 %15)
%17 = bitcast i8* %16 to i8**
store i8** %17, i8*** %4
%18 = load i8*, i8** %2
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %5
store i32 0, i32* %7
br label %20
20: ; preds = %68, %1
%21 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* %7
%25 = load i32, i32* %3
%26 = icmp slt i32 %24, %25
br label %27
27: ; preds = %23, %20
%28 = phi i1 [ false, %20 ], [ %26, %23 ]
br i1 %28, label %29, label %69
29: ; preds = %27
%30 = load i32, i32* %7
%31 = load i32, i32* %3
%32 = sub nsw i32 %31, 1
%33 = icmp eq i32 %30, %32
br i1 %33, label %34, label %44
34: ; preds = %29
%35 = load i8*, i8** %6
%36 = call i32 @strlen(i8* %35)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %34
%39 = call i8* @strsep(i8** %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%40 = load i8**, i8*** %4
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %40, i64 %42
store i8* %39, i8** %43
br label %68
44: ; preds = %34, %29
%45 = load i8*, i8** %6
%46 = call i32 @strlen(i8* %45)
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %67
48: ; preds = %44
%49 = load i8*, i8** %6
%50 = call i32 @strlen(i8* %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = call i8* @malloc(i32 %51)
%53 = load i8**, i8*** %4
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
store i8* %52, i8** %56
%57 = load i8**, i8*** %4
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8*, i8** %57, i64 %59
%61 = load i8*, i8** %60
%62 = load i8*, i8** %6
%63 = load i32, i32* %8
%64 = call i32 @strncpy(i8* %61, i8* %62, i32 %63)
%65 = load i32, i32* %7
%66 = add nsw i32 %65, 1
store i32 %66, i32* %7
br label %67
67: ; preds = %48, %44
br label %68
68: ; preds = %67, %38
br label %20
69: ; preds = %27
%70 = load i8**, i8*** %4
%71 = load i32, i32* %3
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8*, i8** %70, i64 %72
store i8* null, i8** %73
%74 = load i8**, i8*** %4
ret i8** %74
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [2 x i8] c"\0A\00"
define noalias i8** @parse_cmdline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = tail call i32 @count_params(i8* %0)
%5 = shl i32 %4, 3
%6 = add i32 %5, 8
%7 = tail call i8* @malloc(i32 %6)
%8 = bitcast i8* %7 to i8**
%9 = bitcast i8** %2 to i8*
%10 = tail call i8* @strdup(i8* %0)
store i8* %10, i8** %2
%11 = bitcast i8** %3 to i8*
%12 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %3
%13 = icmp ne i8* %12, null
%14 = icmp sgt i32 %4, 0
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %16, label %50
16: ; preds = %1
%17 = add nsw i32 %4, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %8, i64 %18
br label %20
20: ; preds = %16, %44
%21 = phi i8* [ %12, %16 ], [ %46, %44 ]
%22 = phi i32 [ 0, %16 ], [ %45, %44 ]
%23 = icmp eq i32 %22, %17
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = call i32 @strlen(i8* %21)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = load i8*, i8** %3
br label %31
29: ; preds = %24
%30 = call i8* @strsep(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %30, i8** %19
br label %44
31: ; preds = %27, %20
%32 = phi i8* [ %28, %27 ], [ %21, %20 ]
%33 = call i32 @strlen(i8* %32)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %31
%36 = load i8*, i8** %3
%37 = call i32 @strlen(i8* %36)
%38 = call i8* @malloc(i32 %37)
%39 = sext i32 %22 to i64
%40 = getelementptr inbounds i8*, i8** %8, i64 %39
store i8* %38, i8** %40
%41 = load i8*, i8** %3
%42 = call i32 @strncpy(i8* %38, i8* %41, i32 %37)
%43 = add nsw i32 %22, 1
br label %44
44: ; preds = %31, %35, %29
%45 = phi i32 [ %17, %29 ], [ %43, %35 ], [ %22, %31 ]
%46 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %46, i8** %3
%47 = icmp ne i8* %46, null
%48 = icmp slt i32 %45, %4
%49 = select i1 %47, i1 %48, i1 false
br i1 %49, label %20, label %50
50: ; preds = %44, %1
%51 = sext i32 %4 to i64
%52 = getelementptr inbounds i8*, i8** %8, i64 %51
store i8* null, i8** %52
ret i8** %8
}