unsigned char *llib_led_macro_escape(unsigned char *ptr, int c) {
  if (c == '"') {
    *ptr++ = '\\';
    *ptr++ = '"';
  } else if (c == '\\') {
    *ptr++ = '\\';
    *ptr++ = '\\';
  } else if (c == '\'') {
    *ptr++ = '\\';
    *ptr++ = '\'';
  } else if (c < 32 || c > 126) {
    *ptr++ = '\\';
    *ptr++ = 'x';
    *ptr++ = "0123456789ABCDEF"[c >> 4];
    *ptr++ = "0123456789ABCDEF"[c & 15];
  } else
    *ptr++ = c;
  return ptr;
}


x86 assembly (clang, -O0):
<llib_led_macro_escape>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x22,-0xc(%rbp)
jne    113e <llib_led_macro_escape+0x3e>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x22,(%rax)
jmp    124e <llib_led_macro_escape+0x14e>
cmpl   $0x5c,-0xc(%rbp)
jne    1171 <llib_led_macro_escape+0x71>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
jmp    1249 <llib_led_macro_escape+0x149>
cmpl   $0x27,-0xc(%rbp)
jne    11a4 <llib_led_macro_escape+0xa4>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x27,(%rax)
jmp    1244 <llib_led_macro_escape+0x144>
cmpl   $0x20,-0xc(%rbp)
jl     11b8 <llib_led_macro_escape+0xb8>
cmpl   $0x7e,-0xc(%rbp)
jle    1229 <llib_led_macro_escape+0x129>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x5c,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x78,(%rax)
mov    -0xc(%rbp),%eax
sar    $0x4,%eax
movslq %eax,%rcx
lea    0xe14(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xdf0(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    123f <llib_led_macro_escape+0x13f>
mov    -0xc(%rbp),%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    1244 <llib_led_macro_escape+0x144>
jmp    1249 <llib_led_macro_escape+0x149>
jmp    124e <llib_led_macro_escape+0x14e>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<llib_led_macro_escape>:
mov    %rdi,%rax
cmp    $0x5c,%esi
je     111c <llib_led_macro_escape+0x1c>
cmp    $0x27,%esi
je     1126 <llib_led_macro_escape+0x26>
cmp    $0x22,%esi
jne    1130 <llib_led_macro_escape+0x30>
movw   $0x225c,(%rax)
add    $0x2,%rax
ret
movw   $0x5c5c,(%rax)
add    $0x2,%rax
ret
movw   $0x275c,(%rax)
add    $0x2,%rax
ret
lea    -0x7f(%rsi),%ecx
cmp    $0xffffffa0,%ecx
ja     1160 <llib_led_macro_escape+0x60>
movw   $0x785c,(%rax)
mov    %esi,%ecx
sar    $0x4,%ecx
movslq %ecx,%rcx
lea    0xeb4(%rip),%rdx
mov    (%rcx,%rdx,1),%cl
mov    %cl,0x2(%rax)
and    $0xf,%esi
mov    (%rsi,%rdx,1),%cl
mov    %cl,0x3(%rax)
add    $0x4,%rax
ret
mov    %sil,(%rax)
add    $0x1,%rax
ret

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789ABCDEF\00"
define i8* @llib_led_macro_escape(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = icmp eq i32 %5, 34
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = getelementptr inbounds i8, i8* %8, i32 1
store i8* %9, i8** %3
store i8 92, i8* %8
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i32 1
store i8* %11, i8** %3
store i8 34, i8* %10
br label %61
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 92
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %3
store i8 92, i8* %16
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %3
store i8 92, i8* %18
br label %60
20: ; preds = %12
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 39
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %3
store i8 92, i8* %24
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %3
store i8 39, i8* %26
br label %59
28: ; preds = %20
%29 = load i32, i32* %4
%30 = icmp slt i32 %29, 32
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i32, i32* %4
%33 = icmp sgt i32 %32, 126
br i1 %33, label %34, label %53
34: ; preds = %31, %28
%35 = load i8*, i8** %3
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %3
store i8 92, i8* %35
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %3
store i8 120, i8* %37
%39 = load i32, i32* %4
%40 = ashr i32 %39, 4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %3
store i8 %43, i8* %44
%46 = load i32, i32* %4
%47 = and i32 %46, 15
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = load i8*, i8** %3
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %3
store i8 %50, i8* %51
br label %58
53: ; preds = %31
%54 = load i32, i32* %4
%55 = trunc i32 %54 to i8
%56 = load i8*, i8** %3
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %3
store i8 %55, i8* %56
br label %58
58: ; preds = %53, %34
br label %59
59: ; preds = %58, %23
br label %60
60: ; preds = %59, %15
br label %61
61: ; preds = %60, %7
%62 = load i8*, i8** %3
ret i8* %62
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789ABCDEF\00"
define i8* @llib_led_macro_escape(i8* writeonly %0, i32 %1) {
switch i32 %1, label %12 [
i32 34, label %3
i32 92, label %6
i32 39, label %9
]
3: ; preds = %2
%4 = getelementptr inbounds i8, i8* %0, i64 1
store i8 92, i8* %0
%5 = getelementptr inbounds i8, i8* %0, i64 2
store i8 34, i8* %4
br label %31
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 1
store i8 92, i8* %0
%8 = getelementptr inbounds i8, i8* %0, i64 2
store i8 92, i8* %7
br label %31
9: ; preds = %2
%10 = getelementptr inbounds i8, i8* %0, i64 1
store i8 92, i8* %0
%11 = getelementptr inbounds i8, i8* %0, i64 2
store i8 39, i8* %10
br label %31
12: ; preds = %2
%13 = add i32 %1, -127
%14 = icmp ult i32 %13, -95
br i1 %14, label %15, label %28
15: ; preds = %12
%16 = getelementptr inbounds i8, i8* %0, i64 1
store i8 92, i8* %0
%17 = getelementptr inbounds i8, i8* %0, i64 2
store i8 120, i8* %16
%18 = ashr i32 %1, 4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %21, i8* %17
%23 = and i32 %1, 15
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = getelementptr inbounds i8, i8* %0, i64 4
store i8 %26, i8* %22
br label %31
28: ; preds = %12
%29 = trunc i32 %1 to i8
%30 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %29, i8* %0
br label %31
31: ; preds = %6, %15, %28, %9, %3
%32 = phi i8* [ %5, %3 ], [ %8, %6 ], [ %11, %9 ], [ %27, %15 ], [ %30, %28 ]
ret i8* %32
}