#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_GETFD;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
scalar_t__ fcntl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int setstdfds(void) {
  int fd;
  if (fcntl(2, F_GETFD) >= 0)
    return 0;
  if ((fd = open("/dev/null", O_RDWR)) >= 0)
    goto gotfd;
  if ((fd = open("/", O_RDONLY)) >= 0)
    goto gotfd;
  return -1;
gotfd:
  if (fd < 1)
    dup2(fd, 1);
  if (fd < 2)
    dup2(fd, 2);
  if (fd > 2)
    close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<setstdfds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edi
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jl     1171 <setstdfds+0x31>
movl   $0x0,-0x4(%rbp)
jmp    1210 <setstdfds+0xd0>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     1197 <setstdfds+0x57>
jmp    11c9 <setstdfds+0x89>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     11bd <setstdfds+0x7d>
jmp    11c9 <setstdfds+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <setstdfds+0xd0>
cmpl   $0x1,-0x8(%rbp)
jge    11e0 <setstdfds+0xa0>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11f7 <setstdfds+0xb7>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
cmpl   $0x2,-0x8(%rbp)
jle    1209 <setstdfds+0xc9>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setstdfds>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edi
call   1030 <fcntl@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    11c9 <setstdfds+0x89>
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0xe93(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1193 <setstdfds+0x53>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11c4 <setstdfds+0x84>
cmp    $0x1,%ebp
je     11a8 <setstdfds+0x68>
test   %ebp,%ebp
jne    11b6 <setstdfds+0x76>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
jmp    11c9 <setstdfds+0x89>
cmp    $0x3,%ebp
jb     11c9 <setstdfds+0x89>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11c9 <setstdfds+0x89>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
define i32 @setstdfds() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @F_GETFD
%4 = call i64 @fcntl(i32 2, i32 %3)
%5 = icmp sge i64 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 0, i32* %1
br label %37
7: ; preds = %0
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %7
br label %18
12: ; preds = %7
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 %14, i32* %2
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
br label %18
17: ; preds = %12
store i32 -1, i32* %1
br label %37
18: ; preds = %16, %11
%19 = load i32, i32* %2
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* %2
%23 = call i32 @dup2(i32 %22, i32 1)
br label %24
24: ; preds = %21, %18
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 2
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %2
%29 = call i32 @dup2(i32 %28, i32 2)
br label %30
30: ; preds = %27, %24
%31 = load i32, i32* %2
%32 = icmp sgt i32 %31, 2
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %30
store i32 0, i32* %1
br label %37
37: ; preds = %36, %17, %6
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
define i32 @setstdfds() {
%1 = load i32, i32* @F_GETFD
%2 = tail call i64 @fcntl(i32 2, i32 %1)
%3 = icmp sgt i64 %2, -1
br i1 %3, label %22, label %4
4: ; preds = %0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %22
12: ; preds = %8, %4
%13 = phi i32 [ %6, %4 ], [ %10, %8 ]
switch i32 %13, label %18 [
i32 0, label %14
i32 1, label %16
]
14: ; preds = %12
%15 = tail call i32 @dup2(i32 0, i32 1)
br label %16
16: ; preds = %12, %14
%17 = tail call i32 @dup2(i32 %13, i32 2)
br label %22
18: ; preds = %12
%19 = icmp ugt i32 %13, 2
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = tail call i32 @close(i32 %13)
br label %22
22: ; preds = %16, %18, %20, %8, %0
%23 = phi i32 [ 0, %0 ], [ -1, %8 ], [ 0, %20 ], [ 0, %18 ], [ 0, %16 ]
ret i32 %23
}