; 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 i32 @SecureContext_AllocateContext(i32 noundef %0, i32 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr null, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  store ptr null, ptr %12, align 8, !tbaa !9
  %13 = call i32 asm sideeffect "mrs $0, ipsr", "=r,~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !11
  store i32 %13, ptr %9, align 4, !tbaa !5
  %14 = call ptr asm sideeffect "mrs $0, psplim", "=r,~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !12
  store ptr %14, ptr %8, align 8, !tbaa !9
  %15 = load i32, ptr %9, align 4, !tbaa !5
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %86

17:                                               ; preds = %3
  %18 = load ptr, ptr %8, align 8, !tbaa !9
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %86

20:                                               ; preds = %17
  %21 = load ptr, ptr %6, align 8, !tbaa !9
  %22 = call i32 @ulGetSecureContext(ptr noundef %21)
  store i32 %22, ptr %10, align 4, !tbaa !5
  %23 = load i32, ptr %10, align 4, !tbaa !5
  %24 = zext i32 %23 to i64
  %25 = icmp ult i64 %24, 8
  br i1 %25, label %26, label %85

26:                                               ; preds = %20
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = add i32 %27, 8
  %29 = zext i32 %28 to i64
  %30 = call ptr @pvPortMalloc(i64 noundef %29)
  store ptr %30, ptr %7, align 8, !tbaa !9
  %31 = load ptr, ptr %7, align 8, !tbaa !9
  %32 = icmp ne ptr %31, null
  br i1 %32, label %33, label %84

33:                                               ; preds = %26
  %34 = load ptr, ptr %7, align 8, !tbaa !9
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds i8, ptr %34, i64 %36
  %38 = load i32, ptr %10, align 4, !tbaa !5
  %39 = zext i32 %38 to i64
  %40 = getelementptr inbounds [8 x %struct.SecureContext], ptr @xSecureContexts, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.SecureContext, ptr %40, i32 0, i32 2
  store ptr %37, ptr %41, align 16, !tbaa !13
  %42 = load ptr, ptr %7, align 8, !tbaa !9
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %42, i64 %44
  store i32 -17437275, ptr %45, align 4, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !9
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds i8, ptr %46, i64 %48
  %50 = getelementptr inbounds i8, ptr %49, i64 4
  store i32 -17437275, ptr %50, align 4, !tbaa !5
  %51 = load ptr, ptr %7, align 8, !tbaa !9
  %52 = load i32, ptr %10, align 4, !tbaa !5
  %53 = zext i32 %52 to i64
  %54 = getelementptr inbounds [8 x %struct.SecureContext], ptr @xSecureContexts, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.SecureContext, ptr %54, i32 0, i32 1
  store ptr %51, ptr %55, align 8, !tbaa !15
  %56 = load ptr, ptr %6, align 8, !tbaa !9
  %57 = load i32, ptr %10, align 4, !tbaa !5
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds [8 x %struct.SecureContext], ptr @xSecureContexts, i64 0, i64 %58
  %60 = getelementptr inbounds %struct.SecureContext, ptr %59, i32 0, i32 3
  store ptr %56, ptr %60, align 8, !tbaa !16
  %61 = load i32, ptr %10, align 4, !tbaa !5
  %62 = zext i32 %61 to i64
  %63 = getelementptr inbounds [8 x %struct.SecureContext], ptr @xSecureContexts, i64 0, i64 %62
  %64 = getelementptr inbounds %struct.SecureContext, ptr %63, i32 0, i32 2
  %65 = load ptr, ptr %64, align 16, !tbaa !13
  store ptr %65, ptr %12, align 8, !tbaa !9
  %66 = load ptr, ptr %12, align 8, !tbaa !9
  %67 = getelementptr inbounds i32, ptr %66, i32 -1
  store ptr %67, ptr %12, align 8, !tbaa !9
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %72

70:                                               ; preds = %33
  %71 = load ptr, ptr %12, align 8, !tbaa !9
  store i32 2, ptr %71, align 4, !tbaa !5
  br label %74

72:                                               ; preds = %33
  %73 = load ptr, ptr %12, align 8, !tbaa !9
  store i32 3, ptr %73, align 4, !tbaa !5
  br label %74

74:                                               ; preds = %72, %70
  %75 = load ptr, ptr %12, align 8, !tbaa !9
  %76 = load i32, ptr %10, align 4, !tbaa !5
  %77 = zext i32 %76 to i64
  %78 = getelementptr inbounds [8 x %struct.SecureContext], ptr @xSecureContexts, i64 0, i64 %77
  %79 = getelementptr inbounds %struct.SecureContext, ptr %78, i32 0, i32 0
  store ptr %75, ptr %79, align 16, !tbaa !17
  %80 = load i32, ptr %10, align 4, !tbaa !5
  %81 = zext i32 %80 to i64
  %82 = add i64 %81, 1
  %83 = trunc i64 %82 to i32
  store i32 %83, ptr %11, align 4, !tbaa !5
  br label %84

84:                                               ; preds = %74, %26
  br label %85

85:                                               ; preds = %84, %20
  br label %86

86:                                               ; preds = %85, %17, %3
  %87 = load i32, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret i32 %87
}

; Function Attrs: nounwind uwtable
declare hidden i32 @ulGetSecureContext(ptr noundef) #1

declare ptr @pvPortMalloc(i64 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 2147881248}
!12 = !{i64 2147881308}
!13 = !{!14, !10, i64 16}
!14 = !{!"SecureContext", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!15 = !{!14, !10, i64 8}
!16 = !{!14, !10, i64 24}
!17 = !{!14, !10, 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"

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 9) i32 @SecureContext_AllocateContext(i32 noundef %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = tail call i32 asm sideeffect "mrs $0, ipsr", "=r,~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !5
  %5 = tail call ptr asm sideeffect "mrs $0, psplim", "=r,~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !6
  %6 = icmp ne i32 %4, 0
  %7 = icmp eq ptr %5, null
  %or.cond = select i1 %6, i1 %7, i1 false
  br i1 %or.cond, label %8, label %26

8:                                                ; preds = %3
  %9 = tail call i32 @ulGetSecureContext(ptr noundef %2)
  %10 = zext i32 %9 to i64
  %11 = icmp ult i32 %9, 8
  br i1 %11, label %12, label %26

12:                                               ; preds = %8
  %13 = add i32 %0, 8
  %14 = zext i32 %13 to i64
  %15 = tail call ptr @pvPortMalloc(i64 noundef %14) #2
  %.not = icmp eq ptr %15, null
  br i1 %.not, label %26, label %16

16:                                               ; preds = %12
  %17 = zext i32 %0 to i64
  %18 = getelementptr inbounds i8, ptr %15, i64 %17
  %19 = getelementptr inbounds [8 x %struct.SecureContext], ptr @xSecureContexts, i64 0, i64 %10
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  store ptr %18, ptr %20, align 16, !tbaa !7
  store i32 -17437275, ptr %18, align 4, !tbaa !12
  %21 = getelementptr inbounds i8, ptr %18, i64 4
  store i32 -17437275, ptr %21, align 4, !tbaa !12
  %22 = getelementptr inbounds i8, ptr %19, i64 8
  store ptr %15, ptr %22, align 8, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %19, i64 24
  store ptr %2, ptr %23, align 8, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %18, i64 -4
  %.not28 = icmp eq i32 %1, 0
  %. = select i1 %.not28, i32 3, i32 2
  store i32 %., ptr %24, align 4, !tbaa !12
  store ptr %24, ptr %19, align 16, !tbaa !16
  %25 = add nuw nsw i32 %9, 1
  br label %26

26:                                               ; preds = %8, %16, %12, %3
  %.0 = phi i32 [ %25, %16 ], [ 0, %12 ], [ 0, %8 ], [ 0, %3 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @ulGetSecureContext(ptr noundef) local_unnamed_addr #0

declare ptr @pvPortMalloc(i64 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 2147881248}
!6 = !{i64 2147881308}
!7 = !{!8, !9, i64 16}
!8 = !{!"SecureContext", !9, i64 0, !9, i64 8, !9, i64 16, !9, i64 24}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !10, i64 0}
!14 = !{!8, !9, i64 8}
!15 = !{!8, !9, i64 24}
!16 = !{!8, !9, i64 0}


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