#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 */
int /*<<< orphan*/ memset(void *, int, size_t);
int /*<<< orphan*/ printf(char *, ...);
void unit_test(void *buf, size_t alloc_size) {
  size_t i;
  unsigned char *buf_cast = (unsigned char *)buf;
  memset(buf, 0x5A, alloc_size);
  for (i = 0; i != alloc_size; i++) {
    if (buf_cast[i] != 0x5A) {
      printf("malloc() test failed at '%lu'. Expected 0x5A, but got 0x%0.2X\n. "
             "Halting\n",
             i, buf_cast[i]);
      break;
    }
  }
  printf("Test PASSED!\n");
}


x86 assembly (clang, -O0):
<unit_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x5a,%esi
call   1040 <memset@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11ae <unit_test+0x8e>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x5a,%eax
je     1198 <unit_test+0x78>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ae <unit_test+0x8e>
jmp    119d <unit_test+0x7d>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1152 <unit_test+0x32>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unit_test>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0x5a,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
test   %rbx,%rbx
je     1173 <unit_test+0x43>
xor    %esi,%esi
xchg   %ax,%ax
movzbl (%r14,%rsi,1),%edx
cmp    $0x5a,%edx
jne    1165 <unit_test+0x35>
add    $0x1,%rsi
cmp    %rsi,%rbx
jne    1150 <unit_test+0x20>
jmp    1173 <unit_test+0x43>
lea    0xe94(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xecf(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [73 x i8] c"malloc() test failed at '%lu'. Expected 0x5A, but got 0x%0.2X\0A. Halting\0A\00"
@.str.1 = [14 x i8] c"Test PASSED!\0A\00"
define void @unit_test(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i8*, i8** %3
store i8* %7, i8** %6
%8 = load i8*, i8** %3
%9 = load i64, i64* %4
%10 = call i32 @memset(i8* %8, i32 90, i64 %9)
store i64 0, i64* %5
br label %11
11: ; preds = %31, %2
%12 = load i64, i64* %5
%13 = load i64, i64* %4
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %34
15: ; preds = %11
%16 = load i8*, i8** %6
%17 = load i64, i64* %5
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = icmp ne i32 %20, 90
br i1 %21, label %22, label %30
22: ; preds = %15
%23 = load i64, i64* %5
%24 = load i8*, i8** %6
%25 = load i64, i64* %5
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i64 %23, i32 %28)
br label %34
30: ; preds = %15
br label %31
31: ; preds = %30
%32 = load i64, i64* %5
%33 = add i64 %32, 1
store i64 %33, i64* %5
br label %11
34: ; preds = %22, %11
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [73 x i8] c"malloc() test failed at '%lu'. Expected 0x5A, but got 0x%0.2X\0A. Halting\0A\00"
@str = [13 x i8] c"Test PASSED!\00"
define void @unit_test(i8* %0, i64 %1) {
%3 = tail call i32 @memset(i8* %0, i32 90, i64 %1)
%4 = icmp eq i64 %1, 0
br i1 %4, label %16, label %5
5: ; preds = %2, %13
%6 = phi i64 [ %14, %13 ], [ 0, %2 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 90
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = zext i8 %8 to i32
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i64 %6, i32 %11)
br label %16
13: ; preds = %5
%14 = add nuw i64 %6, 1
%15 = icmp eq i64 %14, %1
br i1 %15, label %16, label %5
16: ; preds = %13, %2, %10
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
ret void
}