; 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
declare hidden i32 @psa_get_and_lock_transparent_key_slot_with_policy(i32 noundef, ptr noundef, i32 noundef, i32 noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_asymmetric_decrypt(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 %89

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 512, 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 %89

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, 28672
  br i1 %53, label %55, label %54

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

55:                                               ; preds = %46
  %56 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %24, i32 0, i32 0
  %57 = load ptr, ptr %22, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.psa_key_slot_t, ptr %57, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %56, ptr align 8 %58, i64 28, i1 false), !tbaa.struct !19
  %59 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %24, i32 0, i32 1
  store ptr null, ptr %59, align 8, !tbaa !21
  %60 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %24, i32 0, i32 2
  store i64 0, ptr %60, align 8, !tbaa !23
  %61 = load ptr, ptr %22, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.psa_key_slot_t, ptr %61, i32 0, i32 2
  %63 = getelementptr inbounds %struct.key_data, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !24
  %65 = load ptr, ptr %22, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.psa_key_slot_t, ptr %65, i32 0, i32 2
  %67 = getelementptr inbounds %struct.key_data, ptr %66, i32 0, i32 1
  %68 = load i64, ptr %67, align 8, !tbaa !25
  %69 = load i32, ptr %12, align 4, !tbaa !5
  %70 = load ptr, ptr %13, align 8, !tbaa !9
  %71 = load i64, ptr %14, align 8, !tbaa !11
  %72 = load ptr, ptr %15, align 8, !tbaa !9
  %73 = load i64, ptr %16, align 8, !tbaa !11
  %74 = load ptr, ptr %17, align 8, !tbaa !9
  %75 = load i64, ptr %18, align 8, !tbaa !11
  %76 = load ptr, ptr %19, align 8, !tbaa !9
  %77 = call i32 @psa_driver_wrapper_asymmetric_decrypt(ptr noundef %24, ptr noundef %64, i64 noundef %68, i32 noundef %69, ptr noundef %70, i64 noundef %71, ptr noundef %72, i64 noundef %73, ptr noundef %74, i64 noundef %75, ptr noundef %76)
  store i32 %77, ptr %20, align 4, !tbaa !5
  br label %78

78:                                               ; preds = %55, %54
  %79 = load ptr, ptr %22, align 8, !tbaa !9
  %80 = call i32 @psa_unlock_key_slot(ptr noundef %79)
  store i32 %80, ptr %21, align 4, !tbaa !5
  %81 = load i32, ptr %20, align 4, !tbaa !5
  %82 = icmp eq i32 %81, 0
  br i1 %82, label %83, label %85

83:                                               ; preds = %78
  %84 = load i32, ptr %21, align 4, !tbaa !5
  br label %87

85:                                               ; preds = %78
  %86 = load i32, ptr %20, align 4, !tbaa !5
  br label %87

87:                                               ; preds = %85, %83
  %88 = phi i32 [ %84, %83 ], [ %86, %85 ]
  store i32 %88, ptr %10, align 4
  store i32 1, ptr %23, align 4
  br label %89

89:                                               ; preds = %87, %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
  %90 = load i32, ptr %10, align 4
  ret i32 %90
}

declare i32 @psa_driver_wrapper_asymmetric_decrypt(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
declare hidden i32 @psa_get_and_lock_transparent_key_slot_with_policy(i32 noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_asymmetric_decrypt(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 %33, 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 512, i32 noundef %1)
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %17, label %33

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
  %21 = icmp eq i16 %20, 28672
  br i1 %21, label %23, label %.thread

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

23:                                               ; preds = %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
  %24 = getelementptr inbounds i8, ptr %11, i64 32
  %25 = getelementptr inbounds i8, ptr %18, i64 40
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %24, i8 0, i64 16, i1 false)
  %26 = load ptr, ptr %25, align 8, !tbaa !21
  %27 = getelementptr inbounds i8, ptr %18, i64 48
  %28 = load i64, ptr %27, align 8, !tbaa !22
  %29 = call i32 @psa_driver_wrapper_asymmetric_decrypt(ptr noundef nonnull %11, ptr noundef %26, i64 noundef %28, 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 %29
  %30 = load ptr, ptr %10, align 8, !tbaa !9
  %31 = call i32 @psa_unlock_key_slot(ptr noundef %30) #5
  %32 = icmp eq i32 %.fr, 0
  %spec.select = select i1 %32, i32 %31, i32 %.fr
  br label %33

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

declare i32 @psa_driver_wrapper_asymmetric_decrypt(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