#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 uint8_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ color_split_rgb(int, int *, int *, int *);
int /*<<< orphan*/ term_add_byte(char);
int /*<<< orphan*/ term_add_bytes(char *, int);
int /*<<< orphan*/ term_add_uint(int);
void do_set_color(int32_t color, char ch) {
  if (color < 0) {
    return;
  }
  term_add_byte(';');
  term_add_byte(ch);
  if ((color < 8)) {
    term_add_byte('0' + color);
  } else if (color < 256) {
    term_add_bytes("8;5;", (sizeof(""
                                   "8;5;"
                                   "") -
                            1));
    term_add_uint(color);
  } else {
    uint8_t r, g, b;
    color_split_rgb(color, &r, &g, &b);
    term_add_bytes("8;2;", (sizeof(""
                                   "8;2;"
                                   "") -
                            1));
    term_add_uint(r);
    term_add_byte(';');
    term_add_uint(g);
    term_add_byte(';');
    term_add_uint(b);
  }
}


x86 assembly (clang, -O0):
<do_set_color>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1160 <do_set_color+0x20>
jmp    1211 <do_set_color+0xd1>
mov    $0x3b,%edi
call   1040 <term_add_byte@plt>
movsbl -0x5(%rbp),%edi
call   1040 <term_add_byte@plt>
cmpl   $0x8,-0x4(%rbp)
jge    1190 <do_set_color+0x50>
mov    -0x4(%rbp),%eax
add    $0x30,%eax
movsbl %al,%edi
call   1040 <term_add_byte@plt>
jmp    1211 <do_set_color+0xd1>
cmpl   $0x100,-0x4(%rbp)
jge    11bb <do_set_color+0x7b>
lea    0xe5c(%rip),%rdi
mov    $0x4,%esi
call   1030 <term_add_bytes@plt>
mov    -0x4(%rbp),%edi
call   1060 <term_add_uint@plt>
jmp    120c <do_set_color+0xcc>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x14(%rbp),%rcx
call   1050 <color_split_rgb@plt>
lea    0xe2f(%rip),%rdi
mov    $0x4,%esi
call   1030 <term_add_bytes@plt>
mov    -0xc(%rbp),%edi
call   1060 <term_add_uint@plt>
mov    $0x3b,%edi
call   1040 <term_add_byte@plt>
mov    -0x10(%rbp),%edi
call   1060 <term_add_uint@plt>
mov    $0x3b,%edi
call   1040 <term_add_byte@plt>
mov    -0x14(%rbp),%edi
call   1060 <term_add_uint@plt>
jmp    1211 <do_set_color+0xd1>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_set_color>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
test   %edi,%edi
js     11f7 <do_set_color+0xb7>
mov    %esi,%ebp
mov    %edi,%ebx
mov    $0x3b,%edi
call   1040 <term_add_byte@plt>
movsbl %bpl,%edi
call   1040 <term_add_byte@plt>
cmp    $0x7,%ebx
ja     117b <do_set_color+0x3b>
add    $0x30,%bl
movsbl %bl,%edi
add    $0x18,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <term_add_byte@plt>
cmp    $0xff,%ebx
ja     11a1 <do_set_color+0x61>
lea    0xe76(%rip),%rdi
mov    $0x4,%esi
call   1030 <term_add_bytes@plt>
mov    %ebx,%edi
add    $0x18,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <term_add_uint@plt>
lea    0x14(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
call   1050 <color_split_rgb@plt>
lea    0xe47(%rip),%rdi
mov    $0x4,%esi
call   1030 <term_add_bytes@plt>
mov    0x14(%rsp),%edi
call   1060 <term_add_uint@plt>
mov    $0x3b,%edi
call   1040 <term_add_byte@plt>
mov    0x10(%rsp),%edi
call   1060 <term_add_uint@plt>
mov    $0x3b,%edi
call   1040 <term_add_byte@plt>
mov    0xc(%rsp),%edi
call   1060 <term_add_uint@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"8;5;\00"
@.str.1 = [5 x i8] c"8;2;\00"
define void @do_set_color(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8 %1, i8* %4
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
br label %42
11: ; preds = %2
%12 = call i32 @term_add_byte(i8 signext 59)
%13 = load i8, i8* %4
%14 = call i32 @term_add_byte(i8 signext %13)
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 8
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = load i32, i32* %3
%19 = add nsw i32 48, %18
%20 = trunc i32 %19 to i8
%21 = call i32 @term_add_byte(i8 signext %20)
br label %42
22: ; preds = %11
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 256
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = call i32 @term_add_bytes(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%27 = load i32, i32* %3
%28 = call i32 @term_add_uint(i32 %27)
br label %41
29: ; preds = %22
%30 = load i32, i32* %3
%31 = call i32 @color_split_rgb(i32 %30, i32* %5, i32* %6, i32* %7)
%32 = call i32 @term_add_bytes(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%33 = load i32, i32* %5
%34 = call i32 @term_add_uint(i32 %33)
%35 = call i32 @term_add_byte(i8 signext 59)
%36 = load i32, i32* %6
%37 = call i32 @term_add_uint(i32 %36)
%38 = call i32 @term_add_byte(i8 signext 59)
%39 = load i32, i32* %7
%40 = call i32 @term_add_uint(i32 %39)
br label %41
41: ; preds = %29, %25
br label %42
42: ; preds = %10, %41, %17
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"8;5;\00"
@.str.1 = [5 x i8] c"8;2;\00"
define void @do_set_color(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = icmp slt i32 %0, 0
br i1 %6, label %34, label %7
7: ; preds = %2
%8 = tail call i32 @term_add_byte(i8 signext 59)
%9 = tail call i32 @term_add_byte(i8 signext %1)
%10 = icmp ult i32 %0, 8
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = trunc i32 %0 to i8
%13 = add nuw nsw i8 %12, 48
%14 = tail call i32 @term_add_byte(i8 signext %13)
br label %34
15: ; preds = %7
%16 = icmp ult i32 %0, 256
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = tail call i32 @term_add_bytes(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%19 = tail call i32 @term_add_uint(i32 %0)
br label %34
20: ; preds = %15
%21 = bitcast i32* %3 to i8*
%22 = bitcast i32* %4 to i8*
%23 = bitcast i32* %5 to i8*
%24 = call i32 @color_split_rgb(i32 %0, i32* %3, i32* %4, i32* %5)
%25 = call i32 @term_add_bytes(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%26 = load i32, i32* %3
%27 = call i32 @term_add_uint(i32 %26)
%28 = call i32 @term_add_byte(i8 signext 59)
%29 = load i32, i32* %4
%30 = call i32 @term_add_uint(i32 %29)
%31 = call i32 @term_add_byte(i8 signext 59)
%32 = load i32, i32* %5
%33 = call i32 @term_add_uint(i32 %32)
br label %34
34: ; preds = %17, %20, %2, %11
ret void
}