#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, unsigned int, char, unsigned int,
                ...);
int /*<<< orphan*/ free(char *);
scalar_t__ mem_sz2(unsigned int, unsigned int, unsigned int, unsigned int);
char *cat_mem_ctl(unsigned inputs, unsigned period, unsigned n_mem,
                  unsigned mem_sz, char write_class, char read_class) {
  char *s = NULL;
  unsigned i;
  for (i = 0; i < inputs * n_mem; ++i) {
    unsigned a = i / n_mem, b = i % n_mem;
    if (catf(&s,
             "        if(`enabled(%5$d)) begin\n"
             "          en_%1$d_%2$d_%3$c <= 1;\n"
             "          in_%1$d_%2$d_%3$c <= "
             "inputs_2[`in_from(%5$d)];\n"
             "          addr_%1$d_%2$d_%3$c <= wraddr_%1$d_%2$d;\n"
             "          wraddr_%1$d_%2$d <= wraddr_%1$d_%2$d "
             "== %4$d ? 0 : wraddr_%1$d_%2$d + 1;\n"
             "        end\n"
             "        else begin\n"
             "          en_%1$d_%2$d_%3$c <= 0;\n"
             "        end\n",
             a, b, write_class, mem_sz2(mem_sz, n_mem, period, i) - 1, i,
             0) < 0)
      goto error;
  }
  for (i = 0; i < inputs * n_mem; ++i) {
    unsigned a = i / n_mem, b = i % n_mem;
    if (catf(&s,
             "        en_%1$d_%2$d_%3$c <= 1;\n"
             "        addr_%1$d_%2$d_%3$c <= `raddr(%1$d);\n",
             a, b, read_class, i) < 0)
      goto error;
  }
  return s;
error:
  free(s);
  return NULL;
}


x86 assembly (clang, -O0):
<cat_mem_ctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %r8d,-0x4c(%rbp)
mov    %ecx,%eax
mov    -0x4c(%rbp),%ecx
mov    %eax,-0x48(%rbp)
mov    %edx,%eax
mov    -0x48(%rbp),%edx
mov    %eax,-0x44(%rbp)
mov    %esi,%eax
mov    -0x44(%rbp),%esi
mov    %eax,-0x40(%rbp)
mov    %edi,%r8d
mov    -0x40(%rbp),%edi
mov    %r9b,%al
mov    %r8d,-0xc(%rbp)
mov    %edi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %cl,-0x19(%rbp)
mov    %al,-0x1a(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0xc(%rbp),%ecx
imul   -0x14(%rbp),%ecx
cmp    %ecx,%eax
jae    1224 <cat_mem_ctl+0xf4>
mov    -0x2c(%rbp),%eax
xor    %edx,%edx
divl   -0x14(%rbp)
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
xor    %edx,%edx
divl   -0x14(%rbp)
mov    %edx,-0x34(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x19(%rbp),%al
mov    %al,-0x4d(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x2c(%rbp),%ecx
call   1040 <mem_sz2@plt>
mov    -0x58(%rbp),%edx
mov    -0x54(%rbp),%ecx
mov    -0x4d(%rbp),%r8b
sub    $0x1,%rax
mov    %eax,%r9d
mov    -0x2c(%rbp),%eax
lea    -0x28(%rbp),%rdi
lea    0xe17(%rip),%rsi
xor    %r10d,%r10d
movsbl %r8b,%r8d
mov    %eax,(%rsp)
movl   $0x0,0x8(%rsp)
mov    $0x0,%al
call   1050 <catf@plt>
cmp    $0x0,%rax
jge    1211 <cat_mem_ctl+0xe1>
jmp    12a5 <cat_mem_ctl+0x175>
jmp    1216 <cat_mem_ctl+0xe6>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    117f <cat_mem_ctl+0x4f>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0xc(%rbp),%ecx
imul   -0x14(%rbp),%ecx
cmp    %ecx,%eax
jae    1298 <cat_mem_ctl+0x168>
mov    -0x2c(%rbp),%eax
xor    %edx,%edx
divl   -0x14(%rbp)
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
xor    %edx,%edx
divl   -0x14(%rbp)
mov    %edx,-0x3c(%rbp)
mov    -0x38(%rbp),%edx
mov    -0x3c(%rbp),%ecx
mov    -0x1a(%rbp),%al
mov    -0x2c(%rbp),%r9d
lea    -0x28(%rbp),%rdi
lea    0xee5(%rip),%rsi
movsbl %al,%r8d
mov    $0x0,%al
call   1050 <catf@plt>
cmp    $0x0,%rax
jge    1285 <cat_mem_ctl+0x155>
jmp    12a5 <cat_mem_ctl+0x175>
jmp    128a <cat_mem_ctl+0x15a>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    122b <cat_mem_ctl+0xfb>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b6 <cat_mem_ctl+0x186>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_mem_ctl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x14(%rsp)
mov    %esi,0x10(%rsp)
mov    %edi,%r13d
movq   $0x0,(%rsp)
imul   %edx,%r13d
test   %r13d,%r13d
je     120a <cat_mem_ctl+0xda>
mov    %edx,%ebx
mov    %r9d,0xc(%rsp)
movsbl %r8b,%ebp
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r14d,%eax
xor    %edx,%edx
div    %ebx
mov    %edx,%r15d
mov    %eax,%r12d
mov    0x14(%rsp),%edi
mov    %ebx,%esi
mov    0x10(%rsp),%edx
mov    %r14d,%ecx
call   1040 <mem_sz2@plt>
lea    -0x1(%rax),%r9d
mov    %rsp,%rdi
lea    0xe63(%rip),%rsi
mov    %r12d,%edx
mov    %r15d,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
push   $0x0
push   %r14
call   1050 <catf@plt>
add    $0x10,%rsp
test   %rax,%rax
js     1210 <cat_mem_ctl+0xe0>
add    $0x1,%r14d
cmp    %r14d,%r13d
jne    1170 <cat_mem_ctl+0x40>
test   %r13d,%r13d
mov    0xc(%rsp),%eax
je     120a <cat_mem_ctl+0xda>
movsbl %al,%r12d
lea    0xf79(%rip),%r14
mov    %rsp,%r15
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %ebp,%eax
xor    %edx,%edx
div    %ebx
mov    %edx,%ecx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
mov    %r12d,%r8d
mov    %ebp,%r9d
xor    %eax,%eax
call   1050 <catf@plt>
test   %rax,%rax
js     1210 <cat_mem_ctl+0xe0>
add    $0x1,%ebp
cmp    %ebp,%r13d
jne    11e0 <cat_mem_ctl+0xb0>
mov    (%rsp),%rax
jmp    121b <cat_mem_ctl+0xeb>
mov    (%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [336 x i8] c" if(`enabled(%5$d)) begin\0A en_%1$d_%2$d_%3$c <= 1;\0A in_%1$d_%2$d_%3$c <= inputs_2[`in_from(%5$d)];\0A addr_%1$d_%2$d_%3$c <= wraddr_%1$d_%2$d;\0A wraddr_%1$d_%2$d <= wraddr_%1$d_%2$d == %4$d ? 0 : wraddr_%1$d_%2$d + 1;\0A end\0A else begin\0A en_%1$d_%2$d_%3$c <= 0;\0A end\0A\00"
@.str.1 = [78 x i8] c" en_%1$d_%2$d_%3$c <= 1;\0A addr_%1$d_%2$d_%3$c <= `raddr(%1$d);\0A\00"
define i8* @cat_mem_ctl(i32 %0, i32 %1, i32 %2, i32 %3, i8 signext %4, i8 signext %5) {
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
%13 = alloca i8
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i8 %4, i8* %12
store i8 %5, i8* %13
store i8* null, i8** %14
store i32 0, i32* %15
br label %20
20: ; preds = %48, %6
%21 = load i32, i32* %15
%22 = load i32, i32* %8
%23 = load i32, i32* %10
%24 = mul i32 %22, %23
%25 = icmp ult i32 %21, %24
br i1 %25, label %26, label %51
26: ; preds = %20
%27 = load i32, i32* %15
%28 = load i32, i32* %10
%29 = udiv i32 %27, %28
store i32 %29, i32* %16
%30 = load i32, i32* %15
%31 = load i32, i32* %10
%32 = urem i32 %30, %31
store i32 %32, i32* %17
%33 = load i32, i32* %16
%34 = load i32, i32* %17
%35 = load i8, i8* %12
%36 = load i32, i32* %11
%37 = load i32, i32* %10
%38 = load i32, i32* %9
%39 = load i32, i32* %15
%40 = call i64 @mem_sz2(i32 %36, i32 %37, i32 %38, i32 %39)
%41 = sub nsw i64 %40, 1
%42 = trunc i64 %41 to i32
%43 = load i32, i32* %15
%44 = call i64 (i8**, i8*, i32, i32, i8, i32, ...) @catf(i8** %14, i8* getelementptr inbounds ([336 x i8], [336 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %34, i8 signext %35, i32 %42, i32 %43, i32 0)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %26
br label %78
47: ; preds = %26
br label %48
48: ; preds = %47
%49 = load i32, i32* %15
%50 = add i32 %49, 1
store i32 %50, i32* %15
br label %20
51: ; preds = %20
store i32 0, i32* %15
br label %52
52: ; preds = %73, %51
%53 = load i32, i32* %15
%54 = load i32, i32* %8
%55 = load i32, i32* %10
%56 = mul i32 %54, %55
%57 = icmp ult i32 %53, %56
br i1 %57, label %58, label %76
58: ; preds = %52
%59 = load i32, i32* %15
%60 = load i32, i32* %10
%61 = udiv i32 %59, %60
store i32 %61, i32* %18
%62 = load i32, i32* %15
%63 = load i32, i32* %10
%64 = urem i32 %62, %63
store i32 %64, i32* %19
%65 = load i32, i32* %18
%66 = load i32, i32* %19
%67 = load i8, i8* %13
%68 = load i32, i32* %15
%69 = call i64 (i8**, i8*, i32, i32, i8, i32, ...) @catf(i8** %14, i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.1, i64 0, i64 0), i32 %65, i32 %66, i8 signext %67, i32 %68)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %58
br label %78
72: ; preds = %58
br label %73
73: ; preds = %72
%74 = load i32, i32* %15
%75 = add i32 %74, 1
store i32 %75, i32* %15
br label %52
76: ; preds = %52
%77 = load i8*, i8** %14
store i8* %77, i8** %7
br label %81
78: ; preds = %71, %46
%79 = load i8*, i8** %14
%80 = call i32 @free(i8* %79)
store i8* null, i8** %7
br label %81
81: ; preds = %78, %76
%82 = load i8*, i8** %7
ret i8* %82
}

LLVM IR (-O2):
@.str = [336 x i8] c" if(`enabled(%5$d)) begin\0A en_%1$d_%2$d_%3$c <= 1;\0A in_%1$d_%2$d_%3$c <= inputs_2[`in_from(%5$d)];\0A addr_%1$d_%2$d_%3$c <= wraddr_%1$d_%2$d;\0A wraddr_%1$d_%2$d <= wraddr_%1$d_%2$d == %4$d ? 0 : wraddr_%1$d_%2$d + 1;\0A end\0A else begin\0A en_%1$d_%2$d_%3$c <= 0;\0A end\0A\00"
@.str.1 = [78 x i8] c" en_%1$d_%2$d_%3$c <= 1;\0A addr_%1$d_%2$d_%3$c <= `raddr(%1$d);\0A\00"
define i8* @cat_mem_ctl(i32 %0, i32 %1, i32 %2, i32 %3, i8 signext %4, i8 signext %5) {
%7 = alloca i8*
%8 = bitcast i8** %7 to i8*
store i8* null, i8** %7
%9 = mul i32 %2, %0
%10 = icmp eq i32 %9, 0
br i1 %10, label %33, label %14
11: ; preds = %14
%12 = icmp eq i32 %23, %9
br i1 %12, label %13, label %14
13: ; preds = %11
br i1 %10, label %33, label %26
14: ; preds = %6, %11
%15 = phi i32 [ %23, %11 ], [ 0, %6 ]
%16 = udiv i32 %15, %2
%17 = urem i32 %15, %2
%18 = call i64 @mem_sz2(i32 %3, i32 %2, i32 %1, i32 %15)
%19 = trunc i64 %18 to i32
%20 = add i32 %19, -1
%21 = call i64 (i8**, i8*, i32, i32, i8, i32, ...) @catf(i8** %7, i8* getelementptr inbounds ([336 x i8], [336 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17, i8 signext %4, i32 %20, i32 %15, i32 0)
%22 = icmp slt i64 %21, 0
%23 = add nuw i32 %15, 1
br i1 %22, label %35, label %11
24: ; preds = %26
%25 = icmp eq i32 %32, %9
br i1 %25, label %33, label %26
26: ; preds = %13, %24
%27 = phi i32 [ %32, %24 ], [ 0, %13 ]
%28 = udiv i32 %27, %2
%29 = urem i32 %27, %2
%30 = call i64 (i8**, i8*, i32, i32, i8, i32, ...) @catf(i8** %7, i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29, i8 signext %5, i32 %27)
%31 = icmp slt i64 %30, 0
%32 = add nuw i32 %27, 1
br i1 %31, label %35, label %24
33: ; preds = %24, %6, %13
%34 = load i8*, i8** %7
br label %38
35: ; preds = %14, %26
%36 = load i8*, i8** %7
%37 = call i32 @free(i8* %36)
br label %38
38: ; preds = %35, %33
%39 = phi i8* [ null, %35 ], [ %34, %33 ]
ret i8* %39
}