#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*/ SIGTTOU;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDIN_FILENO;
int getpgrp();
int /*<<< orphan*/ getpid();
int getsid(int /*<<< orphan*/);
int /*<<< orphan*/ judge();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int tcgetsid(int /*<<< orphan*/);
int tcsetpgrp(int /*<<< orphan*/, int);
int main(void) {
  signal(SIGTTOU, SIG_IGN);
  pid_t spid = tcgetsid(STDIN_FILENO);
  printf("tcgetsid:%d,pgrp=%d,sid=%d\n", spid, getpgrp(), getsid(getpid()));
  judge();
  int result;
  result = tcsetpgrp(STDIN_FILENO, getpgrp());
  if (result == -1) {
    perror("tcsetpgrp");
    return -1;
  }
  judge();
  result = tcsetpgrp(STDIN_FILENO, spid);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1030 <tcgetsid@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <getpgrp@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
call   1060 <getsid@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
lea    0xe0c(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <judge@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1090 <getpgrp@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%esi
call   10a0 <tcsetpgrp@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1242 <main+0xb2>
lea    0xdeb(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <main+0xd4>
mov    $0x0,%al
call   1080 <judge@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
call   10a0 <tcsetpgrp@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e1b(%rip),%r15
mov    (%r15),%edi
call   1030 <tcgetsid@plt>
mov    %eax,%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1090 <getpgrp@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1060 <getsid@plt>
lea    0xe20(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %eax,%ecx
call   1050 <printf@plt>
xor    %eax,%eax
call   1080 <judge@plt>
mov    (%r15),%ebx
xor    %eax,%eax
call   1090 <getpgrp@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   10a0 <tcsetpgrp@plt>
cmp    $0xffffffff,%eax
je     1229 <main+0x99>
xor    %eax,%eax
call   1080 <judge@plt>
mov    (%r15),%edi
mov    %ebp,%esi
call   10a0 <tcsetpgrp@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdec(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    121b <main+0x8b>

LLVM IR (-O0):
@SIGTTOU = global i32 0
@SIG_IGN = global i32 0
@STDIN_FILENO = global i32 0
@.str = [28 x i8] c"tcgetsid:%d,pgrp=%d,sid=%d\0A\00"
@.str.1 = [10 x i8] c"tcsetpgrp\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @SIGTTOU
%5 = load i32, i32* @SIG_IGN
%6 = call i32 @signal(i32 %4, i32 %5)
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetsid(i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = call i32 (...) @getpgrp()
%11 = call i32 (...) @getpid()
%12 = call i32 @getsid(i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10, i32 %12)
%14 = call i32 (...) @judge()
%15 = load i32, i32* @STDIN_FILENO
%16 = call i32 (...) @getpgrp()
%17 = call i32 @tcsetpgrp(i32 %15, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %27
22: ; preds = %0
%23 = call i32 (...) @judge()
%24 = load i32, i32* @STDIN_FILENO
%25 = load i32, i32* %2
%26 = call i32 @tcsetpgrp(i32 %24, i32 %25)
store i32 %26, i32* %3
store i32 0, i32* %1
br label %27
27: ; preds = %22, %20
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@SIGTTOU = global i32 0
@SIG_IGN = global i32 0
@STDIN_FILENO = global i32 0
@.str = [28 x i8] c"tcgetsid:%d,pgrp=%d,sid=%d\0A\00"
@.str.1 = [10 x i8] c"tcsetpgrp\00"
define i32 @main() {
%1 = load i32, i32* @SIGTTOU
%2 = load i32, i32* @SIG_IGN
%3 = tail call i32 @signal(i32 %1, i32 %2)
%4 = load i32, i32* @STDIN_FILENO
%5 = tail call i32 @tcgetsid(i32 %4)
%6 = tail call i32 (...) @getpgrp()
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 @getsid(i32 %7)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6, i32 %8)
%10 = tail call i32 (...) @judge()
%11 = load i32, i32* @STDIN_FILENO
%12 = tail call i32 (...) @getpgrp()
%13 = tail call i32 @tcsetpgrp(i32 %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %21
17: ; preds = %0
%18 = tail call i32 (...) @judge()
%19 = load i32, i32* @STDIN_FILENO
%20 = tail call i32 @tcsetpgrp(i32 %19, i32 %5)
br label %21
21: ; preds = %17, %15
%22 = phi i32 [ -1, %15 ], [ 0, %17 ]
ret i32 %22
}