#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 close(int);
int dup2(int, int);
int open(char *, int);
int /*<<< orphan*/ unused(char *, ...);
int reset_std_fd(int *fd, int flags) {
  int rc = -1;
  int null_fd = open("/dev/null", flags);
  if (null_fd == -1) {
    do {
      unused("open() failed");
    } while (0);
    goto _out;
  }
  if (dup2(null_fd, *fd) == -1) {
    do {
      unused("dup2(%i, %i) failed", null_fd, *fd);
    } while (0);
    goto _out_close_null_fd;
  }
  *fd = -1;
  rc = 0;
_out_close_null_fd:
  if (close(null_fd) == -1)
    do {
      unused("close(%i) failed", null_fd);
    } while (0);
_out:
  return rc;
}


x86 assembly (clang, -O0):
<reset_std_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0xc(%rbp),%esi
lea    0xea0(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    118a <reset_std_fd+0x4a>
jmp    1177 <reset_std_fd+0x37>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1050 <unused@plt>
jmp    1204 <reset_std_fd+0xc4>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11c2 <reset_std_fd+0x82>
jmp    11a6 <reset_std_fd+0x66>
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <unused@plt>
jmp    11d3 <reset_std_fd+0x93>
mov    -0x8(%rbp),%rax
movl   $0xffffffff,(%rax)
movl   $0x0,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11ff <reset_std_fd+0xbf>
jmp    11e9 <reset_std_fd+0xa9>
mov    -0x14(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <unused@plt>
jmp    11ff <reset_std_fd+0xbf>
jmp    1204 <reset_std_fd+0xc4>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reset_std_fd>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbp
lea    0xeb3(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1173 <reset_std_fd+0x33>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
mov    %eax,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1188 <reset_std_fd+0x48>
movl   $0xffffffff,0x0(%rbp)
xor    %ebp,%ebp
jmp    11a0 <reset_std_fd+0x60>
lea    0xe90(%rip),%rdi
xor    %eax,%eax
call   1050 <unused@plt>
mov    $0xffffffff,%ebp
jmp    11bc <reset_std_fd+0x7c>
mov    0x0(%rbp),%edx
lea    0xe86(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <unused@plt>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11bc <reset_std_fd+0x7c>
lea    0xe79(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <unused@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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