#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*/ default_color(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int isatty(int /*<<< orphan*/);
int /*<<< orphan*/ start_color(int /*<<< orphan*/ *, int);
int vfprintf(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int cvfprintf(FILE *stream, int color, const char *fmt, va_list args) {
  int tty = isatty(fileno(stream));
  if (tty)
    start_color(stream, color);
  int ret = vfprintf(stream, fmt, args);
  if (tty)
    default_color(stream);
  return ret;
}


x86 assembly (clang, -O0):
<cvfprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     118f <cvfprintf+0x3f>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <start_color@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <vfprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11b5 <cvfprintf+0x65>
mov    -0x8(%rbp),%rdi
call   1050 <default_color@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cvfprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %rdi,%rbx
call   1060 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     11a3 <cvfprintf+0x53>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <start_color@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1070 <vfprintf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <default_color@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <vfprintf@plt>

LLVM IR (-O0):
define i32 @cvfprintf(i32* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load i32*, i32** %5
%12 = call i32 @fileno(i32* %11)
%13 = call i32 @isatty(i32 %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %4
%17 = load i32*, i32** %5
%18 = load i32, i32* %6
%19 = call i32 @start_color(i32* %17, i32 %18)
br label %20
20: ; preds = %16, %4
%21 = load i32*, i32** %5
%22 = load i8*, i8** %7
%23 = load i32, i32* %8
%24 = call i32 @vfprintf(i32* %21, i8* %22, i32 %23)
store i32 %24, i32* %10
%25 = load i32, i32* %9
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32*, i32** %5
%29 = call i32 @default_color(i32* %28)
br label %30
30: ; preds = %27, %20
%31 = load i32, i32* %10
ret i32 %31
}

LLVM IR (-O2):
define i32 @cvfprintf(i32* %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @fileno(i32* %0)
%6 = tail call i32 @isatty(i32 %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @vfprintf(i32* %0, i8* %2, i32 %3)
br label %14
10: ; preds = %4
%11 = tail call i32 @start_color(i32* %0, i32 %1)
%12 = tail call i32 @vfprintf(i32* %0, i8* %2, i32 %3)
%13 = tail call i32 @default_color(i32* %0)
br label %14
14: ; preds = %8, %10
%15 = phi i32 [ %9, %8 ], [ %12, %10 ]
ret i32 %15
}