#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*/ sprintf(char *, char *, unsigned char);
int hex_encode(char *out, const char *in, int len) {
  int i;
  for (i = 0; i < len; i++) {
    if (!sprintf(&out[i * 2], "%02X", (unsigned char)in[i]))
      return 0;
  }
  return i * 2;
}


x86 assembly (clang, -O0):
<hex_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1185 <hex_encode+0x75>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
lea    0xeac(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
cmp    $0x0,%eax
jne    1172 <hex_encode+0x62>
movl   $0x0,-0x4(%rbp)
jmp    118e <hex_encode+0x7e>
jmp    1177 <hex_encode+0x67>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    112a <hex_encode+0x1a>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    116a <hex_encode+0x5a>
mov    %edx,%r15d
mov    %rsi,%r13
mov    %rdi,%rbp
mov    %edx,%ebx
lea    0xecf(%rip),%r12
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl 0x0(%r13,%r14,1),%edx
mov    %rbp,%rdi
mov    %r12,%rsi
call   1030 <sprintf@plt>
test   %eax,%eax
je     116a <hex_encode+0x5a>
add    $0x1,%r14
add    $0x2,%rbp
cmp    %r14,%rbx
jne    1140 <hex_encode+0x30>
add    %r15d,%r15d
mov    %r15d,%eax
jmp    116c <hex_encode+0x5c>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%02X\00"
define i32 @hex_encode(i8* nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %20
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %15
%8 = phi i64 [ 0, %5 ], [ %16, %15 ]
%9 = shl nuw nsw i64 %8, 1
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = getelementptr inbounds i8, i8* %1, i64 %8
%12 = load i8, i8* %11
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %7
%16 = add nuw nsw i64 %8, 1
%17 = icmp eq i64 %16, %6
br i1 %17, label %18, label %7
18: ; preds = %15
%19 = shl i32 %2, 1
br label %20
20: ; preds = %7, %3, %18
%21 = phi i32 [ 0, %3 ], [ %19, %18 ], [ 0, %7 ]
ret i32 %21
}