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

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ vfprintf(int /*<<< orphan*/ *, char const *,
                            int /*<<< orphan*/);
void color_log(FILE *f, const char *color, const char *fmt, va_list vl) {
  char *reset = "\033[0m";
  if (!isatty(fileno(f))) {
    color = "";
    reset = "";
  }
  fputs(color, f);
  vfprintf(f, fmt, vl);
  fputs(reset, f);
  if (fmt[strlen(fmt) - 1] == '\n') {
    fflush(f);
  }
}


x86 assembly (clang, -O0):
<color_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    0xe82(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    11b1 <color_log+0x51>
lea    0xe62(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe57(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <vfprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    120e <color_log+0xae>
mov    -0x8(%rbp),%rdi
call   1070 <fflush@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<color_log>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%r12
call   1060 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
test   %eax,%eax
lea    0xe7b(%rip),%rax
cmove  %rax,%rbp
lea    0xe6c(%rip),%rbx
cmove  %rax,%rbx
mov    %rbp,%rdi
mov    %r12,%rsi
call   1050 <fputs@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1080 <vfprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <fputs@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r14,1)
jne    11de <color_log+0x7e>
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fflush@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"\1B[0m\00"
@.str.1 = [1 x i8] zeroinitializer
define void @color_log(i32* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i32*, i32** %5
%11 = call i32 @fileno(i32* %10)
%12 = call i32 @isatty(i32 %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %4
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %9
br label %15
15: ; preds = %14, %4
%16 = load i8*, i8** %6
%17 = load i32*, i32** %5
%18 = call i32 @fputs(i8* %16, i32* %17)
%19 = load i32*, i32** %5
%20 = load i8*, i8** %7
%21 = load i32, i32* %8
%22 = call i32 @vfprintf(i32* %19, i8* %20, i32 %21)
%23 = load i8*, i8** %9
%24 = load i32*, i32** %5
%25 = call i32 @fputs(i8* %23, i32* %24)
%26 = load i8*, i8** %7
%27 = load i8*, i8** %7
%28 = call i32 @strlen(i8* %27)
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %26, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 10
br i1 %34, label %35, label %38
35: ; preds = %15
%36 = load i32*, i32** %5
%37 = call i32 @fflush(i32* %36)
br label %38
38: ; preds = %35, %15
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"\1B[0m\00"
@.str.1 = [1 x i8] zeroinitializer
define void @color_log(i32* nocapture %0, i8* nocapture %1, i8* %2, i32 %3) {
%5 = tail call i32 @fileno(i32* %0)
%6 = tail call i32 @isatty(i32 %5)
%7 = icmp eq i32 %6, 0
%8 = select i1 %7, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %1
%9 = select i1 %7, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0)
%10 = tail call i32 @fputs(i8* %8, i32* %0)
%11 = tail call i32 @vfprintf(i32* %0, i8* %2, i32 %3)
%12 = tail call i32 @fputs(i8* %9, i32* %0)
%13 = tail call i32 @strlen(i8* %2)
%14 = add nsw i32 %13, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %2, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 10
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = tail call i32 @fflush(i32* %0)
br label %21
21: ; preds = %19, %4
ret void
}