#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 [%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, ulog2(length - 1) - 1, 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)
mov    -0x10(%rbp),%edi
sub    $0x1,%edi
call   1030 <ulog2@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    -0x24(%rbp),%r8d
mov    %rax,%r9
sub    $0x1,%r9
mov    -0x20(%rbp),%rax
lea    0xe78(%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    %rdi,%r12
lea    -0x1(%rsi),%ebp
lea    -0x1(%rdx),%ebx
mov    %ebx,%edi
call   1030 <ulog2@plt>
lea    -0x1(%rax),%r9
mov    %r15,(%rsp)
lea    0xeaf(%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 = [266 x i8] c"reg [%1$d:0] memory_%2$s[0:%3$d];\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 = sub i32 %17, 1
%19 = call i64 @ulog2(i32 %18)
%20 = sub nsw i64 %19, 1
%21 = load i8*, i8** %10
%22 = call i32 @catf(i8** %11, i8* getelementptr inbounds ([266 x i8], [266 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %14, i32 %16, i64 %20, i8* %21)
ret i32 %22
}

LLVM IR (-O2):
@.str = [266 x i8] c"reg [%1$d:0] memory_%2$s[0:%3$d];\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 %1, -1
%7 = add i32 %2, -1
%8 = tail call i64 @ulog2(i32 %7)
%9 = add nsw i64 %8, -1
%10 = tail call i32 @catf(i8** %0, i8* getelementptr inbounds ([266 x i8], [266 x i8]* @.str, i64 0, i64 0), i32 %6, i8* %3, i32 %7, i64 %9, i8* %4)
ret i32 %10
}