#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__ is_urlreservedsafe(char const);
scalar_t__ is_urlsafe(char const);
scalar_t__ isalnum(char const);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
void urlencode_common(const char *from, char *to, unsigned preserve_reserved) {
  while (*from) {
    if (isalnum(*from) || is_urlsafe(*from) ||
        (preserve_reserved && is_urlreservedsafe(*from))) {
      *(to++) = *from;
    } else {
      sprintf(to, "%%%02x", (unsigned char)*from);
      to += 3;
    }
    from++;
  }
}


x86 assembly (clang, -O0):
<urlencode_common>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11fc <urlencode_common+0xbc>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
jne    11ac <urlencode_common+0x6c>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <is_urlsafe@plt>
cmp    $0x0,%rax
jne    11ac <urlencode_common+0x6c>
cmpl   $0x0,-0x14(%rbp)
je     11c8 <urlencode_common+0x88>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1050 <is_urlreservedsafe@plt>
cmp    $0x0,%rax
je     11c8 <urlencode_common+0x88>
mov    -0x8(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11eb <urlencode_common+0xab>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
lea    0xe29(%rip),%rsi
movzbl (%rax),%edx
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1153 <urlencode_common+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urlencode_common>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%al
test   %al,%al
je     11bb <urlencode_common+0x7b>
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xea3(%rip),%r15
nopl   (%rax)
movsbl %al,%edi
call   1040 <isalnum@plt>
test   %rax,%rax
jne    117b <urlencode_common+0x3b>
movsbl 0x0(%rbp),%edi
call   1030 <is_urlsafe@plt>
test   %rax,%rax
je     1193 <urlencode_common+0x53>
movzbl 0x0(%rbp),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl 0x1(%rbp),%eax
add    $0x1,%rbp
test   %al,%al
jne    1160 <urlencode_common+0x20>
jmp    11bb <urlencode_common+0x7b>
test   %r14d,%r14d
je     11a6 <urlencode_common+0x66>
movsbl 0x0(%rbp),%edi
call   1050 <is_urlreservedsafe@plt>
test   %rax,%rax
jne    117b <urlencode_common+0x3b>
movzbl 0x0(%rbp),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <sprintf@plt>
add    $0x3,%rbx
jmp    1185 <urlencode_common+0x45>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%%%02x\00"
define void @urlencode_common(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %7
7: ; preds = %41, %3
%8 = load i8*, i8** %4
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 0
br i1 %10, label %11, label %44
11: ; preds = %7
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = call i64 @isalnum(i8 signext %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %29, label %16
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = call i64 @is_urlsafe(i8 signext %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %16
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = load i8*, i8** %4
%26 = load i8, i8* %25
%27 = call i64 @is_urlreservedsafe(i8 signext %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %24, %16, %11
%30 = load i8*, i8** %4
%31 = load i8, i8* %30
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %5
store i8 %31, i8* %32
br label %41
34: ; preds = %24, %21
%35 = load i8*, i8** %5
%36 = load i8*, i8** %4
%37 = load i8, i8* %36
%38 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %37)
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i64 3
store i8* %40, i8** %5
br label %41
41: ; preds = %34, %29
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %4
br label %7
44: ; preds = %7
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%%%02x\00"
define void @urlencode_common(i8* nocapture %0, i8* nocapture writeonly %1, i32 %2) {
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %35, label %6
6: ; preds = %3
%7 = icmp eq i32 %2, 0
br label %8
8: ; preds = %6, %30
%9 = phi i8 [ %4, %6 ], [ %33, %30 ]
%10 = phi i8* [ %0, %6 ], [ %32, %30 ]
%11 = phi i8* [ %1, %6 ], [ %31, %30 ]
%12 = tail call i64 @isalnum(i8 signext %9)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %8
%15 = load i8, i8* %10
%16 = tail call i64 @is_urlsafe(i8 signext %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
br i1 %7, label %26, label %19
19: ; preds = %18
%20 = load i8, i8* %10
%21 = tail call i64 @is_urlreservedsafe(i8 signext %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %19, %14, %8
%24 = load i8, i8* %10
%25 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %24, i8* %11
br label %30
26: ; preds = %19, %18
%27 = load i8, i8* %10
%28 = tail call i32 @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %27)
%29 = getelementptr inbounds i8, i8* %11, i64 3
br label %30
30: ; preds = %26, %23
%31 = phi i8* [ %25, %23 ], [ %29, %26 ]
%32 = getelementptr inbounds i8, i8* %10, i64 1
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 0
br i1 %34, label %35, label %8
35: ; preds = %30, %3
ret void
}