#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 */
unsigned char *calloc(size_t, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
unsigned char *escape_hex(unsigned char *bytes, size_t nbytes) {
  unsigned char *escaped;
  char *pEscaped;
  escaped = calloc(nbytes * 4 + 1, sizeof(*escaped));
  pEscaped = escaped;
  while (nbytes) {
    snprintf(pEscaped, 5, "\\x%02x", *bytes & 0xff);
    pEscaped += 4;
    bytes++;
    nbytes--;
  }
  return escaped;
}


x86 assembly (clang, -O0):
<escape_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x2,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11aa <escape_hex+0x8a>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
and    $0xff,%eax
mov    $0x5,%esi
lea    0xe87(%rip),%rdx
movzbl %al,%ecx
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1152 <escape_hex+0x32>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
lea    0x1(,%rsi,4),%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r15
test   %r13,%r13
je     1182 <escape_hex+0x62>
xor    %ebp,%ebp
lea    0xeac(%rip),%r12
mov    %r15,%rbx
nopw   0x0(%rax,%rax,1)
movzbl (%r14,%rbp,1),%ecx
mov    %rbx,%rdi
mov    $0x5,%esi
mov    %r12,%rdx
call   1030 <snprintf@plt>
add    $0x4,%rbx
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1160 <escape_hex+0x40>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"\\x%02x\00"
define i8* @escape_hex(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = mul i64 %7, 4
%9 = add i64 %8, 1
%10 = call i8* @calloc(i64 %9, i32 1)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
store i8* %11, i8** %6
br label %12
12: ; preds = %15, %2
%13 = load i64, i64* %4
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = load i8*, i8** %6
%17 = load i8*, i8** %3
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = and i32 %19, 255
%21 = trunc i32 %20 to i8
%22 = call i32 @snprintf(i8* %16, i32 5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %21)
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i64 4
store i8* %24, i8** %6
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %3
%27 = load i64, i64* %4
%28 = add i64 %27, -1
store i64 %28, i64* %4
br label %12
29: ; preds = %12
%30 = load i8*, i8** %5
ret i8* %30
}

LLVM IR (-O2):
@.str = [7 x i8] c"\\x%02x\00"
define i8* @escape_hex(i8* nocapture %0, i64 %1) {
%3 = shl i64 %1, 2
%4 = or i64 %3, 1
%5 = tail call i8* @calloc(i64 %4, i32 1)
%6 = icmp eq i64 %1, 0
br i1 %6, label %17, label %7
7: ; preds = %2, %7
%8 = phi i8* [ %13, %7 ], [ %5, %2 ]
%9 = phi i8* [ %14, %7 ], [ %0, %2 ]
%10 = phi i64 [ %15, %7 ], [ %1, %2 ]
%11 = load i8, i8* %9
%12 = tail call i32 @snprintf(i8* %8, i32 5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %11)
%13 = getelementptr inbounds i8, i8* %8, i64 4
%14 = getelementptr inbounds i8, i8* %9, i64 1
%15 = add i64 %10, -1
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %7
17: ; preds = %7, %2
ret i8* %5
}