#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);
scalar_t__ malloc(int);
int strlen(char const *);
void *to_hex(char const);
char *fast_url_encode(const char *str) {
  const char *pstr = str;
  char *buf = (char *)malloc(strlen(str) * 3 + 1), *pbuf = buf;
  while (*pstr) {
    if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' ||
        *pstr == '~')
      *pbuf++ = *pstr;
    else if (*pstr == ' ')
      *pbuf++ = '+';
    else
      *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
    pstr++;
  }
  *pbuf = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<fast_url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
imul   $0x3,%eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     128c <fast_url_encode+0x14c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isalnum@plt>
cmp    $0x0,%rax
jne    11d7 <fast_url_encode+0x97>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     11d7 <fast_url_encode+0x97>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
je     11d7 <fast_url_encode+0x97>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     11d7 <fast_url_encode+0x97>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    11f3 <fast_url_encode+0xb3>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    127b <fast_url_encode+0x13b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    121a <fast_url_encode+0xda>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x2b,(%rax)
jmp    1276 <fast_url_encode+0x136>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x25,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
sar    $0x4,%eax
movsbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0xf,%eax
movsbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    127b <fast_url_encode+0x13b>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1174 <fast_url_encode+0x34>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fast_url_encode>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    (%rax,%rax,2),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
mov    %r14,%r15
jmp    117f <fast_url_encode+0x3f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %cl,(%r15)
add    $0x1,%r15
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11df <fast_url_encode+0x9f>
movsbl %al,%edi
call   1050 <isalnum@plt>
movzbl (%rbx),%ecx
test   %rax,%rax
jne    1170 <fast_url_encode+0x30>
cmp    $0x5e,%cl
jg     11aa <fast_url_encode+0x6a>
lea    -0x2d(%rcx),%eax
cmp    $0x2,%al
jb     1170 <fast_url_encode+0x30>
cmp    $0x20,%cl
jne    11b4 <fast_url_encode+0x74>
movb   $0x2b,(%r15)
jmp    1173 <fast_url_encode+0x33>
cmp    $0x7e,%cl
je     1170 <fast_url_encode+0x30>
cmp    $0x5f,%cl
je     1170 <fast_url_encode+0x30>
movb   $0x25,(%r15)
movzbl (%rbx),%eax
sar    $0x4,%al
movsbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,0x1(%r15)
movzbl (%rbx),%edi
and    $0xf,%edi
call   1030 <to_hex@plt>
mov    %al,0x2(%r15)
add    $0x3,%r15
jmp    1177 <fast_url_encode+0x37>
movb   $0x0,(%r15)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @fast_url_encode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
store i8* %6, i8** %3
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = mul nsw i32 %8, 3
%10 = add nsw i32 %9, 1
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %4
%13 = load i8*, i8** %4
store i8* %13, i8** %5
br label %14
14: ; preds = %78, %1
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 0
br i1 %17, label %18, label %81
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = call i64 @isalnum(i8 signext %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %43, label %23
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 45
br i1 %27, label %43, label %28
28: ; preds = %23
%29 = load i8*, i8** %3
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 95
br i1 %32, label %43, label %33
33: ; preds = %28
%34 = load i8*, i8** %3
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 46
br i1 %37, label %43, label %38
38: ; preds = %33
%39 = load i8*, i8** %3
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 126
br i1 %42, label %43, label %48
43: ; preds = %38, %33, %28, %23, %18
%44 = load i8*, i8** %3
%45 = load i8, i8* %44
%46 = load i8*, i8** %5
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %5
store i8 %45, i8* %46
br label %78
48: ; preds = %38
%49 = load i8*, i8** %3
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 32
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = load i8*, i8** %5
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %5
store i8 43, i8* %54
br label %77
56: ; preds = %48
%57 = load i8*, i8** %5
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %5
store i8 37, i8* %57
%59 = load i8*, i8** %3
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = ashr i32 %61, 4
%63 = trunc i32 %62 to i8
%64 = call i8* @to_hex(i8 signext %63)
%65 = ptrtoint i8* %64 to i8
%66 = load i8*, i8** %5
%67 = getelementptr inbounds i8, i8* %66, i32 1
store i8* %67, i8** %5
store i8 %65, i8* %66
%68 = load i8*, i8** %3
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = and i32 %70, 15
%72 = trunc i32 %71 to i8
%73 = call i8* @to_hex(i8 signext %72)
%74 = ptrtoint i8* %73 to i8
%75 = load i8*, i8** %5
%76 = getelementptr inbounds i8, i8* %75, i32 1
store i8* %76, i8** %5
store i8 %74, i8* %75
br label %77
77: ; preds = %56, %53
br label %78
78: ; preds = %77, %43
%79 = load i8*, i8** %3
%80 = getelementptr inbounds i8, i8* %79, i32 1
store i8* %80, i8** %3
br label %14
81: ; preds = %14
%82 = load i8*, i8** %5
store i8 0, i8* %82
%83 = load i8*, i8** %4
ret i8* %83
}

LLVM IR (-O2):
define i8* @fast_url_encode(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = mul nsw i32 %2, 3
%4 = add nsw i32 %3, 1
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %40, label %9
9: ; preds = %1, %35
%10 = phi i8 [ %38, %35 ], [ %7, %1 ]
%11 = phi i8* [ %36, %35 ], [ %6, %1 ]
%12 = phi i8* [ %37, %35 ], [ %0, %1 ]
%13 = tail call i64 @isalnum(i8 signext %10)
%14 = icmp eq i64 %13, 0
%15 = load i8, i8* %12
br i1 %14, label %16, label %17
16: ; preds = %9
switch i8 %15, label %21 [
i8 45, label %17
i8 95, label %17
i8 46, label %17
i8 126, label %17
i8 32, label %19
]
17: ; preds = %9, %16, %16, %16, %16
%18 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %15, i8* %11
br label %35
19: ; preds = %16
%20 = getelementptr inbounds i8, i8* %11, i64 1
store i8 43, i8* %11
br label %35
21: ; preds = %16
%22 = getelementptr inbounds i8, i8* %11, i64 1
store i8 37, i8* %11
%23 = load i8, i8* %12
%24 = ashr i8 %23, 4
%25 = tail call i8* @to_hex(i8 signext %24)
%26 = ptrtoint i8* %25 to i64
%27 = trunc i64 %26 to i8
%28 = getelementptr inbounds i8, i8* %11, i64 2
store i8 %27, i8* %22
%29 = load i8, i8* %12
%30 = and i8 %29, 15
%31 = tail call i8* @to_hex(i8 signext %30)
%32 = ptrtoint i8* %31 to i64
%33 = trunc i64 %32 to i8
%34 = getelementptr inbounds i8, i8* %11, i64 3
store i8 %33, i8* %28
br label %35
35: ; preds = %19, %21, %17
%36 = phi i8* [ %18, %17 ], [ %20, %19 ], [ %34, %21 ]
%37 = getelementptr inbounds i8, i8* %12, i64 1
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 0
br i1 %39, label %40, label %9
40: ; preds = %35, %1
%41 = phi i8* [ %6, %1 ], [ %36, %35 ]
store i8 0, i8* %41
ret i8* %6
}