#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__ is_non_symbol(char const);
char *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, char const);
int strlen(char const *);
char *url_encode(const char *input) {
  int end = strlen(input);
  size_t final_size = (end * 3) + 1;
  char *working = malloc(final_size * sizeof(char)), *output = working;
  while (*input) {
    const char c = *input;
    if (c < 0) {
      input++;
    } else if (is_non_symbol(c)) {
      *working++ = *input++;
    } else {
      char encoded[4] = {0};
      snprintf(encoded, 4, "%%%02x", c);
      *working++ = encoded[0];
      *working++ = encoded[1];
      *working++ = encoded[2];
      input++;
    }
  }
  *working = 0;
  return output;
}


x86 assembly (clang, -O0):
<url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
imul   $0x3,-0xc(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1277 <url_encode+0x127>
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0x0,%eax
jge    11c2 <url_encode+0x72>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1272 <url_encode+0x122>
movsbl -0x29(%rbp),%edi
call   1070 <is_non_symbol@plt>
cmp    $0x0,%rax
je     11fc <url_encode+0xac>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    126d <url_encode+0x11d>
lea    -0x2d(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    -0x2d(%rbp),%rdi
mov    $0x4,%esi
lea    0xde4(%rip),%rdx
movsbl -0x29(%rbp),%ecx
call   1040 <snprintf@plt>
mov    -0x2d(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x2c(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x2b(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1272 <url_encode+0x122>
jmp    118e <url_encode+0x3e>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%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    %rdi,%rbx
call   1030 <strlen@plt>
lea    (%rax,%rax,2),%eax
add    $0x1,%eax
movslq %eax,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
mov    %r14,%r13
test   %al,%al
je     11f5 <url_encode+0xb5>
add    $0x1,%rbx
lea    0xe84(%rip),%r15
lea    0x4(%rsp),%r12
mov    %r14,%r13
jmp    11d4 <url_encode+0x94>
cs nopw 0x0(%rax,%rax,1)
movl   $0x0,0x4(%rsp)
mov    %r12,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
mov    %ebp,%ecx
call   1040 <snprintf@plt>
movzbl 0x4(%rsp),%eax
mov    %al,0x0(%r13)
movzbl 0x5(%rsp),%eax
mov    %al,0x1(%r13)
movzbl 0x6(%rsp),%eax
mov    %al,0x2(%r13)
add    $0x3,%r13
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11f5 <url_encode+0xb5>
test   %al,%al
js     11c9 <url_encode+0x89>
movsbl %al,%ebp
mov    %ebp,%edi
call   1060 <is_non_symbol@plt>
test   %rax,%rax
je     1190 <url_encode+0x50>
movzbl -0x1(%rbx),%eax
mov    %al,0x0(%r13)
add    $0x1,%r13
jmp    11c9 <url_encode+0x89>
movb   $0x0,0x0(%r13)
mov    %r14,%rax
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 i8* @url_encode(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8
%8 = alloca [4 x i8]
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = mul nsw i32 %11, 3
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
store i64 %14, i64* %4
%15 = load i64, i64* %4
%16 = mul i64 %15, 1
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %5
%18 = load i8*, i8** %5
store i8* %18, i8** %6
br label %19
19: ; preds = %62, %1
%20 = load i8*, i8** %2
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 0
br i1 %22, label %23, label %63
23: ; preds = %19
%24 = load i8*, i8** %2
%25 = load i8, i8* %24
store i8 %25, i8* %7
%26 = load i8, i8* %7
%27 = sext i8 %26 to i32
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i8*, i8** %2
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %2
br label %62
32: ; preds = %23
%33 = load i8, i8* %7
%34 = call i64 @is_non_symbol(i8 signext %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i8*, i8** %2
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %2
%39 = load i8, i8* %37
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %5
store i8 %39, i8* %40
br label %61
42: ; preds = %32
%43 = bitcast [4 x i8]* %8 to i8*
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%45 = load i8, i8* %7
%46 = call i32 @snprintf(i8* %44, i32 4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 signext %45)
%47 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%48 = load i8, i8* %47
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %5
store i8 %48, i8* %49
%51 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 1
%52 = load i8, i8* %51
%53 = load i8*, i8** %5
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %5
store i8 %52, i8* %53
%55 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 2
%56 = load i8, i8* %55
%57 = load i8*, i8** %5
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %5
store i8 %56, i8* %57
%59 = load i8*, i8** %2
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %2
br label %61
61: ; preds = %42, %36
br label %62
62: ; preds = %61, %29
br label %19
63: ; preds = %19
%64 = load i8*, i8** %5
store i8 0, i8* %64
%65 = load i8*, i8** %6
ret i8* %65
}

LLVM IR (-O2):
@.str = [7 x i8] c"%%%02x\00"
define noalias i8* @url_encode(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to [4 x i8]*
%4 = tail call i32 @strlen(i8* %0)
%5 = mul nsw i32 %4, 3
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
%8 = tail call i8* @malloc(i64 %7)
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 0
br i1 %10, label %39, label %11
11: ; preds = %1
%12 = bitcast i32* %2 to i8*
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
br label %15
15: ; preds = %11, %34
%16 = phi i8 [ %9, %11 ], [ %37, %34 ]
%17 = phi i8* [ %0, %11 ], [ %36, %34 ]
%18 = phi i8* [ %8, %11 ], [ %35, %34 ]
%19 = icmp slt i8 %16, 0
br i1 %19, label %34, label %20
20: ; preds = %15
%21 = call i64 @is_non_symbol(i8 signext %16)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i8, i8* %17
%25 = getelementptr inbounds i8, i8* %18, i64 1
store i8 %24, i8* %18
br label %34
26: ; preds = %20
store i32 0, i32* %2
%27 = call i32 @snprintf(i8* %12, i32 4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 signext %16)
%28 = load i8, i8* %12
%29 = getelementptr inbounds i8, i8* %18, i64 1
store i8 %28, i8* %18
%30 = load i8, i8* %13
%31 = getelementptr inbounds i8, i8* %18, i64 2
store i8 %30, i8* %29
%32 = load i8, i8* %14
%33 = getelementptr inbounds i8, i8* %18, i64 3
store i8 %32, i8* %31
br label %34
34: ; preds = %15, %23, %26
%35 = phi i8* [ %25, %23 ], [ %33, %26 ], [ %18, %15 ]
%36 = getelementptr inbounds i8, i8* %17, i64 1
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, 0
br i1 %38, label %39, label %15
39: ; preds = %34, %1
%40 = phi i8* [ %8, %1 ], [ %35, %34 ]
store i8 0, i8* %40
ret i8* %8
}