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

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *bin2hex(uint8_t dig[], char str[], size_t n) {
  size_t i;
  for (i = 0; i < n; i++) {
    snprintf(&str[i * 2], 3, "%02x", dig[i]);
  }
  return str;
}


x86 assembly (clang, -O0):
<bin2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1176 <bin2hex+0x66>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    $0x3,%esi
lea    0xea0(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112c <bin2hex+0x1c>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdx,%rdx
je     1161 <bin2hex+0x51>
mov    %rdx,%r13
mov    %rdi,%r15
xor    %ebp,%ebp
lea    0xece(%rip),%r12
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15,%rbp,4),%ecx
mov    %rbx,%rdi
mov    $0x3,%esi
mov    %r12,%rdx
call   1030 <snprintf@plt>
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r13
jne    1140 <bin2hex+0x30>
mov    %r14,%rax
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 i8* @bin2hex(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %8
8: ; preds = %22, %3
%9 = load i64, i64* %7
%10 = load i64, i64* %6
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %25
12: ; preds = %8
%13 = load i8*, i8** %5
%14 = load i64, i64* %7
%15 = mul i64 %14, 2
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i32*, i32** %4
%18 = load i64, i64* %7
%19 = getelementptr inbounds i32, i32* %17, i64 %18
%20 = load i32, i32* %19
%21 = call i32 @snprintf(i8* %16, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %12
%23 = load i64, i64* %7
%24 = add i64 %23, 1
store i64 %24, i64* %7
br label %8
25: ; preds = %8
%26 = load i8*, i8** %5
ret i8* %26
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i8* @bin2hex(i32* nocapture %0, i8* returned %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %14, label %5
5: ; preds = %3, %5
%6 = phi i64 [ %12, %5 ], [ 0, %3 ]
%7 = shl i64 %6, 1
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = getelementptr inbounds i32, i32* %0, i64 %6
%10 = load i32, i32* %9
%11 = tail call i32 @snprintf(i8* %8, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = add nuw i64 %6, 1
%13 = icmp eq i64 %12, %2
br i1 %13, label %14, label %5
14: ; preds = %5, %3
ret i8* %1
}