#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*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char **, int);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strtok(char *, char *);
char **parse_cmdline(const char *cmdline) {
  size_t cmdline_len = strlen(cmdline);
  char *buf_cmd_line = (char *)malloc(cmdline_len);
  if (buf_cmd_line == NULL) {
    fprintf(stderr, "Could not allocate memory for command line buffer");
    return NULL;
  }
  strncpy(buf_cmd_line, cmdline, cmdline_len - 1);
  buf_cmd_line[cmdline_len - 1] = '\0';
  char **args = (char **)malloc(sizeof(char *));
  if (args == NULL) {
    fprintf(stderr, "Could not allocate memory for command line args");
    return NULL;
  }
  char *current_arg = strtok(buf_cmd_line, " ");
  args[0] = current_arg;
  int args_count = 2;
  while ((current_arg = strtok(NULL, " ")) != NULL) {
    args = (char **)realloc(args, args_count * sizeof(char *));
    if (args == NULL) {
      fprintf(stderr, "Could not realloc for command line args.");
      return NULL;
    }
    args[args_count - 1] = current_arg;
    args_count++;
  }
  args = (char **)realloc(args,
                          ((args_count - 1) * sizeof(char *)) + sizeof(NULL));
  if (args == NULL) {
    fprintf(stderr, "Could not realloc for args' sentinel value.");
    return NULL;
  }
  args[args_count - 1] = NULL;
  return args;
}


x86 assembly (clang, -O0):
<parse_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <parse_cmdline+0x55>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1329 <parse_cmdline+0x1c9>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1215 <parse_cmdline+0xb5>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1329 <parse_cmdline+0x1c9>
mov    -0x20(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x2,-0x34(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe1c(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     12c0 <parse_cmdline+0x160>
mov    -0x28(%rbp),%rdi
movslq -0x34(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    129d <parse_cmdline+0x13d>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1329 <parse_cmdline+0x1c9>
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    123b <parse_cmdline+0xdb>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x3,%rax
add    $0x8,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    130c <parse_cmdline+0x1ac>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1329 <parse_cmdline+0x1c9>
mov    -0x28(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_cmdline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1221 <parse_cmdline+0xc1>
mov    %rax,%rbx
mov    %rbp,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%rbp,1)
mov    $0x8,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1233 <parse_cmdline+0xd3>
mov    %rax,%r14
lea    0xea8(%rip),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <strtok@plt>
mov    %rax,(%r14)
xor    %edi,%edi
mov    %rbp,%rsi
call   1080 <strtok@plt>
test   %rax,%rax
je     1245 <parse_cmdline+0xe5>
mov    %rax,%rbx
mov    $0x1,%r12d
mov    $0x10,%ebp
lea    0xe76(%rip),%r15
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
test   %rax,%rax
je     126d <parse_cmdline+0x10d>
mov    %rax,%r14
mov    %rbx,(%rax,%r12,8)
xor    %edi,%edi
mov    %r15,%rsi
call   1080 <strtok@plt>
mov    %rax,%rbx
add    $0x1,%r12
add    $0x8,%ebp
test   %rax,%rax
jne    11f0 <parse_cmdline+0x90>
jmp    124b <parse_cmdline+0xeb>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
jmp    128f <parse_cmdline+0x12f>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
jmp    128f <parse_cmdline+0x12f>
mov    $0x1,%r12d
lea    0x8(,%r12,8),%esi
mov    %r14,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     127f <parse_cmdline+0x11f>
mov    %r12d,%ecx
movq   $0x0,(%rax,%rcx,8)
jmp    1296 <parse_cmdline+0x136>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
jmp    128f <parse_cmdline+0x12f>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [50 x i8] c"Could not allocate memory for command line buffer\00"
@.str.1 = [48 x i8] c"Could not allocate memory for command line args\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [41 x i8] c"Could not realloc for command line args.\00"
@.str.4 = [44 x i8] c"Could not realloc for args' sentinel value.\00"
define i8** @parse_cmdline(i8* %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %4
%11 = load i64, i64* %4
%12 = trunc i64 %11 to i32
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
store i8** null, i8*** %2
br label %90
20: ; preds = %1
%21 = load i8*, i8** %5
%22 = load i8*, i8** %3
%23 = load i64, i64* %4
%24 = sub i64 %23, 1
%25 = call i32 @strncpy(i8* %21, i8* %22, i64 %24)
%26 = load i8*, i8** %5
%27 = load i64, i64* %4
%28 = sub i64 %27, 1
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = call i64 @malloc(i32 8)
%31 = inttoptr i64 %30 to i8**
store i8** %31, i8*** %6
%32 = load i8**, i8*** %6
%33 = icmp eq i8** %32, null
br i1 %33, label %34, label %37
34: ; preds = %20
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
store i8** null, i8*** %2
br label %90
37: ; preds = %20
%38 = load i8*, i8** %5
%39 = call i8* @strtok(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %39, i8** %7
%40 = load i8*, i8** %7
%41 = load i8**, i8*** %6
%42 = getelementptr inbounds i8*, i8** %41, i64 0
store i8* %40, i8** %42
store i32 2, i32* %8
br label %43
43: ; preds = %59, %37
%44 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %44, i8** %7
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %68
46: ; preds = %43
%47 = load i8**, i8*** %6
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = mul i64 %49, 8
%51 = trunc i64 %50 to i32
%52 = call i64 @realloc(i8** %47, i32 %51)
%53 = inttoptr i64 %52 to i8**
store i8** %53, i8*** %6
%54 = load i8**, i8*** %6
%55 = icmp eq i8** %54, null
br i1 %55, label %56, label %59
56: ; preds = %46
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
store i8** null, i8*** %2
br label %90
59: ; preds = %46
%60 = load i8*, i8** %7
%61 = load i8**, i8*** %6
%62 = load i32, i32* %8
%63 = sub nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8*, i8** %61, i64 %64
store i8* %60, i8** %65
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %43
68: ; preds = %43
%69 = load i8**, i8*** %6
%70 = load i32, i32* %8
%71 = sub nsw i32 %70, 1
%72 = sext i32 %71 to i64
%73 = mul i64 %72, 8
%74 = add i64 %73, 8
%75 = trunc i64 %74 to i32
%76 = call i64 @realloc(i8** %69, i32 %75)
%77 = inttoptr i64 %76 to i8**
store i8** %77, i8*** %6
%78 = load i8**, i8*** %6
%79 = icmp eq i8** %78, null
br i1 %79, label %80, label %83
80: ; preds = %68
%81 = load i32, i32* @stderr
%82 = call i32 @fprintf(i32 %81, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
store i8** null, i8*** %2
br label %90
83: ; preds = %68
%84 = load i8**, i8*** %6
%85 = load i32, i32* %8
%86 = sub nsw i32 %85, 1
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8*, i8** %84, i64 %87
store i8* null, i8** %88
%89 = load i8**, i8*** %6
store i8** %89, i8*** %2
br label %90
90: ; preds = %83, %80, %56, %34, %17
%91 = load i8**, i8*** %2
ret i8** %91
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [50 x i8] c"Could not allocate memory for command line buffer\00"
@.str.1 = [48 x i8] c"Could not allocate memory for command line args\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [41 x i8] c"Could not realloc for command line args.\00"
@.str.4 = [44 x i8] c"Could not realloc for args' sentinel value.\00"
define i8** @parse_cmdline(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = trunc i64 %2 to i32
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %59
10: ; preds = %1
%11 = add i64 %2, -1
%12 = tail call i32 @strncpy(i8* %5, i8* %0, i64 %11)
%13 = getelementptr inbounds i8, i8* %5, i64 %11
store i8 0, i8* %13
%14 = tail call i64 @malloc(i32 8)
%15 = inttoptr i64 %14 to i8**
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %59
20: ; preds = %10
%21 = tail call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %21, i8** %15
%22 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i8* %22, null
br i1 %23, label %45, label %24
24: ; preds = %20, %36
%25 = phi i64 [ %39, %36 ], [ 2, %20 ]
%26 = phi i8* [ %40, %36 ], [ %22, %20 ]
%27 = phi i8** [ %31, %36 ], [ %15, %20 ]
%28 = trunc i64 %25 to i32
%29 = shl i32 %28, 3
%30 = tail call i64 @realloc(i8** %27, i32 %29)
%31 = inttoptr i64 %30 to i8**
%32 = icmp eq i64 %30, 0
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %59
36: ; preds = %24
%37 = add nsw i64 %25, -1
%38 = getelementptr inbounds i8*, i8** %31, i64 %37
store i8* %26, i8** %38
%39 = add nuw i64 %25, 1
%40 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %24
42: ; preds = %36
%43 = trunc i64 %25 to i32
%44 = inttoptr i64 %30 to i8**
br label %45
45: ; preds = %42, %20
%46 = phi i8** [ %15, %20 ], [ %44, %42 ]
%47 = phi i32 [ 1, %20 ], [ %43, %42 ]
%48 = shl i32 %47, 3
%49 = add i32 %48, 8
%50 = tail call i64 @realloc(i8** %46, i32 %49)
%51 = inttoptr i64 %50 to i8**
%52 = icmp eq i64 %50, 0
br i1 %52, label %53, label %56
53: ; preds = %45
%54 = load i32, i32* @stderr
%55 = tail call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
br label %59
56: ; preds = %45
%57 = zext i32 %47 to i64
%58 = getelementptr inbounds i8*, i8** %51, i64 %57
store i8* null, i8** %58
br label %59
59: ; preds = %17, %56, %53, %33, %7
%60 = phi i8** [ null, %7 ], [ null, %17 ], [ null, %33 ], [ null, %53 ], [ %51, %56 ]
ret i8** %60
}