; LLVM IR from CactiLab/Sherloc-Cortex-M-CFVD :: Sherloc_runtime/freertos/portable/GCC/ARM_CM23/secure (secure_context)

; 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"

%struct.SecureContext = type { ptr, ptr, ptr, ptr }

@xSecureContexts = external dso_local global [8 x %struct.SecureContext], align 16

; 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 @SecureContext_LoadContext(i32 noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = zext i32 %7 to i64
  %9 = icmp ugt i64 %8, 0
  br i1 %9, label %10, label %35

10:                                               ; preds = %2
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = zext i32 %11 to i64
  %13 = icmp ule i64 %12, 8
  br i1 %13, label %14, label %35

14:                                               ; preds = %10
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = zext i32 %15 to i64
  %17 = sub i64 %16, 1
  %18 = trunc i64 %17 to i32
  store i32 %18, ptr %6, align 4, !tbaa !5
  %19 = call ptr asm sideeffect "mrs $0, psplim", "=r,~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !11
  store ptr %19, ptr %5, align 8, !tbaa !9
  %20 = load ptr, ptr %5, align 8, !tbaa !9
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %34

22:                                               ; preds = %14
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds [8 x %struct.SecureContext], ptr @xSecureContexts, i64 0, i64 %24
  %26 = getelementptr inbounds %struct.SecureContext, ptr %25, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !12
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  %29 = icmp eq ptr %27, %28
  br i1 %29, label %30, label %34

30:                                               ; preds = %22
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = zext i32 %31 to i64
  %33 = getelementptr inbounds [8 x %struct.SecureContext], ptr @xSecureContexts, i64 0, i64 %32
  call void @SecureContext_LoadContextAsm(ptr noundef %33)
  br label %34

34:                                               ; preds = %30, %22, %14
  br label %35

35:                                               ; preds = %34, %10, %2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void
}

declare void @SecureContext_LoadContextAsm(ptr noundef) #2

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 = { "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 #3 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{i64 2147881646}
!12 = !{!13, !10, i64 24}
!13 = !{!"SecureContext", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}


; 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"

%struct.SecureContext = type { ptr, ptr, ptr, ptr }

@xSecureContexts = external dso_local global [8 x %struct.SecureContext], align 16

; Function Attrs: nounwind uwtable
define dso_local void @SecureContext_LoadContext(i32 noundef %0, ptr noundef readnone %1) local_unnamed_addr #0 {
  %3 = add i32 %0, -1
  %or.cond = icmp ult i32 %3, 8
  br i1 %or.cond, label %4, label %14

4:                                                ; preds = %2
  %5 = tail call ptr asm sideeffect "mrs $0, psplim", "=r,~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %14

7:                                                ; preds = %4
  %8 = zext nneg i32 %3 to i64
  %9 = getelementptr inbounds [8 x %struct.SecureContext], ptr @xSecureContexts, i64 0, i64 %8
  %10 = getelementptr inbounds i8, ptr %9, i64 24
  %11 = load ptr, ptr %10, align 8, !tbaa !6
  %12 = icmp eq ptr %11, %1
  br i1 %12, label %13, label %14

13:                                               ; preds = %7
  tail call void @SecureContext_LoadContextAsm(ptr noundef nonnull %9) #2
  br label %14

14:                                               ; preds = %4, %7, %13, %2
  ret void
}

declare void @SecureContext_LoadContextAsm(ptr noundef) local_unnamed_addr #1

attributes #0 = { 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 #1 = { "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 = !{i64 2147881646}
!6 = !{!7, !8, i64 24}
!7 = !{!"SecureContext", !8, i64 0, !8, i64 8, !8, i64 16, !8, i64 24}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass