#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ setsid();
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  pid_t id;
  int tty;
  puts("--- start ---");
  system("ps axj | grep getctty | grep -v grep");
  id = fork();
  if (id < 0) {
    perror("fork");
    exit(1);
  }
  if (id != 0) {
    _exit(0);
  }
  if (setsid() < 0) {
    perror("setsid");
    exit(1);
  }
  puts("--- ctty detouched ---");
  system("ps axj | grep getctty | grep -v grep");
  tty = open("/dev/tty1", O_RDWR);
  if (tty < 0) {
    perror("open("
           "/dev/tty1"
           ")");
    exit(1);
  }
  puts("--- open OK ---");
  system("ps axj | grep getctty | grep -v grep");
  if (ioctl(tty, TIOCSCTTY, 1) < 0) {
    perror("ioctl(TIOCSCTTY)");
    exit(1);
  }
  puts("--- ioctl(TIOCSCTTY) OK ---");
  system("ps axj | grep getctty | grep -v grep");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rdi
call   1040 <puts@plt>
lea    0xe55(%rip),%rdi
call   1050 <system@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11ea <main+0x5a>
lea    0xe58(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
je     11fc <main+0x6c>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1223 <main+0x93>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe15(%rip),%rdi
call   1040 <puts@plt>
lea    0xdd8(%rip),%rdi
call   1050 <system@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
lea    0xe0b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1273 <main+0xe3>
lea    0xdfc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdf6(%rip),%rdi
call   1040 <puts@plt>
lea    0xd88(%rip),%rdi
call   1050 <system@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    12c1 <main+0x131>
lea    0xdce(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdc9(%rip),%rdi
call   1040 <puts@plt>
lea    0xd3a(%rip),%rdi
call   1050 <system@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe68(%rip),%rdi
call   1040 <puts@plt>
lea    0xe6a(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jns    11cb <main+0x3b>
lea    0xe77(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jne    11e2 <main+0x52>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
jns    11e9 <main+0x59>
lea    0xe58(%rip),%rdi
jmp    11bc <main+0x2c>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xe4f(%rip),%rdi
call   1040 <puts@plt>
lea    0xe12(%rip),%rdi
call   1050 <system@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    0xe45(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
jns    1223 <main+0x93>
lea    0xe3f(%rip),%rdi
jmp    11bc <main+0x2c>
mov    %eax,%ebx
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
lea    0xdd6(%rip),%rdi
call   1050 <system@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <ioctl@plt>
test   %rax,%rax
jns    1263 <main+0xd3>
lea    0xe22(%rip),%rdi
jmp    11bc <main+0x2c>
lea    0xe27(%rip),%rdi
call   1040 <puts@plt>
lea    0xd98(%rip),%rdi
call   1050 <system@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"--- start ---\00"
@.str.1 = [37 x i8] c"ps axj | grep getctty | grep -v grep\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [7 x i8] c"setsid\00"
@.str.4 = [23 x i8] c"--- ctty detouched ---\00"
@.str.5 = [10 x i8] c"/dev/tty1\00"
@O_RDWR = global i32 0
@.str.6 = [16 x i8] c"open(/dev/tty1)\00"
@.str.7 = [16 x i8] c"--- open OK ---\00"
@TIOCSCTTY = global i32 0
@.str.8 = [17 x i8] c"ioctl(TIOCSCTTY)\00"
@.str.9 = [28 x i8] c"--- ioctl(TIOCSCTTY) OK ---\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @system(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%10 = call i64 (...) @fork()
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i64, i64* %6
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @_exit(i32 0)
unreachable
21: ; preds = %16
%22 = call i64 (...) @setsid()
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 @system(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27
%38 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%39 = call i32 @system(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32, i32* %7
%41 = load i32, i32* @TIOCSCTTY
%42 = call i64 @ioctl(i32 %40, i32 %41, i32 1)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %37
%48 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
%49 = call i32 @system(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"--- start ---\00"
@.str.1 = [37 x i8] c"ps axj | grep getctty | grep -v grep\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [7 x i8] c"setsid\00"
@.str.4 = [23 x i8] c"--- ctty detouched ---\00"
@.str.5 = [10 x i8] c"/dev/tty1\00"
@O_RDWR = global i32 0
@.str.6 = [16 x i8] c"open(/dev/tty1)\00"
@.str.7 = [16 x i8] c"--- open OK ---\00"
@TIOCSCTTY = global i32 0
@.str.8 = [17 x i8] c"ioctl(TIOCSCTTY)\00"
@.str.9 = [28 x i8] c"--- ioctl(TIOCSCTTY) OK ---\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @system(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = icmp eq i64 %5, 0
br i1 %11, label %14, label %12
12: ; preds = %10
%13 = tail call i32 @_exit(i32 0)
unreachable
14: ; preds = %10
%15 = tail call i64 (...) @setsid()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%22 = tail call i32 @system(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @O_RDWR
%24 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%31 = tail call i32 @system(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @TIOCSCTTY
%33 = tail call i64 @ioctl(i32 %24, i32 %32, i32 1)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %29
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
%40 = tail call i32 @system(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%41 = tail call i32 @exit(i32 0)
unreachable
}