#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 */
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
void print_bin(const unsigned char *str, size_t len) {
  int i;
  char *strhex = (char *)calloc(len * 2 + 1, sizeof(char));
  for (i = 0; i < len; ++i)
    sprintf(&strhex[i * 2], "%02x", (unsigned int)str[i]);
  printf("Hexdump: %s\n", strhex);
  free(strhex);
}


x86 assembly (clang, -O0):
<print_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x1,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11b4 <print_bin+0x74>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xe5f(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1171 <print_bin+0x31>
mov    -0x20(%rbp),%rsi
lea    0xe46(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_bin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
lea    (%rsi,%rsi,1),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
test   %r13,%r13
je     119d <print_bin+0x5d>
xor    %ebp,%ebp
lea    0xe8c(%rip),%r12
mov    %r15,%rbx
nopw   0x0(%rax,%rax,1)
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <sprintf@plt>
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r13
jne    1180 <print_bin+0x40>
lea    0xe61(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

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