; LLVM IR from square/subzero :: core/trezor-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

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

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

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @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) #2

; 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 i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %struct.curve_point, align 4
  %11 = alloca %struct.curve_point, align 4
  %12 = alloca %struct.bignum256, align 4
  %13 = alloca %struct.bignum256, align 4
  %14 = alloca %struct.bignum256, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 72, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 72, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr %14) #3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = call i32 @ecdsa_read_pubkey(ptr noundef %17, ptr noundef %18, ptr noundef %10)
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %22, label %21

21:                                               ; preds = %4
  store i32 1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %75

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

29:                                               ; preds = %22
  %30 = call i32 @bn_is_zero(ptr noundef %13)
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %42, label %32

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

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

42:                                               ; preds = %37, %32, %29, %22
  store i32 2, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %75

43:                                               ; preds = %37
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.ecdsa_curve, ptr %44, i32 0, i32 2
  call void @bn_inverse(ptr noundef %13, ptr noundef %45)
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.ecdsa_curve, ptr %46, i32 0, i32 2
  call void @bn_multiply(ptr noundef %13, ptr noundef %14, ptr noundef %47)
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.ecdsa_curve, ptr %48, i32 0, i32 2
  call void @bn_mod(ptr noundef %14, ptr noundef %49)
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ecdsa_curve, ptr %50, i32 0, i32 2
  call void @bn_multiply(ptr noundef %12, ptr noundef %13, ptr noundef %51)
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.ecdsa_curve, ptr %52, i32 0, i32 2
  call void @bn_mod(ptr noundef %13, ptr noundef %53)
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  store i32 0, ptr %16, align 4, !tbaa !9
  %54 = call i32 @bn_is_zero(ptr noundef %14)
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %43
  store i32 3, ptr %16, align 4, !tbaa !9
  br label %59

57:                                               ; preds = %43
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  call void @scalar_multiply(ptr noundef %58, ptr noundef %14, ptr noundef %11)
  br label %59

59:                                               ; preds = %57, %56
  %60 = load i32, ptr %16, align 4, !tbaa !9
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %62, label %73

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

71:                                               ; preds = %62
  store i32 5, ptr %16, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %71, %62
  br label %73

73:                                               ; preds = %72, %59
  call void @memzero(ptr noundef %10, i64 noundef 72)
  call void @memzero(ptr noundef %11, i64 noundef 72)
  call void @memzero(ptr noundef %12, i64 noundef 36)
  call void @memzero(ptr noundef %13, i64 noundef 36)
  call void @memzero(ptr noundef %14, i64 noundef 36)
  %74 = load i32, ptr %16, align 4, !tbaa !9
  store i32 %74, ptr %5, align 4
  store i32 1, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  br label %75

75:                                               ; preds = %73, %42, %21
  call void @llvm.lifetime.end.p0(i64 36, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr %10) #3
  %76 = load i32, ptr %5, align 4
  ret i32 %76
}

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 }

!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

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

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

declare void @bn_mod(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @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 #2

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

; Function Attrs: nounwind uwtable
declare dso_local void @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 #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 6) 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) #3
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %6) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %7) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %8) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %9) #3
  %10 = call i32 @ecdsa_read_pubkey(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %5)
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %26, label %11

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

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

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) #3
  %.not21 = icmp eq i32 %18, 0
  br i1 %.not21, label %26, label %19

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

21:                                               ; preds = %19
  call void @bn_inverse(ptr noundef nonnull %8, ptr noundef nonnull %17) #3
  call void @bn_multiply(ptr noundef nonnull %8, ptr noundef nonnull %9, ptr noundef nonnull %17) #3
  call void @bn_mod(ptr noundef nonnull %9, ptr noundef nonnull %17) #3
  call void @bn_multiply(ptr noundef nonnull %7, ptr noundef nonnull %8, ptr noundef nonnull %17) #3
  call void @bn_mod(ptr noundef nonnull %8, ptr noundef nonnull %17) #3
  %22 = call i32 @bn_is_zero(ptr noundef nonnull %9) #3
  %.not23 = icmp eq i32 %22, 0
  br i1 %.not23, label %23, label %25

23:                                               ; preds = %21
  call void @scalar_multiply(ptr noundef %0, ptr noundef nonnull %9, ptr noundef nonnull %6)
  call void @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)
  call void @bn_mod(ptr noundef nonnull %6, ptr noundef nonnull %17) #3
  %24 = call i32 @bn_is_equal(ptr noundef nonnull %6, ptr noundef nonnull %7) #3
  %.not24 = icmp eq i32 %24, 0
  %spec.select = select i1 %.not24, i32 5, i32 0
  br label %25

25:                                               ; preds = %21, %23
  %.1 = phi i32 [ %spec.select, %23 ], [ 3, %21 ]
  call void @memzero(ptr noundef nonnull %5, i64 noundef 72) #3
  call void @memzero(ptr noundef nonnull %6, i64 noundef 72) #3
  call void @memzero(ptr noundef nonnull %7, i64 noundef 36) #3
  call void @memzero(ptr noundef nonnull %8, i64 noundef 36) #3
  call void @memzero(ptr noundef nonnull %9, i64 noundef 36) #3
  br label %26

26:                                               ; preds = %11, %14, %16, %19, %4, %25
  %.018 = phi i32 [ %.1, %25 ], [ 1, %4 ], [ 2, %19 ], [ 2, %16 ], [ 2, %14 ], [ 2, %11 ]
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %9) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %8) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %7) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %5) #3
  ret i32 %.018
}

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 = { 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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass