#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
FILE *reopen_term_stdout(void) {
  FILE *fp;
  int outfd, ttyfd;
  outfd = dup(STDOUT_FILENO);
  if (outfd == -1) {
    fprintf(stderr, "Failed to store original output stream.");
    return NULL;
  }
  fp = fdopen(outfd, "w");
  if (fp == NULL) {
    fprintf(stderr, "Failed to open original output stream.");
    return NULL;
  }
  ttyfd = open("/dev/tty", O_WRONLY);
  if (ttyfd == -1) {
    fclose(fp);
    fprintf(stderr, "Failed to open terminal for output.");
    return NULL;
  }
  if (dup2(ttyfd, STDOUT_FILENO) == -1) {
    close(ttyfd);
    fclose(fp);
    fprintf(stderr, "Failed to setup terminal as standard output stream.");
    return NULL;
  }
  close(ttyfd);
  return fp;
}


x86 assembly (clang, -O0):
<reopen_term_stdout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1050 <dup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b5 <reopen_term_stdout+0x45>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    129f <reopen_term_stdout+0x12f>
mov    -0x14(%rbp),%edi
lea    0xe69(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f5 <reopen_term_stdout+0x85>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    129f <reopen_term_stdout+0x12f>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1242 <reopen_term_stdout+0xd2>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    129f <reopen_term_stdout+0x12f>
mov    -0x18(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    128f <reopen_term_stdout+0x11f>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    129f <reopen_term_stdout+0x12f>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reopen_term_stdout>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e75(%rip),%r14
mov    (%r14),%edi
call   1050 <dup@plt>
cmp    $0xffffffff,%eax
je     11dd <reopen_term_stdout+0x6d>
lea    0xe99(%rip),%rsi
mov    %eax,%edi
call   1080 <fdopen@plt>
test   %rax,%rax
je     11e6 <reopen_term_stdout+0x76>
mov    %rax,%rbx
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xea3(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     11ef <reopen_term_stdout+0x7f>
mov    %eax,%ebp
mov    (%r14),%esi
mov    %eax,%edi
call   1040 <dup2@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%r14d
jne    1211 <reopen_term_stdout+0xa1>
lea    0xea3(%rip),%r14
jmp    11f6 <reopen_term_stdout+0x86>
lea    0xe1c(%rip),%r14
jmp    11fe <reopen_term_stdout+0x8e>
lea    0xe3d(%rip),%r14
jmp    11fe <reopen_term_stdout+0x8e>
lea    0xe64(%rip),%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1070 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Failed to store original output stream.\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [39 x i8] c"Failed to open original output stream.\00"
@.str.3 = [9 x i8] c"/dev/tty\00"
@O_WRONLY = global i32 0
@.str.4 = [36 x i8] c"Failed to open terminal for output.\00"
@.str.5 = [52 x i8] c"Failed to setup terminal as standard output stream.\00"
define i32* @reopen_term_stdout() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @STDOUT_FILENO
%6 = call i32 @dup(i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %1
br label %46
12: ; preds = %0
%13 = load i32, i32* %3
%14 = call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %2
%15 = load i32*, i32** %2
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
store i32* null, i32** %1
br label %46
20: ; preds = %12
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %21)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32*, i32** %2
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
store i32* null, i32** %1
br label %46
30: ; preds = %20
%31 = load i32, i32* %4
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %42
35: ; preds = %30
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
%38 = load i32*, i32** %2
%39 = call i32 @fclose(i32* %38)
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0))
store i32* null, i32** %1
br label %46
42: ; preds = %30
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
%45 = load i32*, i32** %2
store i32* %45, i32** %1
br label %46
46: ; preds = %42, %35, %25, %17, %9
%47 = load i32*, i32** %1
ret i32* %47
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"Failed to store original output stream.\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [39 x i8] c"Failed to open original output stream.\00"
@.str.3 = [9 x i8] c"/dev/tty\00"
@O_WRONLY = global i32 0
@.str.4 = [36 x i8] c"Failed to open terminal for output.\00"
@.str.5 = [52 x i8] c"Failed to setup terminal as standard output stream.\00"
define noalias i32* @reopen_term_stdout() {
%1 = load i32, i32* @STDOUT_FILENO
%2 = tail call i32 @dup(i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %19, label %4
4: ; preds = %0
%5 = tail call i32* @fdopen(i32 %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %19, label %7
7: ; preds = %4
%8 = load i32, i32* @O_WRONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %16, label %11
11: ; preds = %7
%12 = load i32, i32* @STDOUT_FILENO
%13 = tail call i32 @dup2(i32 %9, i32 %12)
%14 = icmp eq i32 %13, -1
%15 = tail call i32 @close(i32 %9)
br i1 %14, label %16, label %23
16: ; preds = %11, %7
%17 = phi i8* [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), %7 ], [ getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0), %11 ]
%18 = tail call i32 @fclose(i32* %5)
br label %19
19: ; preds = %16, %4, %0
%20 = phi i8* [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), %4 ], [ %17, %16 ]
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* %20)
br label %23
23: ; preds = %19, %11
%24 = phi i32* [ %5, %11 ], [ null, %19 ]
ret i32* %24
}