; LLVM IR from ARMmbed/mbed-crypto :: library (psa_crypto_slot_management)

; 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.psa_global_data_t = type { [32 x %struct.psa_key_slot_t], i8 }
%struct.psa_key_slot_t = type { %struct.psa_core_key_attributes_t, %union.anon }
%struct.psa_core_key_attributes_t = type { i16, i16, i32, i32, %struct.psa_key_policy_s, i16 }
%struct.psa_key_policy_s = type { i32, i32, i32 }
%union.anon = type { %struct.raw_data }
%struct.raw_data = type { ptr, i64 }
%struct.mbedtls_psa_stats_s = type { i64, i64, i64, i64, i64, i64, i32, i32 }

@global_data = external hidden global %struct.psa_global_data_t, align 8

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

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

; 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 @mbedtls_psa_get_stats(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i16, align 2
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #4
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %8, i8 0, i64 56, i1 false)
  store i16 1, ptr %3, align 2, !tbaa !9
  br label %9

9:                                                ; preds = %87, %1
  %10 = load i16, ptr %3, align 2, !tbaa !9
  %11 = zext i16 %10 to i32
  %12 = icmp sle i32 %11, 32
  br i1 %12, label %13, label %90

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %14 = load i16, ptr %3, align 2, !tbaa !9
  %15 = zext i16 %14 to i32
  %16 = sub nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [32 x %struct.psa_key_slot_t], ptr @global_data, i64 0, i64 %17
  store ptr %18, ptr %4, align 8, !tbaa !5
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = call i32 @psa_is_key_slot_occupied(ptr noundef %19)
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %27, label %22

22:                                               ; preds = %13
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mbedtls_psa_stats_s, ptr %23, i32 0, i32 5
  %25 = load i64, ptr %24, align 8, !tbaa !11
  %26 = add i64 %25, 1
  store i64 %26, ptr %24, align 8, !tbaa !11
  store i32 4, ptr %5, align 4
  br label %84

27:                                               ; preds = %13
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.psa_key_slot_t, ptr %28, i32 0, i32 0
  %30 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 4, !tbaa !15
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %33, label %38

33:                                               ; preds = %27
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.mbedtls_psa_stats_s, ptr %34, i32 0, i32 0
  %36 = load i64, ptr %35, align 8, !tbaa !19
  %37 = add i64 %36, 1
  store i64 %37, ptr %35, align 8, !tbaa !19
  br label %83

38:                                               ; preds = %27
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.psa_key_slot_t, ptr %39, i32 0, i32 0
  %41 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %40, i32 0, i32 2
  %42 = load i32, ptr %41, align 4, !tbaa !15
  %43 = icmp eq i32 %42, 1
  br i1 %43, label %44, label %63

44:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.psa_key_slot_t, ptr %45, i32 0, i32 0
  %47 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %46, i32 0, i32 3
  %48 = load i32, ptr %47, align 8, !tbaa !20
  store i32 %48, ptr %6, align 4, !tbaa !21
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mbedtls_psa_stats_s, ptr %49, i32 0, i32 1
  %51 = load i64, ptr %50, align 8, !tbaa !22
  %52 = add i64 %51, 1
  store i64 %52, ptr %50, align 8, !tbaa !22
  %53 = load i32, ptr %6, align 4, !tbaa !21
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mbedtls_psa_stats_s, ptr %54, i32 0, i32 6
  %56 = load i32, ptr %55, align 8, !tbaa !23
  %57 = icmp ugt i32 %53, %56
  br i1 %57, label %58, label %62

58:                                               ; preds = %44
  %59 = load i32, ptr %6, align 4, !tbaa !21
  %60 = load ptr, ptr %2, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.mbedtls_psa_stats_s, ptr %60, i32 0, i32 6
  store i32 %59, ptr %61, align 8, !tbaa !23
  br label %62

62:                                               ; preds = %58, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %82

63:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.psa_key_slot_t, ptr %64, i32 0, i32 0
  %66 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %65, i32 0, i32 3
  %67 = load i32, ptr %66, align 8, !tbaa !20
  store i32 %67, ptr %7, align 4, !tbaa !21
  %68 = load ptr, ptr %2, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.mbedtls_psa_stats_s, ptr %68, i32 0, i32 2
  %70 = load i64, ptr %69, align 8, !tbaa !24
  %71 = add i64 %70, 1
  store i64 %71, ptr %69, align 8, !tbaa !24
  %72 = load i32, ptr %7, align 4, !tbaa !21
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.mbedtls_psa_stats_s, ptr %73, i32 0, i32 7
  %75 = load i32, ptr %74, align 4, !tbaa !25
  %76 = icmp ugt i32 %72, %75
  br i1 %76, label %77, label %81

77:                                               ; preds = %63
  %78 = load i32, ptr %7, align 4, !tbaa !21
  %79 = load ptr, ptr %2, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.mbedtls_psa_stats_s, ptr %79, i32 0, i32 7
  store i32 %78, ptr %80, align 4, !tbaa !25
  br label %81

81:                                               ; preds = %77, %63
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %82

82:                                               ; preds = %81, %62
  br label %83

83:                                               ; preds = %82, %33
  store i32 0, ptr %5, align 4
  br label %84

84:                                               ; preds = %83, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %85 = load i32, ptr %5, align 4
  switch i32 %85, label %91 [
    i32 0, label %86
    i32 4, label %87
  ]

86:                                               ; preds = %84
  br label %87

87:                                               ; preds = %86, %84
  %88 = load i16, ptr %3, align 2, !tbaa !9
  %89 = add i16 %88, 1
  store i16 %89, ptr %3, align 2, !tbaa !9
  br label %9, !llvm.loop !26

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

91:                                               ; preds = %84
  unreachable
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !13, i64 40}
!12 = !{!"mbedtls_psa_stats_s", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !13, i64 32, !13, i64 40, !14, i64 48, !14, i64 52}
!13 = !{!"long", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !14, i64 4}
!16 = !{!"", !17, i64 0, !7, i64 32}
!17 = !{!"", !10, i64 0, !10, i64 2, !14, i64 4, !14, i64 8, !18, i64 12, !10, i64 24}
!18 = !{!"psa_key_policy_s", !14, i64 0, !14, i64 4, !14, i64 8}
!19 = !{!12, !13, i64 0}
!20 = !{!16, !14, i64 8}
!21 = !{!14, !14, i64 0}
!22 = !{!12, !13, i64 8}
!23 = !{!12, !14, i64 48}
!24 = !{!12, !13, i64 16}
!25 = !{!12, !14, i64 52}
!26 = distinct !{!26, !27}
!27 = !{!"llvm.loop.mustprogress"}


; 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.psa_global_data_t = type { [32 x %struct.psa_key_slot_t], i8 }
%struct.psa_key_slot_t = type { %struct.psa_core_key_attributes_t, %union.anon }
%struct.psa_core_key_attributes_t = type { i16, i16, i32, i32, %struct.psa_key_policy_s, i16 }
%struct.psa_key_policy_s = type { i32, i32, i32 }
%union.anon = type { %struct.raw_data }
%struct.raw_data = type { ptr, i64 }

@global_data = external hidden global %struct.psa_global_data_t, align 8

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

; Function Attrs: nounwind uwtable
define dso_local void @mbedtls_psa_get_stats(ptr nocapture noundef %0) local_unnamed_addr #1 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(56) %0, i8 0, i64 56, i1 false)
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = getelementptr inbounds i8, ptr %0, i64 48
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = getelementptr inbounds i8, ptr %0, i64 52
  %6 = getelementptr inbounds i8, ptr %0, i64 40
  br label %7

7:                                                ; preds = %1, %36
  %indvars.iv = phi i64 [ 1, %1 ], [ %indvars.iv.next, %36 ]
  %8 = add nsw i64 %indvars.iv, -1
  %9 = getelementptr inbounds [32 x %struct.psa_key_slot_t], ptr @global_data, i64 0, i64 %8
  %10 = tail call i32 @psa_is_key_slot_occupied(ptr noundef nonnull %9)
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %11, label %14

11:                                               ; preds = %7
  %12 = load i64, ptr %6, align 8, !tbaa !5
  %13 = add i64 %12, 1
  store i64 %13, ptr %6, align 8, !tbaa !5
  br label %36

14:                                               ; preds = %7
  %15 = getelementptr inbounds i8, ptr %9, i64 4
  %16 = load i32, ptr %15, align 4, !tbaa !11
  switch i32 %16, label %28 [
    i32 0, label %17
    i32 1, label %20
  ]

17:                                               ; preds = %14
  %18 = load i64, ptr %0, align 8, !tbaa !16
  %19 = add i64 %18, 1
  store i64 %19, ptr %0, align 8, !tbaa !16
  br label %36

20:                                               ; preds = %14
  %21 = getelementptr inbounds i8, ptr %9, i64 8
  %22 = load i32, ptr %21, align 8, !tbaa !17
  %23 = load i64, ptr %2, align 8, !tbaa !18
  %24 = add i64 %23, 1
  store i64 %24, ptr %2, align 8, !tbaa !18
  %25 = load i32, ptr %3, align 8, !tbaa !19
  %26 = icmp ugt i32 %22, %25
  br i1 %26, label %27, label %36

27:                                               ; preds = %20
  store i32 %22, ptr %3, align 8, !tbaa !19
  br label %36

28:                                               ; preds = %14
  %29 = getelementptr inbounds i8, ptr %9, i64 8
  %30 = load i32, ptr %29, align 8, !tbaa !17
  %31 = load i64, ptr %4, align 8, !tbaa !20
  %32 = add i64 %31, 1
  store i64 %32, ptr %4, align 8, !tbaa !20
  %33 = load i32, ptr %5, align 4, !tbaa !21
  %34 = icmp ugt i32 %30, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %28
  store i32 %30, ptr %5, align 4, !tbaa !21
  br label %36

36:                                               ; preds = %17, %27, %20, %35, %28, %11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 33
  br i1 %exitcond.not, label %37, label %7, !llvm.loop !22

37:                                               ; preds = %36
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { inlinehint 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 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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, !7, i64 40}
!6 = !{!"mbedtls_psa_stats_s", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !10, i64 48, !10, i64 52}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"", !13, i64 0, !8, i64 32}
!13 = !{!"", !14, i64 0, !14, i64 2, !10, i64 4, !10, i64 8, !15, i64 12, !14, i64 24}
!14 = !{!"short", !8, i64 0}
!15 = !{!"psa_key_policy_s", !10, i64 0, !10, i64 4, !10, i64 8}
!16 = !{!6, !7, i64 0}
!17 = !{!12, !10, i64 8}
!18 = !{!6, !7, i64 8}
!19 = !{!6, !10, i64 48}
!20 = !{!6, !7, i64 16}
!21 = !{!6, !10, i64 52}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass