#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);
void M_URLEncode(const char *str, const int strSize, char *result,
                 const int resultSize) {
  int j = 0;
  char ch;
  for (int i = 0; (i < strSize) && (j + 3 < 360); i++) {
    ch = str[i];
    if (ch >= 36 && ch <= 125)
      result[j++] = ch;
    else {
      sprintf(result + j, "%%%02X", (unsigned char)ch);
      j += 3;
    }
  }
  result[j] = '\0';
}


x86 assembly (clang, -O0):
<M_URLEncode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x29(%rbp)
jge    1156 <M_URLEncode+0x46>
mov    -0x20(%rbp),%eax
add    $0x3,%eax
cmp    $0x168,%eax
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1166 <M_URLEncode+0x56>
jmp    11e2 <M_URLEncode+0xd2>
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
cmp    $0x24,%eax
jl     11ab <M_URLEncode+0x9b>
movsbl -0x21(%rbp),%eax
cmp    $0x7d,%eax
jg     11ab <M_URLEncode+0x9b>
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11cf <M_URLEncode+0xbf>
mov    -0x18(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
lea    0xe43(%rip),%rsi
movzbl -0x21(%rbp),%edx
call   1030 <sprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x20(%rbp)
jmp    11d4 <M_URLEncode+0xc4>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1134 <M_URLEncode+0x24>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<M_URLEncode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %esi,%esi
jle    1185 <M_URLEncode+0x75>
mov    %rdi,%r12
mov    %esi,%r13d
xor    %eax,%eax
mov    $0x3,%ebp
mov    $0x1,%ebx
lea    0xec5(%rip),%r15
nopl   0x0(%rax,%rax,1)
movzbl -0x1(%r12,%rbx,1),%ecx
lea    -0x24(%rcx),%edx
movslq %eax,%rdi
cmp    $0x59,%dl
ja     1160 <M_URLEncode+0x50>
lea    0x1(%rdi),%eax
mov    %cl,(%r14,%rdi,1)
cmp    %r13,%rbx
jb     1175 <M_URLEncode+0x65>
jmp    1187 <M_URLEncode+0x77>
nop
add    %r14,%rdi
movzbl %cl,%edx
mov    %r15,%rsi
call   1030 <sprintf@plt>
mov    %ebp,%eax
cmp    %r13,%rbx
jae    1187 <M_URLEncode+0x77>
lea    0x3(%rax),%ebp
add    $0x1,%rbx
cmp    $0x165,%eax
jl     1140 <M_URLEncode+0x30>
jmp    1187 <M_URLEncode+0x77>
xor    %eax,%eax
cltq
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%%%02X\00"
define void @M_URLEncode(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
store i32 0, i32* %11
br label %12
12: ; preds = %52, %4
%13 = load i32, i32* %11
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* %9
%18 = add nsw i32 %17, 3
%19 = icmp slt i32 %18, 360
br label %20
20: ; preds = %16, %12
%21 = phi i1 [ false, %12 ], [ %19, %16 ]
br i1 %21, label %22, label %55
22: ; preds = %20
%23 = load i8*, i8** %5
%24 = load i32, i32* %11
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
store i8 %27, i8* %10
%28 = load i8, i8* %10
%29 = sext i8 %28 to i32
%30 = icmp sge i32 %29, 36
br i1 %30, label %31, label %42
31: ; preds = %22
%32 = load i8, i8* %10
%33 = sext i8 %32 to i32
%34 = icmp sle i32 %33, 125
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = load i8, i8* %10
%37 = load i8*, i8** %7
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
store i32 %39, i32* %9
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 %36, i8* %41
br label %51
42: ; preds = %31, %22
%43 = load i8*, i8** %7
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i8, i8* %10
%48 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %47)
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 3
store i32 %50, i32* %9
br label %51
51: ; preds = %42, %35
br label %52
52: ; preds = %51
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
br label %12
55: ; preds = %20
%56 = load i8*, i8** %7
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%%%02X\00"
define void @M_URLEncode(i8* nocapture %0, i32 %1, i8* nocapture writeonly %2, i32 %3) {
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = zext i32 %1 to i64
br label %12
8: ; preds = %28, %4
%9 = phi i32 [ 0, %4 ], [ %29, %28 ]
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %2, i64 %10
store i8 0, i8* %11
ret void
12: ; preds = %6, %28
%13 = phi i64 [ 0, %6 ], [ %30, %28 ]
%14 = phi i32 [ 3, %6 ], [ %32, %28 ]
%15 = phi i32 [ 0, %6 ], [ %29, %28 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %13
%17 = load i8, i8* %16
%18 = add i8 %17, -36
%19 = icmp ult i8 %18, 90
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = add nsw i32 %15, 1
%22 = sext i32 %15 to i64
%23 = getelementptr inbounds i8, i8* %2, i64 %22
store i8 %17, i8* %23
br label %28
24: ; preds = %12
%25 = sext i32 %15 to i64
%26 = getelementptr inbounds i8, i8* %2, i64 %25
%27 = tail call i32 @sprintf(i8* dereferenceable(1) %26, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %17)
br label %28
28: ; preds = %20, %24
%29 = phi i32 [ %21, %20 ], [ %14, %24 ]
%30 = add nuw nsw i64 %13, 1
%31 = icmp ult i64 %30, %7
%32 = add nsw i32 %29, 3
%33 = icmp slt i32 %29, 357
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %12, label %8
}