#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ is_url_safe(unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int strlen(char const *);
const char *fc_url_encode(const char *txt) {
  static char buf[2048];
  unsigned ch;
  char *ptr;
  if (sizeof(buf) <= (3 * strlen(txt))) {
    return "";
  }
  for (ptr = buf; *txt != '\0'; txt++) {
    ch = (unsigned char)*txt;
    if (is_url_safe(ch)) {
      *ptr++ = *txt;
    } else if (ch == ' ') {
      *ptr++ = '+';
    } else {
      sprintf(ptr, "%%%2.2X", ch);
      ptr += 3;
    }
  }
  *ptr++ = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<fc_url_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
imul   $0x3,%eax,%eax
movslq %eax,%rcx
mov    $0x800,%eax
cmp    %rcx,%rax
ja     1169 <fc_url_encode+0x39>
lea    0xea7(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <fc_url_encode+0x104>
lea    0x2ee0(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1217 <fc_url_encode+0xe7>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <is_url_safe@plt>
cmp    $0x0,%rax
je     11bc <fc_url_encode+0x8c>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    1201 <fc_url_encode+0xd1>
cmpl   $0x20,-0x14(%rbp)
jne    11dd <fc_url_encode+0xad>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x2b,(%rax)
jmp    11fc <fc_url_encode+0xcc>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe15(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
jmp    1201 <fc_url_encode+0xd1>
jmp    1206 <fc_url_encode+0xd6>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1174 <fc_url_encode+0x44>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
lea    0x2e20(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fc_url_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
call   1030 <strlen@plt>
lea    (%rax,%rax,2),%eax
cmp    $0x7ff,%eax
jbe    1156 <fc_url_encode+0x26>
lea    0xeb3(%rip),%r14
jmp    11cb <fc_url_encode+0x9b>
mov    0x0(%rbp),%bl
lea    0x2ef0(%rip),%r14
mov    %r14,%r15
test   %bl,%bl
je     11c7 <fc_url_encode+0x97>
add    $0x1,%rbp
lea    0x2ede(%rip),%r15
lea    0xe87(%rip),%r12
jmp    1197 <fc_url_encode+0x67>
nopl   0x0(%rax,%rax,1)
movzbl -0x1(%rbp),%eax
mov    %al,(%r15)
add    $0x1,%r15
movzbl 0x0(%rbp),%ebx
add    $0x1,%rbp
test   %bl,%bl
je     11c7 <fc_url_encode+0x97>
movzbl %bl,%r13d
mov    %r13d,%edi
call   1050 <is_url_safe@plt>
test   %rax,%rax
jne    1180 <fc_url_encode+0x50>
cmp    $0x20,%bl
jne    11b3 <fc_url_encode+0x83>
movb   $0x2b,(%r15)
jmp    1187 <fc_url_encode+0x57>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1040 <sprintf@plt>
add    $0x3,%r15
jmp    118b <fc_url_encode+0x5b>
movb   $0x0,(%r15)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@fc_url_encode.buf = internal global [2048 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [8 x i8] c"%%%2.2X\00"
define i8* @fc_url_encode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = mul nsw i32 3, %7
%9 = sext i32 %8 to i64
%10 = icmp ule i64 2048, %9
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %2
br label %50
12: ; preds = %1
store i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @fc_url_encode.buf, i64 0, i64 0), i8** %5
br label %13
13: ; preds = %44, %12
%14 = load i8*, i8** %3
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %47
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = call i64 @is_url_safe(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %18
%26 = load i8*, i8** %3
%27 = load i8, i8* %26
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
store i8 %27, i8* %28
br label %43
30: ; preds = %18
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 32
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %5
store i8 43, i8* %34
br label %42
36: ; preds = %30
%37 = load i8*, i8** %5
%38 = load i32, i32* %4
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i64 3
store i8* %41, i8** %5
br label %42
42: ; preds = %36, %33
br label %43
43: ; preds = %42, %25
br label %44
44: ; preds = %43
%45 = load i8*, i8** %3
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %3
br label %13
47: ; preds = %13
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %5
store i8 0, i8* %48
store i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @fc_url_encode.buf, i64 0, i64 0), i8** %2
br label %50
50: ; preds = %47, %11
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
@fc_url_encode.buf = internal global [2048 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [8 x i8] c"%%%2.2X\00"
define i8* @fc_url_encode(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = mul nsw i32 %2, 3
%4 = icmp ugt i32 %3, 2047
br i1 %4, label %32, label %5
5: ; preds = %1
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %30, label %8
8: ; preds = %5, %25
%9 = phi i8 [ %28, %25 ], [ %6, %5 ]
%10 = phi i8* [ %26, %25 ], [ getelementptr inbounds ([2048 x i8], [2048 x i8]* @fc_url_encode.buf, i64 0, i64 0), %5 ]
%11 = phi i8* [ %27, %25 ], [ %0, %5 ]
%12 = zext i8 %9 to i32
%13 = tail call i64 @is_url_safe(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %8
%16 = load i8, i8* %11
%17 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %16, i8* %10
br label %25
18: ; preds = %8
%19 = icmp eq i8 %9, 32
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = getelementptr inbounds i8, i8* %10, i64 1
store i8 43, i8* %10
br label %25
22: ; preds = %18
%23 = tail call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%24 = getelementptr inbounds i8, i8* %10, i64 3
br label %25
25: ; preds = %15, %22, %20
%26 = phi i8* [ %17, %15 ], [ %21, %20 ], [ %24, %22 ]
%27 = getelementptr inbounds i8, i8* %11, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %8
30: ; preds = %25, %5
%31 = phi i8* [ getelementptr inbounds ([2048 x i8], [2048 x i8]* @fc_url_encode.buf, i64 0, i64 0), %5 ], [ %26, %25 ]
store i8 0, i8* %31
br label %32
32: ; preds = %1, %30
%33 = phi i8* [ getelementptr inbounds ([2048 x i8], [2048 x i8]* @fc_url_encode.buf, i64 0, i64 0), %30 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %1 ]
ret i8* %33
}