#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 EBADF;
int close(int);
int dup2(int, int);
int errno;
int mx_dup2_close_new(int oldfd, int newfd) {
  int res;
  if (oldfd == newfd)
    return 0;
  res = close(newfd);
  if (res == -1 && errno == EBADF)
    return -errno;
  res = dup2(oldfd, newfd);
  if (res == -1)
    return -errno;
  return res;
}


x86 assembly (clang, -O0):
<mx_dup2_close_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1146 <mx_dup2_close_new+0x26>
movl   $0x0,-0x4(%rbp)
jmp    11b7 <mx_dup2_close_new+0x97>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1186 <mx_dup2_close_new+0x66>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%eax
jne    1186 <mx_dup2_close_new+0x66>
xor    %eax,%eax
mov    0x2e64(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <mx_dup2_close_new+0x97>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <dup2@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11b1 <mx_dup2_close_new+0x91>
xor    %eax,%eax
mov    0x2e39(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <mx_dup2_close_new+0x97>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mx_dup2_close_new>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
cmp    %esi,%edi
je     116a <mx_dup2_close_new+0x4a>
mov    %esi,%ebx
mov    %edi,%ebp
mov    %esi,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1151 <mx_dup2_close_new+0x31>
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
mov    0x2e8f(%rip),%rcx
cmp    (%rcx),%eax
jne    1151 <mx_dup2_close_new+0x31>
neg    %eax
jmp    116a <mx_dup2_close_new+0x4a>
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    116a <mx_dup2_close_new+0x4a>
mov    0x2e7a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EBADF = global i32 0
define i32 @mx_dup2_close_new(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = load i32, i32* %5
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 0, i32* %3
br label %34
11: ; preds = %2
%12 = load i32, i32* %5
%13 = call i32 @close(i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = load i32, i32* @errno
%18 = load i32, i32* @EBADF
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
store i32 %22, i32* %3
br label %34
23: ; preds = %16, %11
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = call i32 @dup2(i32 %24, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @errno
%31 = sub nsw i32 0, %30
store i32 %31, i32* %3
br label %34
32: ; preds = %23
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %29, %20, %10
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@errno = global i32 0
@EBADF = global i32 0
define i32 @mx_dup2_close_new(i32 %0, i32 %1) {
%3 = icmp eq i32 %0, %1
br i1 %3, label %19, label %4
4: ; preds = %2
%5 = tail call i32 @close(i32 %1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %13
7: ; preds = %4
%8 = load i32, i32* @errno
%9 = load i32, i32* @EBADF
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = sub nsw i32 0, %8
br label %19
13: ; preds = %7, %4
%14 = tail call i32 @dup2(i32 %0, i32 %1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
br label %19
19: ; preds = %13, %2, %16, %11
%20 = phi i32 [ %12, %11 ], [ %18, %16 ], [ 0, %2 ], [ %14, %13 ]
ret i32 %20
}