#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*/ sprintf(char *, char *, int);
int /*<<< orphan*/ str_insertChar(char *, char);
void str_encodeNonPrintableChar(char *pStr) {
  while (*pStr) {
    if (*pStr < 32 || *pStr > 0x7F) {
      char sHexCode[4];
      sprintf(sHexCode, "%2.2x", ((int)*pStr));
      *pStr++ = '%';
      str_insertChar(pStr, sHexCode[1]);
      str_insertChar(pStr, sHexCode[0]);
      pStr += 2;
    } else {
      pStr++;
    }
  }
}


x86 assembly (clang, -O0):
<str_encodeNonPrintableChar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11be <str_encodeNonPrintableChar+0x9e>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jl     1159 <str_encodeNonPrintableChar+0x39>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7f,%eax
jle    11ad <str_encodeNonPrintableChar+0x8d>
lea    -0xc(%rbp),%rdi
mov    -0x8(%rbp),%rax
movsbl (%rax),%edx
lea    0xe95(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x25,(%rax)
mov    -0x8(%rbp),%rdi
movsbl -0xb(%rbp),%esi
call   1030 <str_insertChar@plt>
mov    -0x8(%rbp),%rdi
movsbl -0xc(%rbp),%esi
call   1030 <str_insertChar@plt>
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
jmp    11b9 <str_encodeNonPrintableChar+0x99>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    112c <str_encodeNonPrintableChar+0xc>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_encodeNonPrintableChar>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    (%rdi),%al
test   %al,%al
je     1184 <str_encodeNonPrintableChar+0x64>
mov    %rdi,%rbx
lea    0xec8(%rip),%r14
lea    0x4(%rsp),%r15
jmp    114b <str_encodeNonPrintableChar+0x2b>
nop
add    $0x1,%rbx
movzbl (%rbx),%eax
test   %al,%al
je     1184 <str_encodeNonPrintableChar+0x64>
cmp    $0x1f,%al
jg     1140 <str_encodeNonPrintableChar+0x20>
movsbl %al,%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <sprintf@plt>
lea    0x1(%rbx),%r12
movb   $0x25,(%rbx)
movsbl 0x5(%rsp),%esi
mov    %r12,%rdi
call   1030 <str_insertChar@plt>
movsbl 0x4(%rsp),%esi
mov    %r12,%rdi
call   1030 <str_insertChar@plt>
add    $0x3,%rbx
jmp    1144 <str_encodeNonPrintableChar+0x24>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%2.2x\00"
define void @str_encodeNonPrintableChar(i8* %0) {
%2 = alloca [4 x i8]
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %27, label %5
5: ; preds = %1
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 1
br label %8
8: ; preds = %5, %23
%9 = phi i8 [ %3, %5 ], [ %25, %23 ]
%10 = phi i8* [ %0, %5 ], [ %24, %23 ]
%11 = icmp slt i8 %9, 32
br i1 %11, label %12, label %21
12: ; preds = %8
%13 = sext i8 %9 to i32
%14 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds i8, i8* %10, i64 1
store i8 37, i8* %10
%16 = load i8, i8* %7
%17 = tail call i32 @str_insertChar(i8* %15, i8 signext %16)
%18 = load i8, i8* %6
%19 = tail call i32 @str_insertChar(i8* %15, i8 signext %18)
%20 = getelementptr inbounds i8, i8* %10, i64 3
br label %23
21: ; preds = %8
%22 = getelementptr inbounds i8, i8* %10, i64 1
br label %23
23: ; preds = %21, %12
%24 = phi i8* [ %20, %12 ], [ %22, %21 ]
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %8
27: ; preds = %23, %1
ret void
}