; LLVM IR from machinefi/psa-crypto-arduino :: src/psa_layer (psa_crypto_ecp)

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare void @iotex_ecp_keypair_free(ptr noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @iotex_psa_ecp_export_key(i16 noundef zeroext, ptr noundef, ptr noundef, i64 noundef, ptr noundef) #0

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

31:                                               ; preds = %6
  %32 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %32, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %64

33:                                               ; preds = %6
  %34 = load ptr, ptr %8, 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 = zext i16 %37 to i32
  %39 = and i32 %38, -12289
  %40 = and i32 %39, -256
  %41 = icmp eq i32 %40, 16640
  br i1 %41, label %42, label %49

42:                                               ; preds = %33
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.psa_key_attributes_s, 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, 255
  br label %50

49:                                               ; preds = %33
  br label %50

50:                                               ; preds = %49, %42
  %51 = phi i32 [ %48, %42 ], [ 0, %49 ]
  %52 = trunc i32 %51 to i8
  %53 = zext i8 %52 to i32
  %54 = or i32 16640, %53
  %55 = trunc i32 %54 to i16
  %56 = load ptr, ptr %15, align 8, !tbaa !5
  %57 = load ptr, ptr %11, align 8, !tbaa !5
  %58 = load i64, ptr %12, align 8, !tbaa !9
  %59 = load ptr, ptr %13, align 8, !tbaa !5
  %60 = call i32 @iotex_psa_ecp_export_key(i16 noundef zeroext %55, ptr noundef %56, ptr noundef %57, i64 noundef %58, ptr noundef %59)
  store i32 %60, ptr %14, align 4, !tbaa !11
  %61 = load ptr, ptr %15, align 8, !tbaa !5
  call void @iotex_ecp_keypair_free(ptr noundef %61)
  %62 = load ptr, ptr %15, align 8, !tbaa !5
  call void @free(ptr noundef %62) #4
  %63 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %63, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %64

64:                                               ; preds = %50, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  %65 = load i32, ptr %7, align 4
  ret i32 %65
}

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 = { nocallback nofree nosync 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 "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 = !{!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: nounwind uwtable
declare dso_local i32 @iotex_psa_ecp_load_representation(i16 noundef zeroext, i64 noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare void @iotex_ecp_keypair_free(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @iotex_psa_ecp_export_key(i16 noundef zeroext, ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @iotex_psa_ecp_export_public_key(ptr nocapture noundef readonly %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %7 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #4
  store ptr null, ptr %7, align 8, !tbaa !5
  %8 = load i16, ptr %0, align 8, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %0, i64 2
  %10 = load i16, ptr %9, align 2, !tbaa !16
  %11 = zext i16 %10 to i64
  %12 = call i32 @iotex_psa_ecp_load_representation(i16 noundef zeroext %8, i64 noundef %11, ptr noundef %1, i64 noundef %2, ptr noundef nonnull %7)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %13, label %22

13:                                               ; preds = %6
  %14 = load i16, ptr %0, align 8, !tbaa !9
  %15 = and i16 %14, -12544
  %16 = icmp eq i16 %15, 16640
  %17 = and i16 %14, 16895
  %spec.select = select i1 %16, i16 %17, i16 16640
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = call i32 @iotex_psa_ecp_export_key(i16 noundef zeroext %spec.select, ptr noundef %18, ptr noundef %3, i64 noundef %4, ptr noundef %5)
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  call void @iotex_ecp_keypair_free(ptr noundef %20) #4
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  call void @free(ptr noundef %21) #4
  br label %22

22:                                               ; preds = %6, %13
  %.0 = phi i32 [ %19, %13 ], [ %12, %6 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  ret i32 %.0
}

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 nosync 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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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, !12, i64 0}
!10 = !{!"psa_key_attributes_s", !11, i64 0, !6, i64 32, !15, 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 = !{!10, !12, i64 2}


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