#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 EBUSY;
int EINVAL;
int ENOSYS;
int FD_CLOEXEC;
int F_SETFD;
int O_CLOEXEC;
int __syscall(int, int, int, ...);
int __syscall_ret(int);
int __dup3(int old, int new, int flags) {
  int r;
  if (old == new)
    return __syscall_ret(-EINVAL);
  if (flags & O_CLOEXEC) {
    while ((r = __syscall(358, old, new, flags)) == -EBUSY)
      ;
    if (r != -ENOSYS)
      return __syscall_ret(r);
  }
  while ((r = __syscall(63, old, new)) == -EBUSY)
    ;
  if (flags & O_CLOEXEC)
    __syscall(55, new, F_SETFD, FD_CLOEXEC);
  return __syscall_ret(r);
}


x86 assembly (clang, -O0):
<__dup3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1155 <__dup3+0x35>
xor    %edi,%edi
mov    0x2e82(%rip),%rax
sub    (%rax),%edi
call   1040 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
jmp    123d <__dup3+0x11d>
mov    -0x10(%rbp),%eax
mov    0x2e51(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11ca <__dup3+0xaa>
jmp    116f <__dup3+0x4f>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    $0x166,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    %eax,-0x14(%rbp)
xor    %ecx,%ecx
mov    0x2e30(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    119f <__dup3+0x7f>
jmp    116f <__dup3+0x4f>
mov    -0x14(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e3d(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
je     11c5 <__dup3+0xa5>
mov    -0x14(%rbp),%edi
call   1040 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
jmp    123d <__dup3+0x11d>
jmp    11ca <__dup3+0xaa>
jmp    11cf <__dup3+0xaf>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    $0x3f,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    %eax,-0x14(%rbp)
xor    %ecx,%ecx
mov    0x2dd3(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    11fc <__dup3+0xdc>
jmp    11cf <__dup3+0xaf>
mov    -0x10(%rbp),%eax
mov    0x2daa(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1232 <__dup3+0x112>
mov    -0xc(%rbp),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    0x2dac(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    -0x14(%rbp),%edi
call   1040 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__dup3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    %esi,%edi
jne    113f <__dup3+0x1f>
mov    0x2e92(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11e2 <__dup3+0xc2>
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%ebp
mov    0x2e62(%rip),%r12
test   %edx,(%r12)
je     118b <__dup3+0x6b>
mov    0x2e65(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    $0x166,%edi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    %eax,%ebx
mov    0x0(%r13),%eax
add    %ebx,%eax
je     1160 <__dup3+0x40>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
add    %ebx,%eax
jne    11e2 <__dup3+0xc2>
mov    0x2e2e(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x3f,%edi
mov    %ebp,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    %eax,%ebx
mov    0x0(%r13),%eax
add    %ebx,%eax
je     11a0 <__dup3+0x80>
test   %r14d,(%r12)
je     11e2 <__dup3+0xc2>
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    0x2dff(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    %r15d,%esi
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <__syscall_ret@plt>

LLVM IR (-O0):
@EINVAL = global i32 0
@O_CLOEXEC = global i32 0
@EBUSY = global i32 0
@ENOSYS = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @__dup3(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32, i32* %6
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
%15 = call i32 @__syscall_ret(i32 %14)
store i32 %15, i32* %4
br label %62
16: ; preds = %3
%17 = load i32, i32* %7
%18 = load i32, i32* @O_CLOEXEC
%19 = and i32 %17, %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %40
21: ; preds = %16
br label %22
22: ; preds = %30, %21
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = call i32 (i32, i32, i32, ...) @__syscall(i32 358, i32 %23, i32 %24, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* @EBUSY
%28 = sub nsw i32 0, %27
%29 = icmp eq i32 %26, %28
br i1 %29, label %30, label %31
30: ; preds = %22
br label %22
31: ; preds = %22
%32 = load i32, i32* %8
%33 = load i32, i32* @ENOSYS
%34 = sub nsw i32 0, %33
%35 = icmp ne i32 %32, %34
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* %8
%38 = call i32 @__syscall_ret(i32 %37)
store i32 %38, i32* %4
br label %62
39: ; preds = %31
br label %40
40: ; preds = %39, %16
br label %41
41: ; preds = %48, %40
%42 = load i32, i32* %5
%43 = load i32, i32* %6
%44 = call i32 (i32, i32, i32, ...) @__syscall(i32 63, i32 %42, i32 %43)
store i32 %44, i32* %8
%45 = load i32, i32* @EBUSY
%46 = sub nsw i32 0, %45
%47 = icmp eq i32 %44, %46
br i1 %47, label %48, label %49
48: ; preds = %41
br label %41
49: ; preds = %41
%50 = load i32, i32* %7
%51 = load i32, i32* @O_CLOEXEC
%52 = and i32 %50, %51
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %49
%55 = load i32, i32* %6
%56 = load i32, i32* @F_SETFD
%57 = load i32, i32* @FD_CLOEXEC
%58 = call i32 (i32, i32, i32, ...) @__syscall(i32 55, i32 %55, i32 %56, i32 %57)
br label %59
59: ; preds = %54, %49
%60 = load i32, i32* %8
%61 = call i32 @__syscall_ret(i32 %60)
store i32 %61, i32* %4
br label %62
62: ; preds = %59, %36, %12
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@EINVAL = global i32 0
@O_CLOEXEC = global i32 0
@EBUSY = global i32 0
@ENOSYS = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @__dup3(i32 %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %0, %1
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = load i32, i32* @EINVAL
%7 = sub nsw i32 0, %6
br label %35
8: ; preds = %3
%9 = load i32, i32* @O_CLOEXEC
%10 = and i32 %9, %2
%11 = icmp eq i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %8, %12
%13 = tail call i32 (i32, i32, i32, ...) @__syscall(i32 358, i32 %0, i32 %1, i32 %2)
%14 = load i32, i32* @EBUSY
%15 = sub nsw i32 0, %14
%16 = icmp eq i32 %13, %15
br i1 %16, label %12, label %17
17: ; preds = %12
%18 = load i32, i32* @ENOSYS
%19 = sub nsw i32 0, %18
%20 = icmp eq i32 %13, %19
br i1 %20, label %21, label %35
21: ; preds = %17, %8
br label %22
22: ; preds = %21, %22
%23 = tail call i32 (i32, i32, i32, ...) @__syscall(i32 63, i32 %0, i32 %1)
%24 = load i32, i32* @EBUSY
%25 = sub nsw i32 0, %24
%26 = icmp eq i32 %23, %25
br i1 %26, label %22, label %27
27: ; preds = %22
%28 = load i32, i32* @O_CLOEXEC
%29 = and i32 %28, %2
%30 = icmp eq i32 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i32, i32* @F_SETFD
%33 = load i32, i32* @FD_CLOEXEC
%34 = tail call i32 (i32, i32, i32, ...) @__syscall(i32 55, i32 %1, i32 %32, i32 %33)
br label %35
35: ; preds = %27, %31, %17, %5
%36 = phi i32 [ %7, %5 ], [ %13, %17 ], [ %23, %31 ], [ %23, %27 ]
%37 = tail call i32 @__syscall_ret(i32 %36)
ret i32 %37
}