#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*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
scalar_t__ close(int);
int reset_std_fd(int *, int /*<<< orphan*/);
int /*<<< orphan*/ unused(char *, int);
int reset_fd(int *fd) {
  if (*fd == -1)
    return 0;
  if (*fd == STDIN_FILENO || *fd == STDOUT_FILENO || *fd == STDERR_FILENO)
    return reset_std_fd(fd, (*fd == STDIN_FILENO) ? O_RDONLY : O_WRONLY);
  if (close(*fd))
    do {
      unused("close(%i) failed", *fd);
    } while (0);
  *fd = -1;
  return 0;
}


x86 assembly (clang, -O0):
<reset_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    1155 <reset_fd+0x25>
movl   $0x0,-0x4(%rbp)
jmp    1224 <reset_fd+0xf4>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%eax
je     1194 <reset_fd+0x64>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%eax
je     1194 <reset_fd+0x64>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%eax
jne    11e2 <reset_fd+0xb2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%eax
jne    11c2 <reset_fd+0x92>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11ce <reset_fd+0x9e>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <reset_std_fd@plt>
mov    %eax,-0x4(%rbp)
jmp    1224 <reset_fd+0xf4>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1213 <reset_fd+0xe3>
jmp    11fc <reset_fd+0xcc>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xdf7(%rip),%rdi
call   1050 <unused@plt>
jmp    1213 <reset_fd+0xe3>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reset_fd>:
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%edi
cmp    $0xffffffff,%edi
je     117c <reset_fd+0x4c>
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
cmp    %eax,%edi
je     1180 <reset_fd+0x50>
mov    0x2ea1(%rip),%rcx
cmp    (%rcx),%edi
je     1180 <reset_fd+0x50>
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%edi
je     1180 <reset_fd+0x50>
call   1040 <close@plt>
test   %rax,%rax
je     1176 <reset_fd+0x46>
mov    (%rbx),%esi
lea    0xe8f(%rip),%rdi
call   1050 <unused@plt>
movl   $0xffffffff,(%rbx)
xor    %eax,%eax
pop    %rbx
ret
cmp    %eax,%edi
jne    118d <reset_fd+0x5d>
mov    0x2e55(%rip),%rax
jmp    1194 <reset_fd+0x64>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <reset_std_fd@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str = [17 x i8] c"close(%i) failed\00"
define i32 @reset_fd(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
store i32* %0, i32** %3
%4 = load i32*, i32** %3
%5 = load i32, i32* %4
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 0, i32* %2
br label %49
8: ; preds = %1
%9 = load i32*, i32** %3
%10 = load i32, i32* %9
%11 = load i32, i32* @STDIN_FILENO
%12 = icmp eq i32 %10, %11
br i1 %12, label %23, label %13
13: ; preds = %8
%14 = load i32*, i32** %3
%15 = load i32, i32* %14
%16 = load i32, i32* @STDOUT_FILENO
%17 = icmp eq i32 %15, %16
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = load i32*, i32** %3
%20 = load i32, i32* %19
%21 = load i32, i32* @STDERR_FILENO
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %36
23: ; preds = %18, %13, %8
%24 = load i32*, i32** %3
%25 = load i32*, i32** %3
%26 = load i32, i32* %25
%27 = load i32, i32* @STDIN_FILENO
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = load i32, i32* @O_RDONLY
br label %33
31: ; preds = %23
%32 = load i32, i32* @O_WRONLY
br label %33
33: ; preds = %31, %29
%34 = phi i32 [ %30, %29 ], [ %32, %31 ]
%35 = call i32 @reset_std_fd(i32* %24, i32 %34)
store i32 %35, i32* %2
br label %49
36: ; preds = %18
%37 = load i32*, i32** %3
%38 = load i32, i32* %37
%39 = call i64 @close(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %36
br label %42
42: ; preds = %41
%43 = load i32*, i32** %3
%44 = load i32, i32* %43
%45 = call i32 @unused(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %42
br label %47
47: ; preds = %46, %36
%48 = load i32*, i32** %3
store i32 -1, i32* %48
store i32 0, i32* %2
br label %49
49: ; preds = %47, %33, %7
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str = [17 x i8] c"close(%i) failed\00"
define i32 @reset_fd(i32* %0) {
%2 = load i32, i32* %0
%3 = icmp eq i32 %2, -1
br i1 %3, label %25, label %4
4: ; preds = %1
%5 = load i32, i32* @STDIN_FILENO
%6 = icmp eq i32 %2, %5
%7 = load i32, i32* @STDOUT_FILENO
%8 = icmp eq i32 %2, %7
%9 = select i1 %6, i1 true, i1 %8
%10 = load i32, i32* @STDERR_FILENO
%11 = icmp eq i32 %2, %10
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %13, label %18
13: ; preds = %4
%14 = load i32, i32* @O_RDONLY
%15 = load i32, i32* @O_WRONLY
%16 = select i1 %6, i32 %14, i32 %15
%17 = tail call i32 @reset_std_fd(i32* %0, i32 %16)
br label %25
18: ; preds = %4
%19 = tail call i64 @close(i32 %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %0
%23 = tail call i32 @unused(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %21, %18
store i32 -1, i32* %0
br label %25
25: ; preds = %1, %24, %13
%26 = phi i32 [ %17, %13 ], [ 0, %24 ], [ 0, %1 ]
ret i32 %26
}