#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 */
scalar_t__ isatty(int);
bool on_tty(int fd) {
  static int cached_on_tty[256];
  if (fd >= 256)
    return isatty(fd) > 0;
  if (cached_on_tty[fd] == 0)
    cached_on_tty[fd] = isatty(fd) > 0 ? 2 : 1;
  return cached_on_tty[fd] == 2;
}


x86 assembly (clang, -O0):
<on_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x100,-0x8(%rbp)
jl     1144 <on_tty+0x34>
mov    -0x8(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
setg   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    119d <on_tty+0x8d>
movslq -0x8(%rbp),%rcx
lea    0x2ef1(%rip),%rax
cmpl   $0x0,(%rax,%rcx,4)
jne    1183 <on_tty+0x73>
mov    -0x8(%rbp),%edi
call   1030 <isatty@plt>
mov    %rax,%rcx
mov    $0x1,%edx
mov    $0x2,%eax
cmp    $0x0,%rcx
cmovg  %eax,%edx
movslq -0x8(%rbp),%rcx
lea    0x2ec0(%rip),%rax
mov    %edx,(%rax,%rcx,4)
movslq -0x8(%rbp),%rcx
lea    0x2eb2(%rip),%rax
cmpl   $0x2,(%rax,%rcx,4)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<on_tty>:
push   %r14
push   %rbx
push   %rax
cmp    $0x100,%edi
jl     1129 <on_tty+0x19>
call   1030 <isatty@plt>
test   %rax,%rax
setg   %al
jmp    1155 <on_tty+0x45>
movslq %edi,%rbx
lea    0x2f0d(%rip),%r14
mov    (%r14,%rbx,4),%ecx
test   %ecx,%ecx
jne    114f <on_tty+0x3f>
call   1030 <isatty@plt>
xor    %ecx,%ecx
test   %rax,%rax
setg   %cl
add    $0x1,%ecx
mov    %ecx,(%r14,%rbx,4)
cmp    $0x2,%ecx
sete   %al
movzbl %al,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@on_tty.cached_on_tty = internal global [256 x i32] zeroinitializer
define i32 @on_tty(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp sge i32 %4, 256
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* %3
%8 = call i64 @isatty(i32 %7)
%9 = icmp sgt i64 %8, 0
%10 = zext i1 %9 to i32
store i32 %10, i32* %2
br label %33
11: ; preds = %1
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [256 x i32], [256 x i32]* @on_tty.cached_on_tty, i64 0, i64 %13
%15 = load i32, i32* %14
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %11
%18 = load i32, i32* %3
%19 = call i64 @isatty(i32 %18)
%20 = icmp sgt i64 %19, 0
%21 = zext i1 %20 to i64
%22 = select i1 %20, i32 2, i32 1
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [256 x i32], [256 x i32]* @on_tty.cached_on_tty, i64 0, i64 %24
store i32 %22, i32* %25
br label %26
26: ; preds = %17, %11
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [256 x i32], [256 x i32]* @on_tty.cached_on_tty, i64 0, i64 %28
%30 = load i32, i32* %29
%31 = icmp eq i32 %30, 2
%32 = zext i1 %31 to i32
store i32 %32, i32* %2
br label %33
33: ; preds = %26, %6
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@on_tty.cached_on_tty = internal unnamed_addr global [256 x i32] zeroinitializer
define i32 @on_tty(i32 %0) {
%2 = icmp sgt i32 %0, 255
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = tail call i64 @isatty(i32 %0)
%5 = icmp sgt i64 %4, 0
br label %18
6: ; preds = %1
%7 = sext i32 %0 to i64
%8 = getelementptr inbounds [256 x i32], [256 x i32]* @on_tty.cached_on_tty, i64 0, i64 %7
%9 = load i32, i32* %8
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = tail call i64 @isatty(i32 %0)
%13 = icmp sgt i64 %12, 0
%14 = select i1 %13, i32 2, i32 1
store i32 %14, i32* %8
br label %15
15: ; preds = %11, %6
%16 = phi i32 [ %14, %11 ], [ %9, %6 ]
%17 = icmp eq i32 %16, 2
br label %18
18: ; preds = %15, %3
%19 = phi i1 [ %5, %3 ], [ %17, %15 ]
%20 = zext i1 %19 to i32
ret i32 %20
}