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

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIGTSTP;
int /*<<< orphan*/ SIGTTIN;
int /*<<< orphan*/ SIGTTOU;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getpgrp();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetpgrp(int /*<<< orphan*/);
int /*<<< orphan*/ tcsetpgrp(int /*<<< orphan*/, int /*<<< orphan*/);
void ish_init_job_control(void) {
  pid_t pgid;
  while (tcgetpgrp(STDIN_FILENO) != (pgid = getpgrp())) {
    kill(-pgid, SIGTTIN);
  }
  signal(SIGINT, SIG_IGN);
  signal(SIGQUIT, SIG_IGN);
  signal(SIGTSTP, SIG_IGN);
  signal(SIGTTIN, SIG_IGN);
  signal(SIGTTOU, SIG_IGN);
  signal(SIGCHLD, SIG_IGN);
  pgid = getpid();
  if (setpgid(pgid, pgid) < 0) {
    perror("setpgid");
    exit(1);
  }
  tcsetpgrp(STDIN_FILENO, pgid);
}


x86 assembly (clang, -O0):
<ish_init_job_control>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1060 <tcgetpgrp@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%eax
mov    %ecx,-0x4(%rbp)
cmp    %ecx,%eax
je     11d8 <ish_init_job_control+0x48>
xor    %edi,%edi
sub    -0x4(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
jmp    1198 <ish_init_job_control+0x8>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
call   10a0 <setpgid@plt>
cmp    $0x0,%rax
jge    1297 <ish_init_job_control+0x107>
lea    0xd78(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
call   1080 <tcsetpgrp@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ish_init_job_control>:
push   %r15
push   %r14
push   %rbx
mov    0x2e0c(%rip),%r14
mov    (%r14),%edi
call   1060 <tcgetpgrp@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getpgrp@plt>
cmp    %eax,%ebx
je     11e1 <ish_init_job_control+0x51>
mov    0x2e08(%rip),%r15
nopl   0x0(%rax,%rax,1)
neg    %eax
mov    (%r15),%esi
mov    %eax,%edi
call   1050 <kill@plt>
mov    (%r14),%edi
call   1060 <tcgetpgrp@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getpgrp@plt>
cmp    %eax,%ebx
jne    11c0 <ish_init_job_control+0x30>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rbx
mov    (%rbx),%esi
call   1040 <signal@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1040 <signal@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1040 <signal@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1040 <signal@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1040 <signal@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1040 <signal@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %eax,%esi
call   10a0 <setpgid@plt>
test   %rax,%rax
js     126e <ish_init_job_control+0xde>
mov    (%r14),%edi
mov    %ebx,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <tcsetpgrp@plt>
lea    0xd8b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@SIGTTIN = global i32 0
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
@SIGTSTP = global i32 0
@SIGTTOU = global i32 0
@SIGCHLD = global i32 0
@.str = [8 x i8] c"setpgid\00"
define void @ish_init_job_control() {
%1 = alloca i32
br label %2
2: ; preds = %7, %0
%3 = load i32, i32* @STDIN_FILENO
%4 = call i32 @tcgetpgrp(i32 %3)
%5 = call i32 (...) @getpgrp()
store i32 %5, i32* %1
%6 = icmp ne i32 %4, %5
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* %1
%9 = sub nsw i32 0, %8
%10 = load i32, i32* @SIGTTIN
%11 = call i32 @kill(i32 %9, i32 %10)
br label %2
12: ; preds = %2
%13 = load i32, i32* @SIGINT
%14 = load i32, i32* @SIG_IGN
%15 = call i32 @signal(i32 %13, i32 %14)
%16 = load i32, i32* @SIGQUIT
%17 = load i32, i32* @SIG_IGN
%18 = call i32 @signal(i32 %16, i32 %17)
%19 = load i32, i32* @SIGTSTP
%20 = load i32, i32* @SIG_IGN
%21 = call i32 @signal(i32 %19, i32 %20)
%22 = load i32, i32* @SIGTTIN
%23 = load i32, i32* @SIG_IGN
%24 = call i32 @signal(i32 %22, i32 %23)
%25 = load i32, i32* @SIGTTOU
%26 = load i32, i32* @SIG_IGN
%27 = call i32 @signal(i32 %25, i32 %26)
%28 = load i32, i32* @SIGCHLD
%29 = load i32, i32* @SIG_IGN
%30 = call i32 @signal(i32 %28, i32 %29)
%31 = call i32 (...) @getpid()
store i32 %31, i32* %1
%32 = load i32, i32* %1
%33 = load i32, i32* %1
%34 = call i64 @setpgid(i32 %32, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %12
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %12
%40 = load i32, i32* @STDIN_FILENO
%41 = load i32, i32* %1
%42 = call i32 @tcsetpgrp(i32 %40, i32 %41)
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@SIGTTIN = global i32 0
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
@SIGTSTP = global i32 0
@SIGTTOU = global i32 0
@SIGCHLD = global i32 0
@.str = [8 x i8] c"setpgid\00"
define void @ish_init_job_control() {
%1 = load i32, i32* @STDIN_FILENO
%2 = tail call i32 @tcgetpgrp(i32 %1)
%3 = tail call i32 (...) @getpgrp()
%4 = icmp eq i32 %2, %3
br i1 %4, label %14, label %5
5: ; preds = %0, %5
%6 = phi i32 [ %12, %5 ], [ %3, %0 ]
%7 = sub nsw i32 0, %6
%8 = load i32, i32* @SIGTTIN
%9 = tail call i32 @kill(i32 %7, i32 %8)
%10 = load i32, i32* @STDIN_FILENO
%11 = tail call i32 @tcgetpgrp(i32 %10)
%12 = tail call i32 (...) @getpgrp()
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %5
14: ; preds = %5, %0
%15 = load i32, i32* @SIGINT
%16 = load i32, i32* @SIG_IGN
%17 = tail call i32 @signal(i32 %15, i32 %16)
%18 = load i32, i32* @SIGQUIT
%19 = load i32, i32* @SIG_IGN
%20 = tail call i32 @signal(i32 %18, i32 %19)
%21 = load i32, i32* @SIGTSTP
%22 = load i32, i32* @SIG_IGN
%23 = tail call i32 @signal(i32 %21, i32 %22)
%24 = load i32, i32* @SIGTTIN
%25 = load i32, i32* @SIG_IGN
%26 = tail call i32 @signal(i32 %24, i32 %25)
%27 = load i32, i32* @SIGTTOU
%28 = load i32, i32* @SIG_IGN
%29 = tail call i32 @signal(i32 %27, i32 %28)
%30 = load i32, i32* @SIGCHLD
%31 = load i32, i32* @SIG_IGN
%32 = tail call i32 @signal(i32 %30, i32 %31)
%33 = tail call i32 (...) @getpid()
%34 = tail call i64 @setpgid(i32 %33, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %14
%37 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %14
%40 = load i32, i32* @STDIN_FILENO
%41 = tail call i32 @tcsetpgrp(i32 %40, i32 %33)
ret void
}