#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*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char, int /*<<< orphan*/);
int strlen(char *);
char *encode(char *str) {
  static char *query;
  char *ptr;
  int nlen, i;
  nlen = strlen(str) * 3;
  if ((query = (char *)malloc(nlen)) == NULL) {
    perror("malloc");
    return NULL;
  } else
    ptr = str;
  for (i = 0; i < nlen; i += 3)
    sprintf(&query[i], "%c%X", '%', *ptr++);
  query[nlen] = '\0';
  return query;
}


x86 assembly (clang, -O0):
<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
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,0x2ede(%rip)
cmp    $0x0,%rax
jne    118d <encode+0x4d>
lea    0xe85(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1201 <encode+0xc1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e7 <encode+0xa7>
mov    0x2e99(%rip),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl (%rax),%ecx
lea    0xe38(%rip),%rsi
mov    $0x25,%edx
call   1060 <sprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x20(%rbp)
jmp    119c <encode+0x5c>
mov    0x2e5a(%rip),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e4b(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ebp
cltq
lea    (%rax,%rax,2),%r14
mov    %r14d,%edi
call   1040 <malloc@plt>
mov    %rax,0x2ee2(%rip)
test   %rax,%rax
je     11de <encode+0x9e>
test   %ebp,%ebp
jle    11ce <encode+0x8e>
movsbl (%rbx),%ecx
lea    0xe8e(%rip),%rsi
mov    %rax,%rdi
mov    $0x25,%edx
call   1060 <sprintf@plt>
cmp    $0x4,%r14d
jl     11c7 <encode+0x87>
add    $0x1,%rbx
mov    $0x3,%ebp
lea    0xe6b(%rip),%r15
nopl   0x0(%rax)
mov    0x2ea1(%rip),%rdi
add    %rbp,%rdi
movsbl (%rbx),%ecx
mov    %r15,%rsi
mov    $0x25,%edx
call   1060 <sprintf@plt>
add    $0x3,%rbp
add    $0x1,%rbx
cmp    %ebp,%r14d
jg     11a0 <encode+0x60>
mov    0x2e7a(%rip),%rax
movb   $0x0,(%rax,%r14,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1b(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    11d3 <encode+0x93>

LLVM IR (-O0):
@encode.query = internal global i8* null
@.str = [7 x i8] c"malloc\00"
@.str.1 = [5 x i8] c"%c%X\00"
define i8* @encode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = mul nsw i32 %8, 3
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** @encode.query
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %42
16: ; preds = %1
%17 = load i8*, i8** %3
store i8* %17, i8** %4
br label %18
18: ; preds = %16
store i32 0, i32* %6
br label %19
19: ; preds = %33, %18
%20 = load i32, i32* %6
%21 = load i32, i32* %5
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %36
23: ; preds = %19
%24 = load i8*, i8** @encode.query
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %4
%30 = load i8, i8* %28
%31 = sext i8 %30 to i32
%32 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 signext 37, i32 %31)
br label %33
33: ; preds = %23
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 3
store i32 %35, i32* %6
br label %19
36: ; preds = %19
%37 = load i8*, i8** @encode.query
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** @encode.query
store i8* %41, i8** %2
br label %42
42: ; preds = %36, %14
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@encode.query = internal unnamed_addr global i8* null
@.str = [7 x i8] c"malloc\00"
@.str.1 = [5 x i8] c"%c%X\00"
define i8* @encode(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = mul nsw i32 %2, 3
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
store i8* %5, i8** @encode.query
%6 = icmp eq i64 %4, 0
br i1 %6, label %14, label %7
7: ; preds = %1
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %30
9: ; preds = %7
%10 = load i8, i8* %0
%11 = sext i8 %10 to i32
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 signext 37, i32 %11)
%13 = icmp sgt i32 %3, 3
br i1 %13, label %16, label %28
14: ; preds = %1
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %34
16: ; preds = %9, %16
%17 = phi i64 [ %25, %16 ], [ 3, %9 ]
%18 = phi i8* [ %19, %16 ], [ %0, %9 ]
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = load i8*, i8** @encode.query
%21 = getelementptr inbounds i8, i8* %20, i64 %17
%22 = load i8, i8* %19
%23 = sext i8 %22 to i32
%24 = tail call i32 @sprintf(i8* dereferenceable(1) %21, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 signext 37, i32 %23)
%25 = add nuw i64 %17, 3
%26 = trunc i64 %25 to i32
%27 = icmp sgt i32 %3, %26
br i1 %27, label %16, label %28
28: ; preds = %16, %9
%29 = load i8*, i8** @encode.query
br label %30
30: ; preds = %28, %7
%31 = phi i8* [ %29, %28 ], [ %5, %7 ]
%32 = sext i32 %3 to i64
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %30, %14
%35 = phi i8* [ null, %14 ], [ %31, %30 ]
ret i8* %35
}