#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__ isalnum(char const);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strchr(char *, char const);
void strencode(char *to, size_t tosize, const char *from) {
  int tolen;
  for (tolen = 0; *from != '\0' && tolen + 4 < tosize; ++from) {
    if (isalnum(*from) || strchr("/_.-~", *from) != (char *)0) {
      *to = *from;
      ++to;
      ++tolen;
    } else {
      (void)sprintf(to, "%%%02x", (int)*from & 0xff);
      to += 3;
      tolen += 3;
    }
  }
  *to = '\0';
}


x86 assembly (clang, -O0):
<strencode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
je     1172 <strencode+0x42>
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
cltq
cmp    -0x10(%rbp),%rax
setb   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1182 <strencode+0x52>
jmp    1223 <strencode+0xf3>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
jne    11b5 <strencode+0x85>
mov    -0x18(%rbp),%rax
lea    0xe5d(%rip),%rdi
movsbl (%rax),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11db <strencode+0xab>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120d <strencode+0xdd>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%edx
and    $0xff,%edx
lea    0xe13(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x1c(%rbp)
jmp    1212 <strencode+0xe2>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    114b <strencode+0x1b>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strencode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdx),%al
test   %al,%al
je     11c7 <strencode+0x97>
mov    %rdx,%rbx
mov    %rsi,%r15
add    $0x1,%rbx
xor    %ebp,%ebp
lea    0xea5(%rip),%r12
jmp    117d <strencode+0x4d>
nopl   (%rax)
mov    %r13b,(%r14)
mov    $0x1,%eax
mov    $0x1,%ecx
add    %rcx,%r14
add    %eax,%ebp
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11c7 <strencode+0x97>
lea    0x4(%rbp),%ecx
cmp    %r15,%rcx
jae    11c7 <strencode+0x97>
movsbl %al,%edi
call   1040 <isalnum@plt>
movzbl -0x1(%rbx),%r13d
test   %rax,%rax
jne    1160 <strencode+0x30>
movsbl %r13b,%esi
mov    %r12,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1160 <strencode+0x30>
movzbl %r13b,%edx
mov    %r14,%rdi
lea    0xe50(%rip),%rsi
call   1050 <sprintf@plt>
mov    $0x3,%eax
mov    $0x3,%ecx
jmp    116d <strencode+0x3d>
movb   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"/_.-~\00"
@.str.1 = [7 x i8] c"%%%02x\00"
define void @strencode(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
store i32 0, i32* %7
br label %8
8: ; preds = %51, %3
%9 = load i8*, i8** %6
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %8
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 4
%16 = sext i32 %15 to i64
%17 = load i64, i64* %5
%18 = icmp ult i64 %16, %17
br label %19
19: ; preds = %13, %8
%20 = phi i1 [ false, %8 ], [ %18, %13 ]
br i1 %20, label %21, label %54
21: ; preds = %19
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = call i64 @isalnum(i8 signext %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = load i8*, i8** %6
%28 = load i8, i8* %27
%29 = call i8* @strchr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 signext %28)
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %39
31: ; preds = %26, %21
%32 = load i8*, i8** %6
%33 = load i8, i8* %32
%34 = load i8*, i8** %4
store i8 %33, i8* %34
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %4
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %50
39: ; preds = %26
%40 = load i8*, i8** %4
%41 = load i8*, i8** %6
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = and i32 %43, 255
%45 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = load i8*, i8** %4
%47 = getelementptr inbounds i8, i8* %46, i64 3
store i8* %47, i8** %4
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 3
store i32 %49, i32* %7
br label %50
50: ; preds = %39, %31
br label %51
51: ; preds = %50
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %6
br label %8
54: ; preds = %19
%55 = load i8*, i8** %4
store i8 0, i8* %55
ret void
}

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