#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 */

/* Variables and functions */
int /*<<< orphan*/ SIGHUP;
scalar_t__ SIG_DFL;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TIOCNOTTY;
int /*<<< orphan*/ TIOCSCTTY;
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, ...);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(void) {
  if (!isatty(STDIN_FILENO)) {
    perror("isatty()");
    goto end;
  }
  setbuf(stdout, NULL);
  if (signal(SIGHUP, SIG_IGN) != SIG_DFL) {
    perror("signal(SIGHUP, IGN)");
    goto end;
  }
  if (ioctl(STDIN_FILENO, TIOCSCTTY, 0) != 0) {
    perror("ioctl(TIOCSCTTY)");
    goto end;
  }
  if (ioctl(STDIN_FILENO, TIOCNOTTY) != 0) {
    perror("ioctl(TIOCNOTTY)");
    goto end;
  }
end:
  for (;;)
    pause();
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1197 <main+0x37>
lea    0xe73(%rip),%rdi
call   1080 <perror@plt>
jmp    1250 <main+0xf0>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <setbuf@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%rax
je     11e1 <main+0x81>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
jmp    1250 <main+0xf0>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    $0x0,%al
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     1217 <main+0xb7>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
jmp    1250 <main+0xf0>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     124b <main+0xeb>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
jmp    1250 <main+0xf0>
jmp    1250 <main+0xf0>
jmp    1255 <main+0xf5>
mov    $0x0,%al
call   1070 <pause@plt>
jmp    1255 <main+0xf5>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e48(%rip),%rbx
mov    (%rbx),%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     11df <main+0x7f>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
jne    11e8 <main+0x88>
mov    (%rbx),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
xor    %eax,%eax
call   1050 <ioctl@plt>
test   %rax,%rax
jne    11f1 <main+0x91>
mov    (%rbx),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <ioctl@plt>
test   %rax,%rax
je     1200 <main+0xa0>
lea    0xe51(%rip),%rdi
jmp    11f8 <main+0x98>
lea    0xe1a(%rip),%rdi
jmp    11f8 <main+0x98>
lea    0xe1a(%rip),%rdi
jmp    11f8 <main+0x98>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
nopl   (%rax)
xor    %eax,%eax
call   1070 <pause@plt>
jmp    1200 <main+0xa0>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [9 x i8] c"isatty()\00"
@stdout = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIG_DFL = global i64 0
@.str.1 = [20 x i8] c"signal(SIGHUP, IGN)\00"
@TIOCSCTTY = global i32 0
@.str.2 = [17 x i8] c"ioctl(TIOCSCTTY)\00"
@TIOCNOTTY = global i32 0
@.str.3 = [17 x i8] c"ioctl(TIOCNOTTY)\00"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = load i32, i32* @STDIN_FILENO
%3 = call i32 @isatty(i32 %2)
%4 = icmp ne i32 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %32
7: ; preds = %0
%8 = load i32, i32* @stdout
%9 = call i32 @setbuf(i32 %8, i32* null)
%10 = load i32, i32* @SIGHUP
%11 = load i32, i32* @SIG_IGN
%12 = call i64 @signal(i32 %10, i32 %11)
%13 = load i64, i64* @SIG_DFL
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %32
17: ; preds = %7
%18 = load i32, i32* @STDIN_FILENO
%19 = load i32, i32* @TIOCSCTTY
%20 = call i64 (i32, i32, ...) @ioctl(i32 %18, i32 %19, i32 0)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %32
24: ; preds = %17
%25 = load i32, i32* @STDIN_FILENO
%26 = load i32, i32* @TIOCNOTTY
%27 = call i64 (i32, i32, ...) @ioctl(i32 %25, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %32
31: ; preds = %24
br label %32
32: ; preds = %31, %29, %22, %15, %5
br label %33
33: ; preds = %33, %32
%34 = call i32 (...) @pause()
br label %33
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [9 x i8] c"isatty()\00"
@stdout = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIG_DFL = global i64 0
@.str.1 = [20 x i8] c"signal(SIGHUP, IGN)\00"
@TIOCSCTTY = global i32 0
@.str.2 = [17 x i8] c"ioctl(TIOCSCTTY)\00"
@TIOCNOTTY = global i32 0
@.str.3 = [17 x i8] c"ioctl(TIOCNOTTY)\00"
define i32 @main() {
%1 = load i32, i32* @STDIN_FILENO
%2 = tail call i32 @isatty(i32 %1)
%3 = icmp eq i32 %2, 0
br i1 %3, label %22, label %4
4: ; preds = %0
%5 = load i32, i32* @stdout
%6 = tail call i32 @setbuf(i32 %5, i32* null)
%7 = load i32, i32* @SIGHUP
%8 = load i32, i32* @SIG_IGN
%9 = tail call i64 @signal(i32 %7, i32 %8)
%10 = load i64, i64* @SIG_DFL
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %22
12: ; preds = %4
%13 = load i32, i32* @STDIN_FILENO
%14 = load i32, i32* @TIOCSCTTY
%15 = tail call i64 (i32, i32, ...) @ioctl(i32 %13, i32 %14, i32 0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i32, i32* @STDIN_FILENO
%19 = load i32, i32* @TIOCNOTTY
%20 = tail call i64 (i32, i32, ...) @ioctl(i32 %18, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17, %12, %4, %0
%23 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), %12 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), %17 ]
%24 = tail call i32 @perror(i8* %23)
br label %25
25: ; preds = %22, %17
br label %26
26: ; preds = %25, %26
%27 = tail call i32 (...) @pause()
br label %26
}