#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*/ O_RDONLY;
scalar_t__ dup2(int, int);
scalar_t__ execve(char *, char **, char **);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char **argv, char **envp) {
  int fd;
  fd = open(argv[argc - 1], O_RDONLY);
  if (dup2(fd, 3) < 0)
    exit(1);
  if (fd < 0)
    exit(1);
  unlink(argv[argc - 1]);
  argv[argc - 1] = NULL;
  if (execve(argv[1], argv + 1, envp) < 0)
    exit(1);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x3,%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    11ad <main+0x5d>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    11c1 <main+0x71>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
mov    -0x18(%rbp),%rdx
call   1050 <execve@plt>
cmp    $0x0,%rax
jge    1219 <main+0xc9>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
movslq %edi,%r15
mov    -0x8(%rsi,%r15,8),%rdi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x3,%esi
call   1040 <dup2@plt>
test   %rax,%rax
js     11b7 <main+0x67>
test   %ebp,%ebp
js     11b7 <main+0x67>
add    $0xffffffffffffffff,%r15
mov    (%rbx,%r15,8),%rdi
call   1030 <unlink@plt>
movq   $0x0,(%rbx,%r15,8)
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <execve@plt>
test   %rax,%rax
jns    11c1 <main+0x71>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%9 = load i8**, i8*** %6
%10 = load i32, i32* %5
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8*, i8** %9, i64 %12
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = call i64 @dup2(i32 %17, i32 3)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %3
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %22
%28 = load i8**, i8*** %6
%29 = load i32, i32* %5
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
%33 = load i8*, i8** %32
%34 = call i32 @unlink(i8* %33)
%35 = load i8**, i8*** %6
%36 = load i32, i32* %5
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %35, i64 %38
store i8* null, i8** %39
%40 = load i8**, i8*** %6
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = load i8**, i8*** %6
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8**, i8*** %7
%46 = call i64 @execve(i8* %42, i8** %44, i8** %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %27
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %27
%51 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = add nsw i32 %0, -1
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8*, i8** %1, i64 %5
%7 = load i8*, i8** %6
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %7, i32 %8)
%10 = tail call i64 @dup2(i32 %9, i32 3)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %3
%15 = icmp slt i32 %9, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %14
%19 = load i8*, i8** %6
%20 = tail call i32 @unlink(i8* %19)
store i8* null, i8** %6
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call i64 @execve(i8* %22, i8** %21, i8** %2)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = tail call i32 @exit(i32 0)
unreachable
}