#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 STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int diag_fd();
int /*<<< orphan*/ dup2(int, int);
int setup_tty(const char *dev_name) {
  int fd;
  fd = diag_fd();
  if (fd < 0) {
    return fd;
  }
  dup2(fd, STDIN_FILENO);
  dup2(fd, STDOUT_FILENO);
  dup2(fd, STDERR_FILENO);
  if (fd > STDERR_FILENO) {
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <diag_fd@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    115b <setup_tty+0x2b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <setup_tty+0x7f>
mov    -0x14(%rbp),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%eax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%eax
jle    11a8 <setup_tty+0x78>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_tty>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <diag_fd@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1182 <setup_tty+0x52>
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    0x2e6e(%rip),%r14
mov    (%r14),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
cmp    (%r14),%ebp
jle    1184 <setup_tty+0x54>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1184 <setup_tty+0x54>
mov    %ebp,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@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
store i8* %0, i8** %3
%5 = call i32 (...) @diag_fd()
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* %4
store i32 %9, i32* %2
br label %27
10: ; preds = %1
%11 = load i32, i32* %4
%12 = load i32, i32* @STDIN_FILENO
%13 = call i32 @dup2(i32 %11, i32 %12)
%14 = load i32, i32* %4
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @dup2(i32 %14, i32 %15)
%17 = load i32, i32* %4
%18 = load i32, i32* @STDERR_FILENO
%19 = call i32 @dup2(i32 %17, i32 %18)
%20 = load i32, i32* %4
%21 = load i32, i32* @STDERR_FILENO
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %10
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %23, %10
store i32 0, i32* %2
br label %27
27: ; preds = %26, %8
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @setup_tty(i8* nocapture readnone %0) {
%2 = tail call i32 (...) @diag_fd()
%3 = icmp slt i32 %2, 0
br i1 %3, label %15, label %4
4: ; preds = %1
%5 = load i32, i32* @STDIN_FILENO
%6 = tail call i32 @dup2(i32 %2, i32 %5)
%7 = load i32, i32* @STDOUT_FILENO
%8 = tail call i32 @dup2(i32 %2, i32 %7)
%9 = load i32, i32* @STDERR_FILENO
%10 = tail call i32 @dup2(i32 %2, i32 %9)
%11 = load i32, i32* @STDERR_FILENO
%12 = icmp sgt i32 %2, %11
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = tail call i32 @close(i32 %2)
br label %15
15: ; preds = %4, %13, %1
%16 = phi i32 [ %2, %1 ], [ 0, %13 ], [ 0, %4 ]
ret i32 %16
}