#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__);
scalar_t__ ulog2(unsigned int);
int cat_pattern_memory(char **s, unsigned width, unsigned length, char *name) {
  return catf(s,
              "reg [%1$d:0] memory_%2$s[0:%3$d];\n"
              "reg en_%2$s;\n"
              "reg we_%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(en_%2$s) begin\n"
              "\t\tif(we_%2$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);
}


x86 assembly (clang, -O0):
<cat_pattern_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%edi
sub    $0x1,%edi
call   1030 <ulog2@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    %rax,%r9
sub    $0x1,%r9
lea    0xe80(%rip),%rsi
call   1040 <catf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_pattern_memory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdi,%r15
lea    -0x1(%rsi),%ebp
lea    -0x1(%rdx),%ebx
mov    %ebx,%edi
call   1030 <ulog2@plt>
lea    -0x1(%rax),%r9
lea    0xebb(%rip),%rsi
mov    %r15,%rdi
mov    %ebp,%edx
mov    %r14,%rcx
mov    %ebx,%r8d
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <catf@plt>

LLVM IR (-O0):
@.str = [279 x i8] c"reg [%1$d:0] memory_%2$s[0:%3$d];\0Areg en_%2$s;\0Areg we_%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(en_%2$s) begin\0A\09\09if(we_%2$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_pattern_memory(i8** %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%9 = load i8**, i8*** %5
%10 = load i32, i32* %6
%11 = sub i32 %10, 1
%12 = load i8*, i8** %8
%13 = load i32, i32* %7
%14 = sub i32 %13, 1
%15 = load i32, i32* %7
%16 = sub i32 %15, 1
%17 = call i64 @ulog2(i32 %16)
%18 = sub nsw i64 %17, 1
%19 = call i32 @catf(i8** %9, i8* getelementptr inbounds ([279 x i8], [279 x i8]* @.str, i64 0, i64 0), i32 %11, i8* %12, i32 %14, i64 %18)
ret i32 %19
}

LLVM IR (-O2):
@.str = [279 x i8] c"reg [%1$d:0] memory_%2$s[0:%3$d];\0Areg en_%2$s;\0Areg we_%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(en_%2$s) begin\0A\09\09if(we_%2$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_pattern_memory(i8** %0, i32 %1, i32 %2, i8* %3) {
%5 = add i32 %1, -1
%6 = add i32 %2, -1
%7 = tail call i64 @ulog2(i32 %6)
%8 = add nsw i64 %7, -1
%9 = tail call i32 @catf(i8** %0, i8* getelementptr inbounds ([279 x i8], [279 x i8]* @.str, i64 0, i64 0), i32 %5, i8* %3, i32 %6, i64 %8)
ret i32 %9
}