#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
char *ttyname(int);
int login_tty(int fd) {
  (void)setsid();
  {
    char *fdname = ttyname(fd);
    int newfd;
    if (fdname) {
      if (fd != 0)
        (void)close(0);
      if (fd != 1)
        (void)close(1);
      if (fd != 2)
        (void)close(2);
      newfd = open(fdname, O_RDWR);
      (void)close(newfd);
    }
  }
  (void)dup2(fd, 0);
  (void)dup2(fd, 1);
  (void)dup2(fd, 2);
  if (fd > 2)
    (void)close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<login_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1050 <setsid@plt>
mov    -0x4(%rbp),%edi
call   1060 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11cf <login_tty+0x7f>
cmpl   $0x0,-0x4(%rbp)
je     118a <login_tty+0x3a>
xor    %edi,%edi
call   1040 <close@plt>
cmpl   $0x1,-0x4(%rbp)
je     119e <login_tty+0x4e>
mov    $0x1,%edi
call   1040 <close@plt>
cmpl   $0x2,-0x4(%rbp)
je     11b2 <login_tty+0x62>
mov    $0x2,%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmpl   $0x2,-0x4(%rbp)
jle    1205 <login_tty+0xb5>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<login_tty>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
mov    %ebx,%edi
call   1060 <ttyname@plt>
test   %rax,%rax
je     11b9 <login_tty+0x69>
mov    %rax,%r14
test   %ebx,%ebx
je     118d <login_tty+0x3d>
xor    %edi,%edi
call   1040 <close@plt>
cmp    $0x1,%ebx
je     1197 <login_tty+0x47>
mov    $0x1,%edi
call   1040 <close@plt>
cmp    $0x2,%ebx
jne    1197 <login_tty+0x47>
jmp    11a1 <login_tty+0x51>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%edi
call   1040 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmp    $0x3,%ebx
jl     11e6 <login_tty+0x96>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
define i32 @login_tty(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 (...) @setsid()
%6 = load i32, i32* %2
%7 = call i8* @ttyname(i32 %6)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %31
10: ; preds = %1
%11 = load i32, i32* %2
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @close(i32 0)
br label %15
15: ; preds = %13, %10
%16 = load i32, i32* %2
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @close(i32 1)
br label %20
20: ; preds = %18, %15
%21 = load i32, i32* %2
%22 = icmp ne i32 %21, 2
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @close(i32 2)
br label %25
25: ; preds = %23, %20
%26 = load i8*, i8** %3
%27 = load i32, i32* @O_RDWR
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
br label %31
31: ; preds = %25, %1
%32 = load i32, i32* %2
%33 = call i32 @dup2(i32 %32, i32 0)
%34 = load i32, i32* %2
%35 = call i32 @dup2(i32 %34, i32 1)
%36 = load i32, i32* %2
%37 = call i32 @dup2(i32 %36, i32 2)
%38 = load i32, i32* %2
%39 = icmp sgt i32 %38, 2
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = load i32, i32* %2
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40, %31
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
define i32 @login_tty(i32 %0) {
%2 = tail call i32 (...) @setsid()
%3 = tail call i8* @ttyname(i32 %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %21, label %5
5: ; preds = %1
%6 = icmp eq i32 %0, 0
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = tail call i32 @close(i32 1)
br label %15
9: ; preds = %5
%10 = tail call i32 @close(i32 0)
%11 = icmp eq i32 %0, 1
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i32 @close(i32 1)
%14 = icmp eq i32 %0, 2
br i1 %14, label %17, label %15
15: ; preds = %7, %9, %12
%16 = tail call i32 @close(i32 2)
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* %3, i32 %18)
%20 = tail call i32 @close(i32 %19)
br label %21
21: ; preds = %17, %1
%22 = tail call i32 @dup2(i32 %0, i32 0)
%23 = tail call i32 @dup2(i32 %0, i32 1)
%24 = tail call i32 @dup2(i32 %0, i32 2)
%25 = icmp sgt i32 %0, 2
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @close(i32 %0)
br label %28
28: ; preds = %26, %21
ret i32 0
}