#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 */
struct rusage {
  int dummy;
};
typedef scalar_t__ id_t;

/* Variables and functions */
int errno;
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
char **malloc(int);
int /*<<< orphan*/ print_rusage(struct rusage *);
int /*<<< orphan*/ wait3(int *, int /*<<< orphan*/, struct rusage *);
void with_exec(int argc, char **argv) {
  id_t pid = fork();
  if (pid == 0) {
    char **arr = malloc(sizeof(char *) * argc);
    int i = 0;
    for (i = 0; i < argc - 1; i++) {
      arr[i] = argv[i + 1];
    }
    arr[argc - 1] = 0;
    if (execvp(argv[1], arr) == -1) {
      exit(errno);
    } else {
      exit(0);
    }
  } else {
    int status;
    struct rusage ru;
    wait3(&status, 0, &ru);
    if (status > 0) {
      exit(1);
    }
    print_rusage(&ru);
    exit(status);
  }
}


x86 assembly (clang, -O0):
<with_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1226 <with_exec+0xc6>
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11e2 <with_exec+0x82>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a6 <with_exec+0x46>
mov    -0x20(%rbp),%rax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <execvp@plt>
cmp    $0xffffffff,%eax
jne    121f <with_exec+0xbf>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
lea    -0x30(%rbp),%rdx
call   1030 <wait3@plt>
cmpl   $0x0,-0x28(%rbp)
jle    1249 <with_exec+0xe9>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x30(%rbp),%rdi
call   1040 <print_rusage@plt>
mov    -0x28(%rbp),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<with_exec>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11de <with_exec+0x6e>
lea    0x0(,%rbx,8),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    -0x1(%rbx),%ebp
cmp    $0x2,%ebx
jl     11b4 <with_exec+0x44>
lea    0x8(%r14),%rsi
mov    %ebp,%edx
shl    $0x3,%rdx
mov    %r15,%rdi
call   1050 <memcpy@plt>
movslq %ebp,%rax
movq   $0x0,(%r15,%rax,8)
mov    0x8(%r14),%rdi
mov    %r15,%rsi
call   1070 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1200 <with_exec+0x90>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rdx
xor    %esi,%esi
call   1030 <wait3@plt>
cmpl   $0x0,0xc(%rsp)
jle    1207 <with_exec+0x97>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0x10(%rsp),%rdi
call   1040 <print_rusage@plt>
mov    0xc(%rsp),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.rusage = type { i32 }
@errno = global i32 0
define void @with_exec(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.rusage
store i32 %0, i32* %3
store i8** %1, i8*** %4
%10 = call i64 (...) @fork()
store i64 %10, i64* %5
%11 = load i64, i64* %5
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %55
13: ; preds = %2
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = mul i64 8, %15
%17 = trunc i64 %16 to i32
%18 = call i8** @malloc(i32 %17)
store i8** %18, i8*** %6
store i32 0, i32* %7
store i32 0, i32* %7
br label %19
19: ; preds = %35, %13
%20 = load i32, i32* %7
%21 = load i32, i32* %3
%22 = sub nsw i32 %21, 1
%23 = icmp slt i32 %20, %22
br i1 %23, label %24, label %38
24: ; preds = %19
%25 = load i8**, i8*** %4
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %25, i64 %28
%30 = load i8*, i8** %29
%31 = load i8**, i8*** %6
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
store i8* %30, i8** %34
br label %35
35: ; preds = %24
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %19
38: ; preds = %19
%39 = load i8**, i8*** %6
%40 = load i32, i32* %3
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %39, i64 %42
store i8* null, i8** %43
%44 = load i8**, i8*** %4
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = load i8**, i8*** %6
%48 = call i32 @execvp(i8* %46, i8** %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %38
%51 = load i32, i32* @errno
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %38
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %2
%56 = call i32 @wait3(i32* %8, i32 0, %struct.rusage* %9)
%57 = load i32, i32* %8
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %55
%62 = call i32 @print_rusage(%struct.rusage* %9)
%63 = load i32, i32* %8
%64 = call i32 @exit(i32 %63)
unreachable
}

LLVM IR (-O2):
%struct.rusage = type { i32 }
@errno = global i32 0
define void @with_exec(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.rusage
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %30
7: ; preds = %2
%8 = shl i32 %0, 3
%9 = tail call i8** @malloc(i32 %8)
%10 = add i32 %0, -1
%11 = icmp sgt i32 %0, 1
br i1 %11, label %12, label %18
12: ; preds = %7
%13 = bitcast i8** %9 to i8*
%14 = getelementptr i8*, i8** %1, i64 1
%15 = bitcast i8** %14 to i8*
%16 = zext i32 %10 to i64
%17 = shl nuw nsw i64 %16, 3
br label %18
18: ; preds = %12, %7
%19 = sext i32 %10 to i64
%20 = getelementptr inbounds i8*, i8** %9, i64 %19
store i8* null, i8** %20
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call i32 @execvp(i8* %22, i8** %9)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* @errno
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
%29 = tail call i32 @exit(i32 0)
unreachable
30: ; preds = %2
%31 = bitcast i32* %3 to i8*
%32 = bitcast %struct.rusage* %4 to i8*
%33 = call i32 @wait3(i32* %3, i32 0, %struct.rusage* %4)
%34 = load i32, i32* %3
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = call i32 @print_rusage(%struct.rusage* %4)
%40 = load i32, i32* %3
%41 = call i32 @exit(i32 %40)
unreachable
}