#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*/ full_name;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int PATH_MAX;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int diag_fd();
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ putenv(char *);
int /*<<< orphan*/ strcat(char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int setup_tty(const char *dev_name) {
  int fd;
  char full_name[PATH_MAX];
  putenv("TERM=emterm");
  if (strlen(dev_name) != 0) {
    strncpy(full_name, "/dev/", sizeof(full_name));
    strcat(full_name, dev_name);
    if (-1 == (fd = open(full_name, O_RDWR))) {
      fd = diag_fd();
    }
  } else {
    fd = diag_fd();
  }
  if (fd < 0) {
    return fd;
  }
  dup2(fd, STDIN_FILENO);
  dup2(fd, STDOUT_FILENO);
  dup2(fd, STDERR_FILENO);
  if (fd > 2) {
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    0xe3d(%rip),%rdi
call   1070 <putenv@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
je     1231 <setup_tty+0xb1>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%edx
lea    0xe20(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    122c <setup_tty+0xac>
mov    $0x0,%al
call   1080 <diag_fd@plt>
mov    %eax,-0x14(%rbp)
jmp    123b <setup_tty+0xbb>
mov    $0x0,%al
call   1080 <diag_fd@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1257 <setup_tty+0xd7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12aa <setup_tty+0x12a>
mov    -0x14(%rbp),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x14(%rbp)
jle    129c <setup_tty+0x11c>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_tty>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e6c(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe61(%rip),%rdi
call   1060 <putenv@plt>
cmpb   $0x0,(%r14)
je     1224 <setup_tty+0xb4>
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1224 <setup_tty+0xb4>
test   %ebx,%ebx
js     1231 <setup_tty+0xc1>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
xor    %r14d,%r14d
cmp    $0x3,%ebx
jb     1234 <setup_tty+0xc4>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1234 <setup_tty+0xc4>
xor    %eax,%eax
call   1070 <diag_fd@plt>
mov    %eax,%ebx
test   %ebx,%ebx
jns    11e3 <setup_tty+0x73>
mov    %ebx,%r14d
mov    %r14d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [12 x i8] c"TERM=emterm\00"
@.str.1 = [6 x i8] c"/dev/\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @setup_tty(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %5
%11 = alloca i8, i64 %9
store i64 %9, i64* %6
%12 = call i32 @putenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %3
%14 = call i64 @strlen(i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %1
%17 = trunc i64 %9 to i32
%18 = call i32 @strncpy(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %3
%20 = call i32 @strcat(i8* %11, i8* %19)
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %11, i32 %21)
store i32 %22, i32* %4
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 (...) @diag_fd()
store i32 %25, i32* %4
br label %26
26: ; preds = %24, %16
br label %29
27: ; preds = %1
%28 = call i32 (...) @diag_fd()
store i32 %28, i32* %4
br label %29
29: ; preds = %27, %26
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i32, i32* %4
store i32 %33, i32* %2
store i32 1, i32* %7
br label %50
34: ; preds = %29
%35 = load i32, i32* %4
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = load i32, i32* %4
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @dup2(i32 %38, i32 %39)
%41 = load i32, i32* %4
%42 = load i32, i32* @STDERR_FILENO
%43 = call i32 @dup2(i32 %41, i32 %42)
%44 = load i32, i32* %4
%45 = icmp sgt i32 %44, 2
br i1 %45, label %46, label %49
46: ; preds = %34
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46, %34
store i32 0, i32* %2
store i32 1, i32* %7
br label %50
50: ; preds = %49, %32
%51 = load i8*, i8** %5
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [12 x i8] c"TERM=emterm\00"
@.str.1 = [6 x i8] c"/dev/\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @setup_tty(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32 @putenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %1
%9 = call i32 @strncpy(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%10 = call i32 @strcat(i8* %4, i8* %0)
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %4, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = call i32 (...) @diag_fd()
br label %18
16: ; preds = %1
%17 = tail call i32 (...) @diag_fd()
br label %18
18: ; preds = %8, %14, %16
%19 = phi i32 [ %15, %14 ], [ %12, %8 ], [ %17, %16 ]
%20 = icmp slt i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %18
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @dup2(i32 %19, i32 %22)
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @dup2(i32 %19, i32 %24)
%26 = load i32, i32* @STDERR_FILENO
%27 = call i32 @dup2(i32 %19, i32 %26)
%28 = icmp ugt i32 %19, 2
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = call i32 @close(i32 %19)
br label %31
31: ; preds = %21, %29, %18
%32 = phi i32 [ %19, %18 ], [ 0, %29 ], [ 0, %21 ]
ret i32 %32
}