#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(char const);
scalar_t__ strchr(char const *, char const);
char *urlencode(const char *in, char *out, const char *keep) {
  const char hexdigit[] = "0123456789ABCDEF";
  for (; *in != 0; in++) {
    if (isalnum(*in) || (keep != 0 && strchr(keep, *in) != 0)) {
      *out++ = *in;
    } else if (*in == ' ') {
      *out++ = '+';
    } else {
      *out++ = '%';
      *out++ = hexdigit[(*in >> 4) & 0xf];
      *out++ = hexdigit[*in & 0xf];
    }
  }
  *out = 0;
  return out;
}


x86 assembly (clang, -O0):
<urlencode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0xec5(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xec2(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xec0(%rip),%al
mov    %al,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1253 <urlencode+0x133>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
jne    119e <urlencode+0x7e>
cmpq   $0x0,-0x18(%rbp)
je     11ba <urlencode+0x9a>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movsbl (%rax),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11ba <urlencode+0x9a>
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    123d <urlencode+0x11d>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    11e1 <urlencode+0xc1>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x2b,(%rax)
jmp    1238 <urlencode+0x118>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x25,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sar    $0x4,%eax
and    $0xf,%eax
cltq
mov    -0x30(%rbp,%rax,1),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
and    $0xf,%eax
cltq
mov    -0x30(%rbp,%rax,1),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    123d <urlencode+0x11d>
jmp    1242 <urlencode+0x122>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1153 <urlencode+0x33>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urlencode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    (%rdi),%al
test   %al,%al
je     11ac <urlencode+0x8c>
mov    %rdx,%r14
mov    %rdi,%rbx
lea    0xec2(%rip),%r12
jmp    1156 <urlencode+0x36>
movzbl (%rbx),%eax
mov    %al,(%r15)
add    $0x1,%r15
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11ac <urlencode+0x8c>
movsbl %al,%edi
call   1040 <isalnum@plt>
test   %rax,%rax
jne    1140 <urlencode+0x20>
test   %r14,%r14
je     1178 <urlencode+0x58>
movsbl (%rbx),%esi
mov    %r14,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1140 <urlencode+0x20>
cmpb   $0x20,(%rbx)
jne    1183 <urlencode+0x63>
movb   $0x2b,(%r15)
jmp    1146 <urlencode+0x26>
movb   $0x25,(%r15)
movzbl (%rbx),%eax
shr    $0x4,%rax
movzbl (%rax,%r12,1),%eax
mov    %al,0x1(%r15)
movzbl (%rbx),%eax
and    $0xf,%eax
movzbl (%rax,%r12,1),%eax
mov    %al,0x2(%r15)
add    $0x3,%r15
jmp    114a <urlencode+0x2a>
movb   $0x0,(%r15)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.urlencode.hexdigit = [17 x i8] c"0123456789ABCDEF\00"
define i8* @urlencode(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [17 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = bitcast [17 x i8]* %7 to i8*
br label %9
9: ; preds = %65, %3
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %68
14: ; preds = %9
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = call i64 @isalnum(i8 signext %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %14
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %33
22: ; preds = %19
%23 = load i8*, i8** %6
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = call i64 @strchr(i8* %23, i8 signext %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22, %14
%29 = load i8*, i8** %4
%30 = load i8, i8* %29
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %5
store i8 %30, i8* %31
br label %64
33: ; preds = %22, %19
%34 = load i8*, i8** %4
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 32
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %5
store i8 43, i8* %39
br label %63
41: ; preds = %33
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %5
store i8 37, i8* %42
%44 = load i8*, i8** %4
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = ashr i32 %46, 4
%48 = and i32 %47, 15
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = load i8*, i8** %5
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %5
store i8 %51, i8* %52
%54 = load i8*, i8** %4
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = and i32 %56, 15
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 %58
%60 = load i8, i8* %59
%61 = load i8*, i8** %5
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %5
store i8 %60, i8* %61
br label %63
63: ; preds = %41, %38
br label %64
64: ; preds = %63, %28
br label %65
65: ; preds = %64
%66 = load i8*, i8** %4
%67 = getelementptr inbounds i8, i8* %66, i32 1
store i8* %67, i8** %4
br label %9
68: ; preds = %9
%69 = load i8*, i8** %5
store i8 0, i8* %69
%70 = load i8*, i8** %5
ret i8* %70
}

LLVM IR (-O2):
@__const.urlencode.hexdigit = [17 x i8] c"0123456789ABCDEF\00"
define i8* @urlencode(i8* nocapture %0, i8* writeonly %1, i8* %2) {
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %45, label %6
6: ; preds = %3
%7 = icmp eq i8* %2, null
br label %8
8: ; preds = %6, %40
%9 = phi i8 [ %4, %6 ], [ %43, %40 ]
%10 = phi i8* [ %0, %6 ], [ %42, %40 ]
%11 = phi i8* [ %1, %6 ], [ %41, %40 ]
%12 = tail call i64 @isalnum(i8 signext %9)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %8
br i1 %7, label %22, label %15
15: ; preds = %14
%16 = load i8, i8* %10
%17 = tail call i64 @strchr(i8* %2, i8 signext %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15, %8
%20 = load i8, i8* %10
%21 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %20, i8* %11
br label %40
22: ; preds = %15, %14
%23 = load i8, i8* %10
%24 = icmp eq i8 %23, 32
%25 = getelementptr inbounds i8, i8* %11, i64 1
br i1 %24, label %26, label %27
26: ; preds = %22
store i8 43, i8* %11
br label %40
27: ; preds = %22
store i8 37, i8* %11
%28 = load i8, i8* %10
%29 = lshr i8 %28, 4
%30 = zext i8 %29 to i64
%31 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.urlencode.hexdigit, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = getelementptr inbounds i8, i8* %11, i64 2
store i8 %32, i8* %25
%34 = load i8, i8* %10
%35 = and i8 %34, 15
%36 = zext i8 %35 to i64
%37 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.urlencode.hexdigit, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = getelementptr inbounds i8, i8* %11, i64 3
store i8 %38, i8* %33
br label %40
40: ; preds = %19, %27, %26
%41 = phi i8* [ %21, %19 ], [ %25, %26 ], [ %39, %27 ]
%42 = getelementptr inbounds i8, i8* %10, i64 1
%43 = load i8, i8* %42
%44 = icmp eq i8 %43, 0
br i1 %44, label %45, label %8
45: ; preds = %40, %3
%46 = phi i8* [ %1, %3 ], [ %41, %40 ]
store i8 0, i8* %46
ret i8* %46
}