#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*/ ksft_print_msg(char *, ...);
int /*<<< orphan*/ mte_free_memory(void *, size_t, int, int);
int check_allocated_memory(void *ptr, size_t size, int mem_type, bool tags) {
  if (ptr == NULL) {
    ksft_print_msg("FAIL: memory allocation\n");
    return 1;
  }
  if (tags && !(((uintptr_t)ptr >> 56) & (0xFUL))) {
    ksft_print_msg("FAIL: tag not found at addr(%p)\n", ptr);
    mte_free_memory((void *)ptr, size, mem_type, false);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_allocated_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115b <check_allocated_memory+0x3b>
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1040 <ksft_print_msg@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b2 <check_allocated_memory+0x92>
cmpl   $0x0,-0x20(%rbp)
je     11ab <check_allocated_memory+0x8b>
mov    -0x10(%rbp),%rax
shr    $0x38,%rax
and    $0xf,%rax
cmp    $0x0,%rax
jne    11ab <check_allocated_memory+0x8b>
mov    -0x10(%rbp),%rsi
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <ksft_print_msg@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <mte_free_memory@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b2 <check_allocated_memory+0x92>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_allocated_memory>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     116a <check_allocated_memory+0x4a>
mov    %edx,%r14d
mov    %rdi,%rbp
xor    %eax,%eax
movabs $0xf00000000000000,%rdx
test   %rdx,%rdi
jne    117d <check_allocated_memory+0x5d>
test   %ecx,%ecx
je     117d <check_allocated_memory+0x5d>
mov    %rsi,%rbx
lea    0xecb(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <ksft_print_msg@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1030 <mte_free_memory@plt>
jmp    1178 <check_allocated_memory+0x58>
lea    0xe8f(%rip),%rdi
xor    %eax,%eax
call   1040 <ksft_print_msg@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"FAIL: memory allocation\0A\00"
@.str.1 = [33 x i8] c"FAIL: tag not found at addr(%p)\0A\00"
define i32 @check_allocated_memory(i8* %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = call i32 (i8*, ...) @ksft_print_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %31
14: ; preds = %4
%15 = load i32, i32* %9
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %14
%18 = load i8*, i8** %6
%19 = ptrtoint i8* %18 to i64
%20 = lshr i64 %19, 56
%21 = and i64 %20, 15
%22 = icmp ne i64 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %17
%24 = load i8*, i8** %6
%25 = call i32 (i8*, ...) @ksft_print_msg(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = load i32, i32* %8
%29 = call i32 @mte_free_memory(i8* %26, i64 %27, i32 %28, i32 0)
store i32 1, i32* %5
br label %31
30: ; preds = %17, %14
store i32 0, i32* %5
br label %31
31: ; preds = %30, %23, %12
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
@.str = [25 x i8] c"FAIL: memory allocation\0A\00"
@.str.1 = [33 x i8] c"FAIL: tag not found at addr(%p)\0A\00"
define i32 @check_allocated_memory(i8* %0, i64 %1, i32 %2, i32 %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 (i8*, ...) @ksft_print_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %17
8: ; preds = %4
%9 = icmp ne i32 %3, 0
%10 = ptrtoint i8* %0 to i64
%11 = and i64 %10, 1080863910568919040
%12 = icmp eq i64 %11, 0
%13 = and i1 %12, %9
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 (i8*, ...) @ksft_print_msg(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = tail call i32 @mte_free_memory(i8* %0, i64 %1, i32 %2, i32 0)
br label %17
17: ; preds = %8, %14, %6
%18 = phi i32 [ 1, %6 ], [ 1, %14 ], [ 0, %8 ]
ret i32 %18
}