#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
char *getenv(char *);
int isatty(int /*<<< orphan*/);
int /*<<< orphan*/ strcmp(char const *, char *);
bool color_mode(FILE *file) {
  static int mode = -1;
  const char *var;
  if (mode != -1)
    return mode;
  var = getenv("WG_COLOR_MODE");
  if (var && !strcmp(var, "always"))
    mode = true;
  else if (var && !strcmp(var, "never"))
    mode = false;
  else
    return isatty(fileno(file));
  return mode;
}


x86 assembly (clang, -O0):
<color_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpl   $0xffffffff,0x2eed(%rip)
je     1167 <color_mode+0x27>
mov    0x2ee1(%rip),%eax
mov    %eax,-0x4(%rbp)
jmp    1203 <color_mode+0xc3>
lea    0xe92(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11aa <color_mode+0x6a>
mov    -0x18(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11aa <color_mode+0x6a>
movl   $0x1,0x2e9b(%rip)
jmp    11fa <color_mode+0xba>
cmpq   $0x0,-0x18(%rbp)
je     11dd <color_mode+0x9d>
mov    -0x18(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11dd <color_mode+0x9d>
movl   $0x0,0x2e68(%rip)
jmp    11f5 <color_mode+0xb5>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1040 <isatty@plt>
mov    %eax,-0x4(%rbp)
jmp    1203 <color_mode+0xc3>
jmp    11fa <color_mode+0xba>
mov    0x2e40(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<color_mode>:
push   %r14
push   %rbx
push   %rax
mov    0x2ef6(%rip),%eax
cmp    $0xffffffff,%eax
je     1157 <color_mode+0x17>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rdi,%r14
lea    0xe9f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1194 <color_mode+0x54>
mov    %rax,%rbx
lea    0xe99(%rip),%rsi
mov    %rax,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     11aa <color_mode+0x6a>
lea    0xe8d(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     11b1 <color_mode+0x71>
mov    %r14,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <isatty@plt>
mov    $0x1,%eax
jmp    11b3 <color_mode+0x73>
xor    %eax,%eax
mov    %eax,0x2e87(%rip)
jmp    114f <color_mode+0xf>

LLVM IR (-O0):
@color_mode.mode = internal global i32 -1
@.str = [14 x i8] c"WG_COLOR_MODE\00"
@.str.1 = [7 x i8] c"always\00"
@.str.2 = [6 x i8] c"never\00"
define i32 @color_mode(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
store i32* %0, i32** %3
%5 = load i32, i32* @color_mode.mode
%6 = icmp ne i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i32, i32* @color_mode.mode
store i32 %8, i32* %2
br label %33
9: ; preds = %1
%10 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = call i32 @strcmp(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp ne i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %13
store i32 1, i32* @color_mode.mode
br label %31
18: ; preds = %13, %9
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = call i32 @strcmp(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %21
store i32 0, i32* @color_mode.mode
br label %30
26: ; preds = %21, %18
%27 = load i32*, i32** %3
%28 = call i32 @fileno(i32* %27)
%29 = call i32 @isatty(i32 %28)
store i32 %29, i32* %2
br label %33
30: ; preds = %25
br label %31
31: ; preds = %30, %17
%32 = load i32, i32* @color_mode.mode
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %26, %7
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@color_mode.mode = internal unnamed_addr global i32 -1
@.str = [14 x i8] c"WG_COLOR_MODE\00"
@.str.1 = [7 x i8] c"always\00"
@.str.2 = [6 x i8] c"never\00"
define i32 @color_mode(i32* nocapture %0) {
%2 = load i32, i32* @color_mode.mode
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %18
4: ; preds = %1
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = tail call i32 @strcmp(i8* %5, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = tail call i32 @strcmp(i8* %5, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %4, %10
%14 = tail call i32 @fileno(i32* %0)
%15 = tail call i32 @isatty(i32 %14)
br label %18
16: ; preds = %10, %7
%17 = phi i32 [ 1, %7 ], [ 0, %10 ]
store i32 %17, i32* @color_mode.mode
br label %18
18: ; preds = %1, %16, %13
%19 = phi i32 [ %15, %13 ], [ %17, %16 ], [ %2, %1 ]
ret i32 %19
}