; 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_slot_t = type { %struct.psa_core_key_attributes_t, i64, %struct.key_data }
%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.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

declare i32 @psa_unlock_key_slot(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_generate_random(ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_raw_key_agreement(i32 noundef %0, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6) #2 {
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %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 i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca ptr, align 8
  %18 = alloca i64, align 8
  store i32 %0, ptr %8, align 4, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !5
  store ptr %2, ptr %10, align 8, !tbaa !9
  store i64 %3, ptr %11, align 8, !tbaa !11
  store ptr %4, ptr %12, align 8, !tbaa !9
  store i64 %5, ptr %13, align 8, !tbaa !11
  store ptr %6, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  store i32 -151, ptr %15, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  store i32 -151, ptr %16, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #3
  store ptr null, ptr %17, align 8, !tbaa !9
  %19 = load i32, ptr %8, align 4, !tbaa !5
  %20 = and i32 %19, 2130706432
  %21 = icmp eq i32 %20, 150994944
  br i1 %21, label %23, label %22

22:                                               ; preds = %7
  store i32 -135, ptr %15, align 4, !tbaa !5
  br label %63

23:                                               ; preds = %7
  %24 = load i32, ptr %9, align 4, !tbaa !5
  %25 = load i32, ptr %8, align 4, !tbaa !5
  %26 = call i32 @psa_get_and_lock_transparent_key_slot_with_policy(i32 noundef %24, ptr noundef %17, i32 noundef 16384, i32 noundef %25)
  store i32 %26, ptr %15, align 4, !tbaa !5
  %27 = load i32, ptr %15, align 4, !tbaa !5
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %23
  br label %63

30:                                               ; preds = %23
  %31 = load ptr, ptr %17, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.psa_key_slot_t, ptr %31, i32 0, i32 0
  %33 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %32, i32 0, i32 0
  %34 = load i16, ptr %33, align 8, !tbaa !13
  %35 = zext i16 %34 to i32
  %36 = and i32 %35, -256
  %37 = icmp eq i32 %36, 28928
  br i1 %37, label %38, label %46

38:                                               ; preds = %30
  %39 = load ptr, ptr %17, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.psa_key_slot_t, ptr %39, i32 0, i32 0
  %41 = getelementptr inbounds %struct.psa_core_key_attributes_t, ptr %40, i32 0, i32 1
  %42 = load i16, ptr %41, align 2, !tbaa !19
  %43 = zext i16 %42 to i32
  %44 = add nsw i32 %43, 7
  %45 = sdiv i32 %44, 8
  br label %47

46:                                               ; preds = %30
  br label %47

47:                                               ; preds = %46, %38
  %48 = phi i32 [ %45, %38 ], [ 0, %46 ]
  %49 = sext i32 %48 to i64
  store i64 %49, ptr %18, align 8, !tbaa !11
  %50 = load i64, ptr %13, align 8, !tbaa !11
  %51 = load i64, ptr %18, align 8, !tbaa !11
  %52 = icmp ult i64 %50, %51
  br i1 %52, label %53, label %54

53:                                               ; preds = %47
  store i32 -138, ptr %15, align 4, !tbaa !5
  br label %63

54:                                               ; preds = %47
  %55 = load i32, ptr %8, align 4, !tbaa !5
  %56 = load ptr, ptr %17, align 8, !tbaa !9
  %57 = load ptr, ptr %10, align 8, !tbaa !9
  %58 = load i64, ptr %11, align 8, !tbaa !11
  %59 = load ptr, ptr %12, align 8, !tbaa !9
  %60 = load i64, ptr %13, align 8, !tbaa !11
  %61 = load ptr, ptr %14, align 8, !tbaa !9
  %62 = call i32 @psa_key_agreement_raw_internal(i32 noundef %55, ptr noundef %56, ptr noundef %57, i64 noundef %58, ptr noundef %59, i64 noundef %60, ptr noundef %61)
  store i32 %62, ptr %15, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %54, %53, %29, %22
  %64 = load i32, ptr %15, align 4, !tbaa !5
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %72

66:                                               ; preds = %63
  %67 = load ptr, ptr %12, align 8, !tbaa !9
  %68 = load i64, ptr %13, align 8, !tbaa !11
  %69 = call i32 @psa_generate_random(ptr noundef %67, i64 noundef %68)
  %70 = load i64, ptr %13, align 8, !tbaa !11
  %71 = load ptr, ptr %14, align 8, !tbaa !9
  store i64 %70, ptr %71, align 8, !tbaa !11
  br label %72

72:                                               ; preds = %66, %63
  %73 = load ptr, ptr %17, align 8, !tbaa !9
  %74 = call i32 @psa_unlock_key_slot(ptr noundef %73)
  store i32 %74, ptr %16, align 4, !tbaa !5
  %75 = load i32, ptr %15, align 4, !tbaa !5
  %76 = icmp eq i32 %75, 0
  br i1 %76, label %77, label %79

77:                                               ; preds = %72
  %78 = load i32, ptr %16, align 4, !tbaa !5
  br label %81

79:                                               ; preds = %72
  %80 = load i32, ptr %15, align 4, !tbaa !5
  br label %81

81:                                               ; preds = %79, %77
  %82 = phi i32 [ %78, %77 ], [ %80, %79 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  ret i32 %82
}

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_key_agreement_raw_internal(i32 noundef, 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 = { "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 = { 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 #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, !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 = !{!14, !16, i64 2}


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

declare i32 @psa_unlock_key_slot(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_generate_random(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_raw_key_agreement(i32 noundef %0, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6) local_unnamed_addr #2 {
  %8 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #3
  store ptr null, ptr %8, align 8, !tbaa !5
  %9 = and i32 %0, 2130706432
  %10 = icmp eq i32 %9, 150994944
  br i1 %10, label %11, label %26

11:                                               ; preds = %7
  %12 = call i32 @psa_get_and_lock_transparent_key_slot_with_policy(i32 noundef %1, ptr noundef nonnull %8, i32 noundef 16384, i32 noundef %0)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %13, label %26

13:                                               ; preds = %11
  %14 = load ptr, ptr %8, align 8, !tbaa !5
  %15 = load i16, ptr %14, align 8, !tbaa !9
  %16 = and i16 %15, -256
  %17 = icmp eq i16 %16, 28928
  br i1 %17, label %18, label %.thread

18:                                               ; preds = %13
  %19 = getelementptr inbounds i8, ptr %14, i64 2
  %20 = load i16, ptr %19, align 2, !tbaa !17
  %21 = zext i16 %20 to i64
  %22 = add nuw nsw i64 %21, 7
  %23 = lshr i64 %22, 3
  %24 = icmp ugt i64 %23, %5
  br i1 %24, label %26, label %.thread

.thread:                                          ; preds = %13, %18
  %25 = call i32 @psa_key_agreement_raw_internal(i32 noundef %0, ptr noundef nonnull %14, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6)
  %.not21 = icmp eq i32 %25, 0
  br i1 %.not21, label %30, label %26

26:                                               ; preds = %.thread, %11, %7, %18
  %.025 = phi i32 [ %25, %.thread ], [ -138, %18 ], [ -135, %7 ], [ %12, %11 ]
  %27 = call i32 @psa_generate_random(ptr noundef %4, i64 noundef %5)
  store i64 %5, ptr %6, align 8, !tbaa !18
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = call i32 @psa_unlock_key_slot(ptr noundef %28) #3
  br label %33

30:                                               ; preds = %.thread
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = call i32 @psa_unlock_key_slot(ptr noundef %31) #3
  br label %33

33:                                               ; preds = %26, %30
  %34 = phi i32 [ %32, %30 ], [ %.025, %26 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #3
  ret i32 %34
}

; Function Attrs: nounwind uwtable
declare hidden i32 @psa_key_agreement_raw_internal(i32 noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 #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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 0}
!10 = !{!"", !11, i64 0, !15, i64 32, !16, i64 40}
!11 = !{!"", !12, i64 0, !12, i64 2, !13, i64 4, !13, i64 8, !14, i64 12, !12, i64 24}
!12 = !{!"short", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!"psa_key_policy_s", !13, i64 0, !13, i64 4, !13, i64 8}
!15 = !{!"long", !7, i64 0}
!16 = !{!"key_data", !6, i64 0, !15, i64 8}
!17 = !{!10, !12, i64 2}
!18 = !{!15, !15, i64 0}


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