#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*/ O_RDONLY;
int STDIN_FILENO;
scalar_t__ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int reopen_term_stdin(void) {
  int ttyfd;
  if (close(STDIN_FILENO)) {
    fprintf(stderr, "Failed to close original input stream.");
    return 1;
  }
  ttyfd = open("/dev/tty", O_RDONLY);
  if (ttyfd != STDIN_FILENO) {
    fprintf(stderr, "Failed to open terminal for input.");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<reopen_term_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     1171 <reopen_term_stdin+0x41>
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c3 <reopen_term_stdin+0x93>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xea6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%eax
je     11bc <reopen_term_stdin+0x8c>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c3 <reopen_term_stdin+0x93>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reopen_term_stdin>:
push   %rbx
mov    0x2e98(%rip),%rbx
mov    (%rbx),%edi
call   1030 <close@plt>
test   %rax,%rax
je     114d <reopen_term_stdin+0x1d>
lea    0xeb5(%rip),%rsi
jmp    1171 <reopen_term_stdin+0x41>
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0xeca(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    (%rbx),%ecx
je     1184 <reopen_term_stdin+0x54>
lea    0xebf(%rip),%rsi
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1040 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Failed to close original input stream.\00"
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@.str.2 = [35 x i8] c"Failed to open terminal for input.\00"
define i32 @reopen_term_stdin() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @STDIN_FILENO
%4 = call i64 @close(i32 %3)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %19
9: ; preds = %0
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = load i32, i32* @STDIN_FILENO
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %19
18: ; preds = %9
store i32 0, i32* %1
br label %19
19: ; preds = %18, %15, %6
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Failed to close original input stream.\00"
@.str.1 = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@.str.2 = [35 x i8] c"Failed to open terminal for input.\00"
define i32 @reopen_term_stdin() {
%1 = load i32, i32* @STDIN_FILENO
%2 = tail call i64 @close(i32 %1)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @STDIN_FILENO
%8 = icmp eq i32 %6, %7
br i1 %8, label %13, label %9
9: ; preds = %4, %0
%10 = phi i8* [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), %4 ]
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* %10)
br label %13
13: ; preds = %9, %4
%14 = phi i32 [ 0, %4 ], [ 1, %9 ]
ret i32 %14
}