#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 */
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
int open(char const *, int, int /*<<< orphan*/);
int fd_reopen(int desired_fd, char const *file, int flags, mode_t mode) {
  int fd;
  close(desired_fd);
  fd = open(file, flags, mode);
  if (fd == desired_fd || fd < 0)
    return fd;
  else {
    int fd2 = fcntl(fd, F_DUPFD, desired_fd);
    int saved_errno = errno;
    close(fd);
    errno = saved_errno;
    return fd2;
  }
}


x86 assembly (clang, -O0):
<fd_reopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1175 <fd_reopen+0x45>
cmpl   $0x0,-0x1c(%rbp)
jge    1180 <fd_reopen+0x50>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bd <fd_reopen+0x8d>
mov    -0x1c(%rbp),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e23(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_reopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%ebx
call   1040 <close@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1050 <open@plt>
mov    %eax,%ebp
cmp    %ebx,%eax
je     118d <fd_reopen+0x5d>
test   %ebp,%ebp
js     118d <fd_reopen+0x5d>
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <fcntl@plt>
mov    %eax,%r15d
mov    0x2e5d(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,(%r14)
mov    %r15d,%eax
jmp    118f <fd_reopen+0x5f>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@F_DUPFD = global i32 0
@errno = global i32 0
define i32 @fd_reopen(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i32 @close(i32 %0)
%6 = tail call i32 @open(i8* %1, i32 %2, i32 %3)
%7 = icmp eq i32 %6, %0
%8 = icmp slt i32 %6, 0
%9 = or i1 %7, %8
br i1 %9, label %15, label %10
10: ; preds = %4
%11 = load i32, i32* @F_DUPFD
%12 = tail call i32 @fcntl(i32 %6, i32 %11, i32 %0)
%13 = load i32, i32* @errno
%14 = tail call i32 @close(i32 %6)
store i32 %13, i32* @errno
br label %15
15: ; preds = %4, %10
%16 = phi i32 [ %12, %10 ], [ %6, %4 ]
ret i32 %16
}