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


x86 assembly (clang, -O0):
<url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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     1250 <url_encode+0x110>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movzbl -0x21(%rbp),%edi
call   1050 <isalnum@plt>
cmp    $0x0,%rax
jne    11dc <url_encode+0x9c>
movzbl -0x21(%rbp),%eax
cmp    $0x2d,%eax
je     11dc <url_encode+0x9c>
movzbl -0x21(%rbp),%eax
cmp    $0x5f,%eax
je     11dc <url_encode+0x9c>
movzbl -0x21(%rbp),%eax
cmp    $0x2e,%eax
je     11dc <url_encode+0x9c>
movzbl -0x21(%rbp),%eax
cmp    $0x7e,%eax
jne    11f5 <url_encode+0xb5>
mov    -0x21(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    124b <url_encode+0x10b>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x25,(%rax)
movzbl -0x21(%rbp),%eax
sar    $0x4,%eax
movzbl %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)
movzbl -0x21(%rbp),%eax
and    $0xf,%eax
movzbl %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    1174 <url_encode+0x34>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_encode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1040 <strlen@plt>
lea    (%rax,%rax,2),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    (%r12),%bl
mov    %rax,%r15
test   %bl,%bl
je     11db <url_encode+0x9b>
add    $0x1,%r12
mov    %r14,%r15
movzbl %bl,%edi
call   1050 <isalnum@plt>
test   %rax,%rax
je     11a0 <url_encode+0x60>
mov    %bl,(%r15)
add    $0x1,%r15
movzbl (%r12),%ebx
add    $0x1,%r12
test   %bl,%bl
jne    1170 <url_encode+0x30>
jmp    11db <url_encode+0x9b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    -0x2d(%rbx),%eax
cmp    $0x2,%al
jb     117d <url_encode+0x3d>
cmp    $0x7e,%bl
je     117d <url_encode+0x3d>
cmp    $0x5f,%bl
je     117d <url_encode+0x3d>
movb   $0x25,(%r15)
mov    %ebx,%eax
shr    $0x4,%al
movzbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,0x1(%r15)
and    $0xf,%bl
movzbl %bl,%edi
call   1030 <to_hex@plt>
mov    %al,0x2(%r15)
add    $0x3,%r15
jmp    1184 <url_encode+0x44>
movb   $0x0,(%r15)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define noalias i8* @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 i8* @malloc(i32 %4)
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %35, label %8
8: ; preds = %1, %31
%9 = phi i8 [ %33, %31 ], [ %6, %1 ]
%10 = phi i8* [ %12, %31 ], [ %0, %1 ]
%11 = phi i8* [ %32, %31 ], [ %5, %1 ]
%12 = getelementptr inbounds i8, i8* %10, i64 1
%13 = tail call i64 @isalnum(i8 zeroext %9)
%14 = freeze i64 %13
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %8
switch i8 %9, label %19 [
i8 126, label %17
i8 95, label %17
i8 46, label %17
i8 45, label %17
]
17: ; preds = %16, %16, %16, %16, %8
%18 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %9, i8* %11
br label %31
19: ; preds = %16
%20 = getelementptr inbounds i8, i8* %11, i64 1
store i8 37, i8* %11
%21 = lshr i8 %9, 4
%22 = tail call i8* @to_hex(i8 zeroext %21)
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i8
%25 = getelementptr inbounds i8, i8* %11, i64 2
store i8 %24, i8* %20
%26 = and i8 %9, 15
%27 = tail call i8* @to_hex(i8 zeroext %26)
%28 = ptrtoint i8* %27 to i64
%29 = trunc i64 %28 to i8
%30 = getelementptr inbounds i8, i8* %11, i64 3
store i8 %29, i8* %25
br label %31
31: ; preds = %19, %17
%32 = phi i8* [ %18, %17 ], [ %30, %19 ]
%33 = load i8, i8* %12
%34 = icmp eq i8 %33, 0
br i1 %34, label %35, label %8
35: ; preds = %31, %1
%36 = phi i8* [ %5, %1 ], [ %32, %31 ]
store i8 0, i8* %36
ret i8* %5
}