#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 */
int /*<<< orphan*/ Z_DF_ADD_CHAR(int, char *, int, int);
int zhex_encode(const void *src, int src_size, char *dest, int dest_size) {
  unsigned const char dec2hex[17] = "0123456789ABCDEF";
  unsigned char *src_c = (unsigned char *)src;
  int src_pos;
  int addch;
  int rlen = 0;
  for (src_pos = 0; src_pos < src_size; src_pos++) {
    if ((dest_size > 0) && (rlen + 2 > dest_size)) {
      break;
    }
    addch = dec2hex[src_c[src_pos] >> 4];
    Z_DF_ADD_CHAR(dest_size, dest, rlen, addch);
    addch = dec2hex[src_c[src_pos] & 0X0F];
    Z_DF_ADD_CHAR(dest_size, dest, rlen, addch);
  }
  return rlen;
}


x86 assembly (clang, -O0):
<zhex_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0xed3(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xed0(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xece(%rip),%al
mov    %al,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e9 <zhex_encode+0xd9>
cmpl   $0x0,-0x1c(%rbp)
jle    1185 <zhex_encode+0x75>
mov    -0x44(%rbp),%eax
add    $0x2,%eax
cmp    -0x1c(%rbp),%eax
jle    1185 <zhex_encode+0x75>
jmp    11e9 <zhex_encode+0xd9>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
sar    $0x4,%eax
cltq
movzbl -0x30(%rbp,%rax,1),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%ecx
call   1030 <Z_DF_ADD_CHAR@plt>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
and    $0xf,%eax
cltq
movzbl -0x30(%rbp,%rax,1),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%ecx
call   1030 <Z_DF_ADD_CHAR@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    115b <zhex_encode+0x4b>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zhex_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%ecx
je     117c <zhex_encode+0x6c>
test   %esi,%esi
jle    117c <zhex_encode+0x6c>
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rdi,%rbx
mov    %esi,%r15d
xor    %ebp,%ebp
lea    0xec7(%rip),%r13
nopl   0x0(%rax)
movzbl (%rbx,%rbp,1),%eax
shr    $0x4,%rax
movzbl (%rax,%r13,1),%ecx
mov    %r12d,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <Z_DF_ADD_CHAR@plt>
movzbl (%rbx,%rbp,1),%eax
and    $0xf,%eax
movzbl (%rax,%r13,1),%ecx
mov    %r12d,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <Z_DF_ADD_CHAR@plt>
add    $0x1,%rbp
cmp    %r15,%rbp
jb     1140 <zhex_encode+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.zhex_encode.dec2hex = [17 x i8] c"0123456789ABCDEF\00"
define i32 @zhex_encode(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [17 x i8]
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%14 = bitcast [17 x i8]* %9 to i8*
%15 = load i8*, i8** %5
store i8* %15, i8** %10
store i32 0, i32* %13
store i32 0, i32* %11
br label %16
16: ; preds = %62, %4
%17 = load i32, i32* %11
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %65
20: ; preds = %16
%21 = load i32, i32* %8
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i32, i32* %13
%25 = add nsw i32 %24, 2
%26 = load i32, i32* %8
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %23
br label %65
29: ; preds = %23, %20
%30 = load i8*, i8** %10
%31 = load i32, i32* %11
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = ashr i32 %35, 4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
store i32 %40, i32* %12
%41 = load i32, i32* %8
%42 = load i8*, i8** %7
%43 = load i32, i32* %13
%44 = load i32, i32* %12
%45 = call i32 @Z_DF_ADD_CHAR(i32 %41, i8* %42, i32 %43, i32 %44)
%46 = load i8*, i8** %10
%47 = load i32, i32* %11
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = and i32 %51, 15
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 %53
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
store i32 %56, i32* %12
%57 = load i32, i32* %8
%58 = load i8*, i8** %7
%59 = load i32, i32* %13
%60 = load i32, i32* %12
%61 = call i32 @Z_DF_ADD_CHAR(i32 %57, i8* %58, i32 %59, i32 %60)
br label %62
62: ; preds = %29
%63 = load i32, i32* %11
%64 = add nsw i32 %63, 1
store i32 %64, i32* %11
br label %16
65: ; preds = %28, %16
%66 = load i32, i32* %13
ret i32 %66
}

LLVM IR (-O2):
@__const.zhex_encode.dec2hex = [17 x i8] c"0123456789ABCDEF\00"
define i32 @zhex_encode(i8* nocapture %0, i32 %1, i8* %2, i32 %3) {
%5 = icmp eq i32 %3, 1
%6 = icmp slt i32 %1, 1
%7 = or i1 %5, %6
br i1 %7, label %29, label %8
8: ; preds = %4
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %27, %10 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = lshr i8 %13, 4
%15 = zext i8 %14 to i64
%16 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.zhex_encode.dec2hex, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = tail call i32 @Z_DF_ADD_CHAR(i32 %3, i8* %2, i32 0, i32 %18)
%20 = load i8, i8* %12
%21 = and i8 %20, 15
%22 = zext i8 %21 to i64
%23 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.zhex_encode.dec2hex, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = tail call i32 @Z_DF_ADD_CHAR(i32 %3, i8* %2, i32 0, i32 %25)
%27 = add nuw nsw i64 %11, 1
%28 = icmp ult i64 %27, %9
br i1 %28, label %10, label %29
29: ; preds = %10, %4
ret i32 0
}