; LLVM IR from machinefi/psa-crypto-esp32 :: psa_layer (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 { [4 x %struct.psa_key_slot_t], i8 }
%struct.psa_key_slot_t = type { %struct.psa_core_key_attributes_t, i64, %struct.key_data }
%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 }
%struct.key_data = type { ptr, i64 }

@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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare i32 @psa_wipe_key_slot(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_get_empty_key_slot(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_get_and_lock_key_slot(i32 noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 -151, ptr %6, align 4, !tbaa !5
  %9 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr null, ptr %9, align 8, !tbaa !9
  %10 = load i8, ptr getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 1), align 8
  %11 = and i8 %10, 1
  %12 = zext i8 %11 to i32
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %15, label %14

14:                                               ; preds = %2
  store i32 -137, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %66

15:                                               ; preds = %2
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = load ptr, ptr %5, align 8, !tbaa !9
  %18 = call i32 @psa_get_and_lock_key_slot_in_memory(i32 noundef %16, ptr noundef %17)
  store i32 %18, ptr %6, align 4, !tbaa !5
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = icmp ne i32 %19, -140
  br i1 %20, label %21, label %23

21:                                               ; preds = %15
  %22 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %22, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %66

23:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %24 = load ptr, ptr %5, align 8, !tbaa !9
  %25 = call i32 @psa_get_empty_key_slot(ptr noundef %8, ptr noundef %24)
  store i32 %25, ptr %6, align 4, !tbaa !5
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %23
  %29 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %29, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %65

30:                                               ; preds = %23
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = load ptr, ptr %5, align 8, !tbaa !9
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.psa_key_slot_t, ptr %33, i32 0, i32 0
  %35 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %34, i32 0, i32 3
  store i32 %31, ptr %35, align 8, !tbaa !11
  %36 = load ptr, ptr %5, align 8, !tbaa !9
  %37 = load ptr, ptr %36, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.psa_key_slot_t, ptr %37, i32 0, i32 0
  %39 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %38, i32 0, i32 2
  store i32 1, ptr %39, align 4, !tbaa !18
  store i32 -140, ptr %6, align 4, !tbaa !5
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = icmp eq i32 %40, -140
  br i1 %41, label %42, label %46

42:                                               ; preds = %30
  %43 = load ptr, ptr %5, align 8, !tbaa !9
  %44 = load ptr, ptr %43, align 8, !tbaa !9
  %45 = call i32 @psa_load_persistent_key_into_slot(ptr noundef %44)
  store i32 %45, ptr %6, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %42, %30
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %57

49:                                               ; preds = %46
  %50 = load ptr, ptr %5, align 8, !tbaa !9
  %51 = load ptr, ptr %50, align 8, !tbaa !9
  %52 = call i32 @psa_wipe_key_slot(ptr noundef %51)
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = icmp eq i32 %53, -140
  br i1 %54, label %55, label %56

55:                                               ; preds = %49
  store i32 -136, ptr %6, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %55, %49
  br label %63

57:                                               ; preds = %46
  %58 = load ptr, ptr %5, align 8, !tbaa !9
  %59 = load ptr, ptr %58, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.psa_key_slot_t, ptr %59, i32 0, i32 0
  %61 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %60, i32 0, i32 4
  %62 = getelementptr inbounds %struct.psa_key_policy_s, ptr %61, i32 0, i32 0
  call void @psa_extend_key_usage_flags(ptr noundef %62)
  br label %63

63:                                               ; preds = %57, %56
  %64 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %64, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %65

65:                                               ; preds = %63, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %66

66:                                               ; preds = %65, %21, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %67 = load i32, ptr %3, align 4
  ret i32 %67
}

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_get_and_lock_key_slot_in_memory(i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_load_persistent_key_into_slot(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @psa_extend_key_usage_flags(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = { 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 #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 = !{!"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 = !{!12, !6, i64 8}
!12 = !{!"", !13, i64 0, !16, i64 32, !17, i64 40}
!13 = !{!"", !14, i64 0, !14, i64 2, !6, i64 4, !6, i64 8, !15, i64 12, !14, i64 24}
!14 = !{!"short", !7, i64 0}
!15 = !{!"psa_key_policy_s", !6, i64 0, !6, i64 4, !6, i64 8}
!16 = !{!"long", !7, i64 0}
!17 = !{!"key_data", !10, i64 0, !16, i64 8}
!18 = !{!12, !6, i64 4}


; 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 { [4 x %struct.psa_key_slot_t], i8 }
%struct.psa_key_slot_t = type { %struct.psa_core_key_attributes_t, i64, %struct.key_data }
%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 }
%struct.key_data = type { ptr, i64 }

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

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

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

declare i32 @psa_wipe_key_slot(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_get_empty_key_slot(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_get_and_lock_key_slot(i32 noundef %0, ptr noundef %1) local_unnamed_addr #2 {
  %3 = alloca i32, align 4
  store ptr null, ptr %1, align 8, !tbaa !5
  %4 = load i8, ptr getelementptr inbounds (i8, ptr @global_data, i64 224), align 8
  %5 = and i8 %4, 1
  %.not = icmp eq i8 %5, 0
  br i1 %.not, label %22, label %6

6:                                                ; preds = %2
  %7 = tail call i32 @psa_get_and_lock_key_slot_in_memory(i32 noundef %0, ptr noundef nonnull %1)
  %.not22 = icmp eq i32 %7, -140
  br i1 %.not22, label %8, label %22

8:                                                ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #4
  %9 = call i32 @psa_get_empty_key_slot(ptr noundef nonnull %3, ptr noundef nonnull %1)
  %.not23 = icmp eq i32 %9, 0
  br i1 %.not23, label %10, label %21

10:                                               ; preds = %8
  %11 = load ptr, ptr %1, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 8
  store i32 %0, ptr %12, align 8, !tbaa !9
  %13 = getelementptr inbounds i8, ptr %11, i64 4
  store i32 1, ptr %13, align 4, !tbaa !17
  %14 = call i32 @psa_load_persistent_key_into_slot(ptr noundef %11)
  %.not24 = icmp eq i32 %14, 0
  %15 = load ptr, ptr %1, align 8, !tbaa !5
  br i1 %.not24, label %19, label %16

16:                                               ; preds = %10
  %17 = call i32 @psa_wipe_key_slot(ptr noundef %15) #4
  %18 = icmp eq i32 %14, -140
  %spec.store.select = select i1 %18, i32 -136, i32 %14
  br label %21

19:                                               ; preds = %10
  %20 = getelementptr inbounds i8, ptr %15, i64 12
  call void @psa_extend_key_usage_flags(ptr noundef nonnull %20)
  br label %21

21:                                               ; preds = %16, %19, %8
  %.018 = phi i32 [ %9, %8 ], [ %spec.store.select, %16 ], [ 0, %19 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #4
  br label %22

22:                                               ; preds = %6, %2, %21
  %.119 = phi i32 [ %.018, %21 ], [ -137, %2 ], [ %7, %6 ]
  ret i32 %.119
}

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_get_and_lock_key_slot_in_memory(i32 noundef, ptr noundef) local_unnamed_addr #2

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @psa_extend_key_usage_flags(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = { 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 #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, !13, i64 8}
!10 = !{!"", !11, i64 0, !15, i64 32, !16, i64 40}
!11 = !{!"", !12, i64 0, !12, i64 2, !13, i64 4, !13, i64 8, !14, i64 12, !12, i64 24}
!12 = !{!"short", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!"psa_key_policy_s", !13, i64 0, !13, i64 4, !13, i64 8}
!15 = !{!"long", !7, i64 0}
!16 = !{!"key_data", !6, i64 0, !15, i64 8}
!17 = !{!10, !13, i64 4}


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