#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *set_template_var(char *, char const *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *render_template(const char *template_data, int len, const char *keys[],
                      const char *values[]) {
  int template_length = strlen(template_data) + 1;
  char *template = malloc(template_length);
  memset(template, 0, template_length);
  strcpy(template, template_data);
  if (template != NULL) {
    int x = 0;
    for (x = 0; x < len; ++x) {
      char *processed = set_template_var(template, keys[x], values[x]);
      free(template);
      template = processed;
    }
  }
  return template;
}


x86 assembly (clang, -O0):
<render_template>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
cmpq   $0x0,-0x30(%rbp)
je     121b <render_template+0xbb>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1216 <render_template+0xb6>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
call   1070 <set_template_var@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11c6 <render_template+0x66>
jmp    121b <render_template+0xbb>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<render_template>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,0x4(%rsp)
mov    %rdi,%rbp
call   1050 <strlen@plt>
lea    0x1(%rax),%r13d
mov    %r13d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
test   %rbx,%rbx
je     11ec <render_template+0x8c>
mov    0x4(%rsp),%eax
test   %eax,%eax
jle    11e9 <render_template+0x89>
mov    %eax,%r13d
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    (%r15,%rbp,8),%rsi
mov    (%r14,%rbp,8),%rdx
mov    %rbx,%rdi
call   1070 <set_template_var@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x1,%rbp
mov    %r12,%rbx
cmp    %rbp,%r13
jne    11c0 <render_template+0x60>
jmp    11ec <render_template+0x8c>
mov    %rbx,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @render_template(i8* %0, i32 %1, i8** %2, i8** %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i8** %3, i8*** %8
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 1
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %10
%18 = load i8*, i8** %10
%19 = load i32, i32* %9
%20 = call i32 @memset(i8* %18, i32 0, i32 %19)
%21 = load i8*, i8** %10
%22 = load i8*, i8** %5
%23 = call i32 @strcpy(i8* %21, i8* %22)
%24 = load i8*, i8** %10
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %51
26: ; preds = %4
store i32 0, i32* %11
store i32 0, i32* %11
br label %27
27: ; preds = %47, %26
%28 = load i32, i32* %11
%29 = load i32, i32* %6
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %50
31: ; preds = %27
%32 = load i8*, i8** %10
%33 = load i8**, i8*** %7
%34 = load i32, i32* %11
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
%37 = load i8*, i8** %36
%38 = load i8**, i8*** %8
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = call i8* @set_template_var(i8* %32, i8* %37, i8* %42)
store i8* %43, i8** %12
%44 = load i8*, i8** %10
%45 = call i32 @free(i8* %44)
%46 = load i8*, i8** %12
store i8* %46, i8** %10
br label %47
47: ; preds = %31
%48 = load i32, i32* %11
%49 = add nsw i32 %48, 1
store i32 %49, i32* %11
br label %27
50: ; preds = %27
br label %51
51: ; preds = %50, %4
%52 = load i8*, i8** %10
ret i8* %52
}

LLVM IR (-O2):
define i8* @render_template(i8* %0, i32 %1, i8** nocapture %2, i8** nocapture %3) {
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 1
%7 = tail call i8* @malloc(i32 %6)
%8 = tail call i32 @memset(i8* %7, i32 0, i32 %6)
%9 = tail call i32 @strcpy(i8* %7, i8* %0)
%10 = icmp eq i8* %7, null
br i1 %10, label %26, label %11
11: ; preds = %4
%12 = icmp sgt i32 %1, 0
br i1 %12, label %13, label %26
13: ; preds = %11
%14 = zext i32 %1 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %24, %15 ]
%17 = phi i8* [ %7, %13 ], [ %22, %15 ]
%18 = getelementptr inbounds i8*, i8** %2, i64 %16
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8*, i8** %3, i64 %16
%21 = load i8*, i8** %20
%22 = tail call i8* @set_template_var(i8* %17, i8* %19, i8* %21)
%23 = tail call i32 @free(i8* %17)
%24 = add nuw nsw i64 %16, 1
%25 = icmp eq i64 %24, %14
br i1 %25, label %26, label %15
26: ; preds = %15, %11, %4
%27 = phi i8* [ null, %4 ], [ %7, %11 ], [ %22, %15 ]
ret i8* %27
}