#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_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ set_term(int /*<<< orphan*/);
int utils_device_open(char const *, int /*<<< orphan*/);
int open_stdio_to_tty(const char *tty_name) {
  if (tty_name[0]) {
    int fd;
    close(STDIN_FILENO);
    fd = utils_device_open(tty_name, O_RDWR);
    if (fd) {

      ;
      return 0;
    }
    dup2(STDIN_FILENO, STDOUT_FILENO);
    dup2(STDIN_FILENO, STDERR_FILENO);
  }
  set_term(STDIN_FILENO);
  return 1;
}


x86 assembly (clang, -O0):
<open_stdio_to_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11c0 <open_stdio_to_tty+0x80>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1060 <utils_device_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1192 <open_stdio_to_tty+0x52>
movl   $0x0,-0x4(%rbp)
jmp    11d5 <open_stdio_to_tty+0x95>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1050 <set_term@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_stdio_to_tty>:
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,(%rdi)
je     1196 <open_stdio_to_tty+0x56>
mov    %rdi,%rbx
mov    0x2e75(%rip),%r14
mov    (%r14),%edi
call   1040 <close@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <utils_device_open@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    11a9 <open_stdio_to_tty+0x69>
mov    (%r14),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    (%r14),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1050 <set_term@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@O_RDWR = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @open_stdio_to_tty(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = getelementptr inbounds i8, i8* %5, i64 0
%7 = load i8, i8* %6
%8 = icmp ne i8 %7, 0
br i1 %8, label %9, label %25
9: ; preds = %1
%10 = load i32, i32* @STDIN_FILENO
%11 = call i32 @close(i32 %10)
%12 = load i8*, i8** %3
%13 = load i32, i32* @O_RDWR
%14 = call i32 @utils_device_open(i8* %12, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %9
store i32 0, i32* %2
br label %28
18: ; preds = %9
%19 = load i32, i32* @STDIN_FILENO
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @STDERR_FILENO
%24 = call i32 @dup2(i32 %22, i32 %23)
br label %25
25: ; preds = %18, %1
%26 = load i32, i32* @STDIN_FILENO
%27 = call i32 @set_term(i32 %26)
store i32 1, i32* %2
br label %28
28: ; preds = %25, %17
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@O_RDWR = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @open_stdio_to_tty(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = load i32, i32* @STDIN_FILENO
%6 = tail call i32 @close(i32 %5)
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @utils_device_open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %4
%11 = load i32, i32* @STDIN_FILENO
%12 = load i32, i32* @STDOUT_FILENO
%13 = tail call i32 @dup2(i32 %11, i32 %12)
%14 = load i32, i32* @STDIN_FILENO
%15 = load i32, i32* @STDERR_FILENO
%16 = tail call i32 @dup2(i32 %14, i32 %15)
br label %17
17: ; preds = %10, %1
%18 = load i32, i32* @STDIN_FILENO
%19 = tail call i32 @set_term(i32 %18)
br label %20
20: ; preds = %4, %17
%21 = phi i32 [ 1, %17 ], [ 0, %4 ]
ret i32 %21
}