#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__ append_pattern_shifts(char **, unsigned int, unsigned int,
                                 unsigned int, char *, unsigned int *);
scalar_t__ catf(char **, char *, ...);
int /*<<< orphan*/ get_n_switches(unsigned int, unsigned int *, unsigned int *);
int ulog2(unsigned int);
int cat_pattern_task(char **s, char *name, unsigned number, unsigned inputs,
                     unsigned period) {
  unsigned depth, n_switches;
  get_n_switches(inputs, &n_switches, &depth);
  unsigned mem_addr_bits = period > 1 ? ulog2(period - 1) : 1;
  if (catf(s,
           "always @(posedge clk or posedge reset) \n"
           "begin : %1$s_task\n"
           "  integer i;\n"
           "  integer j;\n"
           "  integer k;\n"
           "  if(reset) begin\n"
           "    for(i = 0; i < %2$d; i = i + 1) %1$s[i] <= 0;\n"
           "    for(i = 0; i < %3$d; i = i + 1) begin\n"
           "      for(j = 0; j < %4$d; j = j + 1) begin\n"
           "        for(k = 0; k < %5$d; k = k + 1) begin\n"
           "          %1$s[%2$d + i * %4$d * %5$d + j * %5$d + k] <= "
           "((%4$d - 1 - j) >> k) & 1;\n"
           "        end\n"
           "      end\n"
           "    end\n"
           "    for(i = 0; i < %2$d; i = i + 1) %1$s[%6$d + i] <= 0;\n"
           "  end\n"
           "  else begin\n"
           "    if(pattern_valid && !updating_pattern && "
           "pattern_at[0] != %8$d) begin\n"
           "      for(i = 0; i < 8; i = i + 1) %1$s[i] <= "
           "pattern_data[i];\n"
           "      for(i = 8; i < %7$d; i = i + 1) %1$s[i] <= "
           "%1$s[i - 8];\n"
           "    end\n"
           "    else begin\n"
           "      if(in_valid) begin\n",
           name, n_switches * period, inputs, period, mem_addr_bits,
           n_switches * period + inputs * period * mem_addr_bits,
           2 * n_switches * period + inputs * period * mem_addr_bits,
           number) < 0)
    return -1;
  unsigned pos = 0;
  unsigned i;
  for (i = 0; i < depth; ++i) {
    if (catf(s,
             "        if(active[%1$d] && "
             "pattern_at[%1$d] == %2$d) begin\n",
             i, number) < 0)
      return -1;
    if (append_pattern_shifts(s, inputs, period, i, name, &pos) < 0)
      return -1;
    if (catf(s, "        end\n") < 0)
      return -1;
  }
  if (catf(s,
           "        /* The middle pattern is read one clock cycle in "
           "advance because of\n"
           "         * memory delays. */\n"
           "        if(memory_counter == %3$d ?\n"
           "            active[%2$d] && pattern_at[%2$d] == %4$d :\n"
           "            active[%1$d] && pattern_at[%1$d] == %4$d) begin\n",
           depth + 1, depth, period - 1, number) < 0)
    return -1;
  for (i = 0; i < inputs; ++i) {
    if (catf(s,
             "          for(i = 0; i < %2$d; i = i + 1) begin\n"
             "            for(j = 0; j < %3$d; j = j + 1) begin\n"
             "              %1$s[%4$d + i * %3$d + j] <= "
             "%1$s[%4$d + ((i + %5$d) %% %2$d) * %3$d + j];\n"
             "            end\n"
             "          end\n",
             name, period, mem_addr_bits, pos, period - 1) < 0)
      return -1;
    pos += period * mem_addr_bits;
  }
  if (catf(s, "        end\n"
              "      end\n"
              "      if(run_egress) begin\n") < 0)
    return -1;
  for (i = 0; i < depth; ++i) {
    if (catf(s,
             "        if(active[%1$d] && "
             "pattern_at[%1$d] == %2$d) begin\n",
             depth + 2 + i, number) < 0)
      return -1;
    if (append_pattern_shifts(s, inputs, period, i, name, &pos) < 0)
      return -1;
    if (catf(s, "        end\n") < 0)
      return -1;
  }
  if (catf(s, "      end\n"
              "    end\n"
              "  end\n"
              "end\n"
              "\n") < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<cat_pattern_task>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x68,%rsp
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
mov    %edx,-0x24(%rbp)
mov    %ecx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x34(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <get_n_switches@plt>
cmpl   $0x1,-0x2c(%rbp)
jbe    1188 <cat_pattern_task+0x48>
mov    -0x2c(%rbp),%edi
sub    $0x1,%edi
call   1050 <ulog2@plt>
mov    %eax,-0x44(%rbp)
jmp    1195 <cat_pattern_task+0x55>
mov    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1195 <cat_pattern_task+0x55>
mov    -0x44(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x34(%rbp),%ecx
imul   -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%r8d
mov    -0x2c(%rbp),%r9d
mov    -0x38(%rbp),%ebx
mov    -0x34(%rbp),%r11d
imul   -0x2c(%rbp),%r11d
mov    -0x28(%rbp),%eax
imul   -0x2c(%rbp),%eax
imul   -0x38(%rbp),%eax
add    %eax,%r11d
mov    -0x34(%rbp),%r10d
shl    $0x1,%r10d
imul   -0x2c(%rbp),%r10d
mov    -0x28(%rbp),%eax
imul   -0x2c(%rbp),%eax
imul   -0x38(%rbp),%eax
add    %eax,%r10d
mov    -0x24(%rbp),%eax
lea    0xe0f(%rip),%rsi
mov    %ebx,(%rsp)
mov    %r11d,0x8(%rsp)
mov    %r10d,0x10(%rsp)
mov    %eax,0x18(%rsp)
mov    $0x0,%al
call   1060 <catf@plt>
cmp    $0x0,%rax
jge    121f <cat_pattern_task+0xdf>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x30(%rbp),%eax
jae    12d2 <cat_pattern_task+0x192>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0x1095(%rip),%rsi
mov    $0x0,%al
call   1060 <catf@plt>
cmp    $0x0,%rax
jge    1267 <cat_pattern_task+0x127>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%r8
lea    -0x3c(%rbp),%r9
call   1030 <append_pattern_shifts@plt>
cmp    $0x0,%rax
jge    1297 <cat_pattern_task+0x157>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
mov    -0x18(%rbp),%rdi
lea    0x1247(%rip),%rsi
mov    $0x0,%al
call   1060 <catf@plt>
cmp    $0x0,%rax
jge    12bf <cat_pattern_task+0x17f>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
jmp    12c4 <cat_pattern_task+0x184>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    122d <cat_pattern_task+0xed>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
add    $0x1,%edx
mov    -0x30(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
sub    $0x1,%r8d
mov    -0x24(%rbp),%r9d
lea    0x1029(%rip),%rsi
mov    $0x0,%al
call   1060 <catf@plt>
cmp    $0x0,%rax
jge    130f <cat_pattern_task+0x1cf>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x28(%rbp),%eax
jae    137d <cat_pattern_task+0x23d>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    -0x38(%rbp),%r8d
mov    -0x3c(%rbp),%r9d
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
lea    0x10da(%rip),%rsi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1060 <catf@plt>
cmp    $0x0,%rax
jge    1362 <cat_pattern_task+0x222>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
mov    -0x2c(%rbp),%eax
imul   -0x38(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1316 <cat_pattern_task+0x1d6>
mov    -0x18(%rbp),%rdi
lea    0x116e(%rip),%rsi
mov    $0x0,%al
call   1060 <catf@plt>
cmp    $0x0,%rax
jge    13a5 <cat_pattern_task+0x265>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x30(%rbp),%eax
jae    1457 <cat_pattern_task+0x317>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
add    $0x2,%edx
add    -0x40(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xf10(%rip),%rsi
mov    $0x0,%al
call   1060 <catf@plt>
cmp    $0x0,%rax
jge    13ec <cat_pattern_task+0x2ac>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%r8
lea    -0x3c(%rbp),%r9
call   1030 <append_pattern_shifts@plt>
cmp    $0x0,%rax
jge    141c <cat_pattern_task+0x2dc>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
mov    -0x18(%rbp),%rdi
lea    0x10c2(%rip),%rsi
mov    $0x0,%al
call   1060 <catf@plt>
cmp    $0x0,%rax
jge    1444 <cat_pattern_task+0x304>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
jmp    1449 <cat_pattern_task+0x309>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    13ac <cat_pattern_task+0x26c>
mov    -0x18(%rbp),%rdi
lea    0x10c6(%rip),%rsi
mov    $0x0,%al
call   1060 <catf@plt>
cmp    $0x0,%rax
jge    147f <cat_pattern_task+0x33f>
movl   $0xffffffff,-0xc(%rbp)
jmp    1486 <cat_pattern_task+0x346>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_pattern_task>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %rdx,0x18(%rsp)
mov    %rsi,0x20(%rsp)
mov    %rdi,%r13
lea    0x2c(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %ecx,%edi
call   1040 <get_n_switches@plt>
mov    $0x1,%ebx
cmp    $0x2,%r14d
jb     1188 <cat_pattern_task+0x48>
lea    -0x1(%r14),%edi
call   1050 <ulog2@plt>
mov    %eax,%ebx
mov    0x2c(%rsp),%ecx
imul   %r14d,%ecx
mov    %ebx,%eax
imul   %r14d,%eax
mov    %eax,%r12d
imul   %r15d,%eax
lea    (%rcx,%rax,1),%r10d
lea    (%rax,%rcx,2),%r11d
lea    0xe54(%rip),%rsi
xor    %ebp,%ebp
mov    %r13,%rdi
mov    0x20(%rsp),%rdx
mov    %r15d,%r8d
mov    %r14d,%r9d
xor    %eax,%eax
push   0x18(%rsp)
push   %r11
push   %r10
push   %rbx
call   1060 <catf@plt>
add    $0x20,%rsp
test   %rax,%rax
js     13ba <cat_pattern_task+0x27a>
mov    %rbx,0x30(%rsp)
movl   $0x0,0xc(%rsp)
cmpl   $0x0,0x10(%rsp)
mov    %r12d,%ebx
je     1272 <cat_pattern_task+0x132>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
lea    0x10d5(%rip),%rsi
mov    %r12d,%edx
mov    0x18(%rsp),%rcx
xor    %eax,%eax
call   1060 <catf@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     13bf <cat_pattern_task+0x27f>
mov    %r13,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
mov    %r12d,%ecx
mov    0x20(%rsp),%r8
lea    0xc(%rsp),%r9
call   1030 <append_pattern_shifts@plt>
test   %rax,%rax
js     13bf <cat_pattern_task+0x27f>
mov    %r13,%rdi
lea    0x1294(%rip),%rsi
xor    %eax,%eax
call   1060 <catf@plt>
test   %rax,%rax
js     13bf <cat_pattern_task+0x27f>
add    $0x1,%r12d
mov    0x10(%rsp),%ebp
cmp    %ebp,%r12d
jb     1200 <cat_pattern_task+0xc0>
lea    0x1(%rbp),%edx
lea    -0x1(%r14),%r12d
lea    0x109b(%rip),%rsi
mov    %r13,%rdi
mov    %ebp,%ecx
mov    %r12d,%r8d
mov    0x18(%rsp),%r9
xor    %eax,%eax
call   1060 <catf@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     13bf <cat_pattern_task+0x27f>
mov    %r15d,0x14(%rsp)
test   %r15d,%r15d
je     1304 <cat_pattern_task+0x1c4>
mov    0xc(%rsp),%r9d
mov    0x14(%rsp),%r15d
cs nopw 0x0(%rax,%rax,1)
sub    $0x8,%rsp
mov    %r13,%rdi
lea    0x114e(%rip),%rsi
mov    0x28(%rsp),%rdx
mov    %r14d,%ecx
mov    0x38(%rsp),%r8
xor    %eax,%eax
push   %r12
call   1060 <catf@plt>
add    $0x10,%rsp
test   %rax,%rax
js     13bf <cat_pattern_task+0x27f>
mov    0xc(%rsp),%r9d
add    %ebx,%r9d
mov    %r9d,0xc(%rsp)
add    $0xffffffff,%r15d
jne    12c0 <cat_pattern_task+0x180>
lea    0x11eb(%rip),%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <catf@plt>
test   %rax,%rax
js     13bf <cat_pattern_task+0x27f>
mov    0x10(%rsp),%eax
test   %eax,%eax
je     13a0 <cat_pattern_task+0x260>
lea    0xfb2(%rip),%r15
lea    0xc(%rsp),%rbx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%rax,%r12,1),%edx
add    $0x2,%edx
mov    %r13,%rdi
mov    %r15,%rsi
mov    0x18(%rsp),%rcx
xor    %eax,%eax
call   1060 <catf@plt>
test   %rax,%rax
js     13bf <cat_pattern_task+0x27f>
mov    %r13,%rdi
mov    0x14(%rsp),%esi
mov    %r14d,%edx
mov    %r12d,%ecx
mov    0x20(%rsp),%r8
mov    %rbx,%r9
call   1030 <append_pattern_shifts@plt>
test   %rax,%rax
js     13bf <cat_pattern_task+0x27f>
mov    %r13,%rdi
lea    0x1162(%rip),%rsi
xor    %eax,%eax
call   1060 <catf@plt>
test   %rax,%rax
js     13bf <cat_pattern_task+0x27f>
add    $0x1,%r12d
mov    0x10(%rsp),%eax
cmp    %eax,%r12d
jb     1340 <cat_pattern_task+0x200>
lea    0x1181(%rip),%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <catf@plt>
mov    %rax,%rbp
sar    $0x3f,%rbp
jmp    13bf <cat_pattern_task+0x27f>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [735 x i8] c"always @(posedge clk or posedge reset) \0Abegin : %1$s_task\0A integer i;\0A integer j;\0A integer k;\0A if(reset) begin\0A for(i = 0; i < %2$d; i = i + 1) %1$s[i] <= 0;\0A for(i = 0; i < %3$d; i = i + 1) begin\0A for(j = 0; j < %4$d; j = j + 1) begin\0A for(k = 0; k < %5$d; k = k + 1) begin\0A %1$s[%2$d + i * %4$d * %5$d + j * %5$d + k] <= ((%4$d - 1 - j) >> k) & 1;\0A end\0A end\0A end\0A for(i = 0; i < %2$d; i = i + 1) %1$s[%6$d + i] <= 0;\0A end\0A else begin\0A if(pattern_valid && !updating_pattern && pattern_at[0] != %8$d) begin\0A for(i = 0; i < 8; i = i + 1) %1$s[i] <= pattern_data[i];\0A for(i = 8; i < %7$d; i = i + 1) %1$s[i] <= %1$s[i - 8];\0A end\0A else begin\0A if(in_valid) begin\0A\00"
@.str.1 = [60 x i8] c" if(active[%1$d] && pattern_at[%1$d] == %2$d) begin\0A\00"
@.str.2 = [13 x i8] c" end\0A\00"
@.str.3 = [257 x i8] c" /* The middle pattern is read one clock cycle in advance because of\0A * memory delays. */\0A if(memory_counter == %3$d ?\0A active[%2$d] && pattern_at[%2$d] == %4$d :\0A active[%1$d] && pattern_at[%1$d] == %4$d) begin\0A\00"
@.str.4 = [218 x i8] c" for(i = 0; i < %2$d; i = i + 1) begin\0A for(j = 0; j < %3$d; j = j + 1) begin\0A %1$s[%4$d + i * %3$d + j] <= %1$s[%4$d + ((i + %5$d) %% %2$d) * %3$d + j];\0A end\0A end\0A\00"
@.str.5 = [50 x i8] c" end\0A end\0A if(run_egress) begin\0A\00"
@.str.6 = [30 x i8] c" end\0A end\0A end\0Aend\0A\0A\00"
define i32 @cat_pattern_task(i8** %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8** %0, i8*** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%17 = load i32, i32* %10
%18 = call i32 @get_n_switches(i32 %17, i32* %13, i32* %12)
%19 = load i32, i32* %11
%20 = icmp ugt i32 %19, 1
br i1 %20, label %21, label %25
21: ; preds = %5
%22 = load i32, i32* %11
%23 = sub i32 %22, 1
%24 = call i32 @ulog2(i32 %23)
br label %26
25: ; preds = %5
br label %26
26: ; preds = %25, %21
%27 = phi i32 [ %24, %21 ], [ 1, %25 ]
store i32 %27, i32* %14
%28 = load i8**, i8*** %7
%29 = load i8*, i8** %8
%30 = load i32, i32* %13
%31 = load i32, i32* %11
%32 = mul i32 %30, %31
%33 = load i32, i32* %10
%34 = load i32, i32* %11
%35 = load i32, i32* %14
%36 = load i32, i32* %13
%37 = load i32, i32* %11
%38 = mul i32 %36, %37
%39 = load i32, i32* %10
%40 = load i32, i32* %11
%41 = mul i32 %39, %40
%42 = load i32, i32* %14
%43 = mul i32 %41, %42
%44 = add i32 %38, %43
%45 = load i32, i32* %13
%46 = mul i32 2, %45
%47 = load i32, i32* %11
%48 = mul i32 %46, %47
%49 = load i32, i32* %10
%50 = load i32, i32* %11
%51 = mul i32 %49, %50
%52 = load i32, i32* %14
%53 = mul i32 %51, %52
%54 = add i32 %48, %53
%55 = load i32, i32* %9
%56 = call i64 (i8**, i8*, ...) @catf(i8** %28, i8* getelementptr inbounds ([735 x i8], [735 x i8]* @.str, i64 0, i64 0), i8* %29, i32 %32, i32 %33, i32 %34, i32 %35, i32 %44, i32 %54, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %26
store i32 -1, i32* %6
br label %169
59: ; preds = %26
store i32 0, i32* %15
store i32 0, i32* %16
br label %60
60: ; preds = %86, %59
%61 = load i32, i32* %16
%62 = load i32, i32* %12
%63 = icmp ult i32 %61, %62
br i1 %63, label %64, label %89
64: ; preds = %60
%65 = load i8**, i8*** %7
%66 = load i32, i32* %16
%67 = load i32, i32* %9
%68 = call i64 (i8**, i8*, ...) @catf(i8** %65, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %66, i32 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %64
store i32 -1, i32* %6
br label %169
71: ; preds = %64
%72 = load i8**, i8*** %7
%73 = load i32, i32* %10
%74 = load i32, i32* %11
%75 = load i32, i32* %16
%76 = load i8*, i8** %8
%77 = call i64 @append_pattern_shifts(i8** %72, i32 %73, i32 %74, i32 %75, i8* %76, i32* %15)
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %71
store i32 -1, i32* %6
br label %169
80: ; preds = %71
%81 = load i8**, i8*** %7
%82 = call i64 (i8**, i8*, ...) @catf(i8** %81, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %80
store i32 -1, i32* %6
br label %169
85: ; preds = %80
br label %86
86: ; preds = %85
%87 = load i32, i32* %16
%88 = add i32 %87, 1
store i32 %88, i32* %16
br label %60
89: ; preds = %60
%90 = load i8**, i8*** %7
%91 = load i32, i32* %12
%92 = add i32 %91, 1
%93 = load i32, i32* %12
%94 = load i32, i32* %11
%95 = sub i32 %94, 1
%96 = load i32, i32* %9
%97 = call i64 (i8**, i8*, ...) @catf(i8** %90, i8* getelementptr inbounds ([257 x i8], [257 x i8]* @.str.3, i64 0, i64 0), i32 %92, i32 %93, i32 %95, i32 %96)
%98 = icmp slt i64 %97, 0
br i1 %98, label %99, label %100
99: ; preds = %89
store i32 -1, i32* %6
br label %169
100: ; preds = %89
store i32 0, i32* %16
br label %101
101: ; preds = %122, %100
%102 = load i32, i32* %16
%103 = load i32, i32* %10
%104 = icmp ult i32 %102, %103
br i1 %104, label %105, label %125
105: ; preds = %101
%106 = load i8**, i8*** %7
%107 = load i8*, i8** %8
%108 = load i32, i32* %11
%109 = load i32, i32* %14
%110 = load i32, i32* %15
%111 = load i32, i32* %11
%112 = sub i32 %111, 1
%113 = call i64 (i8**, i8*, ...) @catf(i8** %106, i8* getelementptr inbounds ([218 x i8], [218 x i8]* @.str.4, i64 0, i64 0), i8* %107, i32 %108, i32 %109, i32 %110, i32 %112)
%114 = icmp slt i64 %113, 0
br i1 %114, label %115, label %116
115: ; preds = %105
store i32 -1, i32* %6
br label %169
116: ; preds = %105
%117 = load i32, i32* %11
%118 = load i32, i32* %14
%119 = mul i32 %117, %118
%120 = load i32, i32* %15
%121 = add i32 %120, %119
store i32 %121, i32* %15
br label %122
122: ; preds = %116
%123 = load i32, i32* %16
%124 = add i32 %123, 1
store i32 %124, i32* %16
br label %101
125: ; preds = %101
%126 = load i8**, i8*** %7
%127 = call i64 (i8**, i8*, ...) @catf(i8** %126, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0))
%128 = icmp slt i64 %127, 0
br i1 %128, label %129, label %130
129: ; preds = %125
store i32 -1, i32* %6
br label %169
130: ; preds = %125
store i32 0, i32* %16
br label %131
131: ; preds = %160, %130
%132 = load i32, i32* %16
%133 = load i32, i32* %12
%134 = icmp ult i32 %132, %133
br i1 %134, label %135, label %163
135: ; preds = %131
%136 = load i8**, i8*** %7
%137 = load i32, i32* %12
%138 = add i32 %137, 2
%139 = load i32, i32* %16
%140 = add i32 %138, %139
%141 = load i32, i32* %9
%142 = call i64 (i8**, i8*, ...) @catf(i8** %136, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %140, i32 %141)
%143 = icmp slt i64 %142, 0
br i1 %143, label %144, label %145
144: ; preds = %135
store i32 -1, i32* %6
br label %169
145: ; preds = %135
%146 = load i8**, i8*** %7
%147 = load i32, i32* %10
%148 = load i32, i32* %11
%149 = load i32, i32* %16
%150 = load i8*, i8** %8
%151 = call i64 @append_pattern_shifts(i8** %146, i32 %147, i32 %148, i32 %149, i8* %150, i32* %15)
%152 = icmp slt i64 %151, 0
br i1 %152, label %153, label %154
153: ; preds = %145
store i32 -1, i32* %6
br label %169
154: ; preds = %145
%155 = load i8**, i8*** %7
%156 = call i64 (i8**, i8*, ...) @catf(i8** %155, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%157 = icmp slt i64 %156, 0
br i1 %157, label %158, label %159
158: ; preds = %154
store i32 -1, i32* %6
br label %169
159: ; preds = %154
br label %160
160: ; preds = %159
%161 = load i32, i32* %16
%162 = add i32 %161, 1
store i32 %162, i32* %16
br label %131
163: ; preds = %131
%164 = load i8**, i8*** %7
%165 = call i64 (i8**, i8*, ...) @catf(i8** %164, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%166 = icmp slt i64 %165, 0
br i1 %166, label %167, label %168
167: ; preds = %163
store i32 -1, i32* %6
br label %169
168: ; preds = %163
store i32 0, i32* %6
br label %169
169: ; preds = %168, %167, %158, %153, %144, %129, %115, %99, %84, %79, %70, %58
%170 = load i32, i32* %6
ret i32 %170
}

LLVM IR (-O2):
@.str = [735 x i8] c"always @(posedge clk or posedge reset) \0Abegin : %1$s_task\0A integer i;\0A integer j;\0A integer k;\0A if(reset) begin\0A for(i = 0; i < %2$d; i = i + 1) %1$s[i] <= 0;\0A for(i = 0; i < %3$d; i = i + 1) begin\0A for(j = 0; j < %4$d; j = j + 1) begin\0A for(k = 0; k < %5$d; k = k + 1) begin\0A %1$s[%2$d + i * %4$d * %5$d + j * %5$d + k] <= ((%4$d - 1 - j) >> k) & 1;\0A end\0A end\0A end\0A for(i = 0; i < %2$d; i = i + 1) %1$s[%6$d + i] <= 0;\0A end\0A else begin\0A if(pattern_valid && !updating_pattern && pattern_at[0] != %8$d) begin\0A for(i = 0; i < 8; i = i + 1) %1$s[i] <= pattern_data[i];\0A for(i = 8; i < %7$d; i = i + 1) %1$s[i] <= %1$s[i - 8];\0A end\0A else begin\0A if(in_valid) begin\0A\00"
@.str.1 = [60 x i8] c" if(active[%1$d] && pattern_at[%1$d] == %2$d) begin\0A\00"
@.str.2 = [13 x i8] c" end\0A\00"
@.str.3 = [257 x i8] c" /* The middle pattern is read one clock cycle in advance because of\0A * memory delays. */\0A if(memory_counter == %3$d ?\0A active[%2$d] && pattern_at[%2$d] == %4$d :\0A active[%1$d] && pattern_at[%1$d] == %4$d) begin\0A\00"
@.str.4 = [218 x i8] c" for(i = 0; i < %2$d; i = i + 1) begin\0A for(j = 0; j < %3$d; j = j + 1) begin\0A %1$s[%4$d + i * %3$d + j] <= %1$s[%4$d + ((i + %5$d) %% %2$d) * %3$d + j];\0A end\0A end\0A\00"
@.str.5 = [50 x i8] c" end\0A end\0A if(run_egress) begin\0A\00"
@.str.6 = [30 x i8] c" end\0A end\0A end\0Aend\0A\0A\00"
define i32 @cat_pattern_task(i8** %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = bitcast i32* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = call i32 @get_n_switches(i32 %3, i32* %7, i32* %6)
%12 = icmp ugt i32 %4, 1
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = add i32 %4, -1
%15 = call i32 @ulog2(i32 %14)
br label %16
16: ; preds = %5, %13
%17 = phi i32 [ %15, %13 ], [ 1, %5 ]
%18 = load i32, i32* %7
%19 = mul i32 %18, %4
%20 = mul i32 %17, %4
%21 = mul i32 %20, %3
%22 = add i32 %19, %21
%23 = shl i32 %4, 1
%24 = mul i32 %23, %18
%25 = add i32 %24, %21
%26 = call i64 (i8**, i8*, ...) @catf(i8** %0, i8* getelementptr inbounds ([735 x i8], [735 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %19, i32 %3, i32 %4, i32 %17, i32 %22, i32 %25, i32 %2)
%27 = icmp slt i64 %26, 0
br i1 %27, label %95, label %28
28: ; preds = %16
%29 = bitcast i32* %8 to i8*
store i32 0, i32* %8
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, 0
br i1 %31, label %46, label %35
32: ; preds = %42
%33 = load i32, i32* %6
%34 = icmp ult i32 %45, %33
br i1 %34, label %35, label %46
35: ; preds = %28, %32
%36 = phi i32 [ %45, %32 ], [ 0, %28 ]
%37 = call i64 (i8**, i8*, ...) @catf(i8** %0, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %2)
%38 = icmp slt i64 %37, 0
br i1 %38, label %93, label %39
39: ; preds = %35
%40 = call i64 @append_pattern_shifts(i8** %0, i32 %3, i32 %4, i32 %36, i8* %1, i32* %8)
%41 = icmp slt i64 %40, 0
br i1 %41, label %93, label %42
42: ; preds = %39
%43 = call i64 (i8**, i8*, ...) @catf(i8** %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%44 = icmp slt i64 %43, 0
%45 = add nuw i32 %36, 1
br i1 %44, label %93, label %32
46: ; preds = %32, %28
%47 = phi i32 [ 0, %28 ], [ %33, %32 ]
%48 = add i32 %47, 1
%49 = add i32 %4, -1
%50 = call i64 (i8**, i8*, ...) @catf(i8** %0, i8* getelementptr inbounds ([257 x i8], [257 x i8]* @.str.3, i64 0, i64 0), i32 %48, i32 %47, i32 %49, i32 %2)
%51 = icmp slt i64 %50, 0
br i1 %51, label %93, label %52
52: ; preds = %46
%53 = icmp eq i32 %3, 0
br i1 %53, label %66, label %54
54: ; preds = %52
%55 = load i32, i32* %8
br label %56
56: ; preds = %54, %61
%57 = phi i32 [ %55, %54 ], [ %63, %61 ]
%58 = phi i32 [ 0, %54 ], [ %64, %61 ]
%59 = call i64 (i8**, i8*, ...) @catf(i8** %0, i8* getelementptr inbounds ([218 x i8], [218 x i8]* @.str.4, i64 0, i64 0), i8* %1, i32 %4, i32 %17, i32 %57, i32 %49)
%60 = icmp slt i64 %59, 0
br i1 %60, label %93, label %61
61: ; preds = %56
%62 = load i32, i32* %8
%63 = add i32 %62, %20
store i32 %63, i32* %8
%64 = add nuw i32 %58, 1
%65 = icmp eq i32 %64, %3
br i1 %65, label %66, label %56
66: ; preds = %61, %52
%67 = call i64 (i8**, i8*, ...) @catf(i8** %0, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0))
%68 = icmp slt i64 %67, 0
br i1 %68, label %93, label %69
69: ; preds = %66
%70 = load i32, i32* %6
%71 = icmp eq i32 %70, 0
br i1 %71, label %89, label %75
72: ; preds = %85
%73 = load i32, i32* %6
%74 = icmp ult i32 %88, %73
br i1 %74, label %75, label %89
75: ; preds = %69, %72
%76 = phi i32 [ %73, %72 ], [ %70, %69 ]
%77 = phi i32 [ %88, %72 ], [ 0, %69 ]
%78 = add i32 %77, 2
%79 = add i32 %78, %76
%80 = call i64 (i8**, i8*, ...) @catf(i8** %0, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %79, i32 %2)
%81 = icmp slt i64 %80, 0
br i1 %81, label %93, label %82
82: ; preds = %75
%83 = call i64 @append_pattern_shifts(i8** %0, i32 %3, i32 %4, i32 %77, i8* %1, i32* %8)
%84 = icmp slt i64 %83, 0
br i1 %84, label %93, label %85
85: ; preds = %82
%86 = call i64 (i8**, i8*, ...) @catf(i8** %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%87 = icmp slt i64 %86, 0
%88 = add nuw i32 %77, 1
br i1 %87, label %93, label %72
89: ; preds = %72, %69
%90 = call i64 (i8**, i8*, ...) @catf(i8** %0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%91 = ashr i64 %90, 63
%92 = trunc i64 %91 to i32
br label %93
93: ; preds = %42, %39, %35, %56, %85, %82, %75, %89, %66, %46
%94 = phi i32 [ -1, %46 ], [ -1, %66 ], [ %92, %89 ], [ -1, %75 ], [ -1, %82 ], [ -1, %85 ], [ -1, %56 ], [ -1, %35 ], [ -1, %39 ], [ -1, %42 ]
br label %95
95: ; preds = %16, %93
%96 = phi i32 [ %94, %93 ], [ -1, %16 ]
ret i32 %96
}