#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 (*FuncPtr)(int);

/* Variables and functions */
int /*<<< orphan*/ RTLD_LAZY;
int STDERR_FILENO;
int STDOUT_FILENO;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *const, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int isatty(int fd) {
  typedef int (*FuncPtr)(int);
  static FuncPtr realImpl = NULL;
  if (!realImpl) {
    void *const handle = dlopen("libc.so.6", RTLD_LAZY);
    if (!handle) {
      fprintf(stderr, "dlopen() failed: %s\n", dlerror());
      exit(1);
    }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
    realImpl = (FuncPtr)dlsym(handle, "isatty");
#pragma GCC diagnostic pop
    if (!realImpl) {
      fprintf(stderr, "dlsym() failed: %s\n", dlerror());
      exit(1);
    }
  }
  if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
    return 1;
  } else {
    return realImpl(fd);
  }
}


x86 assembly (clang, -O0):
<isatty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpq   $0x0,0x2eed(%rip)
jne    1215 <isatty+0xc5>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bc <isatty+0x6c>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,0x2e7d(%rip)
cmpq   $0x0,0x2e75(%rip)
jne    1210 <isatty+0xc0>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe25(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1215 <isatty+0xc5>
mov    -0x8(%rbp),%eax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%eax
je     1239 <isatty+0xe9>
mov    -0x8(%rbp),%eax
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%eax
jne    1245 <isatty+0xf5>
movl   $0x1,-0x4(%rbp)
jmp    1254 <isatty+0x104>
mov    0x2e04(%rip),%rax
mov    -0x8(%rbp),%edi
call   *%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isatty>:
push   %rbx
mov    %edi,%ebx
mov    0x2ef6(%rip),%rax
test   %rax,%rax
jne    1194 <isatty+0x44>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11b6 <isatty+0x66>
lea    0xe9f(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
mov    %rax,0x2ec1(%rip)
test   %rax,%rax
je     11cf <isatty+0x7f>
mov    0x2e55(%rip),%rcx
cmp    %ebx,(%rcx)
je     11af <isatty+0x5f>
mov    0x2e2a(%rip),%rcx
cmp    %ebx,(%rcx)
je     11af <isatty+0x5f>
mov    %ebx,%edi
pop    %rbx
jmp    *%rax
mov    $0x1,%eax
pop    %rbx
ret
mov    0x2e1b(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe3d(%rip),%rsi
jmp    11e6 <isatty+0x96>
mov    0x2e02(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe40(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@isatty.realImpl = internal global i32 (i32)* null
@.str = [10 x i8] c"libc.so.6\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"dlopen() failed: %s\0A\00"
@.str.2 = [7 x i8] c"isatty\00"
@.str.3 = [20 x i8] c"dlsym() failed: %s\0A\00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @isatty(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
%5 = load i32 (i32)*, i32 (i32)** @isatty.realImpl
%6 = icmp ne i32 (i32)* %5, null
br i1 %6, label %29, label %7
7: ; preds = %1
%8 = load i32, i32* @RTLD_LAZY
%9 = call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %17, label %12
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = call i8* (...) @dlerror()
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %7
%18 = load i8*, i8** %4
%19 = call i64 @dlsym(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%20 = inttoptr i64 %19 to i32 (i32)*
store i32 (i32)* %20, i32 (i32)** @isatty.realImpl
%21 = load i32 (i32)*, i32 (i32)** @isatty.realImpl
%22 = icmp ne i32 (i32)* %21, null
br i1 %22, label %28, label %23
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = call i8* (...) @dlerror()
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
br label %29
29: ; preds = %28, %1
%30 = load i32, i32* %3
%31 = load i32, i32* @STDOUT_FILENO
%32 = icmp eq i32 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i32, i32* %3
%35 = load i32, i32* @STDERR_FILENO
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33, %29
store i32 1, i32* %2
br label %42
38: ; preds = %33
%39 = load i32 (i32)*, i32 (i32)** @isatty.realImpl
%40 = load i32, i32* %3
%41 = call i32 %39(i32 %40)
store i32 %41, i32* %2
br label %42
42: ; preds = %38, %37
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@isatty.realImpl = internal unnamed_addr global i32 (i32)* null
@.str = [10 x i8] c"libc.so.6\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"dlopen() failed: %s\0A\00"
@.str.2 = [7 x i8] c"isatty\00"
@.str.3 = [20 x i8] c"dlsym() failed: %s\0A\00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @isatty(i32 %0) {
%2 = load i32 (i32)*, i32 (i32)** @isatty.realImpl
%3 = icmp eq i32 (i32)* %2, null
br i1 %3, label %4, label %22
4: ; preds = %1
%5 = load i32, i32* @RTLD_LAZY
%6 = tail call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = tail call i8* (...) @dlerror()
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %4
%14 = tail call i64 @dlsym(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%15 = inttoptr i64 %14 to i32 (i32)*
store i32 (i32)* %15, i32 (i32)** @isatty.realImpl
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i8* (...) @dlerror()
%20 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13, %1
%23 = phi i32 (i32)* [ %15, %13 ], [ %2, %1 ]
%24 = load i32, i32* @STDOUT_FILENO
%25 = icmp eq i32 %24, %0
%26 = load i32, i32* @STDERR_FILENO
%27 = icmp eq i32 %26, %0
%28 = select i1 %25, i1 true, i1 %27
br i1 %28, label %31, label %29
29: ; preds = %22
%30 = tail call i32 %23(i32 %0)
br label %31
31: ; preds = %22, %29
%32 = phi i32 [ %30, %29 ], [ 1, %22 ]
ret i32 %32
}