#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ execve(char *, char **, char *const *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  pid_t pid;
  int i;
  for (i = 0; i < argc; ++i) {
    printf("%02d: %s\n", i, argv[i]);
  }
  if (argc < 3) {
    printf("Usage: %s path argv[]\n", argv[0]);
    exit(1);
  }
  pid = fork();
  if (pid == -1) {
    perror("fork error");
    exit(1);
  } else if (pid == 0) {
    char *const envp[] = {
        "A=1", "B=2", "C=/home/laoguo", "D=hello", NULL,
    };
    execve(argv[1], &argv[2], envp);
  } else {
    int status;
    wait(&status);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11c4 <main+0x54>
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118d <main+0x1d>
cmpl   $0x3,-0x8(%rbp)
jge    11ed <main+0x7d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1217 <main+0xa7>
lea    0xe19(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    127b <main+0x10b>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    0xdf4(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xded(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xde6(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xdea(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
lea    -0x40(%rbp),%rdx
call   1050 <execve@plt>
jmp    1284 <main+0x114>
lea    -0x44(%rbp),%rdi
call   1080 <wait@plt>
jmp    1289 <main+0x119>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
test   %edi,%edi
jle    1222 <main+0xc2>
mov    %edi,%r12d
lea    0xe80(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15,%rbx,8),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1190 <main+0x30>
cmp    $0x2,%r12d
jle    1222 <main+0xc2>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11c9 <main+0x69>
cmp    $0xffffffff,%eax
je     123d <main+0xdd>
mov    %rsp,%rdi
call   1070 <wait@plt>
jmp    1214 <main+0xb4>
movq   $0x0,0x20(%rsp)
lea    0xe53(%rip),%rax
mov    %rax,(%rsp)
lea    0xe4c(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xe44(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe47(%rip),%rax
mov    %rax,0x18(%rsp)
mov    0x8(%r15),%rdi
add    $0x10,%r15
mov    %rsp,%rdx
mov    %r15,%rsi
call   1040 <execve@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%r15),%rsi
lea    0xdde(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xddd(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"%02d: %s\0A\00"
@.str.1 = [23 x i8] c"Usage: %s path argv[]\0A\00"
@.str.2 = [11 x i8] c"fork error\00"
@.str.3 = [4 x i8] c"A=1\00"
@.str.4 = [4 x i8] c"B=2\00"
@.str.5 = [15 x i8] c"C=/home/laoguo\00"
@.str.6 = [8 x i8] c"D=hello\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [5 x i8*]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
br label %10
10: ; preds = %22, %2
%11 = load i32, i32* %7
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = load i32, i32* %7
%16 = load i8**, i8*** %5
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %16, i64 %18
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %20)
br label %22
22: ; preds = %14
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
br label %10
25: ; preds = %10
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 3
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 0
%31 = load i8*, i8** %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %25
%35 = call i32 (...) @fork()
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %34
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %58
44: ; preds = %41
%45 = bitcast [5 x i8*]* %8 to i8*
%46 = bitcast i8* %45 to [5 x i8*]*
%47 = getelementptr inbounds [5 x i8*], [5 x i8*]* %46, i32 0, i32 0
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i32 0, i32 0), i8** %47
%48 = getelementptr inbounds [5 x i8*], [5 x i8*]* %46, i32 0, i32 1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i32 0, i32 0), i8** %48
%49 = getelementptr inbounds [5 x i8*], [5 x i8*]* %46, i32 0, i32 2
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i32 0, i32 0), i8** %49
%50 = getelementptr inbounds [5 x i8*], [5 x i8*]* %46, i32 0, i32 3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i32 0, i32 0), i8** %50
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 2
%56 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 0
%57 = call i32 @execve(i8* %53, i8** %55, i8** %56)
br label %60
58: ; preds = %41
%59 = call i32 @wait(i32* %9)
br label %60
60: ; preds = %58, %44
br label %61
61: ; preds = %60
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"%02d: %s\0A\00"
@.str.1 = [23 x i8] c"Usage: %s path argv[]\0A\00"
@.str.2 = [11 x i8] c"fork error\00"
@.str.3 = [4 x i8] c"A=1\00"
@.str.4 = [4 x i8] c"B=2\00"
@.str.5 = [15 x i8] c"C=/home/laoguo\00"
@.str.6 = [8 x i8] c"D=hello\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [5 x i8*]
%4 = alloca i32
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %18
6: ; preds = %2
%7 = zext i32 %0 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %14, %8 ]
%10 = getelementptr inbounds i8*, i8** %1, i64 %9
%11 = load i8*, i8** %10
%12 = trunc i64 %9 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %11)
%14 = add nuw nsw i64 %9, 1
%15 = icmp eq i64 %14, %7
br i1 %15, label %16, label %8
16: ; preds = %8
%17 = icmp slt i32 %0, 3
br i1 %17, label %18, label %22
18: ; preds = %2, %16
%19 = load i8*, i8** %1
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = tail call i32 (...) @fork()
switch i32 %23, label %39 [
i32 -1, label %24
i32 0, label %27
]
24: ; preds = %22
%25 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %22
%28 = bitcast [5 x i8*]* %3 to i8*
%29 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 4
%30 = bitcast i8** %29 to i64*
store i64 0, i64* %30
%31 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %31
%32 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8** %32
%33 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8** %33
%34 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8** %34
%35 = getelementptr inbounds i8*, i8** %1, i64 1
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = call i32 @execve(i8* %36, i8** %37, i8** %31)
br label %42
39: ; preds = %22
%40 = bitcast i32* %4 to i8*
%41 = call i32 @wait(i32* %4)
br label %42
42: ; preds = %27, %39
ret i32 0
}