#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ *stdout;
int main(int argc, char **argv, const char **envp) {
  FILE *out = stdout;
  if (argc > 1) {
    int fd = atoi(argv[argc - 1]);
    printf("inherited file descriptor %d\n", fd);
    out = fdopen(fd, "a");
    if (!out) {
      perror("fdopen");
      exit(1);
    }
  }
  fprintf(out, "Hello World (%s)!\n", argv[0]);
  fprintf(out, "envp[\'IN_EXECVE\'] = %s\n", getenv("IN_EXECVE"));
  fprintf(out, "000000000000000000000000000000000000000000000000000000000000000"
               "00000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 "
               "000000000000000000000000000000000000000000000000000000000000000"
               "000000000000000000000000000000 \n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1203 <main+0x93>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe3b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
lea    0xe4a(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fe <main+0x8e>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1203 <main+0x93>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xe27(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rdx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
jl     11d4 <main+0x54>
add    $0xffffffff,%edi
mov    (%r14,%rdi,8),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
lea    0xe5f(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xe6f(%rip),%rsi
mov    %ebx,%edi
call   1060 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11de <main+0x5e>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%rbx
mov    (%r14),%rdx
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe59(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe35(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe43(%rip),%rdi
mov    $0x5dd,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stdout = global i32* null
@.str = [30 x i8] c"inherited file descriptor %d\0A\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [7 x i8] c"fdopen\00"
@.str.3 = [19 x i8] c"Hello World (%s)!\0A\00"
@.str.4 = [24 x i8] c"envp['IN_EXECVE'] = %s\0A\00"
@.str.5 = [10 x i8] c"IN_EXECVE\00"
@.str.6 = [1502 x i8] c"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 \0A\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 i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%10 = load i32*, i32** @stdout
store i32* %10, i32** %8
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %31
13: ; preds = %3
%14 = load i8**, i8*** %6
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8*, i8** %14, i64 %17
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %9
%24 = call i32* @fdopen(i32 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %8
%25 = load i32*, i32** %8
%26 = icmp ne i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %13
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %13
br label %31
31: ; preds = %30, %3
%32 = load i32*, i32** %8
%33 = load i8**, i8*** %6
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i32*, i32** %8
%38 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = load i32*, i32** %8
%41 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([1502 x i8], [1502 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32* null
@.str = [30 x i8] c"inherited file descriptor %d\0A\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [7 x i8] c"fdopen\00"
@.str.3 = [19 x i8] c"Hello World (%s)!\0A\00"
@.str.4 = [24 x i8] c"envp['IN_EXECVE'] = %s\0A\00"
@.str.5 = [10 x i8] c"IN_EXECVE\00"
@.str.6 = [1502 x i8] c"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 \0A\00"
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture readnone %2) {
%4 = load i32*, i32** @stdout
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = add nsw i32 %0, -1
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i8*, i8** %1, i64 %8
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = tail call i32* @fdopen(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %6
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %6, %3
%19 = phi i32* [ %4, %3 ], [ %13, %6 ]
%20 = load i8*, i8** %1
%21 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %20)
%22 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%23 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %22)
%24 = tail call i64 @fwrite(i8* getelementptr inbounds ([1502 x i8], [1502 x i8]* @.str.6, i64 0, i64 0), i64 1501, i64 1, i32* %19)
ret i32 0
}