; 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_attributes_s = type { %struct.psa_core_key_attributes_t, ptr, i64 }
%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.psa_key_slot_t = type { %struct.psa_core_key_attributes_t, i64, %struct.key_data }
%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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare i32 @psa_unlock_key_slot(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_asymmetric_encrypt(i32 noundef %0, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7, ptr noundef %8) #3 {
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i64, align 8
  %17 = alloca ptr, align 8
  %18 = alloca i64, align 8
  %19 = alloca ptr, align 8
  %20 = alloca i32, align 4
  %21 = alloca i32, align 4
  %22 = alloca ptr, align 8
  %23 = alloca i32, align 4
  %24 = alloca %struct.psa_key_attributes_s, align 8
  store i32 %0, ptr %11, align 4, !tbaa !5
  store i32 %1, ptr %12, align 4, !tbaa !5
  store ptr %2, ptr %13, align 8, !tbaa !9
  store i64 %3, ptr %14, align 8, !tbaa !11
  store ptr %4, ptr %15, align 8, !tbaa !9
  store i64 %5, ptr %16, align 8, !tbaa !11
  store ptr %6, ptr %17, align 8, !tbaa !9
  store i64 %7, ptr %18, align 8, !tbaa !11
  store ptr %8, ptr %19, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #4
  store i32 -151, ptr %20, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %21) #4
  store i32 -151, ptr %21, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %22) #4
  %25 = load ptr, ptr %13, align 8, !tbaa !9
  %26 = load i64, ptr %14, align 8, !tbaa !11
  %27 = load ptr, ptr %15, align 8, !tbaa !9
  %28 = load ptr, ptr %17, align 8, !tbaa !9
  %29 = load i64, ptr %18, align 8, !tbaa !11
  %30 = load ptr, ptr %19, align 8, !tbaa !9
  store i64 0, ptr %30, align 8, !tbaa !11
  %31 = load i32, ptr %12, align 4, !tbaa !5
  %32 = and i32 %31, -256
  %33 = icmp eq i32 %32, 117441280
  br i1 %33, label %38, label %34

34:                                               ; preds = %9
  %35 = load i64, ptr %16, align 8, !tbaa !11
  %36 = icmp ne i64 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %34
  store i32 -135, ptr %10, align 4
  store i32 1, ptr %23, align 4
  br label %97

38:                                               ; preds = %34, %9
  %39 = load i32, ptr %11, align 4, !tbaa !5
  %40 = load i32, ptr %12, align 4, !tbaa !5
  %41 = call i32 @psa_get_and_lock_transparent_key_slot_with_policy(i32 noundef %39, ptr noundef %22, i32 noundef 256, i32 noundef %40)
  store i32 %41, ptr %20, align 4, !tbaa !5
  %42 = load i32, ptr %20, align 4, !tbaa !5
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %46

44:                                               ; preds = %38
  %45 = load i32, ptr %20, align 4, !tbaa !5
  store i32 %45, ptr %10, align 4
  store i32 1, ptr %23, align 4
  br label %97

46:                                               ; preds = %38
  %47 = load ptr, ptr %22, 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 0
  %50 = load i16, ptr %49, align 8, !tbaa !13
  %51 = zext i16 %50 to i32
  %52 = and i32 %51, 28672
  %53 = icmp eq i32 %52, 16384
  br i1 %53, label %63, label %54

54:                                               ; preds = %46
  %55 = load ptr, ptr %22, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.psa_key_slot_t, ptr %55, i32 0, i32 0
  %57 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %56, i32 0, i32 0
  %58 = load i16, ptr %57, align 8, !tbaa !13
  %59 = zext i16 %58 to i32
  %60 = and i32 %59, 28672
  %61 = icmp eq i32 %60, 28672
  br i1 %61, label %63, label %62

62:                                               ; preds = %54
  store i32 -135, ptr %20, align 4, !tbaa !5
  br label %86

63:                                               ; preds = %54, %46
  %64 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %24, i32 0, i32 0
  %65 = load ptr, ptr %22, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.psa_key_slot_t, ptr %65, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %64, ptr align 8 %66, i64 28, i1 false), !tbaa.struct !19
  %67 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %24, i32 0, i32 1
  store ptr null, ptr %67, align 8, !tbaa !21
  %68 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %24, i32 0, i32 2
  store i64 0, ptr %68, align 8, !tbaa !23
  %69 = load ptr, ptr %22, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.psa_key_slot_t, ptr %69, i32 0, i32 2
  %71 = getelementptr inbounds %struct.key_data, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !24
  %73 = load ptr, ptr %22, align 8, !tbaa !9
  %74 = getelementptr inbounds %struct.psa_key_slot_t, ptr %73, i32 0, i32 2
  %75 = getelementptr inbounds %struct.key_data, ptr %74, i32 0, i32 1
  %76 = load i64, ptr %75, align 8, !tbaa !25
  %77 = load i32, ptr %12, align 4, !tbaa !5
  %78 = load ptr, ptr %13, align 8, !tbaa !9
  %79 = load i64, ptr %14, align 8, !tbaa !11
  %80 = load ptr, ptr %15, align 8, !tbaa !9
  %81 = load i64, ptr %16, align 8, !tbaa !11
  %82 = load ptr, ptr %17, align 8, !tbaa !9
  %83 = load i64, ptr %18, align 8, !tbaa !11
  %84 = load ptr, ptr %19, align 8, !tbaa !9
  %85 = call i32 @psa_driver_wrapper_asymmetric_encrypt(ptr noundef %24, ptr noundef %72, i64 noundef %76, i32 noundef %77, ptr noundef %78, i64 noundef %79, ptr noundef %80, i64 noundef %81, ptr noundef %82, i64 noundef %83, ptr noundef %84)
  store i32 %85, ptr %20, align 4, !tbaa !5
  br label %86

86:                                               ; preds = %63, %62
  %87 = load ptr, ptr %22, align 8, !tbaa !9
  %88 = call i32 @psa_unlock_key_slot(ptr noundef %87)
  store i32 %88, ptr %21, align 4, !tbaa !5
  %89 = load i32, ptr %20, align 4, !tbaa !5
  %90 = icmp eq i32 %89, 0
  br i1 %90, label %91, label %93

91:                                               ; preds = %86
  %92 = load i32, ptr %21, align 4, !tbaa !5
  br label %95

93:                                               ; preds = %86
  %94 = load i32, ptr %20, align 4, !tbaa !5
  br label %95

95:                                               ; preds = %93, %91
  %96 = phi i32 [ %92, %91 ], [ %94, %93 ]
  store i32 %96, ptr %10, align 4
  store i32 1, ptr %23, align 4
  br label %97

97:                                               ; preds = %95, %44, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %22) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %21) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #4
  %98 = load i32, ptr %10, align 4
  ret i32 %98
}

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

declare i32 @psa_driver_wrapper_asymmetric_encrypt(ptr noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) #2

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


; 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_key_attributes_s = type { %struct.psa_core_key_attributes_t, ptr, i64 }
%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 }

; 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare i32 @psa_unlock_key_slot(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_asymmetric_encrypt(i32 noundef %0, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7, ptr noundef %8) local_unnamed_addr #3 {
  %10 = alloca ptr, align 8
  %11 = alloca %struct.psa_key_attributes_s, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %10) #5
  store i64 0, ptr %8, align 8, !tbaa !5
  %12 = and i32 %1, -256
  %13 = icmp ne i32 %12, 117441280
  %14 = icmp ne i64 %5, 0
  %or.cond = and i1 %13, %14
  br i1 %or.cond, label %32, label %15

15:                                               ; preds = %9
  %16 = call i32 @psa_get_and_lock_transparent_key_slot_with_policy(i32 noundef %0, ptr noundef nonnull %10, i32 noundef 256, i32 noundef %1)
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %17, label %32

17:                                               ; preds = %15
  %18 = load ptr, ptr %10, align 8, !tbaa !9
  %19 = load i16, ptr %18, align 8, !tbaa !11
  %20 = and i16 %19, 28672
  switch i16 %20, label %.thread [
    i16 16384, label %22
    i16 28672, label %22
  ]

.thread:                                          ; preds = %17
  %21 = call i32 @psa_unlock_key_slot(ptr noundef nonnull %18) #5
  br label %32

22:                                               ; preds = %17, %17
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(28) %11, ptr noundef nonnull align 8 dereferenceable(28) %18, i64 28, i1 false), !tbaa.struct !18
  %23 = getelementptr inbounds i8, ptr %11, i64 32
  %24 = getelementptr inbounds i8, ptr %18, i64 40
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %23, i8 0, i64 16, i1 false)
  %25 = load ptr, ptr %24, align 8, !tbaa !21
  %26 = getelementptr inbounds i8, ptr %18, i64 48
  %27 = load i64, ptr %26, align 8, !tbaa !22
  %28 = call i32 @psa_driver_wrapper_asymmetric_encrypt(ptr noundef nonnull %11, ptr noundef %25, i64 noundef %27, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7, ptr noundef nonnull %8) #5
  %.fr = freeze i32 %28
  %29 = load ptr, ptr %10, align 8, !tbaa !9
  %30 = call i32 @psa_unlock_key_slot(ptr noundef %29) #5
  %31 = icmp eq i32 %.fr, 0
  %spec.select = select i1 %31, i32 %30, i32 %.fr
  br label %32

32:                                               ; preds = %22, %.thread, %15, %9
  %.024 = phi i32 [ -135, %9 ], [ %16, %15 ], [ -135, %.thread ], [ %spec.select, %22 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %10) #5
  ret i32 %.024
}

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

declare i32 @psa_driver_wrapper_asymmetric_encrypt(ptr noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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, !14, i64 0}
!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 = !{i64 0, i64 2, !19, i64 2, i64 2, !19, i64 4, i64 4, !20, i64 8, i64 4, !20, i64 12, i64 4, !20, i64 16, i64 4, !20, i64 20, i64 4, !20, i64 24, i64 2, !19}
!19 = !{!14, !14, i64 0}
!20 = !{!15, !15, i64 0}
!21 = !{!12, !10, i64 40}
!22 = !{!12, !6, i64 48}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,MemCpyOptPass,SROAPass,SimplifyCFGPass