#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 /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *strdup(char *);
char *scgi_urlencode(const char *dec, size_t decsz) {
  const bool _encc[256] = {
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0,
      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  char *enc, *start, *end, *p;
  if (dec == NULL) {
    return (NULL);
  }
  if (decsz == 0) {
    return (strdup(""));
  }
  enc = (char *)malloc(3 * decsz + 1);
  start = enc;
  end = (char *)(dec + decsz - 1);
  p = (char *)dec;
  while (p <= end) {
    if (!_encc[*((unsigned char *)p)]) {
      *enc = *p;
      enc++;
    } else {
      sprintf(enc, "%%%02x", *((unsigned char *)p));
      enc += 3;
    }
    p++;
  }
  *enc = '\0';
  return (start);
}


x86 assembly (clang, -O0):
<scgi_urlencode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
lea    0xe9f(%rip),%rsi
mov    $0x400,%edx
call   1030 <memcpy@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1183 <scgi_urlencode+0x43>
movq   $0x0,-0x8(%rbp)
jmp    1296 <scgi_urlencode+0x156>
cmpq   $0x0,-0x18(%rbp)
jne    11a3 <scgi_urlencode+0x63>
lea    0x1271(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1296 <scgi_urlencode+0x156>
imul   $0x3,-0x18(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x438(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x440(%rbp),%rax
cmp    -0x438(%rbp),%rax
ja     1281 <scgi_urlencode+0x141>
mov    -0x440(%rbp),%rax
movzbl (%rax),%eax
cmpl   $0x0,-0x420(%rbp,%rax,4)
jne    123b <scgi_urlencode+0xfb>
mov    -0x440(%rbp),%rax
mov    (%rax),%cl
mov    -0x428(%rbp),%rax
mov    %cl,(%rax)
mov    -0x428(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x428(%rbp)
jmp    126a <scgi_urlencode+0x12a>
mov    -0x428(%rbp),%rdi
mov    -0x440(%rbp),%rax
lea    0x11b0(%rip),%rsi
movzbl (%rax),%edx
call   1050 <sprintf@plt>
mov    -0x428(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x428(%rbp)
mov    -0x440(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x440(%rbp)
jmp    11e6 <scgi_urlencode+0xa6>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x430(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scgi_urlencode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     116e <scgi_urlencode+0x3e>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1183 <scgi_urlencode+0x53>
mov    %rdi,%r15
lea    (%rbx,%rbx,2),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%r14
lea    (%r15,%rbx,1),%r13
add    $0xffffffffffffffff,%r13
mov    %rax,%rbx
cmp    %r15,%r13
jae    119d <scgi_urlencode+0x6d>
movb   $0x0,(%rbx)
jmp    1171 <scgi_urlencode+0x41>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x127c(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strdup@plt>
lea    0xe5c(%rip),%rbp
lea    0x1255(%rip),%r12
mov    %r14,%rbx
jmp    11cf <scgi_urlencode+0x9f>
movzbl %al,%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
mov    $0x3,%eax
add    %rax,%rbx
add    $0x1,%r15
cmp    %r13,%r15
ja     1169 <scgi_urlencode+0x39>
movzbl (%r15),%eax
cmpl   $0x0,0x0(%rbp,%rax,4)
jne    11b0 <scgi_urlencode+0x80>
mov    %al,(%rbx)
mov    $0x1,%eax
jmp    11c3 <scgi_urlencode+0x93>

LLVM IR (-O0):
@__const.scgi_urlencode._encc = [256 x i32] [i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 0, i32 0, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 1, i32 0, i32 1, i32 1, i32 0, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1]
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"%%%02x\00"
define i8* @scgi_urlencode(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [256 x i32]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%11 = bitcast [256 x i32]* %6 to i8*
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %63
15: ; preds = %2
%16 = load i64, i64* %5
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %3
br label %63
20: ; preds = %15
%21 = load i64, i64* %5
%22 = mul i64 3, %21
%23 = add i64 %22, 1
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %7
%27 = load i8*, i8** %7
store i8* %27, i8** %8
%28 = load i8*, i8** %4
%29 = load i64, i64* %5
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = getelementptr inbounds i8, i8* %30, i64 -1
store i8* %31, i8** %9
%32 = load i8*, i8** %4
store i8* %32, i8** %10
br label %33
33: ; preds = %57, %20
%34 = load i8*, i8** %10
%35 = load i8*, i8** %9
%36 = icmp ule i8* %34, %35
br i1 %36, label %37, label %60
37: ; preds = %33
%38 = load i8*, i8** %10
%39 = load i8, i8* %38
%40 = zext i8 %39 to i64
%41 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %37
%45 = load i8*, i8** %10
%46 = load i8, i8* %45
%47 = load i8*, i8** %7
store i8 %46, i8* %47
%48 = load i8*, i8** %7
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %7
br label %57
50: ; preds = %37
%51 = load i8*, i8** %7
%52 = load i8*, i8** %10
%53 = load i8, i8* %52
%54 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %53)
%55 = load i8*, i8** %7
%56 = getelementptr inbounds i8, i8* %55, i64 3
store i8* %56, i8** %7
br label %57
57: ; preds = %50, %44
%58 = load i8*, i8** %10
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %10
br label %33
60: ; preds = %33
%61 = load i8*, i8** %7
store i8 0, i8* %61
%62 = load i8*, i8** %8
store i8* %62, i8** %3
br label %63
63: ; preds = %60, %18, %14
%64 = load i8*, i8** %3
ret i8* %64
}

LLVM IR (-O2):
@__const.scgi_urlencode._encc = [256 x i32] [i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 0, i32 0, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 1, i32 0, i32 1, i32 1, i32 0, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1]
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"%%%02x\00"
define i8* @scgi_urlencode(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %35, label %4
4: ; preds = %2
%5 = icmp eq i64 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %35
8: ; preds = %4
%9 = trunc i64 %1 to i32
%10 = mul i32 %9, 3
%11 = add i32 %10, 1
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = getelementptr inbounds i8, i8* %0, i64 %1
%15 = getelementptr inbounds i8, i8* %14, i64 -1
%16 = icmp ult i8* %15, %0
br i1 %16, label %33, label %17
17: ; preds = %8, %28
%18 = phi i8* [ %31, %28 ], [ %0, %8 ]
%19 = phi i8* [ %30, %28 ], [ %13, %8 ]
%20 = load i8, i8* %18
%21 = zext i8 %20 to i64
%22 = getelementptr inbounds [256 x i32], [256 x i32]* @__const.scgi_urlencode._encc, i64 0, i64 %21
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %17
store i8 %20, i8* %19
br label %28
26: ; preds = %17
%27 = tail call i32 @sprintf(i8* dereferenceable(1) %19, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %20)
br label %28
28: ; preds = %26, %25
%29 = phi i64 [ 3, %26 ], [ 1, %25 ]
%30 = getelementptr inbounds i8, i8* %19, i64 %29
%31 = getelementptr inbounds i8, i8* %18, i64 1
%32 = icmp ugt i8* %31, %15
br i1 %32, label %33, label %17
33: ; preds = %28, %8
%34 = phi i8* [ %13, %8 ], [ %30, %28 ]
store i8 0, i8* %34
br label %35
35: ; preds = %2, %33, %6
%36 = phi i8* [ %7, %6 ], [ %13, %33 ], [ null, %2 ]
ret i8* %36
}