#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ ENOTTY;
scalar_t__ ESRCH;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
scalar_t__ errno;
int getsid(int);
int tcgetpgrp(int);
pid_t tcgetsid(int fd) {
  pid_t pgrp;
  pid_t sid;
  pgrp = tcgetpgrp(fd);
  if (pgrp == -1)
    return (pid_t)-1;
  sid = getsid(pgrp);
  if (sid == -1 && errno == ESRCH)
    __set_errno(ENOTTY);
  return sid;
}


x86 assembly (clang, -O0):
<tcgetsid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <tcgetpgrp@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    115c <tcgetsid+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <tcgetsid+0x6f>
mov    -0xc(%rbp),%edi
call   1040 <getsid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1199 <tcgetsid+0x69>
mov    0x2e60(%rip),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <tcgetsid+0x69>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcgetsid>:
push   %rax
call   1050 <tcgetpgrp@plt>
cmp    $0xffffffff,%eax
je     116d <tcgetsid+0x3d>
mov    %eax,%edi
call   1040 <getsid@plt>
cmp    $0xffffffff,%eax
je     1149 <tcgetsid+0x19>
pop    %rcx
ret
mov    0x2e88(%rip),%rax
mov    (%rax),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
jne    116d <tcgetsid+0x3d>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@errno = global i64 0
@ESRCH = global i64 0
@ENOTTY = global i32 0
define i32 @tcgetsid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @tcgetpgrp(i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %25
11: ; preds = %1
%12 = load i32, i32* %4
%13 = call i32 @getsid(i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = load i64, i64* @errno
%18 = load i64, i64* @ESRCH
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @ENOTTY
%22 = call i32 @__set_errno(i32 %21)
br label %23
23: ; preds = %20, %16, %11
%24 = load i32, i32* %5
store i32 %24, i32* %2
br label %25
25: ; preds = %23, %10
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@errno = global i64 0
@ESRCH = global i64 0
@ENOTTY = global i32 0
define i32 @tcgetsid(i32 %0) {
%2 = tail call i32 @tcgetpgrp(i32 %0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = tail call i32 @getsid(i32 %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %14
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @ESRCH
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @ENOTTY
%13 = tail call i32 @__set_errno(i32 %12)
br label %14
14: ; preds = %4, %7, %11, %1
%15 = phi i32 [ -1, %1 ], [ -1, %11 ], [ -1, %7 ], [ %5, %4 ]
ret i32 %15
}