#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 */
scalar_t__ close(int);
int /*<<< orphan*/ die(char *);
scalar_t__ dup2(int, int);
int open(char const *, int, int);
void open_dup2(const char *filename, int flags, int newfd) {
  int fd = open(filename, flags, 0755);
  if (fd < 0)
    die("open");
  if (fd != newfd) {
    if (dup2(fd, newfd) < 0)
      die("dup2");
    if (close(fd) < 0)
      die("close");
  }
}


x86 assembly (clang, -O0):
<open_dup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x1ed,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    117c <open_dup2+0x3c>
lea    0xe89(%rip),%rdi
call   1060 <die@plt>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     11cc <open_dup2+0x8c>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    11a9 <open_dup2+0x69>
lea    0xe61(%rip),%rdi
call   1060 <die@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    11c7 <open_dup2+0x87>
lea    0xe48(%rip),%rdi
call   1060 <die@plt>
jmp    11cc <open_dup2+0x8c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_dup2>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    $0x1ed,%edx
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     115b <open_dup2+0x1b>
cmp    %ebp,%ebx
jne    116b <open_dup2+0x2b>
jmp    1191 <open_dup2+0x51>
lea    0xe9e(%rip),%rdi
call   1060 <die@plt>
cmp    %ebp,%ebx
je     1191 <open_dup2+0x51>
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <dup2@plt>
test   %rax,%rax
jns    1185 <open_dup2+0x45>
lea    0xe85(%rip),%rdi
call   1060 <die@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
js     1198 <open_dup2+0x58>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe6b(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <die@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"dup2\00"
@.str.2 = [6 x i8] c"close\00"
define void @open_dup2(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = call i32 @open(i8* %8, i32 %9, i32 493)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %3
%16 = load i32, i32* %7
%17 = load i32, i32* %6
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %33
19: ; preds = %15
%20 = load i32, i32* %7
%21 = load i32, i32* %6
%22 = call i64 @dup2(i32 %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
%27 = load i32, i32* %7
%28 = call i64 @close(i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
br label %33
33: ; preds = %32, %15
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"dup2\00"
@.str.2 = [6 x i8] c"close\00"
define void @open_dup2(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32 @open(i8* %0, i32 %1, i32 493)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = icmp eq i32 %4, %2
br i1 %9, label %20, label %10
10: ; preds = %8
%11 = tail call i64 @dup2(i32 %4, i32 %2)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = tail call i64 @close(i32 %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %15, %18, %8
ret void
}