#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__ libc_isalnum(char);
char *libc_malloc(int /*<<< orphan*/);
int /*<<< orphan*/ libc_strlen(char *);
void *libc_to_hex(char);
char *libc_urlencode(char *str) {
  char *pstr = str, *buf = libc_malloc(libc_strlen(str)), *pbuf = buf;
  while (*pstr) {
    if (libc_isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' ||
        *pstr == '~')
      *pbuf++ = *pstr;
    else if (*pstr == ' ')
      *pbuf++ = '+';
    else
      *pbuf++ = '%', *pbuf++ = libc_to_hex(*pstr >> 4),
      *pbuf++ = libc_to_hex(*pstr & 15);
    pstr++;
  }
  *pbuf = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<libc_urlencode>:
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   1050 <libc_strlen@plt>
mov    %eax,%edi
call   1060 <libc_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1288 <libc_urlencode+0x148>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <libc_isalnum@plt>
cmp    $0x0,%rax
jne    11d3 <libc_urlencode+0x93>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     11d3 <libc_urlencode+0x93>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
je     11d3 <libc_urlencode+0x93>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     11d3 <libc_urlencode+0x93>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    11ef <libc_urlencode+0xaf>
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    1277 <libc_urlencode+0x137>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    1216 <libc_urlencode+0xd6>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x2b,(%rax)
jmp    1272 <libc_urlencode+0x132>
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   1040 <libc_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   1040 <libc_to_hex@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    1277 <libc_urlencode+0x137>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1170 <libc_urlencode+0x30>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libc_urlencode>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1050 <libc_strlen@plt>
mov    %eax,%edi
call   1060 <libc_malloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
mov    %r14,%r15
test   %al,%al
je     11ec <libc_urlencode+0xac>
mov    %r14,%r15
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <libc_isalnum@plt>
movzbl (%rbx),%ecx
test   %rax,%rax
je     11a0 <libc_urlencode+0x60>
mov    %cl,(%r15)
add    $0x1,%r15
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1170 <libc_urlencode+0x30>
jmp    11ec <libc_urlencode+0xac>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x5e,%cl
jg     11b7 <libc_urlencode+0x77>
lea    -0x2d(%rcx),%eax
cmp    $0x2,%al
jb     1180 <libc_urlencode+0x40>
cmp    $0x20,%cl
jne    11c1 <libc_urlencode+0x81>
movb   $0x2b,(%r15)
jmp    1183 <libc_urlencode+0x43>
cmp    $0x7e,%cl
je     1180 <libc_urlencode+0x40>
cmp    $0x5f,%cl
je     1180 <libc_urlencode+0x40>
movb   $0x25,(%r15)
movzbl (%rbx),%eax
sar    $0x4,%al
movsbl %al,%edi
call   1040 <libc_to_hex@plt>
mov    %al,0x1(%r15)
movzbl (%rbx),%edi
and    $0xf,%edi
call   1040 <libc_to_hex@plt>
mov    %al,0x2(%r15)
add    $0x3,%r15
jmp    1187 <libc_urlencode+0x47>
movb   $0x0,(%r15)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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