#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__ EBADF;
int /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int dup_two(int oldfd, int newfd) {
  int is_exist;
  if ((is_exist = fcntl(oldfd, F_GETFL)) == -1) {
    if (errno == EBADF) {
      close(oldfd);
    }
  }
  newfd = fcntl(oldfd, F_DUPFD, 3);
  return newfd;
}


x86 assembly (clang, -O0):
<dup_two>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1174 <dup_two+0x54>
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
jne    116f <dup_two+0x4f>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
jmp    1174 <dup_two+0x54>
mov    -0x4(%rbp),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    $0x3,%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup_two>:
push   %rbx
mov    %edi,%ebx
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1155 <dup_two+0x35>
mov    0x2e99(%rip),%rax
mov    (%rax),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
jne    1155 <dup_two+0x35>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x3,%edx
xor    %eax,%eax
pop    %rbx
jmp    1030 <fcntl@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@F_DUPFD = global i32 0
define i32 @dup_two(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = load i32, i32* @F_GETFL
%8 = call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %7)
store i32 %8, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = load i64, i64* @EBADF
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* %3
%16 = call i32 @close(i32 %15)
br label %17
17: ; preds = %14, %10
br label %18
18: ; preds = %17, %2
%19 = load i32, i32* %3
%20 = load i32, i32* @F_DUPFD
%21 = call i32 (i32, i32, ...) @fcntl(i32 %19, i32 %20, i32 3)
store i32 %21, i32* %4
%22 = load i32, i32* %4
ret i32 %22
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@F_DUPFD = global i32 0
define i32 @dup_two(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFL
%4 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = load i64, i64* @errno
%8 = load i64, i64* @EBADF
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @close(i32 %0)
br label %12
12: ; preds = %6, %10, %2
%13 = load i32, i32* @F_DUPFD
%14 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %13, i32 3)
ret i32 %14
}