#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(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
int /*<<< orphan*/ stderr;
unsigned char *escape_hex(unsigned char *bytes, int nbytes) {
  unsigned char *escaped;
  char *pEscaped;
  if (!bytes || nbytes <= 0) {
    fprintf(stderr, "Error: escape_hex(): Arguments are not init correctly\n");
    return NULL;
  }
  escaped = calloc(nbytes * 4 + 1, sizeof(*escaped));
  if (!escaped) {
    fprintf(stderr, "Error: escape_hex(): Could not alloc() buffer\n");
    return NULL;
  }
  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    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1154 <escape_hex+0x24>
cmpl   $0x0,-0x14(%rbp)
jg     1176 <escape_hex+0x46>
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
lea    0xe9c(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    121e <escape_hex+0xee>
mov    -0x14(%rbp),%edi
shl    $0x2,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ba <escape_hex+0x8a>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe8f(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    121e <escape_hex+0xee>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1216 <escape_hex+0xe6>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xff,%eax
mov    $0x5,%esi
lea    0xe7e(%rip),%rdx
movzbl %al,%ecx
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11c2 <escape_hex+0x92>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%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    %esi,%r15d
lea    0xebb(%rip),%rsi
test   %rdi,%rdi
je     11ab <escape_hex+0x7b>
test   %r15d,%r15d
jle    11ab <escape_hex+0x7b>
mov    %rdi,%r12
lea    0x1(,%r15,4),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     11a4 <escape_hex+0x74>
mov    %rax,%r14
mov    %r15d,%r13d
xor    %ebp,%ebp
lea    0xeee(%rip),%r15
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
movzbl (%r12,%rbp,1),%ecx
mov    %rbx,%rdi
mov    $0x5,%esi
mov    %r15,%rdx
call   1030 <snprintf@plt>
add    $0x4,%rbx
add    $0x1,%rbp
cmp    %ebp,%r13d
jne    1180 <escape_hex+0x50>
jmp    11bc <escape_hex+0x8c>
lea    0xe8c(%rip),%rsi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1050 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [55 x i8] c"Error: escape_hex(): Arguments are not init correctly\0A\00"
@.str.1 = [47 x i8] c"Error: escape_hex(): Could not alloc() buffer\0A\00"
@.str.2 = [7 x i8] c"\\x%02x\00"
define i8* @escape_hex(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* %5
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10, %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %47
16: ; preds = %10
%17 = load i32, i32* %5
%18 = mul nsw i32 %17, 4
%19 = add nsw i32 %18, 1
%20 = call i8* @calloc(i32 %19, i32 1)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %47
26: ; preds = %16
%27 = load i8*, i8** %6
store i8* %27, i8** %7
br label %28
28: ; preds = %31, %26
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %45
31: ; preds = %28
%32 = load i8*, i8** %7
%33 = load i8*, i8** %4
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = and i32 %35, 255
%37 = trunc i32 %36 to i8
%38 = call i32 @snprintf(i8* %32, i32 5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %37)
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 4
store i8* %40, i8** %7
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %4
%43 = load i32, i32* %5
%44 = add nsw i32 %43, -1
store i32 %44, i32* %5
br label %28
45: ; preds = %28
%46 = load i8*, i8** %6
store i8* %46, i8** %3
br label %47
47: ; preds = %45, %23, %13
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [55 x i8] c"Error: escape_hex(): Arguments are not init correctly\0A\00"
@.str.1 = [47 x i8] c"Error: escape_hex(): Could not alloc() buffer\0A\00"
@.str.2 = [7 x i8] c"\\x%02x\00"
define i8* @escape_hex(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp slt i32 %1, 1
%5 = or i1 %3, %4
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = shl nsw i32 %1, 2
%8 = or i32 %7, 1
%9 = tail call i8* @calloc(i32 %8, i32 1)
%10 = icmp eq i8* %9, null
br i1 %10, label %21, label %11
11: ; preds = %6, %11
%12 = phi i8* [ %17, %11 ], [ %9, %6 ]
%13 = phi i32 [ %19, %11 ], [ %1, %6 ]
%14 = phi i8* [ %18, %11 ], [ %0, %6 ]
%15 = load i8, i8* %14
%16 = tail call i32 @snprintf(i8* %12, i32 5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %15)
%17 = getelementptr inbounds i8, i8* %12, i64 4
%18 = getelementptr inbounds i8, i8* %14, i64 1
%19 = add nsw i32 %13, -1
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %11
21: ; preds = %6, %2
%22 = phi i8* [ getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), %6 ]
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* %22)
br label %25
25: ; preds = %11, %21
%26 = phi i8* [ null, %21 ], [ %9, %11 ]
ret i8* %26
}