#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 */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
void url_encode(char *hash, char *output) {
  char *tmp = malloc(3);
  memset(output, '\0', 61);
  int i, k = 1;
  for (i = 0; i < 20; i++) {
    output[k - 1] = '%';
    tmp[0] = hash[i];
    snprintf(tmp, 3, "%02x", (unsigned char)hash[i]);
    output[k] = tmp[0];
    output[k + 1] = tmp[1];
    k += 3;
  }
  free(tmp);
}


x86 assembly (clang, -O0):
<url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x3,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x3d,%edx
call   1050 <memset@plt>
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x14,-0x1c(%rbp)
jge    1208 <url_encode+0xc8>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x25,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    $0x3,%esi
lea    0xe40(%rip),%rdx
movzbl (%rax,%rcx,1),%ecx
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    0x1(%rax),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117c <url_encode+0x3c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_encode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r13
mov    %rdi,%r14
mov    $0x3,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
xor    %ebx,%ebx
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x3d,%edx
call   1050 <memset@plt>
add    $0x2,%r13
lea    0xe88(%rip),%r12
nopl   0x0(%rax,%rax,1)
movb   $0x25,-0x2(%r13)
movzbl (%r14,%rbx,1),%ecx
mov    %cl,(%r15)
mov    %r15,%rdi
mov    $0x3,%esi
mov    %r12,%rdx
call   1040 <snprintf@plt>
movzbl (%r15),%eax
mov    %al,-0x1(%r13)
movzbl 0x1(%r15),%eax
mov    %al,0x0(%r13)
add    $0x1,%rbx
add    $0x3,%r13
cmp    $0x14,%rbx
jne    1180 <url_encode+0x40>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define void @url_encode(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = call i8* @malloc(i32 3)
store i8* %8, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @memset(i8* %9, i8 signext 0, i32 61)
store i32 1, i32* %7
store i32 0, i32* %6
br label %11
11: ; preds = %51, %2
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 20
br i1 %13, label %14, label %54
14: ; preds = %11
%15 = load i8*, i8** %4
%16 = load i32, i32* %7
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
store i8 37, i8* %19
%20 = load i8*, i8** %3
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i64 0
store i8 %24, i8* %26
%27 = load i8*, i8** %5
%28 = load i8*, i8** %3
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = call i32 @snprintf(i8* %27, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %32)
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = load i8*, i8** %4
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = load i8, i8* %42
%44 = load i8*, i8** %4
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
store i8 %43, i8* %48
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 3
store i32 %50, i32* %7
br label %51
51: ; preds = %14
%52 = load i32, i32* %6
%53 = add nsw i32 %52, 1
store i32 %53, i32* %6
br label %11
54: ; preds = %11
%55 = load i8*, i8** %5
%56 = call i32 @free(i8* %55)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define void @url_encode(i8* nocapture %0, i8* %1) {
%3 = tail call dereferenceable_or_null(3) i8* @malloc(i32 3)
%4 = tail call i32 @memset(i8* %1, i8 signext 0, i32 61)
%5 = getelementptr inbounds i8, i8* %3, i64 1
br label %6
6: ; preds = %2, %6
%7 = phi i64 [ 0, %2 ], [ %20, %6 ]
%8 = phi i64 [ 1, %2 ], [ %19, %6 ]
%9 = add nsw i64 %8, -1
%10 = getelementptr inbounds i8, i8* %1, i64 %9
store i8 37, i8* %10
%11 = getelementptr inbounds i8, i8* %0, i64 %7
%12 = load i8, i8* %11
store i8 %12, i8* %3
%13 = tail call i32 @snprintf(i8* %3, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %12)
%14 = load i8, i8* %3
%15 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 %14, i8* %15
%16 = load i8, i8* %5
%17 = add nuw nsw i64 %8, 1
%18 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 %16, i8* %18
%19 = add nuw nsw i64 %8, 3
%20 = add nuw nsw i64 %7, 1
%21 = icmp eq i64 %20, 20
br i1 %21, label %22, label %6
22: ; preds = %6
%23 = tail call i32 @free(i8* %3)
ret void
}