#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_int8_t;

/* Variables and functions */
int /*<<< orphan*/ log_error(char *, unsigned long);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *raw2hex(u_int8_t *buf, size_t sz) {
  char *s;
  size_t i;
  if ((s = (char *)malloc(sz * 2 + 1)) == NULL) {
    log_error("raw2hex: malloc (%lu) failed", (unsigned long)sz * 2 + 1);
    return NULL;
  }
  for (i = 0; i < sz; i++)
    snprintf(s + (2 * i), 2 * (sz - i) + 1, "%02x", buf[i]);
  s[sz * 2] = '\0';
  return s;
}


x86 assembly (clang, -O0):
<raw2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x1,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1184 <raw2hex+0x54>
mov    -0x18(%rbp),%rsi
shl    $0x1,%rsi
add    $0x1,%rsi
lea    0xe8e(%rip),%rdi
call   1030 <log_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fb <raw2hex+0xcb>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11e3 <raw2hex+0xb3>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xe50(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    118c <raw2hex+0x5c>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<raw2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %rdi,%r15
lea    (%rsi,%rsi,1),%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11c1 <raw2hex+0x91>
mov    %rax,0x8(%rsp)
mov    %r13,%rax
add    %r13,%rax
mov    %rax,0x10(%rsp)
test   %r13,%r13
je     11b1 <raw2hex+0x81>
lea    0x1(%r13,%r13,1),%ebp
xor    %r14d,%r14d
lea    0xea0(%rip),%r12
mov    0x8(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15,%r14,4),%ecx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r12,%rdx
call   1040 <snprintf@plt>
add    $0x1,%r14
add    $0xfffffffe,%ebp
add    $0x2,%rbx
cmp    %r14,%r13
jne    1190 <raw2hex+0x60>
mov    0x8(%rsp),%rax
mov    0x10(%rsp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11d2 <raw2hex+0xa2>
lea    0xe38(%rip),%rdi
mov    %rbx,%rsi
call   1030 <log_error@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"raw2hex: malloc (%lu) failed\00"
@.str.1 = [5 x i8] c"%02x\00"
define i8* @raw2hex(i32* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i32* %0, i32** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = mul i64 %8, 2
%10 = add i64 %9, 1
%11 = call i64 @malloc(i64 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i64, i64* %5
%16 = mul i64 %15, 2
%17 = add i64 %16, 1
%18 = call i32 @log_error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i64 %17)
store i8* null, i8** %3
br label %49
19: ; preds = %2
store i64 0, i64* %7
br label %20
20: ; preds = %40, %19
%21 = load i64, i64* %7
%22 = load i64, i64* %5
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %43
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = mul i64 2, %26
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i64, i64* %5
%30 = load i64, i64* %7
%31 = sub i64 %29, %30
%32 = mul i64 2, %31
%33 = add i64 %32, 1
%34 = trunc i64 %33 to i32
%35 = load i32*, i32** %4
%36 = load i64, i64* %7
%37 = getelementptr inbounds i32, i32* %35, i64 %36
%38 = load i32, i32* %37
%39 = call i32 @snprintf(i8* %28, i32 %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %24
%41 = load i64, i64* %7
%42 = add i64 %41, 1
store i64 %42, i64* %7
br label %20
43: ; preds = %20
%44 = load i8*, i8** %6
%45 = load i64, i64* %5
%46 = mul i64 %45, 2
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %6
store i8* %48, i8** %3
br label %49
49: ; preds = %43, %14
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@.str = [29 x i8] c"raw2hex: malloc (%lu) failed\00"
@.str.1 = [5 x i8] c"%02x\00"
define i8* @raw2hex(i32* nocapture %0, i64 %1) {
%3 = shl i64 %1, 1
%4 = or i64 %3, 1
%5 = tail call i64 @malloc(i64 %4)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = icmp eq i64 %1, 0
br i1 %9, label %25, label %12
10: ; preds = %2
%11 = tail call i32 @log_error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i64 %4)
br label %27
12: ; preds = %8, %12
%13 = phi i64 [ %23, %12 ], [ 0, %8 ]
%14 = shl i64 %13, 1
%15 = getelementptr inbounds i8, i8* %6, i64 %14
%16 = sub i64 %1, %13
%17 = trunc i64 %16 to i32
%18 = shl i32 %17, 1
%19 = or i32 %18, 1
%20 = getelementptr inbounds i32, i32* %0, i64 %13
%21 = load i32, i32* %20
%22 = tail call i32 @snprintf(i8* %15, i32 %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = add nuw i64 %13, 1
%24 = icmp eq i64 %23, %1
br i1 %24, label %25, label %12
25: ; preds = %12, %8
%26 = getelementptr inbounds i8, i8* %6, i64 %3
store i8 0, i8* %26
br label %27
27: ; preds = %25, %10
%28 = phi i8* [ null, %10 ], [ %6, %25 ]
ret i8* %28
}