#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 */
struct winsize {
  int dummy;
};

/* Variables and functions */
scalar_t__ EBADF;
scalar_t__ ENOTTY;
int /*<<< orphan*/ TIOCGWINSZ;
scalar_t__ errno;
unsigned long syscall(int, int, int /*<<< orphan*/, struct winsize *);
int isatty(int fd) {
  struct winsize wsz;
  unsigned long r = syscall(54, fd, TIOCGWINSZ, &wsz);
  if (r == 0)
    return 1;
  if (errno != EBADF)
    errno = ENOTTY;
  return 0;
}


x86 assembly (clang, -O0):
<isatty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
mov    $0x36,%edi
lea    -0x10(%rbp),%rcx
call   1030 <syscall@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1150 <isatty+0x40>
movl   $0x1,-0x4(%rbp)
jmp    1185 <isatty+0x75>
mov    0x2e81(%rip),%rax
mov    (%rax),%rax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
je     117e <isatty+0x6e>
mov    0x2e77(%rip),%rax
mov    (%rax),%rcx
mov    0x2e5d(%rip),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isatty>:
push   %rax
mov    %edi,%esi
mov    0x2ea6(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x36,%edi
call   1030 <syscall@plt>
test   %rax,%rax
je     1155 <isatty+0x45>
mov    0x2ea3(%rip),%rcx
mov    (%rcx),%rdx
mov    0x2e91(%rip),%rsi
xor    %eax,%eax
cmp    (%rsi),%rdx
je     1153 <isatty+0x43>
mov    0x2e9b(%rip),%rdx
mov    (%rdx),%rdx
mov    %rdx,(%rcx)
pop    %rcx
ret
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.winsize = type { i32 }
@TIOCGWINSZ = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@ENOTTY = global i64 0
define i32 @isatty(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.winsize
%5 = alloca i64
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = load i32, i32* @TIOCGWINSZ
%8 = call i64 @syscall(i32 54, i32 %6, i32 %7, %struct.winsize* %4)
store i64 %8, i64* %5
%9 = load i64, i64* %5
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 1, i32* %2
br label %19
12: ; preds = %1
%13 = load i64, i64* @errno
%14 = load i64, i64* @EBADF
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i64, i64* @ENOTTY
store i64 %17, i64* @errno
br label %18
18: ; preds = %16, %12
store i32 0, i32* %2
br label %19
19: ; preds = %18, %11
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
%struct.winsize = type { i32 }
@TIOCGWINSZ = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@ENOTTY = global i64 0
define i32 @isatty(i32 %0) {
%2 = alloca %struct.winsize
%3 = bitcast %struct.winsize* %2 to i8*
%4 = load i32, i32* @TIOCGWINSZ
%5 = call i64 @syscall(i32 54, i32 %0, i32 %4, %struct.winsize* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @EBADF
%10 = icmp eq i64 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = load i64, i64* @ENOTTY
store i64 %12, i64* @errno
br label %13
13: ; preds = %7, %11, %1
%14 = phi i32 [ 1, %1 ], [ 0, %11 ], [ 0, %7 ]
ret i32 %14
}