#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 /*<<< orphan*/ F_DUPFD;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int redup_clean_fd(int fd) {
  int newfd;
  if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO)
    return fd;
  newfd = fcntl(fd, F_DUPFD, 3);
  if (newfd < 0) {
    fprintf(stderr, "dupfd IO: %s\n", strerror(errno));
    exit(1);
  }
  close(fd);
  return newfd;
}


x86 assembly (clang, -O0):
<redup_clean_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%eax
je     119c <redup_clean_fd+0x4c>
mov    -0x8(%rbp),%eax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%eax
je     119c <redup_clean_fd+0x4c>
mov    -0x8(%rbp),%eax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%eax
je     119c <redup_clean_fd+0x4c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1203 <redup_clean_fd+0xb3>
mov    -0x8(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    $0x3,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11f5 <redup_clean_fd+0xa5>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redup_clean_fd>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e5c(%rip),%rax
cmp    %edi,(%rax)
je     1176 <redup_clean_fd+0x26>
mov    0x2e89(%rip),%rax
cmp    %ebx,(%rax)
je     1176 <redup_clean_fd+0x26>
mov    0x2e4e(%rip),%rax
cmp    %ebx,(%rax)
jne    119a <redup_clean_fd+0x4a>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     11a3 <redup_clean_fd+0x53>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e26(%rip),%rax
mov    (%rax),%ebx
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe3f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@F_DUPFD = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"dupfd IO: %s\0A\00"
@errno = global i32 0
define i32 @redup_clean_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @STDIN_FILENO
%7 = icmp ne i32 %5, %6
br i1 %7, label %8, label %18
8: ; preds = %1
%9 = load i32, i32* %3
%10 = load i32, i32* @STDOUT_FILENO
%11 = icmp ne i32 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = load i32, i32* %3
%14 = load i32, i32* @STDERR_FILENO
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* %3
store i32 %17, i32* %2
br label %34
18: ; preds = %12, %8, %1
%19 = load i32, i32* %3
%20 = load i32, i32* @F_DUPFD
%21 = call i32 @fcntl(i32 %19, i32 %20, i32 3)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %18
%31 = load i32, i32* %3
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %4
store i32 %33, i32* %2
br label %34
34: ; preds = %30, %16
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@F_DUPFD = global i32 0
@stderr = global i32 0
@.str = [14 x i8] c"dupfd IO: %s\0A\00"
@errno = global i32 0
define i32 @redup_clean_fd(i32 %0) {
%2 = load i32, i32* @STDIN_FILENO
%3 = icmp eq i32 %2, %0
%4 = load i32, i32* @STDOUT_FILENO
%5 = icmp eq i32 %4, %0
%6 = select i1 %3, i1 true, i1 %5
%7 = load i32, i32* @STDERR_FILENO
%8 = icmp eq i32 %7, %0
%9 = select i1 %6, i1 true, i1 %8
br i1 %9, label %10, label %22
10: ; preds = %1
%11 = load i32, i32* @F_DUPFD
%12 = tail call i32 @fcntl(i32 %0, i32 %11, i32 3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = tail call i32 @close(i32 %0)
br label %22
22: ; preds = %1, %20
%23 = phi i32 [ %12, %20 ], [ %0, %1 ]
ret i32 %23
}