#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 *UrlEncodeChar(char *, char const);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
char *malloc(size_t);
int strlen(char const *);
char *UrlEncodeString(char const *src) {
  size_t nsize;
  char *dest;
  char *p;
  assert(src != NULL);
  nsize = strlen(src) * 3 + 1;
  dest = malloc(nsize);
  for (p = dest; *src; src++)
    p = UrlEncodeChar(p, *src);
  *p = 0;
  return dest;
}


x86 assembly (clang, -O0):
<UrlEncodeString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
imul   $0x3,%eax,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11ba <UrlEncodeString+0x7a>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
movsbl (%rax),%esi
call   1060 <UrlEncodeChar@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1188 <UrlEncodeString+0x48>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UrlEncodeString>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rax,2),%eax
add    $0x1,%eax
movslq %eax,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%cl
test   %cl,%cl
je     1196 <UrlEncodeString+0x56>
add    $0x1,%rbx
mov    %r14,%rax
nopl   0x0(%rax,%rax,1)
movsbl %cl,%esi
mov    %rax,%rdi
call   1060 <UrlEncodeChar@plt>
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1180 <UrlEncodeString+0x40>
movb   $0x0,(%rax)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @UrlEncodeString(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
%12 = mul nsw i32 %11, 3
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
store i8* %17, i8** %5
br label %18
18: ; preds = %27, %1
%19 = load i8*, i8** %2
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = load i8*, i8** %2
%25 = load i8, i8* %24
%26 = call i8* @UrlEncodeChar(i8* %23, i8 signext %25)
store i8* %26, i8** %5
br label %27
27: ; preds = %22
%28 = load i8*, i8** %2
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %2
br label %18
30: ; preds = %18
%31 = load i8*, i8** %5
store i8 0, i8* %31
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
define i8* @UrlEncodeString(i8* %0) {
%2 = icmp ne i8* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 @strlen(i8* %0)
%6 = mul nsw i32 %5, 3
%7 = add nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = tail call i8* @malloc(i64 %8)
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %1, %12
%13 = phi i8 [ %18, %12 ], [ %10, %1 ]
%14 = phi i8* [ %16, %12 ], [ %9, %1 ]
%15 = phi i8* [ %17, %12 ], [ %0, %1 ]
%16 = tail call i8* @UrlEncodeChar(i8* %14, i8 signext %13)
%17 = getelementptr inbounds i8, i8* %15, i64 1
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %12
20: ; preds = %12, %1
%21 = phi i8* [ %9, %1 ], [ %16, %12 ]
store i8 0, i8* %21
ret i8* %9
}