#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*/ SHRINKSAFESTR(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ isalnum(char const);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const);
char *stpcpy(char *, char *);
size_t strnlen(char const *, size_t const);
char *html_encode(const char *string, const size_t length) {
  size_t reallen = strnlen(string, length);
  unsigned int i = 0;
  char *encoded = (char *)malloc((reallen * 6) + 1);
  if (encoded == NULL) {
    return NULL;
  }
  char *tencoded = encoded;
  char *temp = (char *)malloc(6 + 1);
  if (temp == NULL) {
    free(encoded);
    return NULL;
  }
  for (i = 0; i < reallen; i++) {
    snprintf(temp, 6 + 1, isalnum(string[i]) ? "%1c" : "&#x%2X;", string[i]);
    tencoded = stpcpy(tencoded, temp);
  }
  free(temp);
  SHRINKSAFESTR(encoded);
  return encoded;
}


x86 assembly (clang, -O0):
<html_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strnlen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
imul   $0x6,-0x20(%rbp),%rax
add    $0x1,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c4 <html_encode+0x54>
movq   $0x0,-0x8(%rbp)
jmp    1292 <html_encode+0x122>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x7,%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11fb <html_encode+0x8b>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1292 <html_encode+0x122>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%rax
jae    1278 <html_encode+0x108>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1050 <isalnum@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rcx
lea    0xdcf(%rip),%rdx
lea    0xdc4(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    $0x7,%esi
movsbl (%rax,%rcx,1),%ecx
call   1060 <snprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1040 <stpcpy@plt>
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1202 <html_encode+0x92>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1080 <SHRINKSAFESTR@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<html_encode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1070 <strnlen@plt>
mov    %rax,%r15
add    %eax,%eax
lea    (%rax,%rax,2),%edi
or     $0x1,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1231 <html_encode+0xc1>
mov    %rax,%rbx
mov    $0x7,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1229 <html_encode+0xb9>
mov    %rax,%r13
mov    %rbx,(%rsp)
test   %r15,%r15
je     1213 <html_encode+0xa3>
xor    %ebp,%ebp
lea    0xe3d(%rip),%r14
mov    (%rsp),%rbx
nopw   0x0(%rax,%rax,1)
movsbl (%r12,%rbp,1),%edi
call   1050 <isalnum@plt>
test   %rax,%rax
mov    %r14,%rdx
lea    0xe1d(%rip),%rax
cmove  %rax,%rdx
movsbl (%r12,%rbp,1),%ecx
mov    %r13,%rdi
mov    $0x7,%esi
call   1060 <snprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <stpcpy@plt>
mov    %rax,%rbx
add    $0x1,%ebp
cmp    %rbp,%r15
ja     11d0 <html_encode+0x60>
mov    %r13,%rdi
call   1030 <free@plt>
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1080 <SHRINKSAFESTR@plt>
jmp    1233 <html_encode+0xc3>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%1c\00"
@.str.1 = [8 x i8] c"&#x%2X;\00"
define i8* @html_encode(i8* nocapture %0, i64 %1) {
%3 = tail call i64 @strnlen(i8* %0, i64 %1)
%4 = trunc i64 %3 to i32
%5 = mul i32 %4, 6
%6 = or i32 %5, 1
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %35, label %10
10: ; preds = %2
%11 = tail call i64 @malloc(i32 7)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = icmp eq i64 %3, 0
br i1 %15, label %32, label %18
16: ; preds = %10
%17 = tail call i32 @free(i8* %8)
br label %35
18: ; preds = %14, %18
%19 = phi i64 [ %30, %18 ], [ 0, %14 ]
%20 = phi i8* [ %28, %18 ], [ %8, %14 ]
%21 = getelementptr inbounds i8, i8* %0, i64 %19
%22 = load i8, i8* %21
%23 = tail call i64 @isalnum(i8 signext %22)
%24 = icmp eq i64 %23, 0
%25 = select i1 %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0)
%26 = load i8, i8* %21
%27 = tail call i32 @snprintf(i8* %12, i32 7, i8* %25, i8 signext %26)
%28 = tail call i8* @stpcpy(i8* %20, i8* %12)
%29 = add nuw nsw i64 %19, 1
%30 = and i64 %29, 4294967295
%31 = icmp ugt i64 %3, %30
br i1 %31, label %18, label %32
32: ; preds = %18, %14
%33 = tail call i32 @free(i8* %12)
%34 = tail call i32 @SHRINKSAFESTR(i8* %8)
br label %35
35: ; preds = %16, %32, %2
%36 = phi i8* [ null, %2 ], [ null, %16 ], [ %8, %32 ]
ret i8* %36
}