#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*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ sprintf(unsigned char *, char *, unsigned char);
int bin_to_str(unsigned char *bin_buffer, int bin_buffer_len) {
  if (bin_buffer_len <= 0 || bin_buffer_len > 256)
    return 0;
  unsigned char temp_bin_buffer[256];
  memcpy(temp_bin_buffer, bin_buffer, bin_buffer_len);
  int i;
  for (i = 0; i < bin_buffer_len; i++) {
    sprintf(&bin_buffer[i * 2], "%02x", temp_bin_buffer[i]);
  }
  return (i * 2);
}


x86 assembly (clang, -O0):
<bin_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1149 <bin_to_str+0x29>
cmpl   $0x100,-0x14(%rbp)
jle    1155 <bin_to_str+0x35>
movl   $0x0,-0x4(%rbp)
jmp    11ce <bin_to_str+0xae>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <memcpy@plt>
movl   $0x0,-0x124(%rbp)
mov    -0x124(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c2 <bin_to_str+0xa2>
mov    -0x10(%rbp),%rdi
mov    -0x124(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
movslq -0x124(%rbp),%rax
lea    0xe5f(%rip),%rsi
movzbl -0x120(%rbp,%rax,1),%edx
ff
call   1040 <sprintf@plt>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    1172 <bin_to_str+0x52>
mov    -0x124(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin_to_str>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %esi,%r14d
lea    -0x101(%r14),%ecx
xor    %eax,%eax
cmp    $0xffffff00,%ecx
jb     1192 <bin_to_str+0x72>
mov    %rdi,%r13
mov    %rsp,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
call   1030 <memcpy@plt>
mov    %r14d,%r12d
xor    %ebx,%ebx
lea    0xe9f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rsp,%rbx,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
add    $0x1,%rbx
add    $0x2,%r13
cmp    %rbx,%r12
jne    1170 <bin_to_str+0x50>
add    %r14d,%r14d
mov    %r14d,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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