#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*/ isalnum(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strcat(char *, char *);
void MakeHexEncodeFromStr(char *hexamsg, unsigned char *kssmmsg, int opt) {
  int i = 0, j = 0;
  char tmpstr[5];
  hexamsg[j] = '\0';
  while (kssmmsg[i]) {
    if (opt || (kssmmsg[i] & 0x80)) {
      sprintf(tmpstr, "%%%02x", (unsigned char)kssmmsg[i++]);
      strcat(hexamsg, tmpstr);
      if (kssmmsg[i]) {
        sprintf(tmpstr, "%%%02x", (unsigned char)kssmmsg[i++]);
        strcat(hexamsg, tmpstr);
      }
    } else if (!isalnum((int)(kssmmsg[i])) && kssmmsg[i] != '/') {
      sprintf(tmpstr, "%%%02x", (unsigned char)kssmmsg[i++]);
      strcat(hexamsg, tmpstr);
    } else if (kssmmsg[i] == '&') {
      sprintf(tmpstr, "%%%02x", (unsigned char)kssmmsg[i++]);
      strcat(hexamsg, tmpstr);
    } else {
      sprintf(tmpstr, "%c", (unsigned char)kssmmsg[i++]);
      strcat(hexamsg, tmpstr);
    }
  }
}


x86 assembly (clang, -O0):
<MakeHexEncodeFromStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     1306 <MakeHexEncodeFromStr+0x1d6>
cmpl   $0x0,-0x14(%rbp)
jne    1193 <MakeHexEncodeFromStr+0x63>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     1210 <MakeHexEncodeFromStr+0xe0>
lea    -0x21(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
lea    0xe50(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x21(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     120b <MakeHexEncodeFromStr+0xdb>
lea    -0x21(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
lea    0xe0b(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x21(%rbp),%rsi
call   1040 <strcat@plt>
jmp    1301 <MakeHexEncodeFromStr+0x1d1>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <isalnum@plt>
cmp    $0x0,%eax
jne    1277 <MakeHexEncodeFromStr+0x147>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1277 <MakeHexEncodeFromStr+0x147>
lea    -0x21(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
lea    0xda4(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x21(%rbp),%rsi
call   1040 <strcat@plt>
jmp    12fc <MakeHexEncodeFromStr+0x1cc>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x26,%eax
jne    12c4 <MakeHexEncodeFromStr+0x194>
lea    -0x21(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
lea    0xd57(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x21(%rbp),%rsi
call   1040 <strcat@plt>
jmp    12f7 <MakeHexEncodeFromStr+0x1c7>
lea    -0x21(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
lea    0xd26(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x21(%rbp),%rsi
call   1040 <strcat@plt>
jmp    12fc <MakeHexEncodeFromStr+0x1cc>
jmp    1301 <MakeHexEncodeFromStr+0x1d1>
jmp    115d <MakeHexEncodeFromStr+0x2d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MakeHexEncodeFromStr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,0xc(%rsp)
mov    %rdi,0x10(%rsp)
movb   $0x0,(%rdi)
mov    (%rsi),%r14b
test   %r14b,%r14b
je     1239 <MakeHexEncodeFromStr+0x109>
mov    %rsi,%r12
xor    %r15d,%r15d
lea    0x7(%rsp),%r13
jmp    1188 <MakeHexEncodeFromStr+0x58>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbx
mov    %ebx,%r15d
movslq %r15d,%rax
movzbl (%r12,%rax,1),%r14d
test   %r14b,%r14b
je     1239 <MakeHexEncodeFromStr+0x109>
movzbl %r14b,%ebp
cmpl   $0x0,0xc(%rsp)
jne    11c0 <MakeHexEncodeFromStr+0x90>
test   $0x80,%r14b
jne    11c0 <MakeHexEncodeFromStr+0x90>
mov    %ebp,%edi
call   1030 <isalnum@plt>
test   %eax,%eax
jne    1212 <MakeHexEncodeFromStr+0xe2>
cmp    $0x2f,%r14b
je     1212 <MakeHexEncodeFromStr+0xe2>
add    $0x1,%r15d
mov    %r13,%rdi
lea    0xe48(%rip),%rsi
mov    %ebp,%edx
jmp    11fb <MakeHexEncodeFromStr+0xcb>
nopl   0x0(%rax)
movslq %r15d,%rbx
mov    %r13,%rdi
lea    0xe33(%rip),%r14
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <sprintf@plt>
mov    0x10(%rsp),%rdi
mov    %r13,%rsi
call   1040 <strcat@plt>
movzbl 0x1(%r12,%rbx,1),%eax
test   %al,%al
je     1170 <MakeHexEncodeFromStr+0x40>
add    $0x2,%r15d
movzbl %al,%edx
mov    %r13,%rdi
mov    %r14,%rsi
call   1050 <sprintf@plt>
mov    0x10(%rsp),%rdi
mov    %r13,%rsi
call   1040 <strcat@plt>
jmp    1177 <MakeHexEncodeFromStr+0x47>
add    $0x1,%r15d
cmp    $0x26,%r14b
jne    122d <MakeHexEncodeFromStr+0xfd>
mov    %r13,%rdi
lea    0xdda(%rip),%rsi
mov    $0x26,%edx
jmp    11fb <MakeHexEncodeFromStr+0xcb>
mov    %r14b,0x7(%rsp)
movb   $0x0,0x8(%rsp)
jmp    1200 <MakeHexEncodeFromStr+0xd0>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%%%02x\00"
@.str.1 = [3 x i8] c"%c\00"
define void @MakeHexEncodeFromStr(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [5 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%10 = load i8*, i8** %4
%11 = load i32, i32* %8
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %127, %3
%15 = load i8*, i8** %5
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %128
21: ; preds = %14
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %21
%25 = load i8*, i8** %5
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = and i32 %30, 128
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %64
33: ; preds = %24, %21
%34 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%35 = load i8*, i8** %5
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = load i8, i8* %39
%41 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %40)
%42 = load i8*, i8** %4
%43 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%44 = call i32 @strcat(i8* %42, i8* %43)
%45 = load i8*, i8** %5
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = icmp ne i8 %49, 0
br i1 %50, label %51, label %63
51: ; preds = %33
%52 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%53 = load i8*, i8** %5
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
%56 = sext i32 %54 to i64
%57 = getelementptr inbounds i8, i8* %53, i64 %56
%58 = load i8, i8* %57
%59 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %58)
%60 = load i8*, i8** %4
%61 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%62 = call i32 @strcat(i8* %60, i8* %61)
br label %63
63: ; preds = %51, %33
br label %127
64: ; preds = %24
%65 = load i8*, i8** %5
%66 = load i32, i32* %7
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %65, i64 %67
%69 = load i8, i8* %68
%70 = zext i8 %69 to i32
%71 = call i32 @isalnum(i32 %70)
%72 = icmp ne i32 %71, 0
br i1 %72, label %93, label %73
73: ; preds = %64
%74 = load i8*, i8** %5
%75 = load i32, i32* %7
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = load i8, i8* %77
%79 = zext i8 %78 to i32
%80 = icmp ne i32 %79, 47
br i1 %80, label %81, label %93
81: ; preds = %73
%82 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%83 = load i8*, i8** %5
%84 = load i32, i32* %7
%85 = add nsw i32 %84, 1
store i32 %85, i32* %7
%86 = sext i32 %84 to i64
%87 = getelementptr inbounds i8, i8* %83, i64 %86
%88 = load i8, i8* %87
%89 = call i32 @sprintf(i8* %82, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %88)
%90 = load i8*, i8** %4
%91 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%92 = call i32 @strcat(i8* %90, i8* %91)
br label %126
93: ; preds = %73, %64
%94 = load i8*, i8** %5
%95 = load i32, i32* %7
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8, i8* %94, i64 %96
%98 = load i8, i8* %97
%99 = zext i8 %98 to i32
%100 = icmp eq i32 %99, 38
br i1 %100, label %101, label %113
101: ; preds = %93
%102 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%103 = load i8*, i8** %5
%104 = load i32, i32* %7
%105 = add nsw i32 %104, 1
store i32 %105, i32* %7
%106 = sext i32 %104 to i64
%107 = getelementptr inbounds i8, i8* %103, i64 %106
%108 = load i8, i8* %107
%109 = call i32 @sprintf(i8* %102, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %108)
%110 = load i8*, i8** %4
%111 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%112 = call i32 @strcat(i8* %110, i8* %111)
br label %125
113: ; preds = %93
%114 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%115 = load i8*, i8** %5
%116 = load i32, i32* %7
%117 = add nsw i32 %116, 1
store i32 %117, i32* %7
%118 = sext i32 %116 to i64
%119 = getelementptr inbounds i8, i8* %115, i64 %118
%120 = load i8, i8* %119
%121 = call i32 @sprintf(i8* %114, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %120)
%122 = load i8*, i8** %4
%123 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%124 = call i32 @strcat(i8* %122, i8* %123)
br label %125
125: ; preds = %113, %101
br label %126
126: ; preds = %125, %81
br label %127
127: ; preds = %126, %63
br label %14
128: ; preds = %14
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%%%02x\00"
define void @MakeHexEncodeFromStr(i8* %0, i8* nocapture %1, i32 %2) {
%4 = alloca [5 x i8]
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
store i8 0, i8* %0
%6 = load i8, i8* %1
%7 = icmp eq i8 %6, 0
br i1 %7, label %52, label %8
8: ; preds = %3
%9 = icmp eq i32 %2, 0
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 1
br label %11
11: ; preds = %8, %46
%12 = phi i8 [ %6, %8 ], [ %50, %46 ]
%13 = phi i32 [ 0, %8 ], [ %47, %46 ]
br i1 %9, label %14, label %18
14: ; preds = %11
%15 = zext i8 %12 to i32
%16 = and i32 %15, 128
%17 = icmp eq i32 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %14, %11
%19 = add nsw i32 %13, 1
%20 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %12)
%21 = call i32 @strcat(i8* %0, i8* %5)
%22 = sext i32 %19 to i64
%23 = getelementptr inbounds i8, i8* %1, i64 %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %46, label %26
26: ; preds = %18
%27 = add nsw i32 %13, 2
%28 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %24)
br label %43
29: ; preds = %14
%30 = call i32 @isalnum(i32 %15)
%31 = icmp ne i32 %30, 0
%32 = icmp eq i8 %12, 47
%33 = select i1 %31, i1 true, i1 %32
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = add nsw i32 %13, 1
%36 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %12)
br label %43
37: ; preds = %29
%38 = icmp eq i8 %12, 38
%39 = add nsw i32 %13, 1
br i1 %38, label %40, label %42
40: ; preds = %37
%41 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext 38)
br label %43
42: ; preds = %37
store i8 %12, i8* %5
store i8 0, i8* %10
br label %43
43: ; preds = %26, %40, %42, %34
%44 = phi i32 [ %35, %34 ], [ %39, %42 ], [ %39, %40 ], [ %27, %26 ]
%45 = call i32 @strcat(i8* %0, i8* %5)
br label %46
46: ; preds = %43, %18
%47 = phi i32 [ %19, %18 ], [ %44, %43 ]
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %1, i64 %48
%50 = load i8, i8* %49
%51 = icmp eq i8 %50, 0
br i1 %51, label %52, label %11
52: ; preds = %46, %3
ret void
}