; 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

; Function Attrs: nounwind uwtable
define dso_local void @psa_wipe_all_key_slots() #1 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %3

3:                                                ; preds = %13, %0
  %4 = load i64, ptr %1, align 8, !tbaa !5
  %5 = icmp ult i64 %4, 4
  br i1 %5, label %6, label %16

6:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %7 = load i64, ptr %1, align 8, !tbaa !5
  %8 = getelementptr inbounds [4 x %struct.psa_key_slot_t], ptr @global_data, i64 0, i64 %7
  store ptr %8, ptr %2, align 8, !tbaa !9
  %9 = load ptr, ptr %2, align 8, !tbaa !9
  %10 = getelementptr inbounds %struct.psa_key_slot_t, ptr %9, i32 0, i32 1
  store i64 1, ptr %10, align 8, !tbaa !11
  %11 = load ptr, ptr %2, align 8, !tbaa !9
  %12 = call i32 @psa_wipe_key_slot(ptr noundef %11)
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  br label %13

13:                                               ; preds = %6
  %14 = load i64, ptr %1, align 8, !tbaa !5
  %15 = add i64 %14, 1
  store i64 %15, ptr %1, align 8, !tbaa !5
  br label %3, !llvm.loop !18

16:                                               ; preds = %3
  %17 = load i8, ptr getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 1), align 8
  %18 = and i8 %17, -2
  %19 = or i8 %18, 0
  store i8 %19, ptr getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 1), align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

declare i32 @psa_wipe_key_slot(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 = !{!"long", !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 32}
!12 = !{!"", !13, i64 0, !6, i64 32, !17, i64 40}
!13 = !{!"", !14, i64 0, !14, i64 2, !15, i64 4, !15, i64 8, !16, i64 12, !14, i64 24}
!14 = !{!"short", !7, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!"psa_key_policy_s", !15, i64 0, !15, i64 4, !15, i64 8}
!17 = !{!"key_data", !10, i64 0, !6, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"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 { [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: nounwind uwtable
define dso_local void @psa_wipe_all_key_slots() local_unnamed_addr #0 {
  store i64 1, ptr getelementptr inbounds (i8, ptr @global_data, i64 32), align 8, !tbaa !5
  %1 = tail call i32 @psa_wipe_key_slot(ptr noundef nonnull @global_data) #2
  store i64 1, ptr getelementptr inbounds (i8, ptr @global_data, i64 88), align 8, !tbaa !5
  %2 = tail call i32 @psa_wipe_key_slot(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 56)) #2
  store i64 1, ptr getelementptr inbounds (i8, ptr @global_data, i64 144), align 8, !tbaa !5
  %3 = tail call i32 @psa_wipe_key_slot(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 112)) #2
  store i64 1, ptr getelementptr inbounds (i8, ptr @global_data, i64 200), align 8, !tbaa !5
  %4 = tail call i32 @psa_wipe_key_slot(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 168)) #2
  %5 = load i8, ptr getelementptr inbounds (i8, ptr @global_data, i64 224), align 8
  %6 = and i8 %5, -2
  store i8 %6, ptr getelementptr inbounds (i8, ptr @global_data, i64 224), align 8
  ret void
}

declare i32 @psa_wipe_key_slot(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 = !{!6, !13, i64 32}
!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}


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