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

; 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_validate_unstructured_key_bit_size(i16 noundef zeroext, i64 noundef) #1

; 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) #2

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

25:                                               ; preds = %7
  store i32 -134, ptr %8, align 4
  store i32 1, ptr %18, align 4
  br label %76

26:                                               ; preds = %7
  %27 = load i16, ptr %17, align 2, !tbaa !18
  %28 = call i32 @key_type_is_raw_bytes(i16 noundef zeroext %27)
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %52

30:                                               ; preds = %26
  %31 = load i64, ptr %11, align 8, !tbaa !9
  %32 = mul i64 %31, 8
  %33 = load ptr, ptr %15, align 8, !tbaa !5
  store i64 %32, ptr %33, align 8, !tbaa !9
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %34, i32 0, i32 0
  %36 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %35, i32 0, i32 0
  %37 = load i16, ptr %36, align 8, !tbaa !13
  %38 = load ptr, ptr %15, align 8, !tbaa !5
  %39 = load i64, ptr %38, align 8, !tbaa !9
  %40 = call i32 @psa_validate_unstructured_key_bit_size(i16 noundef zeroext %37, i64 noundef %39)
  store i32 %40, ptr %16, align 4, !tbaa !11
  %41 = load i32, ptr %16, align 4, !tbaa !11
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %43, label %45

43:                                               ; preds = %30
  %44 = load i32, ptr %16, align 4, !tbaa !11
  store i32 %44, ptr %8, align 4
  store i32 1, ptr %18, align 4
  br label %76

45:                                               ; preds = %30
  %46 = load ptr, ptr %12, align 8, !tbaa !5
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  %48 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %46, ptr align 1 %47, i64 %48, i1 false)
  %49 = load i64, ptr %11, align 8, !tbaa !9
  %50 = load ptr, ptr %14, align 8, !tbaa !5
  store i64 %49, ptr %50, align 8, !tbaa !9
  %51 = load i64, ptr %13, align 8, !tbaa !9
  store i32 0, ptr %8, align 4
  store i32 1, ptr %18, align 4
  br label %76

52:                                               ; preds = %26
  %53 = load i16, ptr %17, align 2, !tbaa !18
  %54 = zext i16 %53 to i32
  %55 = and i32 %54, 28672
  %56 = and i32 %55, -12289
  %57 = icmp eq i32 %56, 16384
  br i1 %57, label %58, label %74

58:                                               ; preds = %52
  %59 = load i16, ptr %17, align 2, !tbaa !18
  %60 = zext i16 %59 to i32
  %61 = and i32 %60, -12289
  %62 = and i32 %61, -256
  %63 = icmp eq i32 %62, 16640
  br i1 %63, label %64, label %73

64:                                               ; preds = %58
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = load i64, ptr %11, align 8, !tbaa !9
  %68 = load ptr, ptr %12, align 8, !tbaa !5
  %69 = load i64, ptr %13, align 8, !tbaa !9
  %70 = load ptr, ptr %14, align 8, !tbaa !5
  %71 = load ptr, ptr %15, align 8, !tbaa !5
  %72 = call i32 @iotex_psa_ecp_import_key(ptr noundef %65, ptr noundef %66, i64 noundef %67, ptr noundef %68, i64 noundef %69, ptr noundef %70, ptr noundef %71)
  store i32 %72, ptr %8, align 4
  store i32 1, ptr %18, align 4
  br label %76

73:                                               ; preds = %58
  br label %74

74:                                               ; preds = %73, %52
  br label %75

75:                                               ; preds = %74
  store i32 -134, ptr %8, align 4
  store i32 1, ptr %18, align 4
  br label %76

76:                                               ; preds = %75, %64, %45, %43, %25
  call void @llvm.lifetime.end.p0(i64 2, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  %77 = load i32, ptr %8, align 4
  ret i32 %77
}

; Function Attrs: nounwind uwtable
declare hidden i32 @key_type_is_raw_bytes(i16 noundef zeroext) #1

declare i32 @iotex_psa_ecp_import_key(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, ptr 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !16, i64 0}
!14 = !{!"psa_key_attributes_s", !15, i64 0, !6, i64 32, !10, i64 40}
!15 = !{!"", !16, i64 0, !16, i64 2, !12, i64 4, !12, i64 8, !17, i64 12, !16, i64 24}
!16 = !{!"short", !7, i64 0}
!17 = !{!"psa_key_policy_s", !12, i64 0, !12, i64 4, !12, i64 8}
!18 = !{!16, !16, i64 0}


; 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: nounwind uwtable
declare dso_local i32 @psa_validate_unstructured_key_bit_size(i16 noundef zeroext, i64 noundef) local_unnamed_addr #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

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_import_key_into_slot(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, ptr noundef %6) local_unnamed_addr #0 {
  %8 = load i16, ptr %0, align 8, !tbaa !5
  %9 = icmp eq i64 %2, 0
  br i1 %9, label %21, label %10

10:                                               ; preds = %7
  %11 = tail call i32 @key_type_is_raw_bytes(i16 noundef zeroext %8)
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %17, label %12

12:                                               ; preds = %10
  %13 = shl i64 %2, 3
  store i64 %13, ptr %6, align 8, !tbaa !15
  %14 = load i16, ptr %0, align 8, !tbaa !5
  %15 = tail call i32 @psa_validate_unstructured_key_bit_size(i16 noundef zeroext %14, i64 noundef %13)
  %.not27 = icmp eq i32 %15, 0
  br i1 %.not27, label %16, label %21

16:                                               ; preds = %12
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %3, ptr align 1 %1, i64 %2, i1 false)
  store i64 %2, ptr %5, align 8, !tbaa !15
  br label %21

17:                                               ; preds = %10
  %18 = and i16 %8, -12544
  %or.cond = icmp eq i16 %18, 16640
  br i1 %or.cond, label %19, label %21

19:                                               ; preds = %17
  %20 = tail call i32 @iotex_psa_ecp_import_key(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, ptr noundef %6) #3
  br label %21

21:                                               ; preds = %17, %12, %7, %19, %16
  %.0 = phi i32 [ 0, %16 ], [ %20, %19 ], [ -134, %7 ], [ %15, %12 ], [ -134, %17 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @key_type_is_raw_bytes(i16 noundef zeroext) local_unnamed_addr #0

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

attributes #0 = { 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 #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 }

!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, !8, i64 0}
!6 = !{!"psa_key_attributes_s", !7, i64 0, !13, i64 32, !14, i64 40}
!7 = !{!"", !8, i64 0, !8, i64 2, !11, i64 4, !11, i64 8, !12, i64 12, !8, i64 24}
!8 = !{!"short", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"psa_key_policy_s", !11, i64 0, !11, i64 4, !11, i64 8}
!13 = !{!"any pointer", !9, i64 0}
!14 = !{!"long", !9, i64 0}
!15 = !{!14, !14, i64 0}


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