#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*/ STDERR_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ die(char *, int /*<<< orphan*/, char *);
scalar_t__ dup2(int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
void exec(void *args) {
  char **cmd = args;
  if (dup2(STDERR_FILENO, STDOUT_FILENO) != STDOUT_FILENO)
    die("failed to redirect stdout to stderr", errno, NULL);
  execvp(cmd[0], cmd);
  die("failed to run command", errno, cmd[0]);
}


x86 assembly (clang, -O0):
<exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e9c(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2e8d(%rip),%rcx
cmp    (%rcx),%rax
je     1185 <exec+0x55>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <die@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <execvp@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe78(%rip),%rdi
call   1040 <die@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
mov    0x2ea9(%rip),%r14
mov    (%r14),%rsi
call   1030 <dup2@plt>
cmp    (%r14),%rax
je     116b <exec+0x3b>
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
xor    %edx,%edx
call   1040 <die@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1050 <execvp@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    (%rbx),%rdx
lea    0xe9b(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <die@plt>

LLVM IR (-O0):
@STDERR_FILENO = global i32 0
@STDOUT_FILENO = global i64 0
@.str = [36 x i8] c"failed to redirect stdout to stderr\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"failed to run command\00"
define void @exec(i8* %0) {
%2 = alloca i8*
%3 = alloca i8**
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = bitcast i8* %4 to i8**
store i8** %5, i8*** %3
%6 = load i32, i32* @STDERR_FILENO
%7 = load i64, i64* @STDOUT_FILENO
%8 = call i64 @dup2(i32 %6, i64 %7)
%9 = load i64, i64* @STDOUT_FILENO
%10 = icmp ne i64 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %12, i8* null)
br label %14
14: ; preds = %11, %1
%15 = load i8**, i8*** %3
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = load i8**, i8*** %3
%19 = call i32 @execvp(i8* %17, i8** %18)
%20 = load i32, i32* @errno
%21 = load i8**, i8*** %3
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %23)
ret void
}

LLVM IR (-O2):
@STDERR_FILENO = global i32 0
@STDOUT_FILENO = global i64 0
@.str = [36 x i8] c"failed to redirect stdout to stderr\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"failed to run command\00"
define void @exec(i8* %0) {
%2 = bitcast i8* %0 to i8**
%3 = load i32, i32* @STDERR_FILENO
%4 = load i64, i64* @STDOUT_FILENO
%5 = tail call i64 @dup2(i32 %3, i64 %4)
%6 = load i64, i64* @STDOUT_FILENO
%7 = icmp eq i64 %5, %6
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = tail call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %9, i8* null)
br label %11
11: ; preds = %8, %1
%12 = load i8*, i8** %2
%13 = tail call i32 @execvp(i8* %12, i8** %2)
%14 = load i32, i32* @errno
%15 = load i8*, i8** %2
%16 = tail call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %14, i8* %15)
ret void
}