#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 dup(int);
int /*<<< orphan*/ execv(char *const, char *const *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int main(int argc, const char **argv, const char **envp) {
  int newfd = dup(1);
  char fd_argv[4];
  snprintf(fd_argv, 4, "%d", newfd);
  char *const new_argv[] = {"./exec_victim", fd_argv, NULL};
  setenv("IN_EXECVE", "1", 1);
  execv(new_argv[0], new_argv);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x1,%edi
call   1050 <dup@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x4,%esi
lea    0xe86(%rip),%rdx
call   1040 <snprintf@plt>
lea    0xe7d(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe70(%rip),%rdi
lea    0xe73(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1060 <execv@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    $0x1,%edi
call   1050 <dup@plt>
lea    0xea8(%rip),%rdx
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %eax,%ecx
call   1040 <snprintf@plt>
lea    0xe90(%rip),%r14
mov    %r14,0x10(%rsp)
mov    %rbx,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0xe84(%rip),%rdi
lea    0xe87(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1060 <execv@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [14 x i8] c"./exec_victim\00"
@.str.2 = [10 x i8] c"IN_EXECVE\00"
@.str.3 = [2 x i8] c"1\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca [4 x i8]
%10 = alloca [3 x i8*]
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%11 = call i32 @dup(i32 1)
store i32 %11, i32* %8
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%13 = load i32, i32* %8
%14 = call i32 @snprintf(i8* %12, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
store i8* %17, i8** %16
%18 = getelementptr inbounds i8*, i8** %16, i64 1
store i8* null, i8** %18
%19 = call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%20 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%21 = load i8*, i8** %20
%22 = getelementptr inbounds [3 x i8*], [3 x i8*]* %10, i64 0, i64 0
%23 = call i32 @execv(i8* %21, i8** %22)
ret i32 1
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [14 x i8] c"./exec_victim\00"
@.str.2 = [10 x i8] c"IN_EXECVE\00"
@.str.3 = [2 x i8] c"1\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = alloca [4 x i8]
%5 = alloca [3 x i8*]
%6 = tail call i32 @dup(i32 1)
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %6)
%9 = bitcast [3 x i8*]* %5 to i8*
%10 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 1
store i8* %7, i8** %11
%12 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 2
store i8* null, i8** %12
%13 = call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%14 = call i32 @execv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8** %10)
ret i32 1
}