; 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
define dso_local i32 @psa_get_empty_key_slot(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 -151, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %12 = load i8, ptr getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 1), align 8
  %13 = and i8 %12, 1
  %14 = zext i8 %13 to i32
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %17, label %16

16:                                               ; preds = %2
  store i32 -137, ptr %6, align 4, !tbaa !9
  br label %86

17:                                               ; preds = %2
  store ptr null, ptr %9, align 8, !tbaa !5
  store ptr null, ptr %8, align 8, !tbaa !5
  store i64 0, ptr %7, align 8, !tbaa !11
  br label %18

18:                                               ; preds = %51, %17
  %19 = load i64, ptr %7, align 8, !tbaa !11
  %20 = icmp ult i64 %19, 4
  br i1 %20, label %21, label %54

21:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %22 = load i64, ptr %7, align 8, !tbaa !11
  %23 = getelementptr inbounds [4 x %struct.psa_key_slot_t], ptr @global_data, i64 0, i64 %22
  store ptr %23, ptr %10, align 8, !tbaa !5
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = call i32 @psa_is_key_slot_occupied(ptr noundef %24)
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %29, label %27

27:                                               ; preds = %21
  %28 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %28, ptr %8, align 8, !tbaa !5
  store i32 3, ptr %11, align 4
  br label %48

29:                                               ; preds = %21
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = icmp eq ptr %30, null
  br i1 %31, label %32, label %47

32:                                               ; preds = %29
  %33 = load ptr, ptr %10, align 8, !tbaa !5
  %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 2
  %36 = load i32, ptr %35, align 4, !tbaa !13
  %37 = and i32 %36, 255
  %38 = trunc i32 %37 to i8
  %39 = zext i8 %38 to i32
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %47, label %41

41:                                               ; preds = %32
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = call i32 @psa_is_key_slot_locked(ptr noundef %42)
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %47, label %45

45:                                               ; preds = %41
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %46, ptr %9, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %45, %41, %32, %29
  store i32 0, ptr %11, align 4
  br label %48

48:                                               ; preds = %47, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %49 = load i32, ptr %11, align 4
  switch i32 %49, label %92 [
    i32 0, label %50
    i32 3, label %54
  ]

50:                                               ; preds = %48
  br label %51

51:                                               ; preds = %50
  %52 = load i64, ptr %7, align 8, !tbaa !11
  %53 = add i64 %52, 1
  store i64 %53, ptr %7, align 8, !tbaa !11
  br label %18, !llvm.loop !19

54:                                               ; preds = %48, %18
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = icmp eq ptr %55, null
  br i1 %56, label %57, label %66

57:                                               ; preds = %54
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = icmp ne ptr %58, null
  br i1 %59, label %60, label %66

60:                                               ; preds = %57
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %61, ptr %8, align 8, !tbaa !5
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.psa_key_slot_t, ptr %62, i32 0, i32 1
  store i64 1, ptr %63, align 8, !tbaa !21
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = call i32 @psa_wipe_key_slot(ptr noundef %64)
  br label %66

66:                                               ; preds = %60, %57, %54
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = icmp ne ptr %67, null
  br i1 %68, label %69, label %85

69:                                               ; preds = %66
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = call i32 @psa_lock_key_slot(ptr noundef %70)
  store i32 %71, ptr %6, align 4, !tbaa !9
  %72 = load i32, ptr %6, align 4, !tbaa !9
  %73 = icmp ne i32 %72, 0
  br i1 %73, label %74, label %75

74:                                               ; preds = %69
  br label %86

75:                                               ; preds = %69
  %76 = load ptr, ptr %8, align 8, !tbaa !5
  %77 = ptrtoint ptr %76 to i64
  %78 = sub i64 %77, ptrtoint (ptr @global_data to i64)
  %79 = sdiv exact i64 %78, 56
  %80 = trunc i64 %79 to i32
  %81 = add i32 2147483644, %80
  %82 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 %81, ptr %82, align 4, !tbaa !9
  %83 = load ptr, ptr %8, align 8, !tbaa !5
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %83, ptr %84, align 8, !tbaa !5
  store i32 0, ptr %3, align 4
  store i32 1, ptr %11, align 4
  br label %90

85:                                               ; preds = %66
  store i32 -141, ptr %6, align 4, !tbaa !9
  br label %86

86:                                               ; preds = %85, %74, %16
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr null, ptr %87, align 8, !tbaa !5
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 0, ptr %88, align 4, !tbaa !9
  %89 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %89, ptr %3, align 4
  store i32 1, ptr %11, align 4
  br label %90

90:                                               ; preds = %86, %75
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %91 = load i32, ptr %3, align 4
  ret i32 %91

92:                                               ; preds = %48
  unreachable
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @psa_lock_key_slot(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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 4}
!14 = !{!"", !15, i64 0, !12, i64 32, !18, i64 40}
!15 = !{!"", !16, i64 0, !16, i64 2, !10, i64 4, !10, i64 8, !17, i64 12, !16, i64 24}
!16 = !{!"short", !7, i64 0}
!17 = !{!"psa_key_policy_s", !10, i64 0, !10, i64 4, !10, i64 8}
!18 = !{!"key_data", !6, i64 0, !12, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!14, !12, i64 32}


; 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 global %struct.psa_global_data_t, align 8

declare i32 @psa_wipe_key_slot(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_get_empty_key_slot(ptr nocapture noundef writeonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = load i8, ptr getelementptr inbounds (i8, ptr @global_data, i64 224), align 8
  %4 = and i8 %3, 1
  %.not = icmp eq i8 %4, 0
  br i1 %.not, label %.thread73, label %.preheader.preheader

.preheader.preheader:                             ; preds = %2
  %5 = tail call i32 @psa_is_key_slot_occupied(ptr noundef nonnull @global_data)
  %.not35 = icmp eq i32 %5, 0
  br i1 %.not35, label %.thread45, label %6

6:                                                ; preds = %.preheader.preheader
  %7 = load i32, ptr getelementptr inbounds (i8, ptr @global_data, i64 4), align 4, !tbaa !5
  %8 = and i32 %7, 255
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %.preheader.1.thread, label %.preheader.1

.preheader.1:                                     ; preds = %6
  %10 = tail call i32 @psa_is_key_slot_locked(ptr noundef nonnull @global_data)
  %11 = tail call i32 @psa_is_key_slot_occupied(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 56))
  %.not35.1 = icmp eq i32 %11, 0
  br i1 %.not35.1, label %.thread45, label %13

.preheader.1.thread:                              ; preds = %6
  %12 = tail call i32 @psa_is_key_slot_occupied(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 56))
  %.not35.160 = icmp eq i32 %12, 0
  br i1 %.not35.160, label %.thread45, label %.thread

13:                                               ; preds = %.preheader.1
  %.not36.not = icmp eq i32 %10, 0
  %spec.select = select i1 %.not36.not, ptr @global_data, ptr null
  br i1 %.not36.not, label %.preheader.2, label %.thread

.thread:                                          ; preds = %.preheader.1.thread, %13
  %14 = load i32, ptr getelementptr inbounds (i8, ptr @global_data, i64 60), align 4, !tbaa !5
  %15 = and i32 %14, 255
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %.preheader.2.thread, label %17

17:                                               ; preds = %.thread
  %18 = tail call i32 @psa_is_key_slot_locked(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 56))
  %.not36.1 = icmp eq i32 %18, 0
  %spec.select.1 = select i1 %.not36.1, ptr getelementptr inbounds (i8, ptr @global_data, i64 56), ptr null
  br label %.preheader.2

.preheader.2:                                     ; preds = %17, %13
  %.2.ph.1 = phi ptr [ %spec.select.1, %17 ], [ %spec.select, %13 ]
  %19 = tail call i32 @psa_is_key_slot_occupied(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 112))
  %.not35.2 = icmp eq i32 %19, 0
  br i1 %.not35.2, label %.thread45, label %21

.preheader.2.thread:                              ; preds = %.thread
  %20 = tail call i32 @psa_is_key_slot_occupied(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 112))
  %.not35.264 = icmp eq i32 %20, 0
  br i1 %.not35.264, label %.thread45, label %.thread66

21:                                               ; preds = %.preheader.2
  %22 = icmp eq ptr %.2.ph.1, null
  br i1 %22, label %.thread66, label %.preheader.3

.thread66:                                        ; preds = %.preheader.2.thread, %21
  %23 = load i32, ptr getelementptr inbounds (i8, ptr @global_data, i64 116), align 4, !tbaa !5
  %24 = and i32 %23, 255
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %.preheader.3.thread, label %26

26:                                               ; preds = %.thread66
  %27 = tail call i32 @psa_is_key_slot_locked(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 112))
  %.not36.2 = icmp eq i32 %27, 0
  %spec.select.2 = select i1 %.not36.2, ptr getelementptr inbounds (i8, ptr @global_data, i64 112), ptr null
  br label %.preheader.3

.preheader.3:                                     ; preds = %26, %21
  %.2.ph.2 = phi ptr [ %spec.select.2, %26 ], [ %.2.ph.1, %21 ]
  %28 = tail call i32 @psa_is_key_slot_occupied(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 168))
  %.not35.3 = icmp eq i32 %28, 0
  br i1 %.not35.3, label %.thread45, label %30

.preheader.3.thread:                              ; preds = %.thread66
  %29 = tail call i32 @psa_is_key_slot_occupied(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 168))
  %.not35.369 = icmp eq i32 %29, 0
  br i1 %.not35.369, label %.thread45, label %.thread71

30:                                               ; preds = %.preheader.3
  %31 = icmp eq ptr %.2.ph.2, null
  br i1 %31, label %.thread71, label %select.unfold

.thread71:                                        ; preds = %.preheader.3.thread, %30
  %32 = load i32, ptr getelementptr inbounds (i8, ptr @global_data, i64 172), align 4, !tbaa !5
  %33 = and i32 %32, 255
  %34 = icmp eq i32 %33, 0
  br i1 %34, label %.thread73, label %35

35:                                               ; preds = %.thread71
  %36 = tail call i32 @psa_is_key_slot_locked(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 168))
  %.not36.3 = icmp eq i32 %36, 0
  br i1 %.not36.3, label %select.unfold, label %.thread73

select.unfold:                                    ; preds = %35, %30
  %.2.ph.3 = phi ptr [ %.2.ph.2, %30 ], [ getelementptr inbounds (i8, ptr @global_data, i64 168), %35 ]
  %37 = getelementptr inbounds i8, ptr %.2.ph.3, i64 32
  store i64 1, ptr %37, align 8, !tbaa !16
  %38 = tail call i32 @psa_wipe_key_slot(ptr noundef nonnull %.2.ph.3) #3
  br label %.thread45

.thread45:                                        ; preds = %.preheader.preheader, %.preheader.1, %.preheader.2, %.preheader.3, %.preheader.1.thread, %.preheader.2.thread, %.preheader.3.thread, %select.unfold
  %.331.ph = phi ptr [ %.2.ph.3, %select.unfold ], [ @global_data, %.preheader.preheader ], [ getelementptr inbounds (i8, ptr @global_data, i64 56), %.preheader.1 ], [ getelementptr inbounds (i8, ptr @global_data, i64 112), %.preheader.2 ], [ getelementptr inbounds (i8, ptr @global_data, i64 168), %.preheader.3 ], [ getelementptr inbounds (i8, ptr @global_data, i64 56), %.preheader.1.thread ], [ getelementptr inbounds (i8, ptr @global_data, i64 112), %.preheader.2.thread ], [ getelementptr inbounds (i8, ptr @global_data, i64 168), %.preheader.3.thread ]
  %39 = tail call i32 @psa_lock_key_slot(ptr noundef nonnull %.331.ph)
  %.not38 = icmp eq i32 %39, 0
  br i1 %.not38, label %40, label %.thread73

40:                                               ; preds = %.thread45
  %41 = ptrtoint ptr %.331.ph to i64
  %42 = sub i64 %41, ptrtoint (ptr @global_data to i64)
  %43 = sdiv exact i64 %42, 56
  %44 = trunc i64 %43 to i32
  %45 = add i32 %44, 2147483644
  store i32 %45, ptr %0, align 4, !tbaa !17
  store ptr %.331.ph, ptr %1, align 8, !tbaa !18
  br label %46

.thread73:                                        ; preds = %.thread71, %35, %2, %.thread45
  %.033 = phi i32 [ %39, %.thread45 ], [ -137, %2 ], [ -141, %35 ], [ -141, %.thread71 ]
  store ptr null, ptr %1, align 8, !tbaa !18
  store i32 0, ptr %0, align 4, !tbaa !17
  br label %46

46:                                               ; preds = %.thread73, %40
  %.026 = phi i32 [ %.033, %.thread73 ], [ 0, %40 ]
  ret i32 %.026
}

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

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

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

attributes #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 = { 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 #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, !11, i64 4}
!6 = !{!"", !7, i64 0, !13, i64 32, !14, i64 40}
!7 = !{!"", !8, i64 0, !8, i64 2, !11, i64 4, !11, i64 8, !12, i64 12, !8, i64 24}
!8 = !{!"short", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"psa_key_policy_s", !11, i64 0, !11, i64 4, !11, i64 8}
!13 = !{!"long", !9, i64 0}
!14 = !{!"key_data", !15, i64 0, !13, i64 8}
!15 = !{!"any pointer", !9, i64 0}
!16 = !{!6, !13, i64 32}
!17 = !{!11, !11, i64 0}
!18 = !{!15, !15, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass