; LLVM IR from machinefi/psa-crypto-esp32 :: psa_layer (psa_crypto)

; 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_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 }

; 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
declare dso_local i32 @psa_wipe_key_slot(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_destroy_key(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = call i32 @iotex_svc_key_id_is_null(i32 noundef %8)
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store i32 0, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %67

12:                                               ; preds = %1
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = call i32 @psa_get_and_lock_key_slot(i32 noundef %13, ptr noundef %4)
  store i32 %14, ptr %5, align 4, !tbaa !5
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %19

17:                                               ; preds = %12
  %18 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %18, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %67

19:                                               ; preds = %12
  %20 = load ptr, ptr %4, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.psa_key_slot_t, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = icmp ugt i64 %22, 1
  br i1 %23, label %24, label %27

24:                                               ; preds = %19
  %25 = load ptr, ptr %4, align 8, !tbaa !9
  %26 = call i32 @psa_unlock_key_slot(ptr noundef %25)
  store i32 -132, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %67

27:                                               ; preds = %19
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  %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 !18
  %32 = and i32 %31, 255
  %33 = trunc i32 %32 to i8
  %34 = zext i8 %33 to i32
  %35 = icmp eq i32 %34, 255
  br i1 %35, label %36, label %37

36:                                               ; preds = %27
  store i32 -133, ptr %6, align 4, !tbaa !5
  br label %58

37:                                               ; preds = %27
  %38 = load ptr, ptr %4, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.psa_key_slot_t, ptr %38, i32 0, i32 0
  %40 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %39, i32 0, i32 2
  %41 = load i32, ptr %40, align 4, !tbaa !18
  %42 = and i32 %41, 255
  %43 = trunc i32 %42 to i8
  %44 = zext i8 %43 to i32
  %45 = icmp eq i32 %44, 0
  br i1 %45, label %57, label %46

46:                                               ; preds = %37
  %47 = load ptr, ptr %4, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.psa_key_slot_t, ptr %47, i32 0, i32 0
  %49 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %48, i32 0, i32 3
  %50 = load i32, ptr %49, align 8, !tbaa !19
  %51 = call i32 @psa_destroy_persistent_key(i32 noundef %50)
  store i32 %51, ptr %5, align 4, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %56

54:                                               ; preds = %46
  %55 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %55, ptr %6, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %54, %46
  br label %57

57:                                               ; preds = %56, %37
  br label %58

58:                                               ; preds = %57, %36
  %59 = load ptr, ptr %4, align 8, !tbaa !9
  %60 = call i32 @psa_wipe_key_slot(ptr noundef %59)
  store i32 %60, ptr %5, align 4, !tbaa !5
  %61 = load i32, ptr %5, align 4, !tbaa !5
  %62 = icmp ne i32 %61, 0
  br i1 %62, label %63, label %65

63:                                               ; preds = %58
  %64 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %64, ptr %6, align 4, !tbaa !5
  br label %65

65:                                               ; preds = %63, %58
  %66 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %66, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %67

67:                                               ; preds = %65, %24, %17, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %68 = load i32, ptr %2, align 4
  ret i32 %68
}

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

declare i32 @psa_get_and_lock_key_slot(i32 noundef, ptr noundef) #3

declare i32 @psa_unlock_key_slot(ptr noundef) #3

declare i32 @psa_destroy_persistent_key(i32 noundef) #3

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 = { 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 = { "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, !16, i64 32}
!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}
!19 = !{!12, !6, i64 8}


; 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"

; 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

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_wipe_key_slot(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_destroy_key(i32 noundef %0) local_unnamed_addr #1 {
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4
  %3 = tail call i32 @iotex_svc_key_id_is_null(i32 noundef %0)
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %23

4:                                                ; preds = %1
  %5 = call i32 @psa_get_and_lock_key_slot(i32 noundef %0, ptr noundef nonnull %2) #4
  %.not14 = icmp eq i32 %5, 0
  br i1 %.not14, label %6, label %23

6:                                                ; preds = %4
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 32
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %10 = icmp ugt i64 %9, 1
  br i1 %10, label %11, label %13

11:                                               ; preds = %6
  %12 = call i32 @psa_unlock_key_slot(ptr noundef nonnull %7) #4
  br label %23

13:                                               ; preds = %6
  %14 = getelementptr inbounds i8, ptr %7, i64 4
  %15 = load i32, ptr %14, align 4, !tbaa !17
  %trunc = trunc i32 %15 to i8
  switch i8 %trunc, label %16 [
    i8 -1, label %20
    i8 0, label %.fold.split
  ]

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %7, i64 8
  %18 = load i32, ptr %17, align 8, !tbaa !18
  %19 = call i32 @psa_destroy_persistent_key(i32 noundef %18) #4
  %.pre = load ptr, ptr %2, align 8, !tbaa !5
  br label %20

.fold.split:                                      ; preds = %13
  br label %20

20:                                               ; preds = %13, %.fold.split, %16
  %21 = phi ptr [ %.pre, %16 ], [ %7, %13 ], [ %7, %.fold.split ]
  %.0 = phi i32 [ %19, %16 ], [ -133, %13 ], [ 0, %.fold.split ]
  %22 = call i32 @psa_wipe_key_slot(ptr noundef %21)
  %.not15 = icmp eq i32 %22, 0
  %spec.select = select i1 %.not15, i32 %.0, i32 %22
  br label %23

23:                                               ; preds = %4, %1, %20, %11
  %.010 = phi i32 [ -132, %11 ], [ %spec.select, %20 ], [ 0, %1 ], [ %5, %4 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4
  ret i32 %.010
}

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

declare i32 @psa_get_and_lock_key_slot(i32 noundef, ptr noundef) local_unnamed_addr #3

declare i32 @psa_unlock_key_slot(ptr noundef) local_unnamed_addr #3

declare i32 @psa_destroy_persistent_key(i32 noundef) local_unnamed_addr #3

attributes #0 = { mustprogress 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 = { 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 = { "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, !15, i64 32}
!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}
!18 = !{!10, !13, i64 8}


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