#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ isatty(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int test_isatty() {
  int fd;
  printf("isatty 1: stdin %s\n", isatty(0) ? "yes OK" : "no");
  printf("isatty 2: stdout %s\n", isatty(1) ? "yes OK" : "no");
  printf("isatty 3: stderr %s\n", isatty(2) ? "yes OK" : "no");
  printf("isatty 4: invalid %s\n", isatty(999) ? "yes" : "no OK");
  fd = open("foo.fileio.test", O_RDONLY);
  if (fd >= 0) {
    printf("isatty 5: file %s\n", isatty(fd) ? "yes" : "no OK");
    close(fd);
  } else
    printf("isatty 5: file couldn't open\n");
}


x86 assembly (clang, -O0):
<test_isatty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %edi,%edi
call   1030 <isatty@plt>
mov    %rax,%rcx
lea    0xec2(%rip),%rsi
lea    0xeb4(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rsi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1030 <isatty@plt>
mov    %rax,%rcx
lea    0xe91(%rip),%rsi
lea    0xe83(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rsi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1030 <isatty@plt>
mov    %rax,%rcx
lea    0xe60(%rip),%rsi
lea    0xe52(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3e7,%edi
call   1030 <isatty@plt>
mov    %rax,%rcx
lea    0xe76(%rip),%rsi
lea    0xe6b(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1267 <test_isatty+0x127>
mov    -0x8(%rbp),%edi
call   1030 <isatty@plt>
mov    %rax,%rcx
lea    0xe25(%rip),%rsi
lea    0xe1a(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
jmp    1275 <test_isatty+0x135>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_isatty>:
push   %rbp
push   %r14
push   %rbx
xor    %edi,%edi
call   1040 <isatty@plt>
test   %rax,%rax
lea    0xeb6(%rip),%rbp
lea    0xea8(%rip),%rbx
mov    %rbx,%rsi
cmove  %rbp,%rsi
lea    0xe86(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1040 <isatty@plt>
test   %rax,%rax
mov    %rbx,%rsi
cmove  %rbp,%rsi
lea    0xe82(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%edi
call   1040 <isatty@plt>
test   %rax,%rax
cmove  %rbp,%rbx
lea    0xe78(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3e7,%edi
call   1040 <isatty@plt>
test   %rax,%rax
lea    0xe89(%rip),%r14
lea    0xe7e(%rip),%rbx
mov    %rbx,%rsi
cmove  %r14,%rsi
lea    0xe5a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1238 <test_isatty+0xe8>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <isatty@plt>
test   %rax,%rax
cmove  %r14,%rbx
lea    0xe53(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1244 <test_isatty+0xf4>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"isatty 1: stdin %s\0A\00"
@.str.1 = [7 x i8] c"yes OK\00"
@.str.2 = [3 x i8] c"no\00"
@.str.3 = [21 x i8] c"isatty 2: stdout %s\0A\00"
@.str.4 = [21 x i8] c"isatty 3: stderr %s\0A\00"
@.str.5 = [22 x i8] c"isatty 4: invalid %s\0A\00"
@.str.6 = [4 x i8] c"yes\00"
@.str.7 = [6 x i8] c"no OK\00"
@.str.8 = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@.str.9 = [19 x i8] c"isatty 5: file %s\0A\00"
@.str.10 = [30 x i8] c"isatty 5: file couldn't open\0A\00"
define i32 @test_isatty() {
%1 = alloca i32
%2 = alloca i32
%3 = call i64 @isatty(i32 0)
%4 = icmp ne i64 %3, 0
%5 = zext i1 %4 to i64
%6 = select i1 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0)
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = call i64 @isatty(i32 1)
%9 = icmp ne i64 %8, 0
%10 = zext i1 %9 to i64
%11 = select i1 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%13 = call i64 @isatty(i32 2)
%14 = icmp ne i64 %13, 0
%15 = zext i1 %14 to i64
%16 = select i1 %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %16)
%18 = call i64 @isatty(i32 999)
%19 = icmp ne i64 %18, 0
%20 = zext i1 %19 to i64
%21 = select i1 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i32 %23)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %0
%28 = load i32, i32* %2
%29 = call i64 @isatty(i32 %28)
%30 = icmp ne i64 %29, 0
%31 = zext i1 %30 to i64
%32 = select i1 %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
br label %38
36: ; preds = %0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.10, i64 0, i64 0))
br label %38
38: ; preds = %36, %27
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [20 x i8] c"isatty 1: stdin %s\0A\00"
@.str.1 = [7 x i8] c"yes OK\00"
@.str.2 = [3 x i8] c"no\00"
@.str.3 = [21 x i8] c"isatty 2: stdout %s\0A\00"
@.str.4 = [21 x i8] c"isatty 3: stderr %s\0A\00"
@.str.5 = [22 x i8] c"isatty 4: invalid %s\0A\00"
@.str.6 = [4 x i8] c"yes\00"
@.str.7 = [6 x i8] c"no OK\00"
@.str.8 = [16 x i8] c"foo.fileio.test\00"
@O_RDONLY = global i32 0
@.str.9 = [19 x i8] c"isatty 5: file %s\0A\00"
@str = [29 x i8] c"isatty 5: file couldn't open\00"
define i32 @test_isatty() {
%1 = tail call i64 @isatty(i32 0)
%2 = icmp eq i64 %1, 0
%3 = select i1 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %3)
%5 = tail call i64 @isatty(i32 1)
%6 = icmp eq i64 %5, 0
%7 = select i1 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%9 = tail call i64 @isatty(i32 2)
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%13 = tail call i64 @isatty(i32 999)
%14 = icmp eq i64 %13, 0
%15 = select i1 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i32 %17)
%19 = icmp sgt i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %0
%21 = tail call i64 @isatty(i32 %18)
%22 = icmp eq i64 %21, 0
%23 = select i1 %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i8* %23)
%25 = tail call i32 @close(i32 %18)
br label %28
26: ; preds = %0
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
ret i32 undef
}