#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 sprintf(char *, char *, unsigned char const);
int encode_hex(const unsigned char *inbuf, size_t inlen, char *outbuf) {
  for (size_t i = 0; i < inlen; ++i) {
    if (sprintf(&(outbuf[i * 2]), "%02x", inbuf[i]) != 2) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<encode_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    118c <encode_hex+0x7c>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
lea    0xea8(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
cmp    $0x2,%eax
je     1176 <encode_hex+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1193 <encode_hex+0x83>
jmp    117b <encode_hex+0x6b>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    112c <encode_hex+0x1c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rsi,%rsi
je     118c <encode_hex+0x7c>
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r15
movzbl (%rdi),%edx
lea    0xecf(%rip),%rsi
mov    %r13,%rdi
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x2,%ecx
jne    118e <encode_hex+0x7e>
add    $0x2,%r13
mov    $0x1,%ecx
lea    0xeab(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rcx,%rbx
cmp    %rcx,%r14
je     1185 <encode_hex+0x75>
movzbl (%r15,%rbx,1),%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <sprintf@plt>
lea    0x1(%rbx),%rcx
add    $0x2,%r13
cmp    $0x2,%eax
je     1160 <encode_hex+0x50>
cmp    %r14,%rbx
sbb    %eax,%eax
jmp    118e <encode_hex+0x7e>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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