#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*/ bzero(char *, int);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int strlen(char *);
char *url_encode(char *string) {
  char *res;
  int len;
  int i, j;
  len = strlen(string) * 3;
  res = malloc(len + 1);
  bzero(res, len + 1);
  for (i = 0, j = 0; string[i]; ++i, j += 3)
    sprintf(res + j, "%%%02x", (unsigned char)string[i]);
  return res;
}


x86 assembly (clang, -O0):
<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    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1050 <bzero@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11d3 <url_encode+0x93>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
lea    0xe4d(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x1c(%rbp)
jmp    1187 <url_encode+0x47>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_encode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1030 <strlen@plt>
lea    (%rax,%rax,2),%ebx
add    $0x1,%ebx
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
call   1050 <bzero@plt>
mov    (%r12),%al
test   %al,%al
je     119f <url_encode+0x5f>
add    $0x1,%r12
lea    0xe83(%rip),%r15
mov    %r14,%rbx
movzbl %al,%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <sprintf@plt>
movzbl (%r12),%eax
add    $0x1,%r12
add    $0x3,%rbx
test   %al,%al
jne    1180 <url_encode+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"%%%02x\00"
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 = tail call i32 @bzero(i8* %5, i32 %4)
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %20, label %9
9: ; preds = %1, %9
%10 = phi i64 [ %15, %9 ], [ 0, %1 ]
%11 = phi i64 [ %16, %9 ], [ 0, %1 ]
%12 = phi i8 [ %18, %9 ], [ %7, %1 ]
%13 = getelementptr inbounds i8, i8* %5, i64 %11
%14 = tail call i32 @sprintf(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %12)
%15 = add nuw i64 %10, 1
%16 = add nuw i64 %11, 3
%17 = getelementptr inbounds i8, i8* %0, i64 %15
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %9
20: ; preds = %9, %1
ret i8* %5
}