#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _IO(char, int);
scalar_t__ close(int);
scalar_t__ dup2(int, int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int login_tty(int fd) {
  if (setsid() < 0)
    return -1;
  if (ioctl(fd, _IO('T', 0x0E), 0))
    return -1;
  if (dup2(fd, STDIN_FILENO) < 0)
    return -1;
  if (dup2(fd, STDOUT_FILENO) < 0)
    return -1;
  if (dup2(fd, STDERR_FILENO) < 0)
    return -1;
  if (close(fd) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<login_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1178 <login_tty+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <login_tty+0xf9>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x54,%edi
mov    $0xe,%esi
call   1040 <_IO@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11af <login_tty+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <login_tty+0xf9>
mov    -0x8(%rbp),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    11d6 <login_tty+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <login_tty+0xf9>
mov    -0x8(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    11fd <login_tty+0xad>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <login_tty+0xf9>
mov    -0x8(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    1224 <login_tty+0xd4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <login_tty+0xf9>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    1242 <login_tty+0xf2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <login_tty+0xf9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<login_tty>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <setsid@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     1185 <login_tty+0x35>
mov    $0x54,%edi
mov    $0xe,%esi
call   1040 <_IO@plt>
mov    %ebp,%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
je     118e <login_tty+0x3e>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
test   %rax,%rax
js     1185 <login_tty+0x35>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
test   %rax,%rax
js     1185 <login_tty+0x35>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
test   %rax,%rax
js     1185 <login_tty+0x35>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
jmp    1185 <login_tty+0x35>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @login_tty(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = call i64 (...) @setsid()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %1
store i32 -1, i32* %2
br label %37
7: ; preds = %1
%8 = load i32, i32* %3
%9 = call i32 @_IO(i8 signext 84, i32 14)
%10 = call i64 @ioctl(i32 %8, i32 %9, i32 0)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %7
store i32 -1, i32* %2
br label %37
13: ; preds = %7
%14 = load i32, i32* %3
%15 = load i32, i32* @STDIN_FILENO
%16 = call i64 @dup2(i32 %14, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %2
br label %37
19: ; preds = %13
%20 = load i32, i32* %3
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i64 @dup2(i32 %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %2
br label %37
25: ; preds = %19
%26 = load i32, i32* %3
%27 = load i32, i32* @STDERR_FILENO
%28 = call i64 @dup2(i32 %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 -1, i32* %2
br label %37
31: ; preds = %25
%32 = load i32, i32* %3
%33 = call i64 @close(i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 -1, i32* %2
br label %37
36: ; preds = %31
store i32 0, i32* %2
br label %37
37: ; preds = %36, %35, %30, %24, %18, %12, %6
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @login_tty(i32 %0) {
%2 = tail call i64 (...) @setsid()
%3 = icmp slt i64 %2, 0
br i1 %3, label %24, label %4
4: ; preds = %1
%5 = tail call i32 @_IO(i8 signext 84, i32 14)
%6 = tail call i64 @ioctl(i32 %0, i32 %5, i32 0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %24
8: ; preds = %4
%9 = load i32, i32* @STDIN_FILENO
%10 = tail call i64 @dup2(i32 %0, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %8
%13 = load i32, i32* @STDOUT_FILENO
%14 = tail call i64 @dup2(i32 %0, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %12
%17 = load i32, i32* @STDERR_FILENO
%18 = tail call i64 @dup2(i32 %0, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = tail call i64 @close(i32 %0)
%22 = ashr i64 %21, 63
%23 = trunc i64 %22 to i32
br label %24
24: ; preds = %20, %16, %12, %8, %4, %1
%25 = phi i32 [ -1, %1 ], [ -1, %4 ], [ -1, %8 ], [ -1, %12 ], [ -1, %16 ], [ %23, %20 ]
ret i32 %25
}