#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 */
char **malloc(int);
int strlen(char *);
char *strtok_r(char *, char *, char **);
char **build_argv(char *cmd) {
  char **argv = malloc((strlen(cmd) + 1) * sizeof(*argv));
  if (argv == NULL) {
    return NULL;
  }
  size_t i = 0;
  char *next_arg;
  char *saveptr = NULL;
  next_arg = strtok_r(cmd, " ", &saveptr);
  while (next_arg != NULL) {
    argv[i++] = next_arg;
    next_arg = strtok_r(NULL, " ", &saveptr);
  }
  argv[i] = NULL;
  return argv;
}


x86 assembly (clang, -O0):
<build_argv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <build_argv+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11f4 <build_argv+0xc4>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe74(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11dc <build_argv+0xac>
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe36(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
jmp    1199 <build_argv+0x69>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_argv>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    0x8(,%rax,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     11a2 <build_argv+0x72>
movq   $0x0,(%rsp)
lea    0xe9d(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1040 <strtok_r@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     119a <build_argv+0x6a>
lea    0xe84(%rip),%r14
mov    %rsp,%r15
nop
mov    %rax,(%r12,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <strtok_r@plt>
test   %rax,%rax
jne    1180 <build_argv+0x50>
movq   $0x0,(%r12,%rbx,8)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
define i8** @build_argv(i8* %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 8
%13 = trunc i64 %12 to i32
%14 = call i8** @malloc(i32 %13)
store i8** %14, i8*** %4
%15 = load i8**, i8*** %4
%16 = icmp eq i8** %15, null
br i1 %16, label %17, label %18
17: ; preds = %1
store i8** null, i8*** %2
br label %36
18: ; preds = %1
store i64 0, i64* %5
store i8* null, i8** %7
%19 = load i8*, i8** %3
%20 = call i8* @strtok_r(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7)
store i8* %20, i8** %6
br label %21
21: ; preds = %24, %18
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = load i8*, i8** %6
%26 = load i8**, i8*** %4
%27 = load i64, i64* %5
%28 = add i64 %27, 1
store i64 %28, i64* %5
%29 = getelementptr inbounds i8*, i8** %26, i64 %27
store i8* %25, i8** %29
%30 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7)
store i8* %30, i8** %6
br label %21
31: ; preds = %21
%32 = load i8**, i8*** %4
%33 = load i64, i64* %5
%34 = getelementptr inbounds i8*, i8** %32, i64 %33
store i8* null, i8** %34
%35 = load i8**, i8*** %4
store i8** %35, i8*** %2
br label %36
36: ; preds = %31, %17
%37 = load i8**, i8*** %2
ret i8** %37
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define noalias i8** @build_argv(i8* %0) {
%2 = alloca i8*
%3 = tail call i32 @strlen(i8* %0)
%4 = shl i32 %3, 3
%5 = add i32 %4, 8
%6 = tail call i8** @malloc(i32 %5)
%7 = icmp eq i8** %6, null
br i1 %7, label %22, label %8
8: ; preds = %1
%9 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%10 = call i8* @strtok_r(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2)
%11 = icmp eq i8* %10, null
br i1 %11, label %19, label %12
12: ; preds = %8, %12
%13 = phi i8* [ %17, %12 ], [ %10, %8 ]
%14 = phi i64 [ %15, %12 ], [ 0, %8 ]
%15 = add i64 %14, 1
%16 = getelementptr inbounds i8*, i8** %6, i64 %14
store i8* %13, i8** %16
%17 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %12
19: ; preds = %12, %8
%20 = phi i64 [ 0, %8 ], [ %15, %12 ]
%21 = getelementptr inbounds i8*, i8** %6, i64 %20
store i8* null, i8** %21
br label %22
22: ; preds = %1, %19
ret i8** %6
}