int main(void) {
  const int num_tests = 32;
  const int max_accum_len = 256;
  int a, b;
  int accum_hw, accum_sw;
  int i, j;
  int err_cnt = 0;
  for (i = 0; i < num_tests; i++) {
    int accum_len = rand() % max_accum_len + 1;
    for (j = 0; j < accum_len; j++) {
      _Bool clr = j == 0 ? 1 : 0;
      a = (rand() - 0x7fff / 2) >> (8 * sizeof(int) - 12);
      b = (rand() - 0x7fff / 2) >> (8 * sizeof(int) - 12);
      hls_macc(a, b, &accum_hw, clr);
      ref_macc(a, b, &accum_sw, clr);
    }
    if (accum_hw != accum_sw) {
      printf("!!! ERROR mismatch on test #%d - ", i);
      printf("HW returned: %d; ", accum_hw);
      printf("Expected: %d !!!\n", accum_sw);
      err_cnt++;
    }
  }
  if (err_cnt)
    printf("\n!!! %d TESTS FAILED !!!\n\n", err_cnt);
  else
    printf("\n*** %d Tests Passed ***\n\n", num_tests);
  return err_cnt;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x20,-0x8(%rbp)
movl   $0x100,-0xc(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x20,-0x20(%rbp)
jge    1277 <main+0x137>
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0x100,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    121c <main+0xdc>
mov    -0x24(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
cmp    $0x0,%eax
setne  %al
and    $0x1,%al
mov    %al,-0x2d(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
sub    $0x3fff,%eax
sar    $0x14,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
sub    $0x3fff,%eax
sar    $0x14,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x2d(%rbp),%al
and    $0x1,%al
movzbl %al,%ecx
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1050 <hls_macc@plt>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x2d(%rbp),%al
and    $0x1,%al
movzbl %al,%ecx
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1030 <ref_macc@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1191 <main+0x51>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1264 <main+0x124>
mov    -0x20(%rbp),%esi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1269 <main+0x129>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    116b <main+0x2b>
cmpl   $0x0,-0x28(%rbp)
je     1297 <main+0x157>
mov    -0x28(%rbp),%esi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12aa <main+0x16a>
lea    0xdc3(%rip),%rdi
mov    $0x20,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rbp
xor    %ebx,%ebx
movl   $0x0,0x8(%rsp)
jmp    11a9 <main+0x69>
nop
lea    0xe99(%rip),%rdi
mov    0xc(%rsp),%ebx
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x14(%rsp),%esi
lea    0xea3(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rsp),%esi
lea    0xea3(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
addl   $0x1,0x8(%rsp)
add    $0x1,%ebx
cmp    $0x20,%ebx
je     1254 <main+0x114>
mov    %ebx,0xc(%rsp)
xor    %eax,%eax
call   1060 <rand@plt>
mov    %eax,%r15d
lea    0xff(%r15),%eax
test   %r15d,%r15d
cmovns %r15d,%eax
and    $0xffffff00,%eax
sub    %eax,%r15d
js     123d <main+0xfd>
add    $0x1,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %r12d,%r12d
test   %ebx,%ebx
sete   %r12b
xor    %eax,%eax
call   1060 <rand@plt>
lea    -0x3fff(%rax),%r13d
sar    $0x14,%r13d
xor    %eax,%eax
call   1060 <rand@plt>
lea    -0x3fff(%rax),%r14d
sar    $0x14,%r14d
mov    %r13d,%edi
mov    %r14d,%esi
lea    0x14(%rsp),%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <hls_macc@plt>
mov    %r13d,%edi
mov    %r14d,%esi
mov    %rbp,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1030 <ref_macc@plt>
add    $0x1,%ebx
cmp    %ebx,%r15d
jne    11e0 <main+0xa0>
mov    0x14(%rsp),%eax
cmp    0x10(%rsp),%eax
jne    1160 <main+0x20>
mov    0xc(%rsp),%ebx
jmp    119d <main+0x5d>
mov    0x8(%rsp),%ebx
test   %ebx,%ebx
je     1267 <main+0x127>
lea    0xde3(%rip),%rdi
mov    %ebx,%esi
jmp    1273 <main+0x133>
lea    0xdf3(%rip),%rdi
mov    $0x20,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"!!! ERROR mismatch on test #%d - \00"
@.str.1 = [18 x i8] c"HW returned: %d; \00"
@.str.2 = [18 x i8] c"Expected: %d !!!\0A\00"
@.str.3 = [27 x i8] c"\0A!!! %d TESTS FAILED !!!\0A\0A\00"
@.str.4 = [27 x i8] c"\0A*** %d Tests Passed ***\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
store i32 0, i32* %1
store i32 32, i32* %2
store i32 256, i32* %3
store i32 0, i32* %10
store i32 0, i32* %8
br label %13
13: ; preds = %66, %0
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 32
br i1 %15, label %16, label %69
16: ; preds = %13
%17 = call i32 (...) @rand()
%18 = srem i32 %17, 256
%19 = add nsw i32 %18, 1
store i32 %19, i32* %11
store i32 0, i32* %9
br label %20
20: ; preds = %49, %16
%21 = load i32, i32* %9
%22 = load i32, i32* %11
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %52
24: ; preds = %20
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, 0
%27 = zext i1 %26 to i64
%28 = select i1 %26, i32 1, i32 0
%29 = icmp ne i32 %28, 0
%30 = zext i1 %29 to i8
store i8 %30, i8* %12
%31 = call i32 (...) @rand()
%32 = sub nsw i32 %31, 16383
%33 = ashr i32 %32, 20
store i32 %33, i32* %4
%34 = call i32 (...) @rand()
%35 = sub nsw i32 %34, 16383
%36 = ashr i32 %35, 20
store i32 %36, i32* %5
%37 = load i32, i32* %4
%38 = load i32, i32* %5
%39 = load i8, i8* %12
%40 = trunc i8 %39 to i1
%41 = zext i1 %40 to i32
%42 = call i32 (i32, i32, i32*, i32, ...) bitcast (i32 (...)* @hls_macc to i32 (i32, i32, i32*, i32, ...)*)(i32 %37, i32 %38, i32* %6, i32 %41)
%43 = load i32, i32* %4
%44 = load i32, i32* %5
%45 = load i8, i8* %12
%46 = trunc i8 %45 to i1
%47 = zext i1 %46 to i32
%48 = call i32 (i32, i32, i32*, i32, ...) bitcast (i32 (...)* @ref_macc to i32 (i32, i32, i32*, i32, ...)*)(i32 %43, i32 %44, i32* %7, i32 %47)
br label %49
49: ; preds = %24
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %20
52: ; preds = %20
%53 = load i32, i32* %6
%54 = load i32, i32* %7
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %65
56: ; preds = %52
%57 = load i32, i32* %8
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %6
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %7
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %61)
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %10
br label %65
65: ; preds = %56, %52
br label %66
66: ; preds = %65
%67 = load i32, i32* %8
%68 = add nsw i32 %67, 1
store i32 %68, i32* %8
br label %13
69: ; preds = %13
%70 = load i32, i32* %10
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = load i32, i32* %10
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %73)
br label %77
75: ; preds = %69
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 32)
br label %77
77: ; preds = %75, %72
%78 = load i32, i32* %10
ret i32 %78
}

LLVM IR (-O2):
@.str = [34 x i8] c"!!! ERROR mismatch on test #%d - \00"
@.str.1 = [18 x i8] c"HW returned: %d; \00"
@.str.2 = [18 x i8] c"Expected: %d !!!\0A\00"
@.str.3 = [27 x i8] c"\0A!!! %d TESTS FAILED !!!\0A\0A\00"
@.str.4 = [27 x i8] c"\0A*** %d Tests Passed ***\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
br label %5
5: ; preds = %0, %36
%6 = phi i32 [ 0, %0 ], [ %38, %36 ]
%7 = phi i32 [ 0, %0 ], [ %37, %36 ]
%8 = call i32 (...) @rand()
%9 = srem i32 %8, 256
%10 = icmp slt i32 %9, 0
br i1 %10, label %25, label %11
11: ; preds = %5, %11
%12 = phi i32 [ %23, %11 ], [ 0, %5 ]
%13 = icmp eq i32 %12, 0
%14 = call i32 (...) @rand()
%15 = add nsw i32 %14, -16383
%16 = ashr i32 %15, 20
%17 = call i32 (...) @rand()
%18 = add nsw i32 %17, -16383
%19 = ashr i32 %18, 20
%20 = zext i1 %13 to i32
%21 = call i32 (i32, i32, i32*, i32, ...) bitcast (i32 (...)* @hls_macc to i32 (i32, i32, i32*, i32, ...)*)(i32 %16, i32 %19, i32* %1, i32 %20)
%22 = call i32 (i32, i32, i32*, i32, ...) bitcast (i32 (...)* @ref_macc to i32 (i32, i32, i32*, i32, ...)*)(i32 %16, i32 %19, i32* %2, i32 %20)
%23 = add nuw nsw i32 %12, 1
%24 = icmp eq i32 %12, %9
br i1 %24, label %25, label %11
25: ; preds = %11, %5
%26 = load i32, i32* %1
%27 = load i32, i32* %2
%28 = icmp eq i32 %26, %27
br i1 %28, label %36, label %29
29: ; preds = %25
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %6)
%31 = load i32, i32* %1
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %2
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = add nsw i32 %7, 1
br label %36
36: ; preds = %29, %25
%37 = phi i32 [ %35, %29 ], [ %7, %25 ]
%38 = add nuw nsw i32 %6, 1
%39 = icmp eq i32 %38, 32
br i1 %39, label %40, label %5
40: ; preds = %36
%41 = icmp eq i32 %37, 0
br i1 %41, label %44, label %42
42: ; preds = %40
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %46
44: ; preds = %40
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 32)
br label %46
46: ; preds = %44, %42
ret i32 %37
}