#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 */
typedef int /*<<< orphan*/ s;

/* Variables and functions */
char **reallocarray(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strappend(char **, size_t *, char *);
char *strdup(char *);
char **make_execv_array(char shell[], char cmd[]) {
  const size_t safe_arg_len = 1;
  const size_t npieces = 1U;
  char **args = reallocarray(NULL, 3 + npieces + 1, sizeof(*args));
  char *eval_cmd;
  size_t len;
  size_t i;
  if (npieces == 1) {
    args[0] = shell;
    args[1] = "-c";
    args[2] = cmd;
    args[3] = NULL;
    return args;
  }
  eval_cmd = NULL;
  len = 0U;
  (void)strappend(&eval_cmd, &len, "eval \"");
  for (i = 0; i < npieces; ++i) {
    char s[32];
    char c;
    snprintf(s, sizeof(s), "${%d}", (int)i);
    (void)strappend(&eval_cmd, &len, s);
    c = cmd[safe_arg_len];
    cmd[safe_arg_len] = '\0';
    args[3 + i] = strdup(cmd);
    cmd[safe_arg_len] = c;
    cmd += safe_arg_len;
  }
  (void)strappend(&eval_cmd, &len, "\"");
  args[0] = shell;
  args[1] = "-c";
  args[2] = eval_cmd;
  args[3 + npieces] = NULL;
  return args;
}


x86 assembly (clang, -O0):
<make_execv_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
mov    $0x5,%esi
mov    $0x8,%edx
call   1030 <reallocarray@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
lea    0xea3(%rip),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,0x18(%rax)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_execv_array>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
mov    $0x5,%esi
mov    $0x8,%edx
call   1030 <reallocarray@plt>
mov    %rbx,(%rax)
lea    0xecb(%rip),%rcx
mov    %rcx,0x8(%rax)
mov    %r14,0x10(%rax)
movq   $0x0,0x18(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"-c\00"
define i8** @make_execv_array(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i64 1, i64* %5
store i64 1, i64* %6
%11 = call i8** @reallocarray(i32* null, i32 5, i32 8)
store i8** %11, i8*** %7
%12 = load i8*, i8** %3
%13 = load i8**, i8*** %7
%14 = getelementptr inbounds i8*, i8** %13, i64 0
store i8* %12, i8** %14
%15 = load i8**, i8*** %7
%16 = getelementptr inbounds i8*, i8** %15, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %16
%17 = load i8*, i8** %4
%18 = load i8**, i8*** %7
%19 = getelementptr inbounds i8*, i8** %18, i64 2
store i8* %17, i8** %19
%20 = load i8**, i8*** %7
%21 = getelementptr inbounds i8*, i8** %20, i64 3
store i8* null, i8** %21
%22 = load i8**, i8*** %7
ret i8** %22
}

LLVM IR (-O2):
@.str = [3 x i8] c"-c\00"
define i8** @make_execv_array(i8* %0, i8* %1) {
%3 = tail call i8** @reallocarray(i32* null, i32 5, i32 8)
store i8* %0, i8** %3
%4 = getelementptr inbounds i8*, i8** %3, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = getelementptr inbounds i8*, i8** %3, i64 2
store i8* %1, i8** %5
%6 = getelementptr inbounds i8*, i8** %3, i64 3
store i8* null, i8** %6
ret i8** %3
}