#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 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_terminal(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) {
    fclose(fp);
    fprintf(stderr, "Failed to setup terminal as standard output stream.");
    return NULL;
  }
  return fp;
}


x86 assembly (clang, -O0):
<reopen_terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1050 <dup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a5 <reopen_terminal+0x45>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    127f <reopen_terminal+0x11f>
mov    -0x14(%rbp),%edi
lea    0xe79(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e5 <reopen_terminal+0x85>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    127f <reopen_terminal+0x11f>
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1232 <reopen_terminal+0xd2>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    127f <reopen_terminal+0x11f>
mov    -0x18(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1277 <reopen_terminal+0x117>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    127f <reopen_terminal+0x11f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reopen_terminal>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%r14
mov    (%r14),%edi
call   1050 <dup@plt>
cmp    $0xffffffff,%eax
je     11c0 <reopen_terminal+0x60>
lea    0xea9(%rip),%rsi
mov    %eax,%edi
call   1070 <fdopen@plt>
test   %rax,%rax
je     11c9 <reopen_terminal+0x69>
mov    %rax,%rbx
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xeb3(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11d2 <reopen_terminal+0x72>
mov    (%r14),%esi
mov    %eax,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11f4 <reopen_terminal+0x94>
lea    0xec0(%rip),%r14
jmp    11d9 <reopen_terminal+0x79>
lea    0xe39(%rip),%r14
jmp    11e1 <reopen_terminal+0x81>
lea    0xe5a(%rip),%r14
jmp    11e1 <reopen_terminal+0x81>
lea    0xe81(%rip),%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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_terminal() {
%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 %42
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 %42
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 %42
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 %40
35: ; preds = %30
%36 = load i32*, i32** %2
%37 = call i32 @fclose(i32* %36)
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0))
store i32* null, i32** %1
br label %42
40: ; preds = %30
%41 = load i32*, i32** %2
store i32* %41, i32** %1
br label %42
42: ; preds = %40, %35, %25, %17, %9
%43 = load i32*, i32** %1
ret i32* %43
}

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_terminal() {
%1 = load i32, i32* @STDOUT_FILENO
%2 = tail call i32 @dup(i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %18, 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 %18, 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 %15, 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
br i1 %14, label %15, label %22
15: ; preds = %11, %7
%16 = 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 ]
%17 = tail call i32 @fclose(i32* %5)
br label %18
18: ; preds = %15, %4, %0
%19 = 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 ], [ %16, %15 ]
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* %19)
br label %22
22: ; preds = %18, %11
%23 = phi i32* [ %5, %11 ], [ null, %18 ]
ret i32* %23
}