; 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_key_slot_with_policy(i32 noundef, ptr noundef, i32 noundef, i32 noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_export_public_key(i32 noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #3 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca %struct.psa_key_attributes_s, align 8
  store i32 %0, ptr %6, align 4, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !11
  store ptr %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 -151, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 -151, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %15 = load i64, ptr %8, align 8, !tbaa !11
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %4
  store i32 -138, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %65

18:                                               ; preds = %4
  %19 = load ptr, ptr %9, align 8, !tbaa !9
  store i64 0, ptr %19, align 8, !tbaa !11
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = call i32 @psa_get_and_lock_key_slot_with_policy(i32 noundef %20, ptr noundef %12, i32 noundef 0, i32 noundef 0)
  store i32 %21, ptr %10, align 4, !tbaa !5
  %22 = load i32, ptr %10, align 4, !tbaa !5
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %18
  %25 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %25, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %65

26:                                               ; preds = %18
  %27 = load ptr, ptr %12, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.psa_key_slot_t, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %28, i32 0, i32 0
  %30 = load i16, ptr %29, align 8, !tbaa !13
  %31 = zext i16 %30 to i32
  %32 = and i32 %31, 28672
  %33 = and i32 %32, -12289
  %34 = icmp eq i32 %33, 16384
  br i1 %34, label %36, label %35

35:                                               ; preds = %26
  store i32 -135, ptr %10, align 4, !tbaa !5
  br label %54

36:                                               ; preds = %26
  %37 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %14, i32 0, i32 0
  %38 = load ptr, ptr %12, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.psa_key_slot_t, ptr %38, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %37, ptr align 8 %39, i64 28, i1 false), !tbaa.struct !19
  %40 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %14, i32 0, i32 1
  store ptr null, ptr %40, align 8, !tbaa !21
  %41 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %14, i32 0, i32 2
  store i64 0, ptr %41, align 8, !tbaa !23
  %42 = load ptr, ptr %12, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.psa_key_slot_t, ptr %42, i32 0, i32 2
  %44 = getelementptr inbounds %struct.key_data, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !24
  %46 = load ptr, ptr %12, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.psa_key_slot_t, ptr %46, i32 0, i32 2
  %48 = getelementptr inbounds %struct.key_data, ptr %47, i32 0, i32 1
  %49 = load i64, ptr %48, align 8, !tbaa !25
  %50 = load ptr, ptr %7, align 8, !tbaa !9
  %51 = load i64, ptr %8, align 8, !tbaa !11
  %52 = load ptr, ptr %9, align 8, !tbaa !9
  %53 = call i32 @psa_driver_wrapper_export_public_key(ptr noundef %14, ptr noundef %45, i64 noundef %49, ptr noundef %50, i64 noundef %51, ptr noundef %52)
  store i32 %53, ptr %10, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %36, %35
  %55 = load ptr, ptr %12, align 8, !tbaa !9
  %56 = call i32 @psa_unlock_key_slot(ptr noundef %55)
  store i32 %56, ptr %11, align 4, !tbaa !5
  %57 = load i32, ptr %10, align 4, !tbaa !5
  %58 = icmp eq i32 %57, 0
  br i1 %58, label %59, label %61

59:                                               ; preds = %54
  %60 = load i32, ptr %11, align 4, !tbaa !5
  br label %63

61:                                               ; preds = %54
  %62 = load i32, ptr %10, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %61, %59
  %64 = phi i32 [ %60, %59 ], [ %62, %61 ]
  store i32 %64, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %65

65:                                               ; preds = %63, %24, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %66 = load i32, ptr %5, align 4
  ret i32 %66
}

declare i32 @psa_driver_wrapper_export_public_key(ptr 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_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_export_public_key(i32 noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #3 {
  %5 = alloca ptr, align 8
  %6 = alloca %struct.psa_key_attributes_s, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #5
  %7 = icmp eq i64 %2, 0
  br i1 %7, label %25, label %8

8:                                                ; preds = %4
  store i64 0, ptr %3, align 8, !tbaa !5
  %9 = call i32 @psa_get_and_lock_key_slot_with_policy(i32 noundef %0, ptr noundef nonnull %5, i32 noundef 0, i32 noundef 0)
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %10, label %25

10:                                               ; preds = %8
  %11 = load ptr, ptr %5, align 8, !tbaa !9
  %12 = load i16, ptr %11, align 8, !tbaa !11
  %13 = and i16 %12, 16384
  %.not14 = icmp eq i16 %13, 0
  br i1 %.not14, label %.thread, label %15

.thread:                                          ; preds = %10
  %14 = call i32 @psa_unlock_key_slot(ptr noundef nonnull %11) #5
  br label %25

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

25:                                               ; preds = %15, %.thread, %8, %4
  %.011 = phi i32 [ -138, %4 ], [ %9, %8 ], [ -135, %.thread ], [ %spec.select, %15 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #5
  ret i32 %.011
}

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