#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 execv(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char **);
char **malloc(int);
int /*<<< orphan*/ memcpy(char **, char *const *, int);
int /*<<< orphan*/ perror(char *);
int xexec(const int argc, char *const *argv) {
  char **xargv;
  if (!(xargv = malloc(sizeof(char *) * (argc + 1)))) {
    perror("error: unable to allocate\n");
    exit(102);
  }
  memcpy(xargv, argv, sizeof(char *) * argc);
  xargv[argc] = NULL;
  return execv(xargv[0], xargv);
  free(xargv);
}


x86 assembly (clang, -O0):
<xexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1196 <xexec+0x46>
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
mov    $0x66,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1030 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x4(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <execv@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xexec>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x8(,%r15,8),%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11a6 <xexec+0x56>
mov    %rax,%rbx
movslq %r15d,%r12
shl    $0x3,%r15d
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <memcpy@plt>
movq   $0x0,(%rbx,%r12,8)
mov    (%rbx),%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <execv@plt>
lea    0xe53(%rip),%rdi
call   1050 <perror@plt>
mov    $0x66,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@.str = [27 x i8] c"error: unable to allocate\0A\00"
define i32 @xexec(i32 %0, i8** %1) {
%3 = shl i32 %0, 3
%4 = add i32 %3, 8
%5 = tail call i8** @malloc(i32 %4)
%6 = icmp eq i8** %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 102)
unreachable
10: ; preds = %2
%11 = sext i32 %0 to i64
%12 = tail call i32 @memcpy(i8** %5, i8** %1, i32 %3)
%13 = getelementptr inbounds i8*, i8** %5, i64 %11
store i8* null, i8** %13
%14 = load i8*, i8** %5
%15 = tail call i32 @execv(i8* %14, i8** %5)
ret i32 %15
}