#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int EPERM;
int /*<<< orphan*/ TIOCNOTTY;
int /*<<< orphan*/ __close(int);
scalar_t__ __getpgid(scalar_t__);
int /*<<< orphan*/ __ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ __set_errno(int);
int errno;
scalar_t__ getpid();
int open(char *, int /*<<< orphan*/);
scalar_t__ setpgid(scalar_t__, scalar_t__);
int __setsid(void) {
  pid_t pid = getpid();
  int tty;
  int save = errno;
  if (__getpgid(pid) == pid) {
    __set_errno(EPERM);
    return -1;
  }
  if (setpgid(pid, pid) < 0)
    return -1;
  tty = open("/dev/tty", 0);
  if (tty < 0) {
    __set_errno(save);
    return 0;
  }
  (void)__ioctl(tty, TIOCNOTTY, 0);
  (void)__close(tty);
  __set_errno(save);
  return 0;
}


x86 assembly (clang, -O0):
<__setsid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <__getpgid@plt>
cmp    -0x10(%rbp),%rax
jne    11bc <__setsid+0x4c>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <__setsid+0xc8>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <setpgid@plt>
cmp    $0x0,%rax
jge    11df <__setsid+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <__setsid+0xc8>
lea    0xe1a(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120e <__setsid+0x9e>
mov    -0x18(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0x0,-0x4(%rbp)
jmp    1238 <__setsid+0xc8>
mov    -0x14(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <__ioctl@plt>
mov    -0x14(%rbp),%edi
call   1080 <__close@plt>
mov    -0x18(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__setsid>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rbp
mov    0x2e53(%rip),%rax
mov    (%rax),%ebx
mov    %rbp,%rdi
call   1060 <__getpgid@plt>
cmp    %rbp,%rax
jne    11a4 <__setsid+0x34>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ebx
mov    $0xffffffff,%ebp
jmp    11e8 <__setsid+0x78>
mov    %rbp,%rdi
mov    %rbp,%rsi
call   1090 <setpgid@plt>
test   %rax,%rax
js     11f1 <__setsid+0x81>
lea    0xe45(%rip),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
call   1070 <open@plt>
test   %eax,%eax
js     11e8 <__setsid+0x78>
mov    %eax,%r14d
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %r14d,%edi
xor    %edx,%edx
call   1050 <__ioctl@plt>
mov    %r14d,%edi
call   1080 <__close@plt>
mov    %ebx,%edi
call   1030 <__set_errno@plt>
jmp    11f6 <__setsid+0x86>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EPERM = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@TIOCNOTTY = global i32 0
define i32 @__setsid() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = call i64 (...) @getpid()
store i64 %5, i64* %2
%6 = load i32, i32* @errno
store i32 %6, i32* %4
%7 = load i64, i64* %2
%8 = call i64 @__getpgid(i64 %7)
%9 = load i64, i64* %2
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @EPERM
%13 = call i32 @__set_errno(i32 %12)
store i32 -1, i32* %1
br label %35
14: ; preds = %0
%15 = load i64, i64* %2
%16 = load i64, i64* %2
%17 = call i64 @setpgid(i64 %15, i64 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %1
br label %35
20: ; preds = %14
%21 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* %4
%26 = call i32 @__set_errno(i32 %25)
store i32 0, i32* %1
br label %35
27: ; preds = %20
%28 = load i32, i32* %3
%29 = load i32, i32* @TIOCNOTTY
%30 = call i32 @__ioctl(i32 %28, i32 %29, i32 0)
%31 = load i32, i32* %3
%32 = call i32 @__close(i32 %31)
%33 = load i32, i32* %4
%34 = call i32 @__set_errno(i32 %33)
store i32 0, i32* %1
br label %35
35: ; preds = %27, %24, %19, %11
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@errno = global i32 0
@EPERM = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@TIOCNOTTY = global i32 0
define i32 @__setsid() {
%1 = tail call i64 (...) @getpid()
%2 = load i32, i32* @errno
%3 = tail call i64 @__getpgid(i64 %1)
%4 = icmp eq i64 %3, %1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = load i32, i32* @EPERM
br label %17
7: ; preds = %0
%8 = tail call i64 @setpgid(i64 %1, i64 %1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %21, label %10
10: ; preds = %7
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = load i32, i32* @TIOCNOTTY
%15 = tail call i32 @__ioctl(i32 %11, i32 %14, i32 0)
%16 = tail call i32 @__close(i32 %11)
br label %17
17: ; preds = %10, %5, %13
%18 = phi i32 [ %2, %13 ], [ %6, %5 ], [ %2, %10 ]
%19 = phi i32 [ 0, %13 ], [ -1, %5 ], [ 0, %10 ]
%20 = tail call i32 @__set_errno(i32 %18)
br label %21
21: ; preds = %17, %7
%22 = phi i32 [ -1, %7 ], [ %19, %17 ]
ret i32 %22
}