#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*/ strcat(char *, char *);
char *strdup(char *);
int strlen(char *);
char *url_percent_encode_byte(char, int);
char *url_percent_encode(char *str, int set) {
  char *ret = NULL;
  char *byte = NULL;
  char *bytes = NULL;
  int len = strlen(str);
  int i = 0;
  bytes = malloc(sizeof(char) * len);
  for (; i < len; ++i) {
    byte = url_percent_encode_byte(str[i], set);
    strcat(bytes, byte);
  }
  ret = strdup(bytes);
  free(bytes);
  return ret;
}


x86 assembly (clang, -O0):
<url_percent_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movslq -0x2c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    11ee <url_percent_encode+0x8e>
mov    -0x8(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0xc(%rbp),%esi
movsbl %al,%edi
call   1070 <url_percent_encode_byte@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11ad <url_percent_encode+0x4d>
mov    -0x28(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_percent_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
test   %r15d,%r15d
jle    11b1 <url_percent_encode+0x51>
mov    %r15d,%ebx
xor    %ebp,%ebp
nopl   (%rax)
movsbl (%r12,%rbp,1),%edi
mov    %r14d,%esi
call   1070 <url_percent_encode_byte@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1060 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1190 <url_percent_encode+0x30>
mov    %r13,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define noalias i8* @url_percent_encode(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %16
6: ; preds = %2
%7 = zext i32 %3 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %14, %8 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = tail call i8* @url_percent_encode_byte(i8 signext %11, i32 %1)
%13 = tail call i32 @strcat(i8* %4, i8* %12)
%14 = add nuw nsw i64 %9, 1
%15 = icmp eq i64 %14, %7
br i1 %15, label %16, label %8
16: ; preds = %8, %2
%17 = tail call i8* @strdup(i8* %4)
%18 = tail call i32 @free(i8* %4)
ret i8* %17
}