#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__ malloc(int);
int strlen(char *);
char *hex_url_encode(char *hex) {
  int len = strlen(hex) * 3 / 2 + 1;
  char *res = (char *)malloc(len * sizeof(char));
  char *idx = res;
  while (*hex) {
    *idx++ = '%';
    *idx++ = *hex++;
    if (*hex == 0)
      break;
    *idx++ = *hex++;
  }
  *idx = '\0';
  return res;
}


x86 assembly (clang, -O0):
<hex_url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
imul   $0x3,%eax,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11de <hex_url_encode+0xbe>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x25,(%rax)
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)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b7 <hex_url_encode+0x97>
jmp    11de <hex_url_encode+0xbe>
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    1161 <hex_url_encode+0x41>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_url_encode>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    (%rax,%rax,2),%eax
mov    %eax,%edi
shr    $0x1f,%edi
add    %eax,%edi
sar    %edi
add    $0x1,%edi
call   1040 <malloc@plt>
cmpb   $0x0,(%rbx)
je     1178 <hex_url_encode+0x58>
mov    %rax,%rcx
add    $0x2,%rcx
nopl   0x0(%rax)
movb   $0x25,-0x2(%rcx)
movzbl (%rbx),%edx
mov    %dl,-0x1(%rcx)
movzbl 0x1(%rbx),%edx
test   %dl,%dl
je     117b <hex_url_encode+0x5b>
mov    %dl,(%rcx)
add    $0x3,%rcx
cmpb   $0x0,0x2(%rbx)
lea    0x2(%rbx),%rbx
jne    1150 <hex_url_encode+0x30>
add    $0xfffffffffffffffe,%rcx
jmp    117b <hex_url_encode+0x5b>
mov    %rax,%rcx
movb   $0x0,(%rcx)
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @hex_url_encode(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = mul nsw i32 %2, 3
%4 = sdiv i32 %3, 2
%5 = add nsw i32 %4, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %1, %19
%11 = phi i8* [ %21, %19 ], [ %7, %1 ]
%12 = phi i8* [ %20, %19 ], [ %0, %1 ]
%13 = getelementptr inbounds i8, i8* %11, i64 1
store i8 37, i8* %11
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = load i8, i8* %12
%16 = getelementptr inbounds i8, i8* %11, i64 2
store i8 %15, i8* %13
%17 = load i8, i8* %14
%18 = icmp eq i8 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %10
%20 = getelementptr inbounds i8, i8* %12, i64 2
%21 = getelementptr inbounds i8, i8* %11, i64 3
store i8 %17, i8* %16
%22 = load i8, i8* %20
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %10
24: ; preds = %19, %10, %1
%25 = phi i8* [ %7, %1 ], [ %16, %10 ], [ %21, %19 ]
store i8 0, i8* %25
ret i8* %7
}