#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void hexbasenl(FILE *fp, const void *ptr, int len) {
  const unsigned char *buf = (const unsigned char *)ptr;
  for (; len-- > 0; buf++) {
    unsigned char chr = (unsigned char)*buf;
    if (chr >= 32 && chr < 126)
      fprintf(fp, "%c", chr);
    else if (chr == 126)
      fprintf(fp, "%s", "~~");
    else {
      fprintf(fp, "%c", '~');
      do
        fprintf(fp, "%02x", *buf++);
      while (len-- > 0);
      fprintf(fp, "%c", '~');
    }
  }
}


x86 assembly (clang, -O0):
<hexbasenl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    1229 <hexbasenl+0x119>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movzbl -0x21(%rbp),%eax
cmp    $0x20,%eax
jl     117d <hexbasenl+0x6d>
movzbl -0x21(%rbp),%eax
cmp    $0x7e,%eax
jge    117d <hexbasenl+0x6d>
mov    -0x8(%rbp),%rdi
movzbl -0x21(%rbp),%edx
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1213 <hexbasenl+0x103>
movzbl -0x21(%rbp),%eax
cmp    $0x7e,%eax
jne    11a8 <hexbasenl+0x98>
mov    -0x8(%rbp),%rdi
lea    0xe6e(%rip),%rsi
lea    0xe6a(%rip),%rdx
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    120e <hexbasenl+0xfe>
mov    -0x8(%rbp),%rdi
lea    0xe4d(%rip),%rsi
mov    $0x7e,%edx
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movzbl (%rax),%edx
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jg     11bf <hexbasenl+0xaf>
mov    -0x8(%rbp),%rdi
lea    0xdfe(%rip),%rsi
mov    $0x7e,%edx
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1213 <hexbasenl+0x103>
jmp    1218 <hexbasenl+0x108>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112b <hexbasenl+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexbasenl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    119e <hexbasenl+0x6e>
mov    %edx,%r15d
mov    %rsi,%r13
mov    %rdi,%r14
lea    0x1(%r15),%ebp
xor    %ebx,%ebx
lea    0xeab(%rip),%r12
jmp    1174 <hexbasenl+0x44>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rsi
call   1030 <fputc@plt>
add    $0x1,%rbx
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
jle    119e <hexbasenl+0x6e>
movzbl 0x0(%r13,%rbx,1),%edi
lea    -0x20(%rdi),%eax
cmp    $0x5d,%al
jbe    1160 <hexbasenl+0x30>
cmp    $0x7e,%dil
jne    11ad <hexbasenl+0x7d>
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r12,%rdi
mov    %r14,%rcx
call   1050 <fwrite@plt>
jmp    1168 <hexbasenl+0x38>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x7e,%edi
mov    %r14,%rsi
call   1030 <fputc@plt>
mov    %r15d,%ebp
lea    0xe3f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl 0x0(%r13,%rbx,1),%edx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    11d0 <hexbasenl+0xa0>
mov    $0x7e,%edi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fputc@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%c\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [3 x i8] c"~~\00"
@.str.3 = [5 x i8] c"%02x\00"
define void @hexbasenl(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
store i8* %9, i8** %7
br label %10
10: ; preds = %55, %3
%11 = load i32, i32* %6
%12 = add nsw i32 %11, -1
store i32 %12, i32* %6
%13 = icmp sgt i32 %11, 0
br i1 %13, label %14, label %58
14: ; preds = %10
%15 = load i8*, i8** %7
%16 = load i8, i8* %15
store i8 %16, i8* %8
%17 = load i8, i8* %8
%18 = zext i8 %17 to i32
%19 = icmp sge i32 %18, 32
br i1 %19, label %20, label %29
20: ; preds = %14
%21 = load i8, i8* %8
%22 = zext i8 %21 to i32
%23 = icmp slt i32 %22, 126
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32*, i32** %4
%26 = load i8, i8* %8
%27 = zext i8 %26 to i32
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %27)
br label %54
29: ; preds = %20, %14
%30 = load i8, i8* %8
%31 = zext i8 %30 to i32
%32 = icmp eq i32 %31, 126
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32*, i32** %4
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %53
36: ; preds = %29
%37 = load i32*, i32** %4
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 126)
br label %39
39: ; preds = %46, %36
%40 = load i32*, i32** %4
%41 = load i8*, i8** %7
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %7
%43 = load i8, i8* %41
%44 = zext i8 %43 to i32
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %39
%47 = load i32, i32* %6
%48 = add nsw i32 %47, -1
store i32 %48, i32* %6
%49 = icmp sgt i32 %47, 0
br i1 %49, label %39, label %50
50: ; preds = %46
%51 = load i32*, i32** %4
%52 = call i32 (i32*, i8*, ...) @fprintf(i32* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 126)
br label %53
53: ; preds = %50, %33
br label %54
54: ; preds = %53, %24
br label %55
55: ; preds = %54
%56 = load i8*, i8** %7
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %7
br label %10
58: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str.2 = [3 x i8] c"~~\00"
@.str.3 = [5 x i8] c"%02x\00"
define void @hexbasenl(i32* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %35
5: ; preds = %3, %32
%6 = phi i32 [ %8, %32 ], [ %2, %3 ]
%7 = phi i8* [ %33, %32 ], [ %1, %3 ]
%8 = add nsw i32 %6, -1
%9 = load i8, i8* %7
%10 = add i8 %9, -32
%11 = icmp ult i8 %10, 94
br i1 %11, label %12, label %15
12: ; preds = %5
%13 = zext i8 %9 to i32
%14 = tail call i32 @fputc(i32 %13, i32* %0)
br label %32
15: ; preds = %5
%16 = icmp eq i8 %9, 126
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 2, i64 1, i32* %0)
br label %32
19: ; preds = %15
%20 = tail call i32 @fputc(i32 126, i32* %0)
br label %21
21: ; preds = %21, %19
%22 = phi i8* [ %7, %19 ], [ %24, %21 ]
%23 = phi i32 [ %8, %19 ], [ %28, %21 ]
%24 = getelementptr inbounds i8, i8* %22, i64 1
%25 = load i8, i8* %22
%26 = zext i8 %25 to i32
%27 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = add nsw i32 %23, -1
%29 = icmp eq i32 %23, 0
br i1 %29, label %30, label %21
30: ; preds = %21
%31 = tail call i32 @fputc(i32 126, i32* %0)
br label %35
32: ; preds = %17, %12
%33 = getelementptr inbounds i8, i8* %7, i64 1
%34 = icmp sgt i32 %6, 1
br i1 %34, label %5, label %35
35: ; preds = %32, %30, %3
ret void
}