#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"
           "\n",
           width - 1, name, length - 1,
           length > 1 ? ulog2(length - 1) - 1 : 0) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<cat_block_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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)
cmpl   $0x1,-0x18(%rbp)
jbe    117a <cat_block_memory+0x5a>
mov    -0x18(%rbp),%edi
sub    $0x1,%edi
call   1030 <ulog2@plt>
sub    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1185 <cat_block_memory+0x65>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    1185 <cat_block_memory+0x65>
mov    -0x24(%rbp),%r8d
mov    -0x30(%rbp),%rcx
mov    -0x34(%rbp),%edx
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%r9
lea    0xe61(%rip),%rsi
call   1040 <catf@plt>
cmp    $0x0,%rax
jge    11ba <cat_block_memory+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <cat_block_memory+0xa1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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    %esi,%ebp
mov    %rdi,%r15
lea    -0x1(%rdx),%ebx
cmp    $0x2,%edx
jb     1147 <cat_block_memory+0x27>
mov    %ebx,%edi
call   1030 <ulog2@plt>
mov    %rax,%r9
add    $0xffffffffffffffff,%r9
jmp    114a <cat_block_memory+0x2a>
xor    %r9d,%r9d
add    $0xffffffff,%ebp
lea    0xeac(%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 = [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_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 = icmp ugt i32 %16, 1
br i1 %17, label %18, label %23
18: ; preds = %4
%19 = load i32, i32* %8
%20 = sub i32 %19, 1
%21 = call i64 @ulog2(i32 %20)
%22 = sub nsw i64 %21, 1
br label %24
23: ; preds = %4
br label %24
24: ; preds = %23, %18
%25 = phi i64 [ %22, %18 ], [ 0, %23 ]
%26 = call i64 @catf(i8** %10, i8* getelementptr inbounds ([279 x i8], [279 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13, i32 %15, i64 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %5
br label %30
29: ; preds = %24
store i32 0, i32* %5
br label %30
30: ; preds = %29, %28
%31 = load i32, i32* %5
ret i32 %31
}

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_block_memory(i8** %0, i32 %1, i32 %2, i8* %3) {
%5 = add i32 %2, -1
%6 = icmp ugt i32 %2, 1
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i64 @ulog2(i32 %5)
%9 = add nsw i64 %8, -1
br label %10
10: ; preds = %4, %7
%11 = phi i64 [ %9, %7 ], [ 0, %4 ]
%12 = add i32 %1, -1
%13 = tail call i64 @catf(i8** %0, i8* getelementptr inbounds ([279 x i8], [279 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %3, i32 %5, i64 %11)
%14 = ashr i64 %13, 63
%15 = trunc i64 %14 to i32
ret i32 %15
}