#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 */
struct winsize {
  int dummy;
};
struct termios {
  int dummy;
};

/* Variables and functions */
scalar_t__ _internal_get_tty(int *, char const *, struct termios *,
                             struct winsize *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ setsid();
int _internal_login_tty(int fd, const char *path, struct termios *termp,
                        struct winsize *winp) {
  setsid();
  if (_internal_get_tty(&fd, path, termp, winp, 1) != 0)
    return -1;
  dup2(fd, 0);
  dup2(fd, 1);
  dup2(fd, 2);
  if (fd > 2)
    close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<_internal_login_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x0,%al
call   1050 <setsid@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x8(%rbp),%rdi
mov    $0x1,%r8d
call   1060 <_internal_get_tty@plt>
cmp    $0x0,%rax
je     118f <_internal_login_tty+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <_internal_login_tty+0x8c>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmpl   $0x2,-0x8(%rbp)
jle    11c5 <_internal_login_tty+0x85>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_internal_login_tty>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,0xc(%rsp)
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xc(%rsp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    $0x1,%r8d
call   1060 <_internal_get_tty@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    11ba <_internal_login_tty+0x7a>
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
cmp    $0x3,%edi
jl     11b8 <_internal_login_tty+0x78>
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
define i32 @_internal_login_tty(i32 %0, i8* %1, %struct.termios* %2, %struct.winsize* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.termios*
%9 = alloca %struct.winsize*
store i32 %0, i32* %6
store i8* %1, i8** %7
store %struct.termios* %2, %struct.termios** %8
store %struct.winsize* %3, %struct.winsize** %9
%10 = call i32 (...) @setsid()
%11 = load i8*, i8** %7
%12 = load %struct.termios*, %struct.termios** %8
%13 = load %struct.winsize*, %struct.winsize** %9
%14 = call i64 @_internal_get_tty(i32* %6, i8* %11, %struct.termios* %12, %struct.winsize* %13, i32 1)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 -1, i32* %5
br label %30
17: ; preds = %4
%18 = load i32, i32* %6
%19 = call i32 @dup2(i32 %18, i32 0)
%20 = load i32, i32* %6
%21 = call i32 @dup2(i32 %20, i32 1)
%22 = load i32, i32* %6
%23 = call i32 @dup2(i32 %22, i32 2)
%24 = load i32, i32* %6
%25 = icmp sgt i32 %24, 2
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
br label %29
29: ; preds = %26, %17
store i32 0, i32* %5
br label %30
30: ; preds = %29, %16
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
define i32 @_internal_login_tty(i32 %0, i8* %1, %struct.termios* %2, %struct.winsize* %3) {
%5 = alloca i32
store i32 %0, i32* %5
%6 = tail call i32 (...) @setsid()
%7 = call i64 @_internal_get_tty(i32* %5, i8* %1, %struct.termios* %2, %struct.winsize* %3, i32 1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %4
%10 = load i32, i32* %5
%11 = call i32 @dup2(i32 %10, i32 0)
%12 = load i32, i32* %5
%13 = call i32 @dup2(i32 %12, i32 1)
%14 = load i32, i32* %5
%15 = call i32 @dup2(i32 %14, i32 2)
%16 = load i32, i32* %5
%17 = icmp sgt i32 %16, 2
br i1 %17, label %18, label %20
18: ; preds = %9
%19 = call i32 @close(i32 %16)
br label %20
20: ; preds = %9, %18, %4
%21 = phi i32 [ -1, %4 ], [ 0, %18 ], [ 0, %9 ]
ret i32 %21
}