#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 u_char;
typedef int /*<<< orphan*/ b;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
int /*<<< orphan*/ strcat(char *, char *);
char *xcalloc(int, size_t);
char *xstrdup(char *);
char *tohex(const void *vp, size_t l) {
  const u_char *p = (const u_char *)vp;
  char b[3], *r;
  size_t i, hl;
  if (l > 65536)
    return xstrdup("tohex: length > 65536");
  hl = l * 2 + 1;
  r = xcalloc(1, hl);
  for (i = 0; i < l; i++) {
    snprintf(b, sizeof(b), "%02x", p[i]);
    strcat(r, b);
  }
  return (r);
}


x86 assembly (clang, -O0):
<tohex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x10000,-0x18(%rbp)
jbe    117b <tohex+0x3b>
lea    0xe93(%rip),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11f9 <tohex+0xb9>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
mov    $0x1,%edi
call   1060 <xcalloc@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11f1 <tohex+0xb1>
lea    -0x23(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    $0x3,%esi
lea    0xe48(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x30(%rbp),%rdi
lea    -0x23(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11a5 <tohex+0x65>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tohex>:
cmp    $0x10001,%rsi
jb     1155 <tohex+0x15>
lea    0xeb0(%rip),%rdi
jmp    1040 <xstrdup@plt>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
add    %rsi,%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1060 <xcalloc@plt>
mov    %rax,%r13
test   %r15,%r15
je     11b8 <tohex+0x78>
xor    %ebp,%ebp
lea    0xe8e(%rip),%r12
lea    0x5(%rsp),%rbx
nopl   (%rax)
mov    (%r14,%rbp,4),%ecx
mov    %rbx,%rdi
mov    $0x3,%esi
mov    %r12,%rdx
call   1030 <snprintf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1050 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1190 <tohex+0x50>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"tohex: length > 65536\00"
@.str.1 = [5 x i8] c"%02x\00"
define i8* @tohex(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca [3 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%11 = load i8*, i8** %4
%12 = bitcast i8* %11 to i32*
store i32* %12, i32** %6
%13 = load i64, i64* %5
%14 = icmp ugt i64 %13, 65536
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i8* @xstrdup(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %3
br label %42
17: ; preds = %2
%18 = load i64, i64* %5
%19 = mul i64 %18, 2
%20 = add i64 %19, 1
store i64 %20, i64* %10
%21 = load i64, i64* %10
%22 = call i8* @xcalloc(i32 1, i64 %21)
store i8* %22, i8** %8
store i64 0, i64* %9
br label %23
23: ; preds = %37, %17
%24 = load i64, i64* %9
%25 = load i64, i64* %5
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %40
27: ; preds = %23
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%29 = load i32*, i32** %6
%30 = load i64, i64* %9
%31 = getelementptr inbounds i32, i32* %29, i64 %30
%32 = load i32, i32* %31
%33 = call i32 @snprintf(i8* %28, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i8*, i8** %8
%35 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%36 = call i32 @strcat(i8* %34, i8* %35)
br label %37
37: ; preds = %27
%38 = load i64, i64* %9
%39 = add i64 %38, 1
store i64 %39, i64* %9
br label %23
40: ; preds = %23
%41 = load i8*, i8** %8
store i8* %41, i8** %3
br label %42
42: ; preds = %40, %15
%43 = load i8*, i8** %3
ret i8* %43
}

LLVM IR (-O2):
@.str = [22 x i8] c"tohex: length > 65536\00"
@.str.1 = [5 x i8] c"%02x\00"
define i8* @tohex(i8* nocapture %0, i64 %1) {
%3 = alloca [3 x i8]
%4 = bitcast i8* %0 to i32*
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%6 = icmp ugt i64 %1, 65536
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i8* @xstrdup(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %22
9: ; preds = %2
%10 = shl nuw nsw i64 %1, 1
%11 = or i64 %10, 1
%12 = tail call i8* @xcalloc(i32 1, i64 %11)
%13 = icmp eq i64 %1, 0
br i1 %13, label %22, label %14
14: ; preds = %9, %14
%15 = phi i64 [ %20, %14 ], [ 0, %9 ]
%16 = getelementptr inbounds i32, i32* %4, i64 %15
%17 = load i32, i32* %16
%18 = call i32 @snprintf(i8* %5, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @strcat(i8* %12, i8* %5)
%20 = add nuw i64 %15, 1
%21 = icmp eq i64 %20, %1
br i1 %21, label %22, label %14
22: ; preds = %14, %9, %7
%23 = phi i8* [ %8, %7 ], [ %12, %9 ], [ %12, %14 ]
ret i8* %23
}