#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 */
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
scalar_t__ strchr(char *, unsigned char);
int /*<<< orphan*/ strlcpy(char *, char *, int);
void url_encode(char *ps, int size) {
  unsigned char *pd, *p;
  unsigned char str[500];
  pd = (unsigned char *)str;
  p = (unsigned char *)ps;
  while (*p && pd < str + 250) {
    if (strchr("%&=#?+", *p) || *p > 127) {
      sprintf((char *)pd, "%%%02X", *p);
      pd += 3;
      p++;
    } else if (*p == ' ') {
      *pd++ = '+';
      p++;
    } else {
      *pd++ = *p++;
    }
  }
  *pd = '\0';
  strlcpy(ps, (char *)str, size);
}


x86 assembly (clang, -O0):
<url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movzbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x221(%rbp)
je     118b <url_encode+0x5b>
mov    -0x18(%rbp),%rax
lea    -0x220(%rbp),%rcx
add    $0xfa,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x221(%rbp)
mov    -0x221(%rbp),%al
test   $0x1,%al
jne    119e <url_encode+0x6e>
jmp    125e <url_encode+0x12e>
mov    -0x20(%rbp),%rax
lea    0xe57(%rip),%rdi
movzbl (%rax),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
jne    11cb <url_encode+0x9b>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x7f,%eax
jle    11ff <url_encode+0xcf>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
lea    0xe2d(%rip),%rsi
movzbl (%rax),%edx
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1259 <url_encode+0x129>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x20,%eax
jne    1232 <url_encode+0x102>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x2b,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1254 <url_encode+0x124>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1259 <url_encode+0x129>
jmp    1155 <url_encode+0x25>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <strlcpy@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%r15
mov    (%rdi),%al
test   %al,%al
mov    %esi,0xc(%rsp)
je     11e9 <url_encode+0xb9>
lea    0x10a(%rsp),%r13
lea    0x10(%rsp),%rbp
lea    0x1(%r15),%r14
lea    0xe96(%rip),%rbx
lea    0xe96(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl %al,%esi
mov    %rbx,%rdi
call   1030 <strchr@plt>
movzbl -0x1(%r14),%ecx
test   %rax,%rax
jne    11b0 <url_encode+0x80>
test   %cl,%cl
js     11b0 <url_encode+0x80>
lea    0x1(%rbp),%rax
cmp    $0x20,%cl
jne    11d0 <url_encode+0xa0>
movb   $0x2b,0x0(%rbp)
jmp    11d3 <url_encode+0xa3>
nopl   0x0(%rax,%rax,1)
movzbl %cl,%edx
mov    %rbp,%rdi
mov    %r12,%rsi
call   1050 <sprintf@plt>
add    $0x3,%rbp
jmp    11d6 <url_encode+0xa6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %cl,0x0(%rbp)
mov    %rax,%rbp
movzbl (%r14),%eax
test   %al,%al
je     11ee <url_encode+0xbe>
add    $0x1,%r14
cmp    %r13,%rbp
jb     1180 <url_encode+0x50>
jmp    11ee <url_encode+0xbe>
lea    0x10(%rsp),%rbp
movb   $0x0,0x0(%rbp)
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
mov    0xc(%rsp),%edx
call   1040 <strlcpy@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%&=#?+\00"
@.str.1 = [7 x i8] c"%%%02X\00"
define void @url_encode(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [500 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
store i8* %8, i8** %5
%9 = load i8*, i8** %3
store i8* %9, i8** %6
br label %10
10: ; preds = %58, %2
%11 = load i8*, i8** %6
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i8*, i8** %5
%17 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%18 = getelementptr inbounds i8, i8* %17, i64 250
%19 = icmp ult i8* %16, %18
br label %20
20: ; preds = %15, %10
%21 = phi i1 [ false, %10 ], [ %19, %15 ]
br i1 %21, label %22, label %59
22: ; preds = %20
%23 = load i8*, i8** %6
%24 = load i8, i8* %23
%25 = call i64 @strchr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %22
%28 = load i8*, i8** %6
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = icmp sgt i32 %30, 127
br i1 %31, label %32, label %41
32: ; preds = %27, %22
%33 = load i8*, i8** %5
%34 = load i8*, i8** %6
%35 = load i8, i8* %34
%36 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %35)
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i64 3
store i8* %38, i8** %5
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %6
br label %58
41: ; preds = %27
%42 = load i8*, i8** %6
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = icmp eq i32 %44, 32
br i1 %45, label %46, label %51
46: ; preds = %41
%47 = load i8*, i8** %5
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %5
store i8 43, i8* %47
%49 = load i8*, i8** %6
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %6
br label %57
51: ; preds = %41
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %6
%54 = load i8, i8* %52
%55 = load i8*, i8** %5
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %5
store i8 %54, i8* %55
br label %57
57: ; preds = %51, %46
br label %58
58: ; preds = %57, %32
br label %10
59: ; preds = %20
%60 = load i8*, i8** %5
store i8 0, i8* %60
%61 = load i8*, i8** %3
%62 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%63 = load i32, i32* %4
%64 = call i32 @strlcpy(i8* %61, i8* %62, i32 %63)
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%&=#?+\00"
@.str.1 = [7 x i8] c"%%%02X\00"
define void @url_encode(i8* %0, i32 %1) {
%3 = alloca [500 x i8]
%4 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 250
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %32, label %8
8: ; preds = %2, %25
%9 = phi i8 [ %28, %25 ], [ %6, %2 ]
%10 = phi i8* [ %27, %25 ], [ %0, %2 ]
%11 = phi i8* [ %26, %25 ], [ %4, %2 ]
%12 = call i64 @strchr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %9)
%13 = icmp ne i64 %12, 0
%14 = load i8, i8* %10
%15 = icmp slt i8 %14, 0
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = call i32 @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %14)
%19 = getelementptr inbounds i8, i8* %11, i64 3
br label %25
20: ; preds = %8
%21 = icmp eq i8 %14, 32
%22 = getelementptr inbounds i8, i8* %11, i64 1
br i1 %21, label %23, label %24
23: ; preds = %20
store i8 43, i8* %11
br label %25
24: ; preds = %20
store i8 %14, i8* %11
br label %25
25: ; preds = %23, %24, %17
%26 = phi i8* [ %19, %17 ], [ %22, %23 ], [ %22, %24 ]
%27 = getelementptr inbounds i8, i8* %10, i64 1
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
%30 = icmp ult i8* %26, %5
%31 = select i1 %29, i1 %30, i1 false
br i1 %31, label %8, label %32
32: ; preds = %25, %2
%33 = phi i8* [ %4, %2 ], [ %26, %25 ]
store i8 0, i8* %33
%34 = call i32 @strlcpy(i8* %0, i8* %4, i32 %1)
ret void
}