#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 */
char *calloc(size_t, int);
int /*<<< orphan*/ isalnum(char const);
int /*<<< orphan*/ snprintf(char *, int, char *, char const);
size_t strlen(char const *);
char *WaitressUrlEncode(const char *in) {
  size_t inLen = strlen(in);
  char *out = calloc(inLen * 3 + 1, sizeof(*in));
  const char *inPos = in;
  char *outPos = out;
  while (inPos - in < inLen) {
    if (!isalnum(*inPos) && *inPos != '_' && *inPos != '-' && *inPos != '.') {
      *outPos++ = '%';
      snprintf(outPos, 3, "%02x", *inPos & 0xff);
      outPos += 2;
    } else {
      *outPos++ = *inPos;
    }
    ++inPos;
  }
  return out;
}


x86 assembly (clang, -O0):
<WaitressUrlEncode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
imul   $0x3,-0x10(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
cmp    -0x10(%rbp),%rax
jae    1249 <WaitressUrlEncode+0x109>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%eax
jne    1221 <WaitressUrlEncode+0xe1>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
je     1221 <WaitressUrlEncode+0xe1>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     1221 <WaitressUrlEncode+0xe1>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1221 <WaitressUrlEncode+0xe1>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x25,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
and    $0xff,%eax
mov    $0x3,%esi
lea    0xdf8(%rip),%rdx
movsbl %al,%ecx
call   1050 <snprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
jmp    1238 <WaitressUrlEncode+0xf8>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1180 <WaitressUrlEncode+0x40>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WaitressUrlEncode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %rax,%r14
lea    (%rax,%rax,2),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r12
test   %r14,%r14
je     11dc <WaitressUrlEncode+0x9c>
xor    %ebx,%ebx
movabs $0x4000000000003,%rbp
mov    %r12,%r15
nop
movsbl 0x0(%r13,%rbx,1),%edi
call   1040 <isalnum@plt>
movzbl 0x0(%r13,%rbx,1),%ecx
test   %eax,%eax
jne    11a5 <WaitressUrlEncode+0x65>
lea    -0x2d(%rcx),%eax
cmp    $0x32,%al
ja     11b7 <WaitressUrlEncode+0x77>
movzbl %al,%eax
bt     %rax,%rbp
jae    11b7 <WaitressUrlEncode+0x77>
mov    %cl,(%r15)
add    $0x1,%r15
add    $0x1,%rbx
cmp    %r14,%rbx
jb     1180 <WaitressUrlEncode+0x40>
jmp    11dc <WaitressUrlEncode+0x9c>
lea    0x1(%r15),%rdi
movb   $0x25,(%r15)
movsbl 0x0(%r13,%rbx,1),%ecx
mov    $0x3,%esi
lea    0xe2f(%rip),%rdx
call   1050 <snprintf@plt>
add    $0x3,%r15
jmp    11ac <WaitressUrlEncode+0x6c>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define i8* @WaitressUrlEncode(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = mul i64 %9, 3
%11 = add i64 %10, 1
%12 = call i8* @calloc(i64 %11, i32 1)
store i8* %12, i8** %4
%13 = load i8*, i8** %2
store i8* %13, i8** %5
%14 = load i8*, i8** %4
store i8* %14, i8** %6
br label %15
15: ; preds = %60, %1
%16 = load i8*, i8** %5
%17 = load i8*, i8** %2
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
%21 = load i64, i64* %3
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %63
23: ; preds = %15
%24 = load i8*, i8** %5
%25 = load i8, i8* %24
%26 = call i32 @isalnum(i8 signext %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %55, label %28
28: ; preds = %23
%29 = load i8*, i8** %5
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 95
br i1 %32, label %33, label %55
33: ; preds = %28
%34 = load i8*, i8** %5
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 45
br i1 %37, label %38, label %55
38: ; preds = %33
%39 = load i8*, i8** %5
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 46
br i1 %42, label %43, label %55
43: ; preds = %38
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %6
store i8 37, i8* %44
%46 = load i8*, i8** %6
%47 = load i8*, i8** %5
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = and i32 %49, 255
%51 = trunc i32 %50 to i8
%52 = call i32 @snprintf(i8* %46, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %51)
%53 = load i8*, i8** %6
%54 = getelementptr inbounds i8, i8* %53, i64 2
store i8* %54, i8** %6
br label %60
55: ; preds = %38, %33, %28, %23
%56 = load i8*, i8** %5
%57 = load i8, i8* %56
%58 = load i8*, i8** %6
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %6
store i8 %57, i8* %58
br label %60
60: ; preds = %55, %43
%61 = load i8*, i8** %5
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %5
br label %15
63: ; preds = %15
%64 = load i8*, i8** %4
ret i8* %64
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i8* @WaitressUrlEncode(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = mul i64 %2, 3
%4 = add i64 %3, 1
%5 = tail call i8* @calloc(i64 %4, i32 1)
%6 = ptrtoint i8* %0 to i64
%7 = icmp eq i64 %2, 0
br i1 %7, label %29, label %8
8: ; preds = %1, %23
%9 = phi i8* [ %24, %23 ], [ %5, %1 ]
%10 = phi i8* [ %25, %23 ], [ %0, %1 ]
%11 = load i8, i8* %10
%12 = tail call i32 @isalnum(i8 signext %11)
%13 = icmp eq i32 %12, 0
%14 = load i8, i8* %10
br i1 %13, label %15, label %21
15: ; preds = %8
switch i8 %14, label %16 [
i8 95, label %21
i8 45, label %21
i8 46, label %21
]
16: ; preds = %15
%17 = getelementptr inbounds i8, i8* %9, i64 1
store i8 37, i8* %9
%18 = load i8, i8* %10
%19 = tail call i32 @snprintf(i8* %17, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %18)
%20 = getelementptr inbounds i8, i8* %9, i64 3
br label %23
21: ; preds = %8, %15, %15, %15
%22 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %14, i8* %9
br label %23
23: ; preds = %21, %16
%24 = phi i8* [ %22, %21 ], [ %20, %16 ]
%25 = getelementptr inbounds i8, i8* %10, i64 1
%26 = ptrtoint i8* %25 to i64
%27 = sub i64 %26, %6
%28 = icmp ult i64 %27, %2
br i1 %28, label %8, label %29
29: ; preds = %23, %1
ret i8* %5
}