#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 /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ setsid();
int terminal_set_stdfds(int);
int login_pty(int fd) {
  int ret;
  setsid();
  ret = ioctl(fd, TIOCSCTTY, NULL);
  if (ret < 0)
    return -1;
  ret = terminal_set_stdfds(fd);
  if (ret < 0)
    return -1;
  if (fd > STDERR_FILENO)
    close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<login_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <setsid@plt>
mov    -0x8(%rbp),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1180 <login_pty+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <login_pty+0x82>
mov    -0x8(%rbp),%edi
call   1060 <terminal_set_stdfds@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a1 <login_pty+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <login_pty+0x82>
mov    -0x8(%rbp),%eax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%eax
jle    11bb <login_pty+0x7b>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<login_pty>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <ioctl@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1186 <login_pty+0x46>
mov    %ebx,%edi
call   1060 <terminal_set_stdfds@plt>
test   %eax,%eax
js     1186 <login_pty+0x46>
mov    0x2e67(%rip),%rax
xor    %ebp,%ebp
cmp    %ebx,(%rax)
jge    1186 <login_pty+0x46>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@TIOCSCTTY = global i32 0
@STDERR_FILENO = global i32 0
define i32 @login_pty(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = call i32 (...) @setsid()
%6 = load i32, i32* %3
%7 = load i32, i32* @TIOCSCTTY
%8 = call i32 @ioctl(i32 %6, i32 %7, i32* null)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %26
12: ; preds = %1
%13 = load i32, i32* %3
%14 = call i32 @terminal_set_stdfds(i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %2
br label %26
18: ; preds = %12
%19 = load i32, i32* %3
%20 = load i32, i32* @STDERR_FILENO
%21 = icmp sgt i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %22, %18
store i32 0, i32* %2
br label %26
26: ; preds = %25, %17, %11
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@TIOCSCTTY = global i32 0
@STDERR_FILENO = global i32 0
define i32 @login_pty(i32 %0) {
%2 = tail call i32 (...) @setsid()
%3 = load i32, i32* @TIOCSCTTY
%4 = tail call i32 @ioctl(i32 %0, i32 %3, i32* null)
%5 = icmp slt i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %1
%7 = tail call i32 @terminal_set_stdfds(i32 %0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %6
%10 = load i32, i32* @STDERR_FILENO
%11 = icmp slt i32 %10, %0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @close(i32 %0)
br label %14
14: ; preds = %9, %12, %6, %1
%15 = phi i32 [ -1, %1 ], [ -1, %6 ], [ 0, %12 ], [ 0, %9 ]
ret i32 %15
}