; LLVM IR from CactiLab/Sherloc-Cortex-M-CFVD :: Sherloc_runtime/freertos/demo/ARMv8M/mpu_demo (mpu_demo)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@ucROTaskFaultTracker = external hidden global [32 x i8], align 32

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @vHandleMemoryFault(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i16, align 2
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  %5 = load i8, ptr @ucROTaskFaultTracker, align 32, !tbaa !9
  %6 = zext i8 %5 to i32
  %7 = icmp eq i32 %6, 1
  br i1 %7, label %8, label %45

8:                                                ; preds = %1
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds i32, ptr %9, i64 6
  %11 = load i32, ptr %10, align 4, !tbaa !10
  store i32 %11, ptr %3, align 4, !tbaa !10
  %12 = load i32, ptr %3, align 4, !tbaa !10
  %13 = zext i32 %12 to i64
  %14 = inttoptr i64 %13 to ptr
  %15 = load i16, ptr %14, align 2, !tbaa !12
  store i16 %15, ptr %4, align 2, !tbaa !12
  %16 = load i16, ptr %4, align 2, !tbaa !12
  %17 = zext i16 %16 to i32
  %18 = and i32 %17, 63488
  %19 = trunc i32 %18 to i16
  store i16 %19, ptr %4, align 2, !tbaa !12
  %20 = load i16, ptr %4, align 2, !tbaa !12
  %21 = zext i16 %20 to i32
  %22 = ashr i32 %21, 11
  %23 = trunc i32 %22 to i16
  store i16 %23, ptr %4, align 2, !tbaa !12
  %24 = load i16, ptr %4, align 2, !tbaa !12
  %25 = zext i16 %24 to i32
  %26 = icmp eq i32 %25, 31
  br i1 %26, label %35, label %27

27:                                               ; preds = %8
  %28 = load i16, ptr %4, align 2, !tbaa !12
  %29 = zext i16 %28 to i32
  %30 = icmp eq i32 %29, 30
  br i1 %30, label %35, label %31

31:                                               ; preds = %27
  %32 = load i16, ptr %4, align 2, !tbaa !12
  %33 = zext i16 %32 to i32
  %34 = icmp eq i32 %33, 29
  br i1 %34, label %35, label %38

35:                                               ; preds = %31, %27, %8
  %36 = load i32, ptr %3, align 4, !tbaa !10
  %37 = add i32 %36, 4
  store i32 %37, ptr %3, align 4, !tbaa !10
  br label %41

38:                                               ; preds = %31
  %39 = load i32, ptr %3, align 4, !tbaa !10
  %40 = add i32 %39, 2
  store i32 %40, ptr %3, align 4, !tbaa !10
  br label %41

41:                                               ; preds = %38, %35
  %42 = load i32, ptr %3, align 4, !tbaa !10
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = getelementptr inbounds i32, ptr %43, i64 6
  store i32 %42, ptr %44, align 4, !tbaa !10
  store i8 0, ptr @ucROTaskFaultTracker, align 32, !tbaa !9
  br label %47

45:                                               ; preds = %1
  br label %46

46:                                               ; preds = %46, %45
  br label %46

47:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@ucROTaskFaultTracker = external hidden local_unnamed_addr global [32 x i8], align 32

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @vHandleMemoryFault(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = load i8, ptr @ucROTaskFaultTracker, align 32, !tbaa !5
  %3 = icmp eq i8 %2, 1
  br i1 %3, label %4, label %.preheader

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load i32, ptr %5, align 4, !tbaa !8
  %7 = zext i32 %6 to i64
  %8 = inttoptr i64 %7 to ptr
  %9 = load i16, ptr %8, align 2, !tbaa !10
  %or.cond = icmp ugt i16 %9, -4097
  %.mask = and i16 %9, -2048
  %10 = icmp eq i16 %.mask, -6144
  %or.cond5 = or i1 %or.cond, %10
  %.0.v = select i1 %or.cond5, i32 4, i32 2
  %.0 = add i32 %.0.v, %6
  store i32 %.0, ptr %5, align 4, !tbaa !8
  store i8 0, ptr @ucROTaskFaultTracker, align 32, !tbaa !5
  ret void

.preheader:                                       ; preds = %1, %.preheader
  br label %.preheader
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass