#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ header(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
do_exec(char *prog, int fd) {
  FILE *fp;
  fp = fdopen(fd, "w");
  header(fp, NULL);
  fflush(fp);
  dup2(fd, 1);
  dup2(fd, 2);
  fclose(fp);
  close(fd);
  execl(prog, prog, NULL);
  perror(prog);
}


x86 assembly (clang, -O0):
<do_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xe67(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <header@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <execl@plt>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_exec>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xe70(%rip),%rsi
mov    %ebp,%edi
call   1080 <fdopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <header@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   10a0 <execl@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <perror@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
define i32 @do_exec(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = call i32 @header(i32* %9, i32* null)
%11 = load i32*, i32** %6
%12 = call i32 @fflush(i32* %11)
%13 = load i32, i32* %5
%14 = call i32 @dup2(i32 %13, i32 1)
%15 = load i32, i32* %5
%16 = call i32 @dup2(i32 %15, i32 2)
%17 = load i32*, i32** %6
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* %5
%20 = call i32 @close(i32 %19)
%21 = load i8*, i8** %4
%22 = load i8*, i8** %4
%23 = call i32 @execl(i8* %21, i8* %22, i32* null)
%24 = load i8*, i8** %4
%25 = call i32 @perror(i8* %24)
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
define i32 @do_exec(i8* %0, i32 %1) {
%3 = tail call i32* @fdopen(i32 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @header(i32* %3, i32* null)
%5 = tail call i32 @fflush(i32* %3)
%6 = tail call i32 @dup2(i32 %1, i32 1)
%7 = tail call i32 @dup2(i32 %1, i32 2)
%8 = tail call i32 @fclose(i32* %3)
%9 = tail call i32 @close(i32 %1)
%10 = tail call i32 @execl(i8* %0, i8* %0, i32* null)
%11 = tail call i32 @perror(i8* %0)
ret i32 undef
}