; 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.bignum256 = type { [9 x i32] }
%struct.curve_point = type { %struct.bignum256, %struct.bignum256 }
%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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

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

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

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

declare void @bn_addmod(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 void @bn_subi(ptr noundef, i32 noundef, ptr noundef) #3

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @ecdsa_validate_pubkey(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.bignum256, align 4
  %7 = alloca %struct.bignum256, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 36, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 36, ptr %7) #4
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = call i32 @point_is_infinity(ptr noundef %9)
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %63

13:                                               ; preds = %2
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.curve_point, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.ecdsa_curve, ptr %16, i32 0, i32 0
  %18 = call i32 @bn_is_less(ptr noundef %15, ptr noundef %17)
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %27

20:                                               ; preds = %13
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.curve_point, ptr %21, i32 0, i32 1
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.ecdsa_curve, ptr %23, i32 0, i32 0
  %25 = call i32 @bn_is_less(ptr noundef %22, ptr noundef %24)
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %28, label %27

27:                                               ; preds = %20, %13
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %63

28:                                               ; preds = %20
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.curve_point, ptr %29, i32 0, i32 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %30, i64 36, i1 false)
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.curve_point, ptr %31, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %7, ptr align 4 %32, i64 36, i1 false)
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.curve_point, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.ecdsa_curve, ptr %35, i32 0, i32 0
  call void @bn_multiply(ptr noundef %34, ptr noundef %6, ptr noundef %36)
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.ecdsa_curve, ptr %37, i32 0, i32 0
  call void @bn_mod(ptr noundef %6, ptr noundef %38)
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.curve_point, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.ecdsa_curve, ptr %41, i32 0, i32 0
  call void @bn_multiply(ptr noundef %40, ptr noundef %7, ptr noundef %42)
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.ecdsa_curve, ptr %43, i32 0, i32 4
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = sub nsw i32 0, %45
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.ecdsa_curve, ptr %47, i32 0, i32 0
  call void @bn_subi(ptr noundef %7, i32 noundef %46, ptr noundef %48)
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.curve_point, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.ecdsa_curve, ptr %51, i32 0, i32 0
  call void @bn_multiply(ptr noundef %50, ptr noundef %7, ptr noundef %52)
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.ecdsa_curve, ptr %53, i32 0, i32 5
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.ecdsa_curve, ptr %55, i32 0, i32 0
  call void @bn_addmod(ptr noundef %7, ptr noundef %54, ptr noundef %56)
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.ecdsa_curve, ptr %57, i32 0, i32 0
  call void @bn_mod(ptr noundef %7, ptr noundef %58)
  %59 = call i32 @bn_is_equal(ptr noundef %7, ptr noundef %6)
  %60 = icmp ne i32 %59, 0
  br i1 %60, label %62, label %61

61:                                               ; preds = %28
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %63

62:                                               ; preds = %28
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %63

63:                                               ; preds = %62, %61, %27, %12
  call void @llvm.lifetime.end.p0(i64 36, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %6) #4
  %64 = load i32, ptr %3, align 4
  ret i32 %64
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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, !13, i64 180}
!10 = !{!"", !11, i64 0, !12, i64 36, !11, i64 108, !11, i64 144, !13, i64 180, !11, i64 184, !7, i64 220}
!11 = !{!"", !7, i64 0}
!12 = !{!"", !11, i64 0, !11, i64 36}
!13 = !{!"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.bignum256 = type { [9 x i32] }

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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: nounwind uwtable
declare dso_local i32 @point_is_infinity(ptr noundef) local_unnamed_addr #2

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

declare void @bn_addmod(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 void @bn_subi(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #3

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @ecdsa_validate_pubkey(ptr noundef %0, ptr noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.bignum256, align 4
  %4 = alloca %struct.bignum256, align 4
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %3) #4
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %4) #4
  %5 = tail call i32 @point_is_infinity(ptr noundef %1)
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %6, label %17

6:                                                ; preds = %2
  %7 = tail call i32 @bn_is_less(ptr noundef %1, ptr noundef %0) #4
  %.not19 = icmp eq i32 %7, 0
  br i1 %.not19, label %17, label %8

8:                                                ; preds = %6
  %9 = getelementptr inbounds i8, ptr %1, i64 36
  %10 = tail call i32 @bn_is_less(ptr noundef nonnull %9, ptr noundef %0) #4
  %.not20 = icmp eq i32 %10, 0
  br i1 %.not20, label %17, label %11

11:                                               ; preds = %8
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %3, ptr noundef nonnull align 4 dereferenceable(36) %9, i64 36, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %4, ptr noundef nonnull align 4 dereferenceable(36) %1, i64 36, i1 false)
  call void @bn_multiply(ptr noundef nonnull %9, ptr noundef nonnull %3, ptr noundef %0) #4
  call void @bn_mod(ptr noundef nonnull %3, ptr noundef %0) #4
  call void @bn_multiply(ptr noundef %1, ptr noundef nonnull %4, ptr noundef %0) #4
  %12 = getelementptr inbounds i8, ptr %0, i64 180
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = sub nsw i32 0, %13
  call void @bn_subi(ptr noundef nonnull %4, i32 noundef %14, ptr noundef %0) #4
  call void @bn_multiply(ptr noundef %1, ptr noundef nonnull %4, ptr noundef %0) #4
  %15 = getelementptr inbounds i8, ptr %0, i64 184
  call void @bn_addmod(ptr noundef nonnull %4, ptr noundef nonnull %15, ptr noundef %0) #4
  call void @bn_mod(ptr noundef nonnull %4, ptr noundef %0) #4
  %16 = call i32 @bn_is_equal(ptr noundef nonnull %4, ptr noundef nonnull %3) #4
  %.not21 = icmp ne i32 %16, 0
  %. = zext i1 %.not21 to i32
  br label %17

17:                                               ; preds = %11, %6, %8, %2
  %.0 = phi i32 [ 0, %2 ], [ 0, %8 ], [ 0, %6 ], [ %., %11 ]
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %3) #4
  ret i32 %.0
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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, !11, i64 180}
!6 = !{!"", !7, i64 0, !10, i64 36, !7, i64 108, !7, i64 144, !11, i64 180, !7, i64 184, !8, i64 220}
!7 = !{!"", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"", !7, i64 0, !7, i64 36}
!11 = !{!"int", !8, i64 0}


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