#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 catf(char **, char *, unsigned int, char *, unsigned int, scalar_t__,
         char *);
scalar_t__ ulog2(unsigned int);
int cat_data_memory(char **s, unsigned width, unsigned length, char *name,
                    char *class) {
  return catf(s,
              "reg [%1$d:0] memory_%2$s[0:%3$d];\n"
              "reg en_%2$s;\n"
              "reg [%4$d:0] addr_%2$s;\n"
              "reg [%1$d:0] in_%2$s;\n"
              "reg [%1$d:0] out_%2$s;\n"
              "always @(posedge clk) begin\n"
              "\tif(enable_data_memories) begin\n"
              "\t\tif(we_%5$s) memory_%2$s[addr_%2$s] <= in_%2$s;\n"
              "\t\telse out_%2$s <= memory_%2$s[addr_%2$s];\n"
              "\tend\n"
              "end\n"
              "\n",
              width - 1, name, length - 1,
              length > 1 ? ulog2(length - 1) - 1 : 0, class);
}


x86 assembly (clang, -O0):
<cat_data_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x10(%rbp)
jbe    117e <cat_data_memory+0x5e>
mov    -0x10(%rbp),%edi
sub    $0x1,%edi
call   1030 <ulog2@plt>
sub    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1189 <cat_data_memory+0x69>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    1189 <cat_data_memory+0x69>
mov    -0x24(%rbp),%r8d
mov    -0x30(%rbp),%rcx
mov    -0x34(%rbp),%edx
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%r9
mov    -0x20(%rbp),%rax
lea    0xe59(%rip),%rsi
mov    %rax,(%rsp)
call   1040 <catf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_data_memory>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r15
mov    %rcx,%r14
mov    %esi,%ebp
mov    %rdi,%r12
lea    -0x1(%rdx),%ebx
cmp    $0x2,%edx
jb     114f <cat_data_memory+0x2f>
mov    %ebx,%edi
call   1030 <ulog2@plt>
mov    %rax,%r9
add    $0xffffffffffffffff,%r9
jmp    1152 <cat_data_memory+0x32>
xor    %r9d,%r9d
add    $0xffffffff,%ebp
mov    %r15,(%rsp)
lea    0xea0(%rip),%rsi
mov    %r12,%rdi
mov    %ebp,%edx
mov    %r14,%rcx
mov    %ebx,%r8d
call   1040 <catf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [279 x i8] c"reg [%1$d:0] memory_%2$s[0:%3$d];\0Areg en_%2$s;\0Areg [%4$d:0] addr_%2$s;\0Areg [%1$d:0] in_%2$s;\0Areg [%1$d:0] out_%2$s;\0Aalways @(posedge clk) begin\0A\09if(enable_data_memories) begin\0A\09\09if(we_%5$s) memory_%2$s[addr_%2$s] <= in_%2$s;\0A\09\09else out_%2$s <= memory_%2$s[addr_%2$s];\0A\09end\0Aend\0A\0A\00"
define i32 @cat_data_memory(i8** %0, i32 %1, i32 %2, i8* %3, i8* %4) {
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i8** %0, i8*** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%11 = load i8**, i8*** %6
%12 = load i32, i32* %7
%13 = sub i32 %12, 1
%14 = load i8*, i8** %9
%15 = load i32, i32* %8
%16 = sub i32 %15, 1
%17 = load i32, i32* %8
%18 = icmp ugt i32 %17, 1
br i1 %18, label %19, label %24
19: ; preds = %5
%20 = load i32, i32* %8
%21 = sub i32 %20, 1
%22 = call i64 @ulog2(i32 %21)
%23 = sub nsw i64 %22, 1
br label %25
24: ; preds = %5
br label %25
25: ; preds = %24, %19
%26 = phi i64 [ %23, %19 ], [ 0, %24 ]
%27 = load i8*, i8** %10
%28 = call i32 @catf(i8** %11, i8* getelementptr inbounds ([279 x i8], [279 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %14, i32 %16, i64 %26, i8* %27)
ret i32 %28
}

LLVM IR (-O2):
@.str = [279 x i8] c"reg [%1$d:0] memory_%2$s[0:%3$d];\0Areg en_%2$s;\0Areg [%4$d:0] addr_%2$s;\0Areg [%1$d:0] in_%2$s;\0Areg [%1$d:0] out_%2$s;\0Aalways @(posedge clk) begin\0A\09if(enable_data_memories) begin\0A\09\09if(we_%5$s) memory_%2$s[addr_%2$s] <= in_%2$s;\0A\09\09else out_%2$s <= memory_%2$s[addr_%2$s];\0A\09end\0Aend\0A\0A\00"
define i32 @cat_data_memory(i8** %0, i32 %1, i32 %2, i8* %3, i8* %4) {
%6 = add i32 %2, -1
%7 = icmp ugt i32 %2, 1
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i64 @ulog2(i32 %6)
%10 = add nsw i64 %9, -1
br label %11
11: ; preds = %5, %8
%12 = phi i64 [ %10, %8 ], [ 0, %5 ]
%13 = add i32 %1, -1
%14 = tail call i32 @catf(i8** %0, i8* getelementptr inbounds ([279 x i8], [279 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %3, i32 %6, i64 %12, i8* %4)
ret i32 %14
}