char *shardcache_hex_escape(char *buf, int len, int limit, int include_prefix) {
  int i;
  static __thread char str[(1 << 16) + 6];
  int olen = (limit > 0 && limit < len) ? limit : len;
  if (olen > (1 << 16) / 2)
    olen = (1 << 16) / 2;
  char *p = str;
  if (include_prefix) {
    strcpy(str, "0x");
    p += 2;
  }
  for (i = 0; i < olen; i++) {
    sprintf(p, "%02x", (unsigned char)buf[i]);
    p += 2;
  }
  if (olen < len)
    strcat(str, "...");
  return str;
}


x86 assembly (clang, -O0):
<shardcache_hex_escape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1176 <shardcache_hex_escape+0x36>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1176 <shardcache_hex_escape+0x36>
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    117c <shardcache_hex_escape+0x3c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x8000,-0x1c(%rbp)
jle    1196 <shardcache_hex_escape+0x56>
movl   $0x8000,-0x1c(%rbp)
lea    0x2e33(%rip),%rdi
call   1040 <__tls_get_addr@plt>
lea    0x0(%rax),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11e2 <shardcache_hex_escape+0xa2>
lea    0x2e12(%rip),%rdi
call   1040 <__tls_get_addr@plt>
lea    0x0(%rax),%rdi
lea    0xe2f(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    122d <shardcache_hex_escape+0xed>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11e9 <shardcache_hex_escape+0xa9>
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1258 <shardcache_hex_escape+0x118>
lea    0x2d90(%rip),%rdi
call   1040 <__tls_get_addr@plt>
lea    0x0(%rax),%rdi
lea    0xdb5(%rip),%rsi
call   1050 <strcat@plt>
lea    0x2d71(%rip),%rdi
call   1040 <__tls_get_addr@plt>
lea    0x0(%rax),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shardcache_hex_escape>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    %esi,%edx
mov    %esi,%r14d
cmovl  %edx,%r14d
mov    %ecx,%ebp
mov    %rdi,%rbx
test   %edx,%edx
mov    %esi,0xc(%rsp)
cmovle %esi,%r14d
cmp    $0x8000,%r14d
mov    $0x8000,%r13d
cmovl  %r14d,%r13d
lea    0x2e62(%rip),%rdi
call   1040 <__tls_get_addr@plt>
test   %ebp,%ebp
mov    %rax,0x10(%rsp)
je     119d <shardcache_hex_escape+0x6d>
mov    %rax,%rcx
movb   $0x0,0x2(%rcx)
movw   $0x7830,0x0(%rcx)
30 78
lea    0x2(%rcx),%rbp
test   %r14d,%r14d
jg     11a9 <shardcache_hex_escape+0x79>
jmp    11ef <shardcache_hex_escape+0xbf>
lea    0x0(%rax),%rbp
test   %r14d,%r14d
jle    11ef <shardcache_hex_escape+0xbf>
cmp    $0x2,%r13d
mov    $0x1,%r14d
cmovge %r13d,%r14d
lea    0xe45(%rip),%r12
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rbx,%r15,1),%edx
mov    %rbp,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
add    $0x2,%rbp
add    $0x1,%r15
cmp    %r15,%r14
jne    11d0 <shardcache_hex_escape+0xa0>
cmp    0xc(%rsp),%r13d
mov    0x10(%rsp),%rbp
jge    1218 <shardcache_hex_escape+0xe8>
mov    %rbp,%rax
mov    %rax,%rbx
lea    0x0(%rax),%rdi
call   1030 <strlen@plt>
movl   $0x2e2e2e,0x0(%rbx,%rax,1)
lea    0x0(%rbp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@shardcache_hex_escape.str = internal thread_local global [65542 x i8] zeroinitializer
@.str = [3 x i8] c"0x\00"
@.str.1 = [5 x i8] c"%02x\00"
@.str.2 = [4 x i8] c"...\00"
define i8* @shardcache_hex_escape(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %4
%15 = load i32, i32* %7
%16 = load i32, i32* %6
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %7
br label %22
20: ; preds = %14, %4
%21 = load i32, i32* %6
br label %22
22: ; preds = %20, %18
%23 = phi i32 [ %19, %18 ], [ %21, %20 ]
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp sgt i32 %24, 32768
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 32768, i32* %10
br label %27
27: ; preds = %26, %22
store i8* getelementptr inbounds ([65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 0), i8** %11
%28 = load i32, i32* %8
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = call i8* @strcpy(i8* getelementptr inbounds ([65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%32 = load i8*, i8** %11
%33 = getelementptr inbounds i8, i8* %32, i64 2
store i8* %33, i8** %11
br label %34
34: ; preds = %30, %27
store i32 0, i32* %9
br label %35
35: ; preds = %50, %34
%36 = load i32, i32* %9
%37 = load i32, i32* %10
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %53
39: ; preds = %35
%40 = load i8*, i8** %11
%41 = load i8*, i8** %5
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = call i32 (i8*, i8*, ...) @sprintf(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %46)
%48 = load i8*, i8** %11
%49 = getelementptr inbounds i8, i8* %48, i64 2
store i8* %49, i8** %11
br label %50
50: ; preds = %39
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %35
53: ; preds = %35
%54 = load i32, i32* %10
%55 = load i32, i32* %6
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i8* @strcat(i8* getelementptr inbounds ([65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %57, %53
ret i8* getelementptr inbounds ([65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 0)
}

LLVM IR (-O2):
@shardcache_hex_escape.str = internal thread_local global [65542 x i8] zeroinitializer
@.str = [3 x i8] c"0x\00"
@.str.1 = [5 x i8] c"%02x\00"
define i8* @shardcache_hex_escape(i8* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
%6 = icmp slt i32 %2, %1
%7 = and i1 %5, %6
%8 = select i1 %7, i32 %2, i32 %1
%9 = icmp slt i32 %8, 32768
%10 = select i1 %9, i32 %8, i32 32768
%11 = icmp eq i32 %3, 0
br i1 %11, label %13, label %12
12: ; preds = %4
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 dereferenceable(3) getelementptr inbounds ([65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 0), i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 3, i1 false)
br label %13
13: ; preds = %12, %4
%14 = phi i8* [ getelementptr inbounds ([65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 2), %12 ], [ getelementptr inbounds ([65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 0), %4 ]
%15 = icmp sgt i32 %8, 0
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = call i32 @llvm.smax.i32(i32 %10, i32 1)
%18 = zext i32 %17 to i64
br label %19
19: ; preds = %16, %19
%20 = phi i64 [ 0, %16 ], [ %27, %19 ]
%21 = phi i8* [ %14, %16 ], [ %26, %19 ]
%22 = getelementptr inbounds i8, i8* %0, i64 %20
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %21, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds i8, i8* %21, i64 2
%27 = add nuw nsw i64 %20, 1
%28 = icmp eq i64 %27, %18
br i1 %28, label %29, label %19
29: ; preds = %19, %13
%30 = icmp slt i32 %10, %1
br i1 %30, label %31, label %35
31: ; preds = %29
%32 = tail call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 0))
%33 = getelementptr [65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 %32
%34 = bitcast i8* %33 to i32*
store i32 3026478, i32* %34
br label %35
35: ; preds = %31, %29
ret i8* getelementptr inbounds ([65542 x i8], [65542 x i8]* @shardcache_hex_escape.str, i64 0, i64 0)
}