#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 dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void dup2_msg(int f1, int f2) {
  if (dup2(f1, f2) == -1) {
    fprintf(stderr, "Failed to dup2() fd %d to fd %d: %s\n", f1, f2,
            strerror(errno));
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<dup2_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11aa <dup2_msg+0x6a>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    %rax,%r8
lea    0xe65(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup2_msg>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%ebp
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1157 <dup2_msg+0x17>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e82(%rip),%rax
mov    (%rax),%r14d
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe8a(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %rax,%r8
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Failed to dup2() fd %d to fd %d: %s\0A\00"
@errno = global i32 0
define void @dup2_msg(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%5 = load i32, i32* %3
%6 = load i32, i32* %4
%7 = call i32 @dup2(i32 %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12, i8* %14)
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Failed to dup2() fd %d to fd %d: %s\0A\00"
@errno = global i32 0
define void @dup2_msg(i32 %0, i32 %1) {
%3 = tail call i32 @dup2(i32 %0, i32 %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1, i8* %8)
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
ret void
}