#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ EBADF;
int /*<<< orphan*/ ENOTTY;
scalar_t__ S_ISCHR(int /*<<< orphan*/);
scalar_t__ _fstat(int, struct stat *);
int /*<<< orphan*/ errno;
int _isatty(int fd) {
  struct stat buf;
  if (_fstat(fd, &buf) < 0) {
    errno = EBADF;
    return 0;
  }
  if (S_ISCHR(buf.st_mode))
    return 1;
  errno = ENOTTY;
  return 0;
}


x86 assembly (clang, -O0):
<_isatty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <_fstat@plt>
cmp    $0x0,%rax
jge    115f <_isatty+0x3f>
mov    0x2e88(%rip),%rax
mov    (%rax),%ecx
mov    0x2e87(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1196 <_isatty+0x76>
mov    -0x10(%rbp),%edi
call   1030 <S_ISCHR@plt>
cmp    $0x0,%rax
je     117d <_isatty+0x5d>
movl   $0x1,-0x4(%rbp)
jmp    1196 <_isatty+0x76>
mov    0x2e64(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4b(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_isatty>:
push   %rax
mov    %rsp,%rsi
call   1040 <_fstat@plt>
test   %rax,%rax
js     1145 <_isatty+0x25>
mov    (%rsp),%edi
call   1030 <S_ISCHR@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
je     114e <_isatty+0x2e>
pop    %rcx
ret
mov    0x2e84(%rip),%rax
jmp    1155 <_isatty+0x35>
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    0x2e7a(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
pop    %rcx
ret

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

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