#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 */
void *tohex(char const);
int bin2hex(const char *bin, char *hex, int count) {
  int i;
  for (i = 0; i < count; i++) {
    *hex++ = tohex((*bin >> 4) & 0xf);
    *hex++ = tohex(*bin++ & 0xf);
  }
  *hex = 0;
  return i;
}


x86 assembly (clang, -O0):
<bin2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119c <bin2hex+0x8c>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sar    $0x4,%eax
and    $0xf,%eax
movsbl %al,%edi
call   1030 <tohex@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%eax
and    $0xf,%eax
movsbl %al,%edi
call   1030 <tohex@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112a <bin2hex+0x1a>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
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,%r15
test   %edx,%edx
jle    116a <bin2hex+0x5a>
mov    %edx,%r14d
mov    %rdi,%r13
mov    %edx,%r12d
xor    %ebx,%ebx
xor    %ebp,%ebp
nop
movzbl 0x0(%r13,%rbp,1),%eax
shr    $0x4,%al
movzbl %al,%edi
call   1030 <tohex@plt>
mov    %al,(%r15,%rbp,2)
movzbl 0x0(%r13,%rbp,1),%edi
and    $0xf,%edi
call   1030 <tohex@plt>
mov    %al,0x1(%r15,%rbp,2)
add    $0x1,%rbp
add    $0xfffffffffffffffe,%rbx
cmp    %ebp,%r12d
jne    1130 <bin2hex+0x20>
sub    %rbx,%r15
jmp    116d <bin2hex+0x5d>
xor    %r14d,%r14d
movb   $0x0,(%r15)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bin2hex(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %33, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %36
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = ashr i32 %15, 4
%17 = and i32 %16, 15
%18 = trunc i32 %17 to i8
%19 = call i8* @tohex(i8 signext %18)
%20 = ptrtoint i8* %19 to i8
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %5
store i8 %20, i8* %21
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %4
%25 = load i8, i8* %23
%26 = sext i8 %25 to i32
%27 = and i32 %26, 15
%28 = trunc i32 %27 to i8
%29 = call i8* @tohex(i8 signext %28)
%30 = ptrtoint i8* %29 to i8
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %5
store i8 %30, i8* %31
br label %33
33: ; preds = %12
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
br label %8
36: ; preds = %8
%37 = load i8*, i8** %5
store i8 0, i8* %37
%38 = load i32, i32* %7
ret i32 %38
}

LLVM IR (-O2):
define i32 @bin2hex(i8* nocapture %0, i8* nocapture writeonly %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %24
5: ; preds = %3, %5
%6 = phi i32 [ %22, %5 ], [ 0, %3 ]
%7 = phi i8* [ %15, %5 ], [ %0, %3 ]
%8 = phi i8* [ %21, %5 ], [ %1, %3 ]
%9 = load i8, i8* %7
%10 = lshr i8 %9, 4
%11 = tail call i8* @tohex(i8 signext %10)
%12 = ptrtoint i8* %11 to i64
%13 = trunc i64 %12 to i8
%14 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %13, i8* %8
%15 = getelementptr inbounds i8, i8* %7, i64 1
%16 = load i8, i8* %7
%17 = and i8 %16, 15
%18 = tail call i8* @tohex(i8 signext %17)
%19 = ptrtoint i8* %18 to i64
%20 = trunc i64 %19 to i8
%21 = getelementptr inbounds i8, i8* %8, i64 2
store i8 %20, i8* %14
%22 = add nuw nsw i32 %6, 1
%23 = icmp eq i32 %22, %2
br i1 %23, label %24, label %5
24: ; preds = %5, %3
%25 = phi i8* [ %1, %3 ], [ %21, %5 ]
%26 = phi i32 [ 0, %3 ], [ %2, %5 ]
store i8 0, i8* %25
ret i32 %26
}