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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int isatty(int);
int istty() {
  FILE *fp;
  int fd, tty;
  fp = fopen("/dev/tty", "r");
  if (!fp)
    return -1;
  fd = fileno(fp);
  if (fd < 0)
    return -1;
  tty = isatty(fd);
  fclose(fp);
  return tty == 1;
}


x86 assembly (clang, -O0):
<istty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1176 <istty+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <istty+0x7b>
mov    -0x10(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1198 <istty+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <istty+0x7b>
mov    -0x14(%rbp),%edi
call   1030 <isatty@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x1,-0x18(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<istty>:
push   %rbp
push   %r14
push   %rbx
lea    0xeb5(%rip),%rdi
lea    0xeb7(%rip),%rsi
call   1060 <fopen@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1189 <istty+0x49>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <fileno@plt>
test   %eax,%eax
js     1189 <istty+0x49>
mov    %eax,%edi
call   1030 <isatty@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %ebx,%ebx
cmp    $0x1,%ebp
sete   %bl
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @istty() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %9, label %8
8: ; preds = %0
store i32 -1, i32* %1
br label %23
9: ; preds = %0
%10 = load i32*, i32** %2
%11 = call i32 @fileno(i32* %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 -1, i32* %1
br label %23
15: ; preds = %9
%16 = load i32, i32* %3
%17 = call i32 @isatty(i32 %16)
store i32 %17, i32* %4
%18 = load i32*, i32** %2
%19 = call i32 @fclose(i32* %18)
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 1
%22 = zext i1 %21 to i32
store i32 %22, i32* %1
br label %23
23: ; preds = %15, %14, %8
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @istty() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %11, label %3
3: ; preds = %0
%4 = tail call i32 @fileno(i32* %1)
%5 = icmp slt i32 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = tail call i32 @isatty(i32 %4)
%8 = tail call i32 @fclose(i32* %1)
%9 = icmp eq i32 %7, 1
%10 = zext i1 %9 to i32
br label %11
11: ; preds = %3, %0, %6
%12 = phi i32 [ %10, %6 ], [ -1, %0 ], [ -1, %3 ]
ret i32 %12
}