#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*/ wchar_t;

/* Variables and functions */
int /*<<< orphan*/ swprintf(int /*<<< orphan*/ *, size_t, char *, long long);
scalar_t__ wcscmp(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ wcslcpy(int /*<<< orphan*/ *, char *, size_t);
long long wcstonum(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                   int /*<<< orphan*/ const **);
void cmd_color_exec(int argc, wchar_t **argv, wchar_t *out, size_t len) {
  (void)argc;
  (void)argv;
  long long code;
  const wchar_t *errstr = NULL;
  if (argc != 2) {
    wcslcpy(out, L"<color-bad-arg>", len);
    return;
  }
  if (wcscmp(argv[1], L"reset") == 0) {
    wcslcpy(out, L"[0;0m", len);
    return;
  }
  code = wcstonum(argv[1], 0, 255, &errstr);
  if (errstr != NULL) {
    wcslcpy(out, L"<color-bad-code>", len);
    return;
  }
  swprintf(out, len, L"[38;5;%dm", code);
}


x86 assembly (clang, -O0):
<cmd_color_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     1182 <cmd_color_exec+0x42>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe88(%rip),%rsi
call   1060 <wcslcpy@plt>
jmp    1211 <cmd_color_exec+0xd1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xeaf(%rip),%rsi
call   1030 <wcscmp@plt>
cmp    $0x0,%rax
jne    11b9 <cmd_color_exec+0x79>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xea9(%rip),%rsi
call   1060 <wcslcpy@plt>
jmp    1211 <cmd_color_exec+0xd1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %esi,%esi
mov    $0xff,%edx
lea    -0x30(%rbp),%rcx
call   1040 <wcstonum@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11f9 <cmd_color_exec+0xb9>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe85(%rip),%rsi
call   1060 <wcslcpy@plt>
jmp    1211 <cmd_color_exec+0xd1>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rcx
lea    0xeac(%rip),%rdx
call   1050 <swprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_color_exec>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
movq   $0x0,0x8(%rsp)
cmp    $0x2,%edi
jne    11a6 <cmd_color_exec+0x66>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xed5(%rip),%rsi
call   1030 <wcscmp@plt>
test   %rax,%rax
je     11af <cmd_color_exec+0x6f>
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rcx
xor    %esi,%esi
mov    $0xff,%edx
call   1040 <wcstonum@plt>
cmpq   $0x0,0x8(%rsp)
je     11ca <cmd_color_exec+0x8a>
lea    0xedb(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1060 <wcslcpy@plt>
jmp    11df <cmd_color_exec+0x9f>
lea    0xe53(%rip),%rsi
jmp    11b6 <cmd_color_exec+0x76>
lea    0xea2(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <wcslcpy@plt>
lea    0xee7(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rcx
call   1050 <swprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i32] [i32 60, i32 99, i32 111, i32 108, i32 111, i32 114, i32 45, i32 98, i32 97, i32 100, i32 45, i32 97, i32 114, i32 103, i32 62, i32 0]
@.str.1 = [6 x i32] [i32 114, i32 101, i32 115, i32 101, i32 116, i32 0]
@.str.2 = [7 x i32] [i32 27, i32 91, i32 48, i32 59, i32 48, i32 109, i32 0]
@.str.3 = [17 x i32] [i32 60, i32 99, i32 111, i32 108, i32 111, i32 114, i32 45, i32 98, i32 97, i32 100, i32 45, i32 99, i32 111, i32 100, i32 101, i32 62, i32 0]
@.str.4 = [11 x i32] [i32 27, i32 91, i32 51, i32 56, i32 59, i32 53, i32 59, i32 37, i32 100, i32 109, i32 0]
define void @cmd_color_exec(i32 %0, i32** %1, i32* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
store i32 %0, i32* %5
store i32** %1, i32*** %6
store i32* %2, i32** %7
store i64 %3, i64* %8
%11 = load i32, i32* %5
%12 = load i32**, i32*** %6
store i32* null, i32** %10
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %19
15: ; preds = %4
%16 = load i32*, i32** %7
%17 = load i64, i64* %8
%18 = call i32 @wcslcpy(i32* %16, i8* bitcast ([16 x i32]* @.str to i8*), i64 %17)
br label %45
19: ; preds = %4
%20 = load i32**, i32*** %6
%21 = getelementptr inbounds i32*, i32** %20, i64 1
%22 = load i32*, i32** %21
%23 = call i64 @wcscmp(i32* %22, i8* bitcast ([6 x i32]* @.str.1 to i8*))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32*, i32** %7
%27 = load i64, i64* %8
%28 = call i32 @wcslcpy(i32* %26, i8* bitcast ([7 x i32]* @.str.2 to i8*), i64 %27)
br label %45
29: ; preds = %19
%30 = load i32**, i32*** %6
%31 = getelementptr inbounds i32*, i32** %30, i64 1
%32 = load i32*, i32** %31
%33 = call i64 @wcstonum(i32* %32, i32 0, i32 255, i32** %10)
store i64 %33, i64* %9
%34 = load i32*, i32** %10
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i32*, i32** %7
%38 = load i64, i64* %8
%39 = call i32 @wcslcpy(i32* %37, i8* bitcast ([17 x i32]* @.str.3 to i8*), i64 %38)
br label %45
40: ; preds = %29
%41 = load i32*, i32** %7
%42 = load i64, i64* %8
%43 = load i64, i64* %9
%44 = call i32 @swprintf(i32* %41, i64 %42, i8* bitcast ([11 x i32]* @.str.4 to i8*), i64 %43)
br label %45
45: ; preds = %40, %36, %25, %15
ret void
}

LLVM IR (-O2):
@.str = [16 x i32] [i32 60, i32 99, i32 111, i32 108, i32 111, i32 114, i32 45, i32 98, i32 97, i32 100, i32 45, i32 97, i32 114, i32 103, i32 62, i32 0]
@.str.1 = [6 x i32] [i32 114, i32 101, i32 115, i32 101, i32 116, i32 0]
@.str.2 = [7 x i32] [i32 27, i32 91, i32 48, i32 59, i32 48, i32 109, i32 0]
@.str.3 = [17 x i32] [i32 60, i32 99, i32 111, i32 108, i32 111, i32 114, i32 45, i32 98, i32 97, i32 100, i32 45, i32 99, i32 111, i32 100, i32 101, i32 62, i32 0]
@.str.4 = [11 x i32] [i32 27, i32 91, i32 51, i32 56, i32 59, i32 53, i32 59, i32 37, i32 100, i32 109, i32 0]
define void @cmd_color_exec(i32 %0, i32** nocapture %1, i32* %2, i64 %3) {
%5 = alloca i32*
%6 = bitcast i32** %5 to i8*
store i32* null, i32** %5
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call i32 @wcslcpy(i32* %2, i8* bitcast ([16 x i32]* @.str to i8*), i64 %3)
br label %26
10: ; preds = %4
%11 = getelementptr inbounds i32*, i32** %1, i64 1
%12 = load i32*, i32** %11
%13 = tail call i64 @wcscmp(i32* %12, i8* bitcast ([6 x i32]* @.str.1 to i8*))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @wcslcpy(i32* %2, i8* bitcast ([7 x i32]* @.str.2 to i8*), i64 %3)
br label %26
17: ; preds = %10
%18 = load i32*, i32** %11
%19 = call i64 @wcstonum(i32* %18, i32 0, i32 255, i32** %5)
%20 = load i32*, i32** %5
%21 = icmp eq i32* %20, null
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = call i32 @wcslcpy(i32* %2, i8* bitcast ([17 x i32]* @.str.3 to i8*), i64 %3)
br label %26
24: ; preds = %17
%25 = call i32 @swprintf(i32* %2, i64 %3, i8* bitcast ([11 x i32]* @.str.4 to i8*), i64 %19)
br label %26
26: ; preds = %24, %22, %15, %8
ret void
}