#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*/ exit(int);
scalar_t__ insist_write(int, char *, scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
scalar_t__ strlen(char *);
void set_xterm_color(int fd, unsigned char color) {
  char buf[100];
  snprintf(buf, 100, "\033[38;5;%dm", color);
  if (insist_write(fd, buf, strlen(buf)) != strlen(buf)) {
    perror("set_xterm_color: insist_write");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<set_xterm_color>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xe8c(%rip),%rdx
movzbl -0x5(%rbp),%ecx
call   1040 <snprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x84(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x70(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rsi
mov    %rax,%rdx
call   1050 <insist_write@plt>
mov    %rax,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x78(%rbp),%rax
cmp    %rcx,%rax
je     11dc <set_xterm_color+0x8c>
lea    0xe3e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_xterm_color>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %esi,%ecx
mov    %edi,%ebp
lea    0xe9f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <insist_write@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    %rax,%rbp
jne    119d <set_xterm_color+0x4d>
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe67(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"\1B[38;5;%dm\00"
@.str.1 = [30 x i8] c"set_xterm_color: insist_write\00"
define void @set_xterm_color(i32 %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca [100 x i8]
store i32 %0, i32* %3
store i8 %1, i8* %4
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%7 = load i8, i8* %4
%8 = call i32 @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 zeroext %7)
%9 = load i32, i32* %3
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%12 = call i64 @strlen(i8* %11)
%13 = call i64 @insist_write(i32 %9, i8* %10, i64 %12)
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%15 = call i64 @strlen(i8* %14)
%16 = icmp ne i64 %13, %15
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"\1B[38;5;%dm\00"
@.str.1 = [30 x i8] c"set_xterm_color: insist_write\00"
define void @set_xterm_color(i32 %0, i8 zeroext %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 100, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 zeroext %1)
%6 = call i64 @strlen(i8* %4)
%7 = call i64 @insist_write(i32 %0, i8* %4, i64 %6)
%8 = call i64 @strlen(i8* %4)
%9 = icmp eq i64 %7, %8
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
ret void
}