#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*/ SYS_execve;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ syscall(int /*<<< orphan*/, char *, char **, char **);
int gen_execve(int argc, char *argv[]) {
  if (argc < 3) {
    printf("Indicate which process to execute.\n");
    return 1;
  }
  char *newargv[129] = {NULL};
  char *newenv[] = {NULL};
  newargv[0] = argv[2];
  int arg_nm = argc - 3;
  if (arg_nm > 128) {
    printf("Too many arguments: at most 128 arguments, %d given\n", arg_nm);
    return 1;
  }
  for (int i = 0; i < argc; ++i) {
    newargv[1 + i] = argv[3 + i];
  }
  syscall(SYS_execve, argv[2], newargv, newenv);
  perror("Error in syscall execve");
  return 1;
}


x86 assembly (clang, -O0):
<gen_execve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1176 <gen_execve+0x36>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1272 <gen_execve+0x132>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x408,%edx
call   1040 <memset@plt>
lea    -0x428(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x420(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x42c(%rbp)
cmpl   $0x80,-0x42c(%rbp)
jle    11e7 <gen_execve+0xa7>
mov    -0x42c(%rbp),%esi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1272 <gen_execve+0x132>
movl   $0x0,-0x430(%rbp)
mov    -0x430(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    123b <gen_execve+0xfb>
mov    -0x10(%rbp),%rax
mov    -0x430(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rcx
mov    -0x430(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rcx,-0x420(%rbp,%rax,8)
ff
mov    -0x430(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x430(%rbp)
jmp    11f1 <gen_execve+0xb1>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x420(%rbp),%rdx
lea    -0x428(%rbp),%rcx
call   1050 <syscall@plt>
lea    0xdf3(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_execve>:
push   %r15
push   %r14
push   %rbx
sub    $0x420,%rsp
cmp    $0x2,%edi
jg     117f <gen_execve+0x1f>
lea    0xed5(%rip),%rdi
call   1030 <puts@plt>
jmp    11c2 <gen_execve+0x62>
mov    %rsi,%r14
mov    %edi,%ebx
lea    0x10(%rsp),%rdi
mov    $0x408,%edx
xor    %esi,%esi
call   1050 <memset@plt>
movq   $0x0,0x8(%rsp)
mov    0x10(%r14),%r15
mov    %r15,0x10(%rsp)
cmp    $0x84,%ebx
jb     11d4 <gen_execve+0x74>
add    $0xfffffffd,%ebx
lea    0xe47(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0x18(%rsp),%rdi
add    $0x18,%r14
mov    %ebx,%edx
shl    $0x3,%rdx
mov    %r14,%rsi
call   1070 <memcpy@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r15,%rsi
call   1060 <syscall@plt>
lea    0xe28(%rip),%rdi
call   1080 <perror@plt>
jmp    11c2 <gen_execve+0x62>

LLVM IR (-O0):
@.str = [36 x i8] c"Indicate which process to execute.\0A\00"
@.str.1 = [53 x i8] c"Too many arguments: at most 128 arguments, %d given\0A\00"
@SYS_execve = global i32 0
@.str.2 = [24 x i8] c"Error in syscall execve\00"
define i32 @gen_execve(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [129 x i8*]
%7 = alloca [1 x i8*]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %56
14: ; preds = %2
%15 = bitcast [129 x i8*]* %6 to i8*
%16 = bitcast [1 x i8*]* %7 to i8*
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = getelementptr inbounds [129 x i8*], [129 x i8*]* %6, i64 0, i64 0
store i8* %19, i8** %20
%21 = load i32, i32* %4
%22 = sub nsw i32 %21, 3
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sgt i32 %23, 128
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = load i32, i32* %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 1, i32* %3
br label %56
28: ; preds = %14
store i32 0, i32* %9
br label %29
29: ; preds = %44, %28
%30 = load i32, i32* %9
%31 = load i32, i32* %4
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %47
33: ; preds = %29
%34 = load i8**, i8*** %5
%35 = load i32, i32* %9
%36 = add nsw i32 3, %35
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %34, i64 %37
%39 = load i8*, i8** %38
%40 = load i32, i32* %9
%41 = add nsw i32 1, %40
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [129 x i8*], [129 x i8*]* %6, i64 0, i64 %42
store i8* %39, i8** %43
br label %44
44: ; preds = %33
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
br label %29
47: ; preds = %29
%48 = load i32, i32* @SYS_execve
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 2
%51 = load i8*, i8** %50
%52 = getelementptr inbounds [129 x i8*], [129 x i8*]* %6, i64 0, i64 0
%53 = getelementptr inbounds [1 x i8*], [1 x i8*]* %7, i64 0, i64 0
%54 = call i32 @syscall(i32 %48, i8* %51, i8** %52, i8** %53)
%55 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %56
56: ; preds = %47, %25, %12
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str.1 = [53 x i8] c"Too many arguments: at most 128 arguments, %d given\0A\00"
@SYS_execve = global i32 0
@.str.2 = [24 x i8] c"Error in syscall execve\00"
@str = [35 x i8] c"Indicate which process to execute.\00"
define i32 @gen_execve(i32 %0, i8** nocapture %1) {
%3 = alloca [129 x i8*]
%4 = alloca [1 x i8*]
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %31
8: ; preds = %2
%9 = bitcast [129 x i8*]* %3 to i8*
%10 = bitcast [1 x i8*]* %4 to i8*
%11 = bitcast [1 x i8*]* %4 to i64*
store i64 0, i64* %11
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = getelementptr inbounds [129 x i8*], [129 x i8*]* %3, i64 0, i64 0
store i8* %13, i8** %14
%15 = icmp ugt i32 %0, 131
br i1 %15, label %16, label %19
16: ; preds = %8
%17 = add nsw i32 %0, -3
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %30
19: ; preds = %8
%20 = getelementptr inbounds [129 x i8*], [129 x i8*]* %3, i64 0, i64 1
%21 = bitcast i8** %20 to i8*
%22 = getelementptr i8*, i8** %1, i64 3
%23 = bitcast i8** %22 to i8*
%24 = zext i32 %0 to i64
%25 = shl nuw nsw i64 %24, 3
%26 = load i32, i32* @SYS_execve
%27 = getelementptr inbounds [1 x i8*], [1 x i8*]* %4, i64 0, i64 0
%28 = call i32 @syscall(i32 %26, i8* %13, i8** %14, i8** %27)
%29 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %19, %16
br label %31
31: ; preds = %30, %6
ret i32 1
}