#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*/ STDOUT_FILENO;
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ stdout;
int uncapture(int stdout_save) {
  if (stdout_save == (-1)) {
    return (-1);
  }
  if (freopen("NUL", "a", stdout) == NULL)
    return (-1);
  if (dup2(stdout_save, STDOUT_FILENO) == -1)
    return (-1);
  remove("NUL");
  return (0);
}


x86 assembly (clang, -O0):
<uncapture>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1151 <uncapture+0x21>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <uncapture+0x8c>
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
lea    0xe9f(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%rax
jne    1183 <uncapture+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <uncapture+0x8c>
mov    -0x8(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11a9 <uncapture+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <uncapture+0x8c>
lea    0xe50(%rip),%rdi
call   1030 <remove@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uncapture>:
push   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%edi
je     1183 <uncapture+0x53>
mov    %edi,%ebp
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
lea    0xeb1(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1050 <freopen@plt>
test   %rax,%rax
je     1183 <uncapture+0x53>
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1183 <uncapture+0x53>
lea    0xe84(%rip),%rdi
call   1030 <remove@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"NUL\00"
@.str.1 = [2 x i8] c"a\00"
@stdout = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @uncapture(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %7
6: ; preds = %1
store i32 -1, i32* %2
br label %20
7: ; preds = %1
%8 = load i32, i32* @stdout
%9 = call i32* @freopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %7
store i32 -1, i32* %2
br label %20
12: ; preds = %7
%13 = load i32, i32* %3
%14 = load i32, i32* @STDOUT_FILENO
%15 = call i32 @dup2(i32 %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %2
br label %20
18: ; preds = %12
%19 = call i32 @remove(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %20
20: ; preds = %18, %17, %11, %6
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@.str = [4 x i8] c"NUL\00"
@.str.1 = [2 x i8] c"a\00"
@stdout = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @uncapture(i32 %0) {
%2 = icmp eq i32 %0, -1
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = load i32, i32* @stdout
%5 = tail call i32* @freopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = load i32, i32* @STDOUT_FILENO
%9 = tail call i32 @dup2(i32 %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @remove(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %7, %3, %1, %11
%14 = phi i32 [ 0, %11 ], [ -1, %1 ], [ -1, %3 ], [ -1, %7 ]
ret i32 %14
}