#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 */
char *malloc(int);
scalar_t__ needs_replace(char);
int strlen(char *);
void *zero_to_f(char);
char *url_encode(char *not_replaced) {
  char *replaced = malloc(strlen(not_replaced) * 3 + 1);
  char *working_buf = replaced;
  while (*not_replaced) {
    if (needs_replace(*not_replaced)) {
      *working_buf++ = '%';
      *working_buf++ = zero_to_f(*not_replaced >> 4);
      *working_buf++ = zero_to_f(*not_replaced & 15);
    } else {
      *working_buf++ = *not_replaced;
    }
    not_replaced++;
  }
  *working_buf++ = '\0';
  return replaced;
}


x86 assembly (clang, -O0):
<url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
imul   $0x3,%eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1218 <url_encode+0xd8>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <needs_replace@plt>
cmp    $0x0,%rax
je     11f0 <url_encode+0xb0>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x25,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sar    $0x4,%eax
movsbl %al,%edi
call   1050 <zero_to_f@plt>
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
and    $0xf,%eax
movsbl %al,%edi
call   1050 <zero_to_f@plt>
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1207 <url_encode+0xc7>
mov    -0x8(%rbp),%rax
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    116c <url_encode+0x2c>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_encode>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    (%rax,%rax,2),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
mov    %r14,%r15
test   %al,%al
je     11be <url_encode+0x7e>
mov    %r14,%r15
jmp    1186 <url_encode+0x46>
nopl   0x0(%rax)
movzbl (%rbx),%eax
mov    %al,(%r15)
add    $0x1,%r15
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11be <url_encode+0x7e>
movsbl %al,%edi
call   1030 <needs_replace@plt>
test   %rax,%rax
je     1170 <url_encode+0x30>
movb   $0x25,(%r15)
movzbl (%rbx),%eax
sar    $0x4,%al
movsbl %al,%edi
call   1050 <zero_to_f@plt>
mov    %al,0x1(%r15)
movzbl (%rbx),%edi
and    $0xf,%edi
call   1050 <zero_to_f@plt>
mov    %al,0x2(%r15)
add    $0x3,%r15
jmp    117a <url_encode+0x3a>
movb   $0x0,(%r15)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @url_encode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
%7 = mul nsw i32 %6, 3
%8 = add nsw i32 %7, 1
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
store i8* %10, i8** %4
br label %11
11: ; preds = %46, %1
%12 = load i8*, i8** %2
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %49
15: ; preds = %11
%16 = load i8*, i8** %2
%17 = load i8, i8* %16
%18 = call i64 @needs_replace(i8 signext %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %41
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %4
store i8 37, i8* %21
%23 = load i8*, i8** %2
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = ashr i32 %25, 4
%27 = trunc i32 %26 to i8
%28 = call i8* @zero_to_f(i8 signext %27)
%29 = ptrtoint i8* %28 to i8
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %4
store i8 %29, i8* %30
%32 = load i8*, i8** %2
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = and i32 %34, 15
%36 = trunc i32 %35 to i8
%37 = call i8* @zero_to_f(i8 signext %36)
%38 = ptrtoint i8* %37 to i8
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %4
store i8 %38, i8* %39
br label %46
41: ; preds = %15
%42 = load i8*, i8** %2
%43 = load i8, i8* %42
%44 = load i8*, i8** %4
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %4
store i8 %43, i8* %44
br label %46
46: ; preds = %41, %20
%47 = load i8*, i8** %2
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %2
br label %11
49: ; preds = %11
%50 = load i8*, i8** %4
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %4
store i8 0, i8* %50
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
define noalias i8* @url_encode(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = mul nsw i32 %2, 3
%4 = add nsw i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %36, label %8
8: ; preds = %1, %31
%9 = phi i8 [ %34, %31 ], [ %6, %1 ]
%10 = phi i8* [ %32, %31 ], [ %5, %1 ]
%11 = phi i8* [ %33, %31 ], [ %0, %1 ]
%12 = tail call i64 @needs_replace(i8 signext %9)
%13 = icmp eq i64 %12, 0
br i1 %13, label %28, label %14
14: ; preds = %8
%15 = getelementptr inbounds i8, i8* %10, i64 1
store i8 37, i8* %10
%16 = load i8, i8* %11
%17 = ashr i8 %16, 4
%18 = tail call i8* @zero_to_f(i8 signext %17)
%19 = ptrtoint i8* %18 to i64
%20 = trunc i64 %19 to i8
%21 = getelementptr inbounds i8, i8* %10, i64 2
store i8 %20, i8* %15
%22 = load i8, i8* %11
%23 = and i8 %22, 15
%24 = tail call i8* @zero_to_f(i8 signext %23)
%25 = ptrtoint i8* %24 to i64
%26 = trunc i64 %25 to i8
%27 = getelementptr inbounds i8, i8* %10, i64 3
store i8 %26, i8* %21
br label %31
28: ; preds = %8
%29 = load i8, i8* %11
%30 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %29, i8* %10
br label %31
31: ; preds = %28, %14
%32 = phi i8* [ %27, %14 ], [ %30, %28 ]
%33 = getelementptr inbounds i8, i8* %11, i64 1
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 0
br i1 %35, label %36, label %8
36: ; preds = %31, %1
%37 = phi i8* [ %5, %1 ], [ %32, %31 ]
store i8 0, i8* %37
ret i8* %5
}