; LLVM IR from machinefi/psa-crypto-esp32 :: 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.iotex_ecp_keypair = type { %struct.iotex_ecp_group, %struct.iotex_mpi, %struct.iotex_ecp_point }
%struct.iotex_ecp_group = type { i32, %struct.iotex_mpi, %struct.iotex_mpi, %struct.iotex_mpi, %struct.iotex_ecp_point, %struct.iotex_mpi, i64, i64, i32, ptr, ptr, ptr, ptr, ptr, i64 }
%struct.iotex_mpi = type { i32, i64, ptr }
%struct.iotex_ecp_point = type { %struct.iotex_mpi, %struct.iotex_mpi, %struct.iotex_mpi }

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

declare i32 @iotex_to_psa_error(i32 noundef) #1

; 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
define dso_local i32 @iotex_psa_ecp_export_key(i16 noundef zeroext %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) #2 {
  %6 = alloca i32, align 4
  %7 = alloca i16, align 2
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i16 %0, ptr %7, align 2, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !9
  store i64 %3, ptr %10, align 8, !tbaa !11
  store ptr %4, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %14 = load i16, ptr %7, align 2, !tbaa !5
  %15 = zext i16 %14 to i32
  %16 = and i32 %15, 28672
  %17 = icmp eq i32 %16, 16384
  br i1 %17, label %18, label %57

18:                                               ; preds = %5
  %19 = load ptr, ptr %8, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %19, i32 0, i32 2
  %21 = call i32 @iotex_ecp_is_zero(ptr noundef %20)
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %40

23:                                               ; preds = %18
  %24 = load ptr, ptr %8, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %8, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %26, i32 0, i32 2
  %28 = load ptr, ptr %8, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %8, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %30, i32 0, i32 0
  %32 = getelementptr inbounds %struct.iotex_ecp_group, ptr %31, i32 0, i32 4
  %33 = call i32 @iotex_ecp_mul(ptr noundef %25, ptr noundef %27, ptr noundef %29, ptr noundef %32, ptr noundef @iotex_psa_get_random, ptr noundef null)
  %34 = call i32 @iotex_to_psa_error(i32 noundef %33)
  store i32 %34, ptr %12, align 4, !tbaa !13
  %35 = load i32, ptr %12, align 4, !tbaa !13
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %39

37:                                               ; preds = %23
  %38 = load i32, ptr %12, align 4, !tbaa !13
  store i32 %38, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %93

39:                                               ; preds = %23
  br label %40

40:                                               ; preds = %39, %18
  %41 = load ptr, ptr %8, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %8, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %43, i32 0, i32 2
  %45 = load ptr, ptr %11, align 8, !tbaa !9
  %46 = load ptr, ptr %9, align 8, !tbaa !9
  %47 = load i64, ptr %10, align 8, !tbaa !11
  %48 = call i32 @iotex_ecp_point_write_binary(ptr noundef %42, ptr noundef %44, i32 noundef 0, ptr noundef %45, ptr noundef %46, i64 noundef %47)
  %49 = call i32 @iotex_to_psa_error(i32 noundef %48)
  store i32 %49, ptr %12, align 4, !tbaa !13
  %50 = load i32, ptr %12, align 4, !tbaa !13
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %55

52:                                               ; preds = %40
  %53 = load ptr, ptr %9, align 8, !tbaa !9
  %54 = load i64, ptr %10, align 8, !tbaa !11
  call void @llvm.memset.p0.i64(ptr align 1 %53, i8 0, i64 %54, i1 false)
  br label %55

55:                                               ; preds = %52, %40
  %56 = load i32, ptr %12, align 4, !tbaa !13
  store i32 %56, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %93

57:                                               ; preds = %5
  %58 = load i64, ptr %10, align 8, !tbaa !11
  %59 = load ptr, ptr %8, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %59, i32 0, i32 0
  %61 = getelementptr inbounds %struct.iotex_ecp_group, ptr %60, i32 0, i32 7
  %62 = load i64, ptr %61, align 8, !tbaa !15
  %63 = add i64 %62, 7
  %64 = udiv i64 %63, 8
  %65 = icmp ult i64 %58, %64
  br i1 %65, label %66, label %67

66:                                               ; preds = %57
  store i32 -138, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %93

67:                                               ; preds = %57
  %68 = load ptr, ptr %8, align 8, !tbaa !9
  %69 = load ptr, ptr %9, align 8, !tbaa !9
  %70 = load ptr, ptr %8, align 8, !tbaa !9
  %71 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %70, i32 0, i32 0
  %72 = getelementptr inbounds %struct.iotex_ecp_group, ptr %71, i32 0, i32 7
  %73 = load i64, ptr %72, align 8, !tbaa !15
  %74 = add i64 %73, 7
  %75 = udiv i64 %74, 8
  %76 = call i32 @iotex_ecp_write_key(ptr noundef %68, ptr noundef %69, i64 noundef %75)
  %77 = call i32 @iotex_to_psa_error(i32 noundef %76)
  store i32 %77, ptr %12, align 4, !tbaa !13
  %78 = load i32, ptr %12, align 4, !tbaa !13
  %79 = icmp eq i32 %78, 0
  br i1 %79, label %80, label %88

80:                                               ; preds = %67
  %81 = load ptr, ptr %8, align 8, !tbaa !9
  %82 = getelementptr inbounds %struct.iotex_ecp_keypair, ptr %81, i32 0, i32 0
  %83 = getelementptr inbounds %struct.iotex_ecp_group, ptr %82, i32 0, i32 7
  %84 = load i64, ptr %83, align 8, !tbaa !15
  %85 = add i64 %84, 7
  %86 = udiv i64 %85, 8
  %87 = load ptr, ptr %11, align 8, !tbaa !9
  store i64 %86, ptr %87, align 8, !tbaa !11
  br label %91

88:                                               ; preds = %67
  %89 = load ptr, ptr %9, align 8, !tbaa !9
  %90 = load i64, ptr %10, align 8, !tbaa !11
  call void @llvm.memset.p0.i64(ptr align 1 %89, i8 0, i64 %90, i1 false)
  br label %91

91:                                               ; preds = %88, %80
  %92 = load i32, ptr %12, align 4, !tbaa !13
  store i32 %92, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %93

93:                                               ; preds = %91, %66, %55, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  %94 = load i32, ptr %6, align 4
  ret i32 %94
}

declare i32 @iotex_ecp_is_zero(ptr noundef) #1

declare i32 @iotex_ecp_mul(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

declare i32 @iotex_psa_get_random(ptr noundef, ptr noundef, i64 noundef) #1

declare i32 @iotex_ecp_point_write_binary(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, i64 noundef) #1

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

declare i32 @iotex_ecp_write_key(ptr noundef, ptr noundef, i64 noundef) #1

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"short", !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, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !12, i64 184}
!16 = !{!"iotex_ecp_keypair", !17, i64 0, !18, i64 248, !19, i64 272}
!17 = !{!"iotex_ecp_group", !14, i64 0, !18, i64 8, !18, i64 32, !18, i64 56, !19, i64 80, !18, i64 152, !12, i64 176, !12, i64 184, !14, i64 192, !10, i64 200, !10, i64 208, !10, i64 216, !10, i64 224, !10, i64 232, !12, i64 240}
!18 = !{!"iotex_mpi", !14, i64 0, !12, i64 8, !10, i64 16}
!19 = !{!"iotex_ecp_point", !18, i64 0, !18, i64 24, !18, 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"

declare i32 @iotex_to_psa_error(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @iotex_psa_ecp_export_key(i16 noundef zeroext %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) local_unnamed_addr #1 {
  %6 = and i16 %0, 28672
  %7 = icmp eq i16 %6, 16384
  br i1 %7, label %8, label %20

8:                                                ; preds = %5
  %9 = getelementptr inbounds i8, ptr %1, i64 272
  %10 = tail call i32 @iotex_ecp_is_zero(ptr noundef nonnull %9) #3
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %16, label %11

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %1, i64 248
  %13 = getelementptr inbounds i8, ptr %1, i64 80
  %14 = tail call i32 @iotex_ecp_mul(ptr noundef %1, ptr noundef nonnull %9, ptr noundef nonnull %12, ptr noundef nonnull %13, ptr noundef nonnull @iotex_psa_get_random, ptr noundef null) #3
  %15 = tail call i32 @iotex_to_psa_error(i32 noundef %14) #3
  %.not31 = icmp eq i32 %15, 0
  br i1 %.not31, label %16, label %35

16:                                               ; preds = %11, %8
  %17 = tail call i32 @iotex_ecp_point_write_binary(ptr noundef %1, ptr noundef nonnull %9, i32 noundef 0, ptr noundef %4, ptr noundef %2, i64 noundef %3) #3
  %18 = tail call i32 @iotex_to_psa_error(i32 noundef %17) #3
  %.not32 = icmp eq i32 %18, 0
  br i1 %.not32, label %35, label %19

19:                                               ; preds = %16
  tail call void @llvm.memset.p0.i64(ptr align 1 %2, i8 0, i64 %3, i1 false)
  br label %35

20:                                               ; preds = %5
  %21 = getelementptr inbounds i8, ptr %1, i64 184
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = add i64 %22, 7
  %24 = lshr i64 %23, 3
  %25 = icmp ugt i64 %24, %3
  br i1 %25, label %35, label %26

26:                                               ; preds = %20
  %27 = tail call i32 @iotex_ecp_write_key(ptr noundef nonnull %1, ptr noundef %2, i64 noundef %24) #3
  %28 = tail call i32 @iotex_to_psa_error(i32 noundef %27) #3
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %34

30:                                               ; preds = %26
  %31 = load i64, ptr %21, align 8, !tbaa !5
  %32 = add i64 %31, 7
  %33 = lshr i64 %32, 3
  store i64 %33, ptr %4, align 8, !tbaa !15
  br label %35

34:                                               ; preds = %26
  tail call void @llvm.memset.p0.i64(ptr align 1 %2, i8 0, i64 %3, i1 false)
  br label %35

35:                                               ; preds = %30, %34, %20, %16, %19, %11
  %.0 = phi i32 [ %15, %11 ], [ %18, %19 ], [ 0, %16 ], [ -138, %20 ], [ %28, %34 ], [ 0, %30 ]
  ret i32 %.0
}

declare i32 @iotex_ecp_is_zero(ptr noundef) local_unnamed_addr #0

declare i32 @iotex_ecp_mul(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

declare i32 @iotex_psa_get_random(ptr noundef, ptr noundef, i64 noundef) #0

declare i32 @iotex_ecp_point_write_binary(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; 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 @iotex_ecp_write_key(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

attributes #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 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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, !12, i64 184}
!6 = !{!"iotex_ecp_keypair", !7, i64 0, !11, i64 248, !14, i64 272}
!7 = !{!"iotex_ecp_group", !8, i64 0, !11, i64 8, !11, i64 32, !11, i64 56, !14, i64 80, !11, i64 152, !12, i64 176, !12, i64 184, !8, i64 192, !13, i64 200, !13, i64 208, !13, i64 216, !13, i64 224, !13, i64 232, !12, i64 240}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"iotex_mpi", !8, i64 0, !12, i64 8, !13, i64 16}
!12 = !{!"long", !9, i64 0}
!13 = !{!"any pointer", !9, i64 0}
!14 = !{!"iotex_ecp_point", !11, i64 0, !11, i64 24, !11, i64 48}
!15 = !{!12, !12, i64 0}


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