#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 size_t ssize_t;

/* Variables and functions */
scalar_t__ isprint(char const);
int /*<<< orphan*/ msg(int, char *, char const);
void binary_print(const char *s, ssize_t len) {
  ssize_t i;
  for (i = 0; i < len; i++) {
    if (isprint(s[i]))
      msg(1, "%c", s[i]);
    else
      msg(1, "0x%02X", (int)s[i]);
  }
}


x86 assembly (clang, -O0):
<binary_print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11b9 <binary_print+0x99>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isprint@plt>
cmp    $0x0,%rax
je     1183 <binary_print+0x63>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    $0x1,%edi
lea    0xe8b(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1040 <msg@plt>
jmp    11a3 <binary_print+0x83>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    $0x1,%edi
lea    0xe68(%rip),%rsi
movsbl %al,%edx
call   1040 <msg@plt>
jmp    11a8 <binary_print+0x88>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1138 <binary_print+0x18>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_print>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rsi,%rsi
je     117c <binary_print+0x5c>
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
lea    0xec6(%rip),%r12
lea    0xebc(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl (%r15,%rbx,1),%edi
call   1030 <isprint@plt>
test   %rax,%rax
movsbl (%r15,%rbx,1),%edx
mov    %r13,%rsi
cmove  %r12,%rsi
mov    $0x1,%edi
call   1040 <msg@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1150 <binary_print+0x30>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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