#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__ isalnum(unsigned char);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char *);
int /*<<< orphan*/ *strchr(char *, unsigned char);
void *to_hex(unsigned char);
int url_encode(char *str, int ext) {
  unsigned char *pstr = (unsigned char *)str;
  unsigned char buf[2048] = {0};
  unsigned char *pbuf = buf;
  printf("orignal str:%s\n", str);
  while (*pstr && (pbuf - buf + 4) < 2048) {
    if (isalnum(*pstr) || NULL != strchr("_-.~", *pstr) ||
        (ext && NULL != strchr("%?&=/:", *pstr))) {
      *pbuf++ = *pstr;
    } else if (*pstr == ' ') {
      *pbuf++ = '+';
    } else {
      *pbuf++ = '%';
      *pbuf++ = to_hex(*pstr >> 4);
      *pbuf++ = to_hex(*pstr & 15);
    }
    pstr++;
  }
  *pbuf = '\0';
  printf("encode: %s\n", buf);
  snprintf(str, 2048, "%s", buf);
  return 0;
}


x86 assembly (clang, -O0):
<url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1080 <memset@plt>
lea    -0x820(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
movzbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x829(%rbp)
je     11e9 <url_encode+0x89>
mov    -0x828(%rbp),%rax
lea    -0x820(%rbp),%rcx
sub    %rcx,%rax
add    $0x4,%rax
cmp    $0x800,%rax
setl   %al
mov    %al,-0x829(%rbp)
mov    -0x829(%rbp),%al
test   $0x1,%al
jne    11fc <url_encode+0x9c>
jmp    1331 <url_encode+0x1d1>
mov    -0x18(%rbp),%rax
movzbl (%rax),%edi
call   1060 <isalnum@plt>
cmp    $0x0,%rax
jne    125e <url_encode+0xfe>
mov    -0x18(%rbp),%rax
lea    0xdf3(%rip),%rdi
movzbl (%rax),%esi
call   1040 <strchr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    125e <url_encode+0xfe>
cmpl   $0x0,-0xc(%rbp)
je     1280 <url_encode+0x120>
mov    -0x18(%rbp),%rax
lea    0xdcd(%rip),%rdi
movzbl (%rax),%esi
call   1040 <strchr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1280 <url_encode+0x120>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x828(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x828(%rbp)
mov    %cl,(%rax)
jmp    1320 <url_encode+0x1c0>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x20,%eax
jne    12ad <url_encode+0x14d>
mov    -0x828(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x828(%rbp)
movb   $0x2b,(%rax)
jmp    131b <url_encode+0x1bb>
mov    -0x828(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x828(%rbp)
movb   $0x25,(%rax)
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
sar    $0x4,%eax
movzbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,%cl
mov    -0x828(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x828(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
and    $0xf,%eax
movzbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,%cl
mov    -0x828(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x828(%rbp)
mov    %cl,(%rax)
jmp    1320 <url_encode+0x1c0>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11ad <url_encode+0x4d>
mov    -0x828(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x820(%rbp),%rsi
lea    0xcd3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x820(%rbp),%rcx
mov    $0x800,%esi
lea    0xcc1(%rip),%rdx
call   1070 <snprintf@plt>
xor    %eax,%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %esi,%r14d
mov    %rdi,%r12
mov    %rsp,%rbp
mov    $0x800,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
lea    0xe70(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%al
test   %al,%al
je     1256 <url_encode+0xf6>
mov    %rsp,%rbx
lea    0xe60(%rip),%r13
mov    %rbx,%rbp
mov    %r12,%r15
cs nopw 0x0(%rax,%rax,1)
movzbl %al,%edi
call   1060 <isalnum@plt>
test   %rax,%rax
jne    11de <url_encode+0x7e>
movzbl (%r15),%esi
mov    %r13,%rdi
call   1040 <strchr@plt>
test   %rax,%rax
je     1207 <url_encode+0xa7>
movzbl (%r15),%eax
mov    %al,0x0(%rbp)
add    $0x1,%rbp
movzbl 0x1(%r15),%eax
test   %al,%al
je     1256 <url_encode+0xf6>
add    $0x1,%r15
mov    %rbp,%rcx
sub    %rbx,%rcx
cmp    $0x7fc,%rcx
jl     11c0 <url_encode+0x60>
jmp    1256 <url_encode+0xf6>
test   %r14d,%r14d
je     1221 <url_encode+0xc1>
movzbl (%r15),%esi
lea    0xdfe(%rip),%rdi
call   1040 <strchr@plt>
test   %rax,%rax
jne    11de <url_encode+0x7e>
movzbl (%r15),%eax
cmp    $0x20,%al
jne    122f <url_encode+0xcf>
movb   $0x2b,0x0(%rbp)
jmp    11e5 <url_encode+0x85>
movb   $0x25,0x0(%rbp)
shr    $0x4,%al
movzbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,0x1(%rbp)
movzbl (%r15),%edi
and    $0xf,%edi
call   1030 <to_hex@plt>
mov    %al,0x2(%rbp)
add    $0x3,%rbp
jmp    11e9 <url_encode+0x89>
movb   $0x0,0x0(%rbp)
lea    0xdbb(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdb3(%rip),%rdx
mov    %r12,%rdi
mov    $0x800,%esi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
xor    %eax,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"orignal str:%s\0A\00"
@.str.1 = [5 x i8] c"_-.~\00"
@.str.2 = [7 x i8] c"%?&=/:\00"
@.str.3 = [12 x i8] c"encode: %s\0A\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @url_encode(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [2048 x i8]
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
store i8* %8, i8** %5
%9 = bitcast [2048 x i8]* %6 to i8*
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
store i8* %10, i8** %7
%11 = load i8*, i8** %3
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %81, %2
%14 = load i8*, i8** %5
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = load i8*, i8** %7
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %20 to i64
%23 = sub i64 %21, %22
%24 = add nsw i64 %23, 4
%25 = icmp slt i64 %24, 2048
br label %26
26: ; preds = %18, %13
%27 = phi i1 [ false, %13 ], [ %25, %18 ]
br i1 %27, label %28, label %84
28: ; preds = %26
%29 = load i8*, i8** %5
%30 = load i8, i8* %29
%31 = call i64 @isalnum(i8 zeroext %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %46, label %33
33: ; preds = %28
%34 = load i8*, i8** %5
%35 = load i8, i8* %34
%36 = call i32* @strchr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %35)
%37 = icmp ne i32* null, %36
br i1 %37, label %46, label %38
38: ; preds = %33
%39 = load i32, i32* %4
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %38
%42 = load i8*, i8** %5
%43 = load i8, i8* %42
%44 = call i32* @strchr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %43)
%45 = icmp ne i32* null, %44
br i1 %45, label %46, label %51
46: ; preds = %41, %33, %28
%47 = load i8*, i8** %5
%48 = load i8, i8* %47
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %7
store i8 %48, i8* %49
br label %81
51: ; preds = %41, %38
%52 = load i8*, i8** %5
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = icmp eq i32 %54, 32
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = load i8*, i8** %7
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %7
store i8 43, i8* %57
br label %80
59: ; preds = %51
%60 = load i8*, i8** %7
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %7
store i8 37, i8* %60
%62 = load i8*, i8** %5
%63 = load i8, i8* %62
%64 = zext i8 %63 to i32
%65 = ashr i32 %64, 4
%66 = trunc i32 %65 to i8
%67 = call i8* @to_hex(i8 zeroext %66)
%68 = ptrtoint i8* %67 to i8
%69 = load i8*, i8** %7
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %7
store i8 %68, i8* %69
%71 = load i8*, i8** %5
%72 = load i8, i8* %71
%73 = zext i8 %72 to i32
%74 = and i32 %73, 15
%75 = trunc i32 %74 to i8
%76 = call i8* @to_hex(i8 zeroext %75)
%77 = ptrtoint i8* %76 to i8
%78 = load i8*, i8** %7
%79 = getelementptr inbounds i8, i8* %78, i32 1
store i8* %79, i8** %7
store i8 %77, i8* %78
br label %80
80: ; preds = %59, %56
br label %81
81: ; preds = %80, %46
%82 = load i8*, i8** %5
%83 = getelementptr inbounds i8, i8* %82, i32 1
store i8* %83, i8** %5
br label %13
84: ; preds = %26
%85 = load i8*, i8** %7
store i8 0, i8* %85
%86 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %86)
%88 = load i8*, i8** %3
%89 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%90 = call i32 @snprintf(i8* %88, i32 2048, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %89)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"orignal str:%s\0A\00"
@.str.1 = [5 x i8] c"_-.~\00"
@.str.2 = [7 x i8] c"%?&=/:\00"
@.str.3 = [12 x i8] c"encode: %s\0A\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @url_encode(i8* %0, i32 %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = ptrtoint [2048 x i8]* %3 to i64
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %55, label %9
9: ; preds = %2
%10 = icmp eq i32 %1, 0
br label %11
11: ; preds = %9, %46
%12 = phi i8 [ %7, %9 ], [ %49, %46 ]
%13 = phi i8* [ %4, %9 ], [ %47, %46 ]
%14 = phi i8* [ %0, %9 ], [ %48, %46 ]
%15 = call i64 @isalnum(i8 zeroext %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %11
%18 = load i8, i8* %14
%19 = call i32* @strchr(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %17
br i1 %10, label %29, label %22
22: ; preds = %21
%23 = load i8, i8* %14
%24 = call i32* @strchr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %29, label %26
26: ; preds = %22, %17, %11
%27 = load i8, i8* %14
%28 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %27, i8* %13
br label %46
29: ; preds = %22, %21
%30 = load i8, i8* %14
%31 = icmp eq i8 %30, 32
%32 = getelementptr inbounds i8, i8* %13, i64 1
br i1 %31, label %33, label %34
33: ; preds = %29
store i8 43, i8* %13
br label %46
34: ; preds = %29
store i8 37, i8* %13
%35 = lshr i8 %30, 4
%36 = call i8* @to_hex(i8 zeroext %35)
%37 = ptrtoint i8* %36 to i64
%38 = trunc i64 %37 to i8
%39 = getelementptr inbounds i8, i8* %13, i64 2
store i8 %38, i8* %32
%40 = load i8, i8* %14
%41 = and i8 %40, 15
%42 = call i8* @to_hex(i8 zeroext %41)
%43 = ptrtoint i8* %42 to i64
%44 = trunc i64 %43 to i8
%45 = getelementptr inbounds i8, i8* %13, i64 3
store i8 %44, i8* %39
br label %46
46: ; preds = %33, %34, %26
%47 = phi i8* [ %28, %26 ], [ %32, %33 ], [ %45, %34 ]
%48 = getelementptr inbounds i8, i8* %14, i64 1
%49 = load i8, i8* %48
%50 = icmp ne i8 %49, 0
%51 = ptrtoint i8* %47 to i64
%52 = sub i64 %51, %6
%53 = icmp slt i64 %52, 2044
%54 = select i1 %50, i1 %53, i1 false
br i1 %54, label %11, label %55
55: ; preds = %46, %2
%56 = phi i8* [ %4, %2 ], [ %47, %46 ]
store i8 0, i8* %56
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%58 = call i32 @snprintf(i8* %0, i32 2048, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %4)
ret i32 0
}