; 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

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

declare i32 @psa_unlock_key_slot(ptr noundef) #3

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

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_aead_check_algorithm(i32 noundef) #4

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_aead_check_nonce_length(i32 noundef, i64 noundef) #4

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

33:                                               ; preds = %11
  %34 = load i32, ptr %24, align 4, !tbaa !5
  store i32 %34, ptr %12, align 4
  store i32 1, ptr %26, align 4
  br label %88

35:                                               ; preds = %11
  %36 = load i32, ptr %13, align 4, !tbaa !5
  %37 = load i32, ptr %14, align 4, !tbaa !5
  %38 = call i32 @psa_get_and_lock_key_slot_with_policy(i32 noundef %36, ptr noundef %25, i32 noundef 512, i32 noundef %37)
  store i32 %38, ptr %24, align 4, !tbaa !5
  %39 = load i32, ptr %24, align 4, !tbaa !5
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %43

41:                                               ; preds = %35
  %42 = load i32, ptr %24, align 4, !tbaa !5
  store i32 %42, ptr %12, align 4
  store i32 1, ptr %26, align 4
  br label %88

43:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 48, ptr %27) #5
  %44 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %27, i32 0, i32 0
  %45 = load ptr, ptr %25, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.psa_key_slot_t, ptr %45, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %44, ptr align 8 %46, i64 28, i1 false), !tbaa.struct !13
  %47 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %27, i32 0, i32 1
  store ptr null, ptr %47, align 8, !tbaa !16
  %48 = getelementptr inbounds %struct.psa_key_attributes_s, ptr %27, i32 0, i32 2
  store i64 0, ptr %48, align 8, !tbaa !20
  %49 = load i32, ptr %14, align 4, !tbaa !5
  %50 = load i64, ptr %16, align 8, !tbaa !11
  %51 = call i32 @psa_aead_check_nonce_length(i32 noundef %49, i64 noundef %50)
  store i32 %51, ptr %24, align 4, !tbaa !5
  %52 = load i32, ptr %24, align 4, !tbaa !5
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %55

54:                                               ; preds = %43
  br label %84

55:                                               ; preds = %43
  %56 = load ptr, ptr %25, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.psa_key_slot_t, ptr %56, i32 0, i32 2
  %58 = getelementptr inbounds %struct.key_data, ptr %57, i32 0, i32 0
  %59 = load ptr, ptr %58, align 8, !tbaa !21
  %60 = load ptr, ptr %25, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.psa_key_slot_t, ptr %60, i32 0, i32 2
  %62 = getelementptr inbounds %struct.key_data, ptr %61, i32 0, i32 1
  %63 = load i64, ptr %62, align 8, !tbaa !24
  %64 = load i32, ptr %14, align 4, !tbaa !5
  %65 = load ptr, ptr %15, align 8, !tbaa !9
  %66 = load i64, ptr %16, align 8, !tbaa !11
  %67 = load ptr, ptr %17, align 8, !tbaa !9
  %68 = load i64, ptr %18, align 8, !tbaa !11
  %69 = load ptr, ptr %19, align 8, !tbaa !9
  %70 = load i64, ptr %20, align 8, !tbaa !11
  %71 = load ptr, ptr %21, align 8, !tbaa !9
  %72 = load i64, ptr %22, align 8, !tbaa !11
  %73 = load ptr, ptr %23, align 8, !tbaa !9
  %74 = call i32 @psa_driver_wrapper_aead_decrypt(ptr noundef %27, ptr noundef %59, i64 noundef %63, i32 noundef %64, ptr noundef %65, i64 noundef %66, ptr noundef %67, i64 noundef %68, ptr noundef %69, i64 noundef %70, ptr noundef %71, i64 noundef %72, ptr noundef %73)
  store i32 %74, ptr %24, align 4, !tbaa !5
  %75 = load i32, ptr %24, align 4, !tbaa !5
  %76 = icmp ne i32 %75, 0
  br i1 %76, label %77, label %83

77:                                               ; preds = %55
  %78 = load i64, ptr %22, align 8, !tbaa !11
  %79 = icmp ne i64 %78, 0
  br i1 %79, label %80, label %83

80:                                               ; preds = %77
  %81 = load ptr, ptr %21, align 8, !tbaa !9
  %82 = load i64, ptr %22, align 8, !tbaa !11
  call void @llvm.memset.p0.i64(ptr align 1 %81, i8 0, i64 %82, i1 false)
  br label %83

83:                                               ; preds = %80, %77, %55
  br label %84

84:                                               ; preds = %83, %54
  %85 = load ptr, ptr %25, align 8, !tbaa !9
  %86 = call i32 @psa_unlock_key_slot(ptr noundef %85)
  %87 = load i32, ptr %24, align 4, !tbaa !5
  store i32 %87, ptr %12, align 4
  store i32 1, ptr %26, align 4
  call void @llvm.lifetime.end.p0(i64 48, ptr %27) #5
  br label %88

88:                                               ; preds = %84, %41, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %25) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %24) #5
  %89 = load i32, ptr %12, align 4
  ret i32 %89
}

declare i32 @psa_driver_wrapper_aead_decrypt(ptr noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) #3

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

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

declare i32 @psa_unlock_key_slot(ptr noundef) local_unnamed_addr #3

; 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 #4

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_aead_check_algorithm(i32 noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_aead_check_nonce_length(i32 noundef, i64 noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_aead_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, i64 noundef %9, ptr noundef %10) local_unnamed_addr #4 {
  %12 = alloca ptr, align 8
  %13 = alloca %struct.psa_key_attributes_s, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %12) #5
  store i64 0, ptr %10, align 8, !tbaa !5
  %14 = tail call i32 @psa_aead_check_algorithm(i32 noundef %1)
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %15, label %34

15:                                               ; preds = %11
  %16 = call i32 @psa_get_and_lock_key_slot_with_policy(i32 noundef %0, ptr noundef nonnull %12, i32 noundef 512, i32 noundef %1)
  %.not33 = icmp eq i32 %16, 0
  br i1 %.not33, label %17, label %34

17:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %13) #5
  %18 = load ptr, ptr %12, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(28) %13, ptr noundef nonnull align 8 dereferenceable(28) %18, i64 28, i1 false), !tbaa.struct !11
  %19 = getelementptr inbounds i8, ptr %13, i64 32
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %19, i8 0, i64 16, i1 false)
  %20 = call i32 @psa_aead_check_nonce_length(i32 noundef %1, i64 noundef %3)
  %.not34 = icmp eq i32 %20, 0
  br i1 %.not34, label %21, label %31

21:                                               ; preds = %17
  %22 = load ptr, ptr %12, align 8, !tbaa !9
  %23 = getelementptr inbounds i8, ptr %22, i64 40
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = getelementptr inbounds i8, ptr %22, i64 48
  %26 = load i64, ptr %25, align 8, !tbaa !21
  %27 = call i32 @psa_driver_wrapper_aead_decrypt(ptr noundef nonnull %13, ptr noundef %24, i64 noundef %26, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6, i64 noundef %7, ptr noundef %8, i64 noundef %9, ptr noundef nonnull %10) #5
  %28 = icmp ne i32 %27, 0
  %29 = icmp ne i64 %9, 0
  %or.cond = and i1 %29, %28
  br i1 %or.cond, label %30, label %31

30:                                               ; preds = %21
  call void @llvm.memset.p0.i64(ptr align 1 %8, i8 0, i64 %9, i1 false)
  br label %31

31:                                               ; preds = %21, %30, %17
  %.0 = phi i32 [ %20, %17 ], [ %27, %30 ], [ %27, %21 ]
  %32 = load ptr, ptr %12, align 8, !tbaa !9
  %33 = call i32 @psa_unlock_key_slot(ptr noundef %32) #5
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %13) #5
  br label %34

34:                                               ; preds = %15, %11, %31
  %.027 = phi i32 [ %.0, %31 ], [ %14, %11 ], [ %16, %15 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %12) #5
  ret i32 %.027
}

declare i32 @psa_driver_wrapper_aead_decrypt(ptr noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #3

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


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