#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 */
char *getenv(char *);
int istty();
int tgetent(char *, char *);
int tgetnum(char *);
int numcolors() {
  int n, tty;
  char *c = "Co";
  char *term, tbuf[1024];
  if ((tty = istty()) != 1 || !(term = getenv("TERM")) ||
      tgetent(tbuf, term) != 1)
    return -1;
  if ((n = tgetnum(c)) != 1)
    return n;
  return -1;
}


x86 assembly (clang, -O0):
<numcolors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xeae(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <istty@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x1,%eax
jne    119c <numcolors+0x5c>
lea    0xe93(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     119c <numcolors+0x5c>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <tgetent@plt>
cmp    $0x1,%eax
je     11a8 <numcolors+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <numcolors+0x8f>
mov    -0x18(%rbp),%rdi
call   1050 <tgetnum@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x1,%eax
je     11c8 <numcolors+0x88>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cf <numcolors+0x8f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<numcolors>:
push   %rbx
sub    $0x400,%rsp
xor    %eax,%eax
call   1060 <istty@plt>
mov    $0xffffffff,%ebx
cmp    $0x1,%eax
jne    1191 <numcolors+0x51>
lea    0xea3(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1191 <numcolors+0x51>
mov    %rsp,%rdi
mov    %rax,%rsi
call   1040 <tgetent@plt>
cmp    $0x1,%eax
jne    1191 <numcolors+0x51>
lea    0xe7f(%rip),%rdi
call   1050 <tgetnum@plt>
cmp    $0x1,%eax
mov    $0xffffffff,%ebx
cmovne %eax,%ebx
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"Co\00"
@.str.1 = [5 x i8] c"TERM\00"
define i32 @numcolors() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [1024 x i8]
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %4
%7 = call i32 (...) @istty()
store i32 %7, i32* %3
%8 = icmp ne i32 %7, 1
br i1 %8, label %17, label %9
9: ; preds = %0
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %10, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%14 = load i8*, i8** %5
%15 = call i32 @tgetent(i8* %13, i8* %14)
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %12, %9, %0
store i32 -1, i32* %1
br label %25
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = call i32 @tgetnum(i8* %19)
store i32 %20, i32* %2
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i32, i32* %2
store i32 %23, i32* %1
br label %25
24: ; preds = %18
store i32 -1, i32* %1
br label %25
25: ; preds = %24, %22, %17
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [3 x i8] c"Co\00"
@.str.1 = [5 x i8] c"TERM\00"
define i32 @numcolors() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @istty()
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %15
5: ; preds = %0
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %15, label %8
8: ; preds = %5
%9 = call i32 @tgetent(i8* %2, i8* %6)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = call i32 @tgetnum(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32 %12, 1
%14 = select i1 %13, i32 -1, i32 %12
br label %15
15: ; preds = %11, %0, %5, %8
%16 = phi i32 [ -1, %8 ], [ -1, %5 ], [ -1, %0 ], [ %14, %11 ]
ret i32 %16
}