; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto (ecdsa)

; 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.curve_point = type { %struct.bignum256, %struct.bignum256 }
%struct.bignum256 = type { [9 x i32] }
%struct.ecdsa_curve = type { %struct.bignum256, %struct.curve_point, %struct.bignum256, %struct.bignum256, i32, %struct.bignum256, [64 x [8 x %struct.curve_point]] }

; Function Attrs: nounwind uwtable
declare dso_local void @point_add(ptr noundef, ptr 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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @point_is_infinity(ptr noundef) #0

declare void @bn_inverse(ptr noundef, ptr noundef) #3

declare void @bn_multiply(ptr noundef, ptr noundef, ptr noundef) #3

declare void @bn_mod(ptr noundef, ptr noundef) #3

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

declare i32 @bn_is_zero(ptr noundef) #3

declare i32 @bn_is_equal(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @point_multiply(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #0

declare i32 @bn_is_less(ptr noundef, ptr noundef) #3

declare void @memzero(ptr noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @scalar_multiply(ptr noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @ecdsa_read_pubkey(ptr noundef, ptr noundef, ptr noundef) #0

declare void @bn_read_be(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @ecdsa_verify_digest(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca %struct.curve_point, align 4
  %10 = alloca %struct.curve_point, align 4
  %11 = alloca %struct.bignum256, align 4
  %12 = alloca %struct.bignum256, align 4
  %13 = alloca %struct.bignum256, align 4
  %14 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 72, ptr %9) #4
  call void @llvm.memset.p0.i64(ptr align 4 %9, i8 0, i64 72, i1 false)
  call void @llvm.lifetime.start.p0(i64 72, ptr %10) #4
  call void @llvm.memset.p0.i64(ptr align 4 %10, i8 0, i64 72, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr %11) #4
  call void @llvm.memset.p0.i64(ptr align 4 %11, i8 0, i64 36, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr %12) #4
  call void @llvm.memset.p0.i64(ptr align 4 %12, i8 0, i64 36, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr %13) #4
  call void @llvm.memset.p0.i64(ptr align 4 %13, i8 0, i64 36, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  store i32 0, ptr %14, align 4, !tbaa !9
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = call i32 @ecdsa_read_pubkey(ptr noundef %15, ptr noundef %16, ptr noundef %9)
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %20, label %19

19:                                               ; preds = %4
  store i32 1, ptr %14, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %19, %4
  %21 = load i32, ptr %14, align 4, !tbaa !9
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %23, label %49

23:                                               ; preds = %20
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  call void @bn_read_be(ptr noundef %24, ptr noundef %11)
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 32
  call void @bn_read_be(ptr noundef %26, ptr noundef %12)
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  call void @bn_read_be(ptr noundef %27, ptr noundef %13)
  %28 = call i32 @bn_is_zero(ptr noundef %11)
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %43, label %30

30:                                               ; preds = %23
  %31 = call i32 @bn_is_zero(ptr noundef %12)
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %43, label %33

33:                                               ; preds = %30
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.ecdsa_curve, ptr %34, i32 0, i32 2
  %36 = call i32 @bn_is_less(ptr noundef %11, ptr noundef %35)
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %43

38:                                               ; preds = %33
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.ecdsa_curve, ptr %39, i32 0, i32 2
  %41 = call i32 @bn_is_less(ptr noundef %12, ptr noundef %40)
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %44, label %43

43:                                               ; preds = %38, %33, %30, %23
  store i32 2, ptr %14, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %43, %38
  %45 = call i32 @bn_is_zero(ptr noundef %13)
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %48

47:                                               ; preds = %44
  store i32 3, ptr %14, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %47, %44
  br label %49

49:                                               ; preds = %48, %20
  %50 = load i32, ptr %14, align 4, !tbaa !9
  %51 = icmp eq i32 %50, 0
  br i1 %51, label %52, label %59

52:                                               ; preds = %49
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.ecdsa_curve, ptr %53, i32 0, i32 2
  call void @bn_inverse(ptr noundef %12, ptr noundef %54)
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.ecdsa_curve, ptr %55, i32 0, i32 2
  call void @bn_multiply(ptr noundef %12, ptr noundef %13, ptr noundef %56)
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.ecdsa_curve, ptr %57, i32 0, i32 2
  call void @bn_mod(ptr noundef %13, ptr noundef %58)
  br label %59

59:                                               ; preds = %52, %49
  %60 = load i32, ptr %14, align 4, !tbaa !9
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %62, label %76

62:                                               ; preds = %59
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.ecdsa_curve, ptr %63, i32 0, i32 2
  call void @bn_multiply(ptr noundef %11, ptr noundef %12, ptr noundef %64)
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.ecdsa_curve, ptr %65, i32 0, i32 2
  call void @bn_mod(ptr noundef %12, ptr noundef %66)
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = call i32 @scalar_multiply(ptr noundef %67, ptr noundef %13, ptr noundef %10)
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = call i32 @point_multiply(ptr noundef %69, ptr noundef %12, ptr noundef %9, ptr noundef %9)
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  call void @point_add(ptr noundef %71, ptr noundef %9, ptr noundef %10)
  %72 = call i32 @point_is_infinity(ptr noundef %10)
  %73 = icmp ne i32 %72, 0
  br i1 %73, label %74, label %75

74:                                               ; preds = %62
  store i32 4, ptr %14, align 4, !tbaa !9
  br label %75

75:                                               ; preds = %74, %62
  br label %76

76:                                               ; preds = %75, %59
  %77 = load i32, ptr %14, align 4, !tbaa !9
  %78 = icmp eq i32 %77, 0
  br i1 %78, label %79, label %88

79:                                               ; preds = %76
  %80 = getelementptr inbounds %struct.curve_point, ptr %10, i32 0, i32 0
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.ecdsa_curve, ptr %81, i32 0, i32 2
  call void @bn_mod(ptr noundef %80, ptr noundef %82)
  %83 = getelementptr inbounds %struct.curve_point, ptr %10, i32 0, i32 0
  %84 = call i32 @bn_is_equal(ptr noundef %83, ptr noundef %11)
  %85 = icmp ne i32 %84, 0
  br i1 %85, label %87, label %86

86:                                               ; preds = %79
  store i32 5, ptr %14, align 4, !tbaa !9
  br label %87

87:                                               ; preds = %86, %79
  br label %88

88:                                               ; preds = %87, %76
  call void @memzero(ptr noundef %9, i64 noundef 72)
  call void @memzero(ptr noundef %10, i64 noundef 72)
  call void @memzero(ptr noundef %11, i64 noundef 36)
  call void @memzero(ptr noundef %12, i64 noundef 36)
  call void @memzero(ptr noundef %13, i64 noundef 36)
  %89 = load i32, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 72, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 72, ptr %9) #4
  ret i32 %89
}

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 = { 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 }

!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 = !{!"int", !7, 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"

%struct.curve_point = type { %struct.bignum256, %struct.bignum256 }
%struct.bignum256 = type { [9 x i32] }

; Function Attrs: nounwind uwtable
declare dso_local void @point_add(ptr noundef, ptr 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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @point_is_infinity(ptr noundef) local_unnamed_addr #0

declare void @bn_inverse(ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @bn_multiply(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @bn_mod(ptr noundef, ptr 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

declare i32 @bn_is_zero(ptr noundef) local_unnamed_addr #3

declare i32 @bn_is_equal(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @point_multiply(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

declare i32 @bn_is_less(ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @memzero(ptr noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @scalar_multiply(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @ecdsa_read_pubkey(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

declare void @bn_read_be(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local i32 @ecdsa_verify_digest(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = alloca %struct.curve_point, align 4
  %6 = alloca %struct.curve_point, align 4
  %7 = alloca %struct.bignum256, align 4
  %8 = alloca %struct.bignum256, align 4
  %9 = alloca %struct.bignum256, align 4
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %5) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(72) %5, i8 0, i64 72, i1 false)
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %6) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(72) %6, i8 0, i64 72, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %7) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %7, i8 0, i64 36, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %8) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %8, i8 0, i64 36, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %9) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %9, i8 0, i64 36, i1 false)
  %10 = call i32 @ecdsa_read_pubkey(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %5)
  %.not.not = icmp eq i32 %10, 0
  br i1 %.not.not, label %.critedge.thread, label %11

11:                                               ; preds = %4
  call void @bn_read_be(ptr noundef %2, ptr noundef nonnull %7) #4
  %12 = getelementptr inbounds i8, ptr %2, i64 32
  call void @bn_read_be(ptr noundef nonnull %12, ptr noundef nonnull %8) #4
  call void @bn_read_be(ptr noundef %3, ptr noundef nonnull %9) #4
  %13 = call i32 @bn_is_zero(ptr noundef nonnull %7) #4
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %14, label %.thread29

14:                                               ; preds = %11
  %15 = call i32 @bn_is_zero(ptr noundef nonnull %8) #4
  %.not20 = icmp eq i32 %15, 0
  br i1 %.not20, label %16, label %.thread29

16:                                               ; preds = %14
  %17 = getelementptr inbounds i8, ptr %0, i64 108
  %18 = call i32 @bn_is_less(ptr noundef nonnull %7, ptr noundef nonnull %17) #4
  %.not21 = icmp eq i32 %18, 0
  br i1 %.not21, label %.thread29, label %19

19:                                               ; preds = %16
  %20 = call i32 @bn_is_less(ptr noundef nonnull %8, ptr noundef nonnull %17) #4
  %.not22 = icmp eq i32 %20, 0
  br i1 %.not22, label %.thread29, label %21

21:                                               ; preds = %19
  %22 = call i32 @bn_is_zero(ptr noundef nonnull %9) #4
  %.not23 = icmp eq i32 %22, 0
  br i1 %.not23, label %24, label %.critedge.thread

.thread29:                                        ; preds = %11, %14, %16, %19
  %23 = call i32 @bn_is_zero(ptr noundef nonnull %9) #4
  %.not2331 = icmp eq i32 %23, 0
  %spec.select = select i1 %.not2331, i32 2, i32 3
  br label %.critedge.thread

24:                                               ; preds = %21
  call void @bn_inverse(ptr noundef nonnull %8, ptr noundef nonnull %17) #4
  call void @bn_multiply(ptr noundef nonnull %8, ptr noundef nonnull %9, ptr noundef nonnull %17) #4
  call void @bn_mod(ptr noundef nonnull %9, ptr noundef nonnull %17) #4
  call void @bn_multiply(ptr noundef nonnull %7, ptr noundef nonnull %8, ptr noundef nonnull %17) #4
  call void @bn_mod(ptr noundef nonnull %8, ptr noundef nonnull %17) #4
  %25 = call i32 @scalar_multiply(ptr noundef %0, ptr noundef nonnull %9, ptr noundef nonnull %6)
  %26 = call i32 @point_multiply(ptr noundef %0, ptr noundef nonnull %8, ptr noundef nonnull %5, ptr noundef nonnull %5)
  call void @point_add(ptr noundef %0, ptr noundef nonnull %5, ptr noundef nonnull %6)
  %27 = call i32 @point_is_infinity(ptr noundef nonnull %6)
  %.not24 = icmp eq i32 %27, 0
  br i1 %.not24, label %.critedge, label %.critedge.thread

.critedge:                                        ; preds = %24
  call void @bn_mod(ptr noundef nonnull %6, ptr noundef nonnull %17) #4
  %28 = call i32 @bn_is_equal(ptr noundef nonnull %6, ptr noundef nonnull %7) #4
  %.not25 = icmp eq i32 %28, 0
  %spec.select27 = select i1 %.not25, i32 5, i32 0
  br label %.critedge.thread

.critedge.thread:                                 ; preds = %.thread29, %21, %4, %24, %.critedge
  %.4 = phi i32 [ %spec.select27, %.critedge ], [ 4, %24 ], [ 3, %21 ], [ 1, %4 ], [ %spec.select, %.thread29 ]
  call void @memzero(ptr noundef nonnull %5, i64 noundef 72) #4
  call void @memzero(ptr noundef nonnull %6, i64 noundef 72) #4
  call void @memzero(ptr noundef nonnull %7, i64 noundef 36) #4
  call void @memzero(ptr noundef nonnull %8, i64 noundef 36) #4
  call void @memzero(ptr noundef nonnull %9, i64 noundef 36) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %9) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %5) #4
  ret i32 %.4
}

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 = { 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 }

!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)"}


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