#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__ catf(char **, char *, unsigned int, char *, unsigned int,
                scalar_t__);
scalar_t__ ulog2(unsigned int);
int cat_block_memory(char **s, unsigned width, unsigned length, char *name) {
  if (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",
           width - 1, name, length - 1, ulog2(length - 1) - 1) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<cat_block_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%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
lea    0xe80(%rip),%rsi
call   1040 <catf@plt>
cmp    $0x0,%rax
jge    119b <cat_block_memory+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <cat_block_memory+0x82>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_block_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
call   1040 <catf@plt>
sar    $0x3f,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [278 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\00"
define i32 @cat_block_memory(i8** %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8** %0, i8*** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%10 = load i8**, i8*** %6
%11 = load i32, i32* %7
%12 = sub i32 %11, 1
%13 = load i8*, i8** %9
%14 = load i32, i32* %8
%15 = sub i32 %14, 1
%16 = load i32, i32* %8
%17 = sub i32 %16, 1
%18 = call i64 @ulog2(i32 %17)
%19 = sub nsw i64 %18, 1
%20 = call i64 @catf(i8** %10, i8* getelementptr inbounds ([278 x i8], [278 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13, i32 %15, i64 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %4
store i32 -1, i32* %5
br label %24
23: ; preds = %4
store i32 0, i32* %5
br label %24
24: ; preds = %23, %22
%25 = load i32, i32* %5
ret i32 %25
}

LLVM IR (-O2):
@.str = [278 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\00"
define i32 @cat_block_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 i64 @catf(i8** %0, i8* getelementptr inbounds ([278 x i8], [278 x i8]* @.str, i64 0, i64 0), i32 %5, i8* %3, i32 %6, i64 %8)
%10 = ashr i64 %9, 63
%11 = trunc i64 %10 to i32
ret i32 %11
}