#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
void urlencode(char *src, char **dst, unsigned int size, int type) {
  unsigned int i, j = 0;
  unsigned char c;
  char newchar[4];
  char newdst[2048];
  if (type > 2)
    return;
  for (i = 0; i < size && j < (2048 - 1); i++) {
    c = src[i];
    if ((c < 48) || (c > 57 && c < 65) || (c > 90 && c < 97) ||
        (c > 122 && c < 128)) {
      if (type == 2 && c == 32) {
        newdst[j++] = '+';
      } else {
        sprintf(newchar, "\%%%2x", c);
        newdst[j] = newchar[0];
        newdst[j + 1] = newchar[1];
        newdst[j + 2] = newchar[2];
        j = j + 3;
      }
    } else {
      newdst[j] = c;
      j++;
    }
  }
  if (j < (2048 - 1))
    newdst[j] = '\0';
  else
    newdst[j - 1] = '\0';
  *dst = realloc(*dst, strlen(newdst) + 1);
  if (*dst == NULL) {
    printf("\n Realloc failed \n");
    exit(0);
  }
  strcpy(*dst, newdst);
}


x86 assembly (clang, -O0):
<urlencode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x2,-0x18(%rbp)
jle    118f <urlencode+0x2f>
jmp    137c <urlencode+0x21c>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x831(%rbp)
jae    11ba <urlencode+0x5a>
cmpl   $0x7ff,-0x20(%rbp)
setb   %al
mov    %al,-0x831(%rbp)
mov    -0x831(%rbp),%al
test   $0x1,%al
jne    11cd <urlencode+0x6d>
jmp    12e4 <urlencode+0x184>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
movzbl -0x21(%rbp),%eax
cmp    $0x30,%eax
jl     1237 <urlencode+0xd7>
movzbl -0x21(%rbp),%eax
cmp    $0x39,%eax
jle    1201 <urlencode+0xa1>
movzbl -0x21(%rbp),%eax
cmp    $0x41,%eax
jl     1237 <urlencode+0xd7>
movzbl -0x21(%rbp),%eax
cmp    $0x5a,%eax
jle    121b <urlencode+0xbb>
movzbl -0x21(%rbp),%eax
cmp    $0x61,%eax
jl     1237 <urlencode+0xd7>
movzbl -0x21(%rbp),%eax
cmp    $0x7a,%eax
jle    12bb <urlencode+0x15b>
movzbl -0x21(%rbp),%eax
cmp    $0x80,%eax
jge    12bb <urlencode+0x15b>
cmpl   $0x2,-0x18(%rbp)
jne    1268 <urlencode+0x108>
movzbl -0x21(%rbp),%eax
cmp    $0x20,%eax
jne    1268 <urlencode+0x108>
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x20(%rbp)
mov    %eax,%eax
movb   $0x2b,-0x830(%rbp,%rax,1)
2b
jmp    12b6 <urlencode+0x156>
lea    -0x25(%rbp),%rdi
lea    0xd8d(%rip),%rsi
movzbl -0x21(%rbp),%edx
call   1070 <sprintf@plt>
mov    -0x25(%rbp),%cl
mov    -0x20(%rbp),%eax
mov    %cl,-0x830(%rbp,%rax,1)
mov    -0x24(%rbp),%cl
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
mov    %cl,-0x830(%rbp,%rax,1)
mov    -0x23(%rbp),%cl
mov    -0x20(%rbp),%eax
add    $0x2,%eax
mov    %eax,%eax
mov    %cl,-0x830(%rbp,%rax,1)
mov    -0x20(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x20(%rbp)
jmp    12d1 <urlencode+0x171>
mov    -0x21(%rbp),%cl
mov    -0x20(%rbp),%eax
mov    %cl,-0x830(%rbp,%rax,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12d6 <urlencode+0x176>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1196 <urlencode+0x36>
cmpl   $0x7ff,-0x20(%rbp)
jae    1301 <urlencode+0x1a1>
mov    -0x20(%rbp),%eax
movb   $0x0,-0x830(%rbp,%rax,1)
jmp    1311 <urlencode+0x1b1>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,%eax
movb   $0x0,-0x830(%rbp,%rax,1)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x840(%rbp)
lea    -0x830(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x840(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1369 <urlencode+0x209>
lea    0xca9(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x830(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urlencode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
cmp    $0x2,%ecx
jg     1288 <urlencode+0x128>
mov    %rsi,%r14
xor    %ebp,%ebp
test   %edx,%edx
je     1255 <urlencode+0xf5>
mov    %ecx,%r13d
mov    %rdi,%rbx
mov    %r14,0x8(%rsp)
mov    %edx,%r14d
mov    $0x1,%r15d
lea    0x4(%rsp),%r12
movzbl -0x1(%rbx,%r15,1),%eax
cmp    $0x30,%al
jae    11d0 <urlencode+0x70>
cmp    $0x2,%r13d
jne    11f8 <urlencode+0x98>
cmp    $0x20,%al
jne    11f8 <urlencode+0x98>
mov    %ebp,%eax
add    $0x1,%ebp
movb   $0x2b,0x10(%rsp,%rax,1)
cmp    %r14,%r15
jb     1227 <urlencode+0xc7>
jmp    1247 <urlencode+0xe7>
nopl   0x0(%rax,%rax,1)
lea    -0x5b(%rax),%ecx
cmp    $0x24,%cl
ja     11f0 <urlencode+0x90>
movzbl %cl,%ecx
movabs $0x1f0000003f,%rdx
bt     %rcx,%rdx
jb     11f8 <urlencode+0x98>
nopl   0x0(%rax,%rax,1)
lea    -0x3a(%rax),%ecx
cmp    $0x7,%cl
jae    1239 <urlencode+0xd9>
movzbl %al,%edx
mov    %r12,%rdi
lea    0xdfb(%rip),%rsi
call   1070 <sprintf@plt>
movzbl 0x4(%rsp),%eax
mov    %ebp,%ecx
mov    %al,0x10(%rsp,%rcx,1)
movzwl 0x5(%rsp),%eax
mov    %ax,0x11(%rsp,%rcx,1)
add    $0x3,%ebp
cmp    %r14,%r15
jae    1247 <urlencode+0xe7>
add    $0x1,%r15
cmp    $0x7ff,%ebp
jb     11a0 <urlencode+0x40>
jmp    1247 <urlencode+0xe7>
mov    %ebp,%ecx
mov    %al,0x10(%rsp,%rcx,1)
add    $0x1,%ebp
cmp    %r14,%r15
jb     1227 <urlencode+0xc7>
cmp    $0x7ff,%ebp
adc    $0xffffffff,%ebp
mov    0x8(%rsp),%r14
movb   $0x0,0x10(%rsp,%rbp,1)
mov    (%r14),%rbx
lea    0x10(%rsp),%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %rbx,%rdi
call   1060 <realloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     129a <urlencode+0x13a>
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd65(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%%%2x\00"
@.str.1 = [19 x i8] c"\0A Realloc failed \0A\00"
define void @urlencode(i8* %0, i8** %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca [4 x i8]
%13 = alloca [2048 x i8]
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %10
%14 = load i32, i32* %8
%15 = icmp sgt i32 %14, 2
br i1 %15, label %16, label %17
16: ; preds = %4
br label %138
17: ; preds = %4
store i32 0, i32* %9
br label %18
18: ; preds = %104, %17
%19 = load i32, i32* %9
%20 = load i32, i32* %7
%21 = icmp ult i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %10
%24 = icmp ult i32 %23, 2047
br label %25
25: ; preds = %22, %18
%26 = phi i1 [ false, %18 ], [ %24, %22 ]
br i1 %26, label %27, label %107
27: ; preds = %25
%28 = load i8*, i8** %5
%29 = load i32, i32* %9
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
store i8 %32, i8* %11
%33 = load i8, i8* %11
%34 = zext i8 %33 to i32
%35 = icmp slt i32 %34, 48
br i1 %35, label %60, label %36
36: ; preds = %27
%37 = load i8, i8* %11
%38 = zext i8 %37 to i32
%39 = icmp sgt i32 %38, 57
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i8, i8* %11
%42 = zext i8 %41 to i32
%43 = icmp slt i32 %42, 65
br i1 %43, label %60, label %44
44: ; preds = %40, %36
%45 = load i8, i8* %11
%46 = zext i8 %45 to i32
%47 = icmp sgt i32 %46, 90
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load i8, i8* %11
%50 = zext i8 %49 to i32
%51 = icmp slt i32 %50, 97
br i1 %51, label %60, label %52
52: ; preds = %48, %44
%53 = load i8, i8* %11
%54 = zext i8 %53 to i32
%55 = icmp sgt i32 %54, 122
br i1 %55, label %56, label %96
56: ; preds = %52
%57 = load i8, i8* %11
%58 = zext i8 %57 to i32
%59 = icmp slt i32 %58, 128
br i1 %59, label %60, label %96
60: ; preds = %56, %48, %40, %27
%61 = load i32, i32* %8
%62 = icmp eq i32 %61, 2
br i1 %62, label %63, label %72
63: ; preds = %60
%64 = load i8, i8* %11
%65 = zext i8 %64 to i32
%66 = icmp eq i32 %65, 32
br i1 %66, label %67, label %72
67: ; preds = %63
%68 = load i32, i32* %10
%69 = add i32 %68, 1
store i32 %69, i32* %10
%70 = zext i32 %68 to i64
%71 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 %70
store i8 43, i8* %71
br label %95
72: ; preds = %63, %60
%73 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%74 = load i8, i8* %11
%75 = call i32 @sprintf(i8* %73, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %74)
%76 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%77 = load i8, i8* %76
%78 = load i32, i32* %10
%79 = zext i32 %78 to i64
%80 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 %79
store i8 %77, i8* %80
%81 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 1
%82 = load i8, i8* %81
%83 = load i32, i32* %10
%84 = add i32 %83, 1
%85 = zext i32 %84 to i64
%86 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 %85
store i8 %82, i8* %86
%87 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 2
%88 = load i8, i8* %87
%89 = load i32, i32* %10
%90 = add i32 %89, 2
%91 = zext i32 %90 to i64
%92 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 %91
store i8 %88, i8* %92
%93 = load i32, i32* %10
%94 = add i32 %93, 3
store i32 %94, i32* %10
br label %95
95: ; preds = %72, %67
br label %103
96: ; preds = %56, %52
%97 = load i8, i8* %11
%98 = load i32, i32* %10
%99 = zext i32 %98 to i64
%100 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 %99
store i8 %97, i8* %100
%101 = load i32, i32* %10
%102 = add i32 %101, 1
store i32 %102, i32* %10
br label %103
103: ; preds = %96, %95
br label %104
104: ; preds = %103
%105 = load i32, i32* %9
%106 = add i32 %105, 1
store i32 %106, i32* %9
br label %18
107: ; preds = %25
%108 = load i32, i32* %10
%109 = icmp ult i32 %108, 2047
br i1 %109, label %110, label %114
110: ; preds = %107
%111 = load i32, i32* %10
%112 = zext i32 %111 to i64
%113 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 %112
store i8 0, i8* %113
br label %119
114: ; preds = %107
%115 = load i32, i32* %10
%116 = sub i32 %115, 1
%117 = zext i32 %116 to i64
%118 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 %117
store i8 0, i8* %118
br label %119
119: ; preds = %114, %110
%120 = load i8**, i8*** %6
%121 = load i8*, i8** %120
%122 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 0
%123 = call i64 @strlen(i8* %122)
%124 = add nsw i64 %123, 1
%125 = call i8* @realloc(i8* %121, i64 %124)
%126 = load i8**, i8*** %6
store i8* %125, i8** %126
%127 = load i8**, i8*** %6
%128 = load i8*, i8** %127
%129 = icmp eq i8* %128, null
br i1 %129, label %130, label %133
130: ; preds = %119
%131 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%132 = call i32 @exit(i32 0)
unreachable
133: ; preds = %119
%134 = load i8**, i8*** %6
%135 = load i8*, i8** %134
%136 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 0
%137 = call i32 @strcpy(i8* %135, i8* %136)
br label %138
138: ; preds = %133, %16
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%%%2x\00"
@str = [18 x i8] c"\0A Realloc failed \00"
define void @urlencode(i8* nocapture %0, i8** nocapture %1, i32 %2, i32 %3) {
%5 = alloca [4 x i8]
%6 = alloca [2048 x i8]
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%9 = icmp sgt i32 %3, 2
br i1 %9, label %74, label %10
10: ; preds = %4
%11 = icmp eq i32 %2, 0
br i1 %11, label %61, label %12
12: ; preds = %10
%13 = icmp eq i32 %3, 2
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
%16 = zext i32 %2 to i64
br label %17
17: ; preds = %12, %50
%18 = phi i64 [ 0, %12 ], [ %52, %50 ]
%19 = phi i32 [ 0, %12 ], [ %51, %50 ]
%20 = getelementptr inbounds i8, i8* %0, i64 %18
%21 = load i8, i8* %20
%22 = freeze i8 %21
%23 = icmp ult i8 %22, 48
br i1 %23, label %25, label %24
24: ; preds = %17
switch i8 %22, label %46 [
i8 127, label %32
i8 126, label %32
i8 125, label %32
i8 124, label %32
i8 123, label %32
i8 96, label %32
i8 95, label %32
i8 94, label %32
i8 93, label %32
i8 92, label %32
i8 91, label %32
i8 64, label %32
i8 63, label %32
i8 62, label %32
i8 61, label %32
i8 60, label %32
i8 59, label %32
i8 58, label %32
]
25: ; preds = %17
%26 = icmp eq i8 %22, 32
%27 = and i1 %13, %26
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = add nuw nsw i32 %19, 1
%30 = zext i32 %19 to i64
%31 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %30
store i8 43, i8* %31
br label %50
32: ; preds = %24, %24, %24, %24, %24, %24, %24, %24, %24, %24, %24, %24, %24, %24, %24, %24, %24, %24, %25
%33 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %22)
%34 = load i8, i8* %7
%35 = zext i32 %19 to i64
%36 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %35
store i8 %34, i8* %36
%37 = load i8, i8* %14
%38 = add nuw nsw i32 %19, 1
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %39
store i8 %37, i8* %40
%41 = load i8, i8* %15
%42 = add nuw nsw i32 %19, 2
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %43
store i8 %41, i8* %44
%45 = add nuw nsw i32 %19, 3
br label %50
46: ; preds = %24
%47 = zext i32 %19 to i64
%48 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %47
store i8 %22, i8* %48
%49 = add nuw nsw i32 %19, 1
br label %50
50: ; preds = %46, %32, %28
%51 = phi i32 [ %29, %28 ], [ %45, %32 ], [ %49, %46 ]
%52 = add nuw nsw i64 %18, 1
%53 = icmp ult i64 %52, %16
%54 = icmp ult i32 %51, 2047
%55 = select i1 %53, i1 %54, i1 false
br i1 %55, label %17, label %56
56: ; preds = %50
%57 = xor i1 %54, true
%58 = sext i1 %57 to i32
%59 = add nsw i32 %51, %58
%60 = zext i32 %59 to i64
br label %61
61: ; preds = %56, %10
%62 = phi i64 [ 0, %10 ], [ %60, %56 ]
%63 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %62
store i8 0, i8* %63
%64 = load i8*, i8** %1
%65 = call i64 @strlen(i8* %8)
%66 = add nsw i64 %65, 1
%67 = tail call i8* @realloc(i8* %64, i64 %66)
store i8* %67, i8** %1
%68 = icmp eq i8* %67, null
br i1 %68, label %69, label %72
69: ; preds = %61
%70 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%71 = tail call i32 @exit(i32 0)
unreachable
72: ; preds = %61
%73 = call i32 @strcpy(i8* %67, i8* %8)
br label %74
74: ; preds = %4, %72
ret void
}