; LLVM IR from machinefi/psa-crypto-arduino :: src/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 %110

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 %110

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 %99

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 i64, ptr %8, align 8, !tbaa !11
  %43 = load ptr, ptr %12, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.psa_key_slot_t, ptr %43, i32 0, i32 0
  %45 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %44, i32 0, i32 0
  %46 = load i16, ptr %45, align 8, !tbaa !13
  %47 = zext i16 %46 to i32
  %48 = and i32 %47, -12289
  %49 = icmp eq i32 %48, 16385
  br i1 %49, label %50, label %59

50:                                               ; preds = %36
  %51 = load ptr, ptr %12, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.psa_key_slot_t, ptr %51, i32 0, i32 0
  %53 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %52, i32 0, i32 1
  %54 = load i16, ptr %53, align 2, !tbaa !24
  %55 = zext i16 %54 to i32
  %56 = sdiv i32 %55, 8
  %57 = add nsw i32 %56, 5
  %58 = add nsw i32 %57, 11
  br label %81

59:                                               ; preds = %36
  %60 = load ptr, ptr %12, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.psa_key_slot_t, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %61, i32 0, i32 0
  %63 = load i16, ptr %62, align 8, !tbaa !13
  %64 = zext i16 %63 to i32
  %65 = and i32 %64, -12289
  %66 = and i32 %65, -256
  %67 = icmp eq i32 %66, 16640
  br i1 %67, label %68, label %78

68:                                               ; preds = %59
  %69 = load ptr, ptr %12, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.psa_key_slot_t, ptr %69, i32 0, i32 0
  %71 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %70, i32 0, i32 1
  %72 = load i16, ptr %71, align 2, !tbaa !24
  %73 = zext i16 %72 to i32
  %74 = add nsw i32 %73, 7
  %75 = sdiv i32 %74, 8
  %76 = mul nsw i32 2, %75
  %77 = add nsw i32 %76, 1
  br label %79

78:                                               ; preds = %59
  br label %79

79:                                               ; preds = %78, %68
  %80 = phi i32 [ %77, %68 ], [ 0, %78 ]
  br label %81

81:                                               ; preds = %79, %50
  %82 = phi i32 [ %58, %50 ], [ %80, %79 ]
  %83 = sext i32 %82 to i64
  %84 = icmp ult i64 %42, %83
  br i1 %84, label %85, label %86

85:                                               ; preds = %81
  store i32 -138, ptr %10, align 4, !tbaa !5
  br label %99

86:                                               ; preds = %81
  %87 = load ptr, ptr %12, align 8, !tbaa !9
  %88 = getelementptr inbounds %struct.psa_key_slot_t, ptr %87, i32 0, i32 2
  %89 = getelementptr inbounds %struct.key_data, ptr %88, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !25
  %91 = load ptr, ptr %12, align 8, !tbaa !9
  %92 = getelementptr inbounds %struct.psa_key_slot_t, ptr %91, i32 0, i32 2
  %93 = getelementptr inbounds %struct.key_data, ptr %92, i32 0, i32 1
  %94 = load i64, ptr %93, align 8, !tbaa !26
  %95 = load ptr, ptr %7, align 8, !tbaa !9
  %96 = load i64, ptr %8, align 8, !tbaa !11
  %97 = load ptr, ptr %9, align 8, !tbaa !9
  %98 = call i32 @psa_driver_wrapper_export_public_key(ptr noundef %14, ptr noundef %90, i64 noundef %94, ptr noundef %95, i64 noundef %96, ptr noundef %97)
  store i32 %98, ptr %10, align 4, !tbaa !5
  br label %99

99:                                               ; preds = %86, %85, %35
  %100 = load ptr, ptr %12, align 8, !tbaa !9
  %101 = call i32 @psa_unlock_key_slot(ptr noundef %100)
  store i32 %101, ptr %11, align 4, !tbaa !5
  %102 = load i32, ptr %10, align 4, !tbaa !5
  %103 = icmp eq i32 %102, 0
  br i1 %103, label %104, label %106

104:                                              ; preds = %99
  %105 = load i32, ptr %11, align 4, !tbaa !5
  br label %108

106:                                              ; preds = %99
  %107 = load i32, ptr %10, align 4, !tbaa !5
  br label %108

108:                                              ; preds = %106, %104
  %109 = phi i32 [ %105, %104 ], [ %107, %106 ]
  store i32 %109, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %110

110:                                              ; preds = %108, %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
  %111 = load i32, ptr %5, align 4
  ret i32 %111
}

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, !16, i64 2}
!25 = !{!14, !10, i64 40}
!26 = !{!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 %47, 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 %47

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
  %.not16 = icmp eq i16 %13, 0
  br i1 %.not16, label %.thread, label %14

14:                                               ; 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
  %15 = getelementptr inbounds i8, ptr %6, i64 32
  %16 = and i16 %12, -12289
  %17 = icmp eq i16 %16, 16385
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %15, i8 0, i64 16, i1 false)
  br i1 %17, label %18, label %23

18:                                               ; preds = %14
  %19 = getelementptr inbounds i8, ptr %11, i64 2
  %20 = load i16, ptr %19, align 2, !tbaa !21
  %21 = lshr i16 %20, 3
  %narrow = add nuw nsw i16 %21, 16
  %22 = zext nneg i16 %narrow to i32
  br label %33

23:                                               ; preds = %14
  %24 = and i16 %12, -12544
  %25 = icmp eq i16 %24, 16640
  br i1 %25, label %26, label %33

26:                                               ; preds = %23
  %27 = getelementptr inbounds i8, ptr %11, i64 2
  %28 = load i16, ptr %27, align 2, !tbaa !21
  %29 = zext i16 %28 to i32
  %30 = add nuw nsw i32 %29, 7
  %31 = lshr i32 %30, 2
  %32 = or i32 %31, 1
  br label %33

33:                                               ; preds = %26, %23, %18
  %34 = phi i32 [ %22, %18 ], [ %32, %26 ], [ 0, %23 ]
  %35 = zext nneg i32 %34 to i64
  %36 = icmp ugt i64 %35, %2
  br i1 %36, label %.thread, label %38

.thread:                                          ; preds = %10, %33
  %.0.ph = phi i32 [ -138, %33 ], [ -135, %10 ]
  %37 = call i32 @psa_unlock_key_slot(ptr noundef nonnull %11) #5
  br label %47

38:                                               ; preds = %33
  %39 = getelementptr inbounds i8, ptr %11, i64 40
  %40 = load ptr, ptr %39, align 8, !tbaa !22
  %41 = getelementptr inbounds i8, ptr %11, i64 48
  %42 = load i64, ptr %41, align 8, !tbaa !23
  %43 = call i32 @psa_driver_wrapper_export_public_key(ptr noundef nonnull %6, ptr noundef %40, i64 noundef %42, ptr noundef %1, i64 noundef %2, ptr noundef nonnull %3) #5
  %.fr = freeze i32 %43
  %44 = load ptr, ptr %5, align 8, !tbaa !9
  %45 = call i32 @psa_unlock_key_slot(ptr noundef %44) #5
  %46 = icmp eq i32 %.fr, 0
  %spec.select = select i1 %46, i32 %45, i32 %.fr
  br label %47

47:                                               ; preds = %38, %.thread, %8, %4
  %.012 = phi i32 [ -138, %4 ], [ %9, %8 ], [ %.0.ph, %.thread ], [ %spec.select, %38 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #5
  ret i32 %.012
}

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, !14, i64 2}
!22 = !{!12, !10, i64 40}
!23 = !{!12, !6, i64 48}


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