#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*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int setsid();
int login_tty(int sfd) {
  int rv;
  close(0);
  close(1);
  close(2);
  if ((rv = setsid()) != 0) {
  }
  ioctl(sfd, TIOCSCTTY, 0);
  dup2(sfd, 0);
  dup2(sfd, 1);
  dup2(sfd, 2);
  if (sfd > 2)
    close(sfd);
  return rv;
}


x86 assembly (clang, -O0):
<login_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     117e <login_tty+0x3e>
jmp    117e <login_tty+0x3e>
mov    -0x4(%rbp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@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    11c7 <login_tty+0x87>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<login_tty>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    %eax,%ebp
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <ioctl@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     11a8 <login_tty+0x68>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@TIOCSCTTY = global i32 0
define i32 @login_tty(i32 %0) {
%2 = tail call i32 @close(i32 0)
%3 = tail call i32 @close(i32 1)
%4 = tail call i32 @close(i32 2)
%5 = tail call i32 (...) @setsid()
%6 = load i32, i32* @TIOCSCTTY
%7 = tail call i32 @ioctl(i32 %0, i32 %6, i32 0)
%8 = tail call i32 @dup2(i32 %0, i32 0)
%9 = tail call i32 @dup2(i32 %0, i32 1)
%10 = tail call i32 @dup2(i32 %0, i32 2)
%11 = icmp sgt i32 %0, 2
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 @close(i32 %0)
br label %14
14: ; preds = %12, %1
ret i32 %5
}