#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 */
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ write_ansi(int *, int *, int, int, char const *, size_t, int,
                              int *);
bool write_color(int *fdp, const char *code, bool *is_on, bool want, int fda,
                 int fdb, bool *try_splice) {
  bool ret = false;
  char highlight[8];
  static const char restore[] = "\x1b[0m";
  const char *ansi = want ? highlight : restore;
  size_t pick = want ? sizeof highlight : sizeof restore;
  snprintf(highlight, sizeof highlight, "\x1b[%s;1m", code);
  if (!write_ansi(fdp, is_on, want, fda, ansi, pick - 1, fdb, try_splice))
    goto fail;
  ret = true;
fail:
  return ret;
}


x86 assembly (clang, -O0):
<write_color>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1161 <write_color+0x41>
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1171 <write_color+0x51>
lea    0xe98(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1171 <write_color+0x51>
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edx
mov    $0x5,%eax
mov    $0x8,%ecx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x8,%esi
lea    0xe60(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x38(%rbp),%r8
mov    -0x40(%rbp),%r9
sub    $0x1,%r9
mov    -0x24(%rbp),%r10d
mov    0x10(%rbp),%rax
mov    %r10d,(%rsp)
mov    %rax,0x8(%rsp)
call   1030 <write_ansi@plt>
cmp    $0x0,%eax
jne    11e8 <write_color+0xc8>
jmp    11ef <write_color+0xcf>
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_color>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x8(%rsp)
mov    %r8d,%r15d
mov    %ecx,%ebx
mov    %rdx,%r12
mov    %rsi,%rcx
mov    %rdi,%r13
xor    %r14d,%r14d
test   %ebx,%ebx
setne  %r14b
lea    0xeaf(%rip),%rbp
lea    0x10(%rsp),%rdi
cmovne %rdi,%rbp
lea    0xea4(%rip),%rdx
mov    $0x8,%esi
call   1040 <snprintf@plt>
lea    (%r14,%r14,2),%r9
or     $0x4,%r9
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %r15d,%ecx
mov    %rbp,%r8
push   0x50(%rsp)
push   0x10(%rsp)
call   1030 <write_ansi@plt>
add    $0x10,%rsp
xor    %ecx,%ecx
test   %eax,%eax
setne  %cl
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@write_color.restore = internal constant [5 x i8] c"\1B[0m\00"
@.str = [8 x i8] c"\1B[%s;1m\00"
define i32 @write_color(i32* %0, i8* %1, i32* %2, i32 %3, i32 %4, i32 %5, i32* %6) {
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32
%16 = alloca [8 x i8]
%17 = alloca i8*
%18 = alloca i64
store i32* %0, i32** %8
store i8* %1, i8** %9
store i32* %2, i32** %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32* %6, i32** %14
store i32 0, i32* %15
%19 = load i32, i32* %11
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %7
%22 = getelementptr inbounds [8 x i8], [8 x i8]* %16, i64 0, i64 0
br label %24
23: ; preds = %7
br label %24
24: ; preds = %23, %21
%25 = phi i8* [ %22, %21 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @write_color.restore, i64 0, i64 0), %23 ]
store i8* %25, i8** %17
%26 = load i32, i32* %11
%27 = icmp ne i32 %26, 0
%28 = zext i1 %27 to i64
%29 = select i1 %27, i64 8, i64 5
store i64 %29, i64* %18
%30 = getelementptr inbounds [8 x i8], [8 x i8]* %16, i64 0, i64 0
%31 = load i8*, i8** %9
%32 = call i32 @snprintf(i8* %30, i32 8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = load i32*, i32** %8
%34 = load i32*, i32** %10
%35 = load i32, i32* %11
%36 = load i32, i32* %12
%37 = load i8*, i8** %17
%38 = load i64, i64* %18
%39 = sub i64 %38, 1
%40 = load i32, i32* %13
%41 = load i32*, i32** %14
%42 = call i32 @write_ansi(i32* %33, i32* %34, i32 %35, i32 %36, i8* %37, i64 %39, i32 %40, i32* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %24
br label %46
45: ; preds = %24
store i32 1, i32* %15
br label %46
46: ; preds = %45, %44
%47 = load i32, i32* %15
ret i32 %47
}

LLVM IR (-O2):
@write_color.restore = internal constant [5 x i8] c"\1B[0m\00"
@.str = [8 x i8] c"\1B[%s;1m\00"
define i32 @write_color(i32* %0, i8* %1, i32* %2, i32 %3, i32 %4, i32 %5, i32* %6) {
%8 = alloca [8 x i8]
%9 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%10 = icmp eq i32 %3, 0
%11 = select i1 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @write_color.restore, i64 0, i64 0), i8* %9
%12 = call i32 @snprintf(i8* %9, i32 8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %1)
%13 = select i1 %10, i64 4, i64 7
%14 = call i32 @write_ansi(i32* %0, i32* %2, i32 %3, i32 %4, i8* %11, i64 %13, i32 %5, i32* %6)
%15 = icmp ne i32 %14, 0
%16 = zext i1 %15 to i32
ret i32 %16
}