#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, char);
char *urlencode(char *str) {
  static char buf[256];
  char *pstr = str, *pbuf = buf, c;
  while (*pstr && pbuf - buf < sizeof(buf) - 3) {
    c = *pstr;
    if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
        ('0' <= c && c <= '9') || c == '-' || c == '_' || c == '.' ||
        c == '\r' || c == '\n') {
      *pbuf = c;
      pstr++;
      pbuf++;
    } else {
      snprintf(pbuf, 4, "%%%02X", c & 0xff);
      pstr++;
      pbuf += 3;
    }
  }
  *pbuf = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<urlencode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    0x2f15(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1a(%rbp)
je     115e <urlencode+0x4e>
mov    -0x18(%rbp),%rax
lea    0x2ef1(%rip),%rcx
sub    %rcx,%rax
cmp    $0xfd,%rax
setb   %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
test   $0x1,%al
jne    116e <urlencode+0x5e>
jmp    1276 <urlencode+0x166>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%ecx
mov    $0x61,%eax
cmp    %ecx,%eax
jg     1195 <urlencode+0x85>
movsbl -0x19(%rbp),%eax
cmp    $0x7a,%eax
jle    1212 <urlencode+0x102>
movsbl -0x19(%rbp),%ecx
mov    $0x41,%eax
cmp    %ecx,%eax
jg     11b3 <urlencode+0xa3>
movsbl -0x19(%rbp),%eax
cmp    $0x5a,%eax
jle    1212 <urlencode+0x102>
movsbl -0x19(%rbp),%ecx
mov    $0x30,%eax
cmp    %ecx,%eax
jg     11d1 <urlencode+0xc1>
movsbl -0x19(%rbp),%eax
cmp    $0x39,%eax
jle    1212 <urlencode+0x102>
movsbl -0x19(%rbp),%eax
cmp    $0x2d,%eax
je     1212 <urlencode+0x102>
movsbl -0x19(%rbp),%eax
cmp    $0x5f,%eax
je     1212 <urlencode+0x102>
movsbl -0x19(%rbp),%eax
cmp    $0x2e,%eax
je     1212 <urlencode+0x102>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
je     1212 <urlencode+0x102>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    1238 <urlencode+0x128>
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1271 <urlencode+0x161>
mov    -0x18(%rbp),%rdi
movsbl -0x19(%rbp),%eax
and    $0xff,%eax
mov    $0x4,%esi
lea    0xdaf(%rip),%rdx
movsbl %al,%ecx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x18(%rbp)
jmp    112f <urlencode+0x1f>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2dbc(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urlencode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    (%rdi),%al
test   %al,%al
je     11c5 <urlencode+0xb5>
mov    %rdi,%r14
add    $0x1,%r14
lea    0x2f0f(%rip),%r12
lea    0xec8(%rip),%r15
movabs $0x600000002400,%r13
mov    %r12,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%ecx
and    $0xdf,%cl
add    $0xbf,%cl
cmp    $0x1a,%cl
jb     1180 <urlencode+0x70>
lea    -0x30(%rax),%ecx
cmp    $0xa,%cl
jb     1180 <urlencode+0x70>
cmp    $0x2e,%al
ja     11a7 <urlencode+0x97>
movzbl %al,%ecx
bt     %rcx,%r13
jae    11a7 <urlencode+0x97>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %al,(%rbx)
mov    $0x1,%eax
add    %rax,%rbx
movzbl (%r14),%eax
test   %al,%al
je     11cc <urlencode+0xbc>
mov    %rbx,%rcx
sub    %r12,%rcx
add    $0x1,%r14
cmp    $0xfd,%rcx
jb     1150 <urlencode+0x40>
jmp    11cc <urlencode+0xbc>
cmp    $0x5f,%al
je     1180 <urlencode+0x70>
movsbl %al,%ecx
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
call   1030 <snprintf@plt>
mov    $0x3,%eax
jmp    1187 <urlencode+0x77>
lea    0x2e74(%rip),%rbx
movb   $0x0,(%rbx)
lea    0x2e6a(%rip),%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@urlencode.buf = internal global [256 x i8] zeroinitializer
@.str = [7 x i8] c"%%%02X\00"
define i8* @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
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @urlencode.buf, i64 0, i64 0), i8** %4
br label %7
7: ; preds = %83, %1
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = load i8*, i8** %4
%14 = ptrtoint i8* %13 to i64
%15 = sub i64 %14, ptrtoint ([256 x i8]* @urlencode.buf to i64)
%16 = icmp ult i64 %15, 253
br label %17
17: ; preds = %12, %7
%18 = phi i1 [ false, %7 ], [ %16, %12 ]
br i1 %18, label %19, label %84
19: ; preds = %17
%20 = load i8*, i8** %3
%21 = load i8, i8* %20
store i8 %21, i8* %5
%22 = load i8, i8* %5
%23 = sext i8 %22 to i32
%24 = icmp sle i32 97, %23
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i8, i8* %5
%27 = sext i8 %26 to i32
%28 = icmp sle i32 %27, 122
br i1 %28, label %65, label %29
29: ; preds = %25, %19
%30 = load i8, i8* %5
%31 = sext i8 %30 to i32
%32 = icmp sle i32 65, %31
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i8, i8* %5
%35 = sext i8 %34 to i32
%36 = icmp sle i32 %35, 90
br i1 %36, label %65, label %37
37: ; preds = %33, %29
%38 = load i8, i8* %5
%39 = sext i8 %38 to i32
%40 = icmp sle i32 48, %39
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i8, i8* %5
%43 = sext i8 %42 to i32
%44 = icmp sle i32 %43, 57
br i1 %44, label %65, label %45
45: ; preds = %41, %37
%46 = load i8, i8* %5
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 45
br i1 %48, label %65, label %49
49: ; preds = %45
%50 = load i8, i8* %5
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 95
br i1 %52, label %65, label %53
53: ; preds = %49
%54 = load i8, i8* %5
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 46
br i1 %56, label %65, label %57
57: ; preds = %53
%58 = load i8, i8* %5
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 13
br i1 %60, label %65, label %61
61: ; preds = %57
%62 = load i8, i8* %5
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 10
br i1 %64, label %65, label %72
65: ; preds = %61, %57, %53, %49, %45, %41, %33, %25
%66 = load i8, i8* %5
%67 = load i8*, i8** %4
store i8 %66, i8* %67
%68 = load i8*, i8** %3
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** %3
%70 = load i8*, i8** %4
%71 = getelementptr inbounds i8, i8* %70, i32 1
store i8* %71, i8** %4
br label %83
72: ; preds = %61
%73 = load i8*, i8** %4
%74 = load i8, i8* %5
%75 = sext i8 %74 to i32
%76 = and i32 %75, 255
%77 = trunc i32 %76 to i8
%78 = call i32 @snprintf(i8* %73, i32 4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 signext %77)
%79 = load i8*, i8** %3
%80 = getelementptr inbounds i8, i8* %79, i32 1
store i8* %80, i8** %3
%81 = load i8*, i8** %4
%82 = getelementptr inbounds i8, i8* %81, i64 3
store i8* %82, i8** %4
br label %83
83: ; preds = %72, %65
br label %7
84: ; preds = %17
%85 = load i8*, i8** %4
store i8 0, i8* %85
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @urlencode.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@urlencode.buf = internal global [256 x i8] zeroinitializer
@.str = [7 x i8] c"%%%02X\00"
define i8* @urlencode(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %30, label %4
4: ; preds = %1, %20
%5 = phi i8 [ %24, %20 ], [ %2, %1 ]
%6 = phi i8* [ %23, %20 ], [ %0, %1 ]
%7 = phi i8* [ %22, %20 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @urlencode.buf, i64 0, i64 0), %1 ]
%8 = and i8 %5, -33
%9 = add i8 %8, -65
%10 = icmp ult i8 %9, 26
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = freeze i8 %5
%13 = add i8 %12, -48
%14 = icmp ult i8 %13, 10
br i1 %14, label %16, label %15
15: ; preds = %11
switch i8 %12, label %18 [
i8 95, label %16
i8 46, label %16
i8 45, label %16
i8 13, label %16
i8 10, label %16
]
16: ; preds = %15, %15, %15, %15, %15, %11, %4
%17 = phi i8 [ %12, %15 ], [ %12, %15 ], [ %12, %15 ], [ %12, %15 ], [ %12, %15 ], [ %12, %11 ], [ %5, %4 ]
store i8 %17, i8* %7
br label %20
18: ; preds = %15
%19 = tail call i32 @snprintf(i8* %7, i32 4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 signext %12)
br label %20
20: ; preds = %18, %16
%21 = phi i64 [ 3, %18 ], [ 1, %16 ]
%22 = getelementptr inbounds i8, i8* %7, i64 %21
%23 = getelementptr inbounds i8, i8* %6, i64 1
%24 = load i8, i8* %23
%25 = icmp ne i8 %24, 0
%26 = ptrtoint i8* %22 to i64
%27 = sub i64 %26, ptrtoint ([256 x i8]* @urlencode.buf to i64)
%28 = icmp ult i64 %27, 253
%29 = select i1 %25, i1 %28, i1 false
br i1 %29, label %4, label %30
30: ; preds = %20, %1
%31 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @urlencode.buf, i64 0, i64 0), %1 ], [ %22, %20 ]
store i8 0, i8* %31
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @urlencode.buf, i64 0, i64 0)
}