#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 */
typedef int /*<<< orphan*/ u_int;
typedef char u_char;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ isprint(int /*<<< orphan*/);
void url_encode(FILE *fd, char *s) {
  for (; *s; s++) {
    switch (*s) {
    case ' ':
    case '"':
    case '#':
    case '%':
    case '<':
    case '>':
    case '[':
    case ']':
    case '^':
    case '\\':
    case '?':
    case '+':
      fprintf(fd, "%%%02X", *s);
      break;
    case '&':
      fprintf(fd, "&amp;");
      break;
    default:
      fprintf(fd, isprint((u_int)*s) ? "%c" : "%%%02X", (u_char)*s);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     125d <url_encode+0x13d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x14(%rbp)
sub    $0x20,%eax
je     11d5 <url_encode+0xb5>
jmp    1155 <url_encode+0x35>
mov    -0x14(%rbp),%eax
add    $0xffffffde,%eax
sub    $0x2,%eax
jb     11d5 <url_encode+0xb5>
jmp    1169 <url_encode+0x49>
mov    -0x14(%rbp),%eax
sub    $0x25,%eax
je     11d5 <url_encode+0xb5>
jmp    117a <url_encode+0x5a>
mov    -0x14(%rbp),%eax
sub    $0x26,%eax
je     11f3 <url_encode+0xd3>
jmp    118b <url_encode+0x6b>
mov    -0x14(%rbp),%eax
sub    $0x2b,%eax
je     11d5 <url_encode+0xb5>
jmp    119c <url_encode+0x7c>
mov    -0x14(%rbp),%eax
sub    $0x3c,%eax
je     11d5 <url_encode+0xb5>
jmp    11ad <url_encode+0x8d>
mov    -0x14(%rbp),%eax
add    $0xffffffc2,%eax
sub    $0x2,%eax
jb     11d5 <url_encode+0xb5>
jmp    11c1 <url_encode+0xa1>
mov    -0x14(%rbp),%eax
add    $0xffffffa5,%eax
sub    $0x3,%eax
ja     120a <url_encode+0xea>
jmp    11d5 <url_encode+0xb5>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movsbl (%rax),%edx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1247 <url_encode+0x127>
mov    -0x8(%rbp),%rdi
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1247 <url_encode+0x127>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isprint@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%ecx
lea    0xdd5(%rip),%rsi
lea    0xddb(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
mov    -0x10(%rbp),%rax
movsbl (%rax),%edx
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    124c <url_encode+0x12c>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1130 <url_encode+0x10>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    (%rsi),%al
test   %al,%al
je     11d7 <url_encode+0xa7>
mov    %rsi,%rbx
mov    %rdi,%r14
add    $0x1,%rbx
lea    0xeaa(%rip),%r15
movabs $0x78000000d000082d,%rbp
lea    0xea0(%rip),%r12
nopw   0x0(%rax,%rax,1)
movsbl %al,%r13d
lea    -0x20(%r13),%eax
cmp    $0x3e,%eax
ja     11bd <url_encode+0x8d>
bt     %rax,%rbp
jae    11a0 <url_encode+0x70>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1170 <url_encode+0x40>
jmp    11d7 <url_encode+0xa7>
cmp    $0x6,%rax
jne    11bd <url_encode+0x8d>
mov    $0x5,%esi
mov    $0x1,%edx
mov    %r12,%rdi
mov    %r14,%rcx
call   1050 <fwrite@plt>
jmp    1193 <url_encode+0x63>
mov    %r13d,%edi
call   1040 <isprint@plt>
test   %eax,%eax
lea    0xe3f(%rip),%rsi
cmove  %r15,%rsi
mov    %r14,%rdi
jmp    1189 <url_encode+0x59>
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"
@.str.1 = [6 x i8] c"&amp;\00"
@.str.2 = [3 x i8] c"%c\00"
define void @url_encode(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
store i32* %0, i32** %3
store i8* %1, i8** %4
br label %5
5: ; preds = %36, %2
%6 = load i8*, i8** %4
%7 = load i8, i8* %6
%8 = icmp ne i8 %7, 0
br i1 %8, label %9, label %39
9: ; preds = %5
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
switch i32 %12, label %22 [
i32 32, label %13
i32 34, label %13
i32 35, label %13
i32 37, label %13
i32 60, label %13
i32 62, label %13
i32 91, label %13
i32 93, label %13
i32 94, label %13
i32 92, label %13
i32 63, label %13
i32 43, label %13
i32 38, label %19
]
13: ; preds = %9, %9, %9, %9, %9, %9, %9, %9, %9, %9, %9, %9
%14 = load i32*, i32** %3
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %35
19: ; preds = %9
%20 = load i32*, i32** %3
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %35
22: ; preds = %9
%23 = load i32*, i32** %3
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = call i32 @isprint(i32 %26)
%28 = icmp ne i32 %27, 0
%29 = zext i1 %28 to i64
%30 = select i1 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0)
%31 = load i8*, i8** %4
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* %30, i32 %33)
br label %35
35: ; preds = %22, %19, %13
br label %36
36: ; preds = %35
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %4
br label %5
39: ; preds = %5
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%%%02X\00"
@.str.1 = [6 x i8] c"&amp;\00"
@.str.2 = [3 x i8] c"%c\00"
define void @url_encode(i32* nocapture %0, i8* nocapture %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 0
br i1 %4, label %22, label %5
5: ; preds = %2, %18
%6 = phi i8 [ %20, %18 ], [ %3, %2 ]
%7 = phi i8* [ %19, %18 ], [ %1, %2 ]
%8 = sext i8 %6 to i32
switch i32 %8, label %13 [
i32 32, label %9
i32 34, label %9
i32 35, label %9
i32 37, label %9
i32 60, label %9
i32 62, label %9
i32 91, label %9
i32 93, label %9
i32 94, label %9
i32 92, label %9
i32 63, label %9
i32 43, label %9
i32 38, label %11
]
9: ; preds = %5, %5, %5, %5, %5, %5, %5, %5, %5, %5, %5, %5
%10 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %18
11: ; preds = %5
%12 = tail call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 5, i64 1, i32* %0)
br label %18
13: ; preds = %5
%14 = tail call i32 @isprint(i32 %8)
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0)
%17 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* %16, i32 %8)
br label %18
18: ; preds = %9, %11, %13
%19 = getelementptr inbounds i8, i8* %7, i64 1
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %5
22: ; preds = %18, %2
ret void
}