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


x86 assembly (clang, -O0):
<mx_dup2_close_both>:
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    1156 <mx_dup2_close_both+0x26>
movl   $0x0,-0x4(%rbp)
jmp    11d2 <mx_dup2_close_both+0xa2>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <mx_dup2_close_new@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1179 <mx_dup2_close_both+0x49>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <mx_dup2_close_both+0xa2>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11cc <mx_dup2_close_both+0x9c>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%eax
jne    11cc <mx_dup2_close_both+0x9c>
xor    %eax,%eax
mov    0x2e1e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <mx_dup2_close_both+0xa2>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mx_dup2_close_both>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
cmp    %esi,%edi
je     117a <mx_dup2_close_both+0x4a>
mov    %esi,%ebx
mov    %edi,%ebp
call   1050 <mx_dup2_close_new@plt>
test   %eax,%eax
js     117a <mx_dup2_close_both+0x4a>
xor    %edi,%edi
cmp    %ebx,%eax
sete   %dil
call   1040 <assert@plt>
mov    %ebp,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    1178 <mx_dup2_close_both+0x48>
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    0x2e69(%rip),%rcx
mov    %eax,%edx
neg    %edx
cmp    (%rcx),%eax
cmove  %edx,%ebx
mov    %ebx,%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_both(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 %38
11: ; preds = %2
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = call i32 @mx_dup2_close_new(i32 %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i32, i32* %6
store i32 %18, i32* %3
br label %38
19: ; preds = %11
%20 = load i32, i32* %6
%21 = load i32, i32* %5
%22 = icmp eq i32 %20, %21
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %19
%30 = load i32, i32* @errno
%31 = load i32, i32* @EBADF
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %3
br label %38
36: ; preds = %29, %19
%37 = load i32, i32* %5
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %33, %17, %10
%39 = load i32, i32* %3
ret i32 %39
}

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