#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 /*<<< orphan*/ _IOFBF;
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
scalar_t__ setvbuf(int /*<<< orphan*/, char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stdout;
int capture(char *f_output) {
  fflush(stdout);
  int stdout_save = dup(STDOUT_FILENO);
  if (stdout_save == -1)
    return (-1);
  if (freopen("NUL", "a", stdout) == NULL)
    return (-1);
  if (setvbuf(stdout, f_output, _IOFBF, 1024))
    return (-1);
  return stdout_save;
}


x86 assembly (clang, -O0):
<capture>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
call   1030 <dup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1181 <capture+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <capture+0xaf>
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
lea    0xe6f(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1040 <freopen@plt>
cmp    $0x0,%rax
jne    11b3 <capture+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <capture+0xaf>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%ecx
call   1060 <setvbuf@plt>
cmp    $0x0,%rax
je     11e9 <capture+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <capture+0xaf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<capture>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e87(%rip),%r15
mov    (%r15),%edi
call   1050 <fflush@plt>
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
call   1030 <dup@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11ae <capture+0x6e>
mov    %eax,%ebp
mov    (%r15),%edx
lea    0xe83(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1040 <freopen@plt>
test   %rax,%rax
je     11ae <capture+0x6e>
mov    (%r15),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
mov    $0x400,%ecx
call   1060 <setvbuf@plt>
neg    %rax
sbb    %ebx,%ebx
or     %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [4 x i8] c"NUL\00"
@.str.1 = [2 x i8] c"a\00"
@_IOFBF = global i32 0
define i32 @capture(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i32, i32* @stdout
%6 = call i32 @fflush(i32 %5)
%7 = load i32, i32* @STDOUT_FILENO
%8 = call i32 @dup(i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %26
12: ; preds = %1
%13 = load i32, i32* @stdout
%14 = 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 %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -1, i32* %2
br label %26
17: ; preds = %12
%18 = load i32, i32* @stdout
%19 = load i8*, i8** %3
%20 = load i32, i32* @_IOFBF
%21 = call i64 @setvbuf(i32 %18, i8* %19, i32 %20, i32 1024)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 -1, i32* %2
br label %26
24: ; preds = %17
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %23, %16, %11
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@stdout = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [4 x i8] c"NUL\00"
@.str.1 = [2 x i8] c"a\00"
@_IOFBF = global i32 0
define i32 @capture(i8* %0) {
%2 = load i32, i32* @stdout
%3 = tail call i32 @fflush(i32 %2)
%4 = load i32, i32* @STDOUT_FILENO
%5 = tail call i32 @dup(i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %17, label %7
7: ; preds = %1
%8 = load i32, i32* @stdout
%9 = 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 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = load i32, i32* @stdout
%13 = load i32, i32* @_IOFBF
%14 = tail call i64 @setvbuf(i32 %12, i8* %0, i32 %13, i32 1024)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i32 %5, i32 -1
br label %17
17: ; preds = %11, %7, %1
%18 = phi i32 [ -1, %1 ], [ -1, %7 ], [ %16, %11 ]
ret i32 %18
}