; LLVM IR from square/subzero :: core/trezor-crypto (schnorr)

; 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.schnorr_sign_pair = 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: nounwind uwtable
define dso_local i32 @schnorr_sign(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, i32 noundef %4, ptr noundef %5) #0 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca %struct.bignum256, align 4
  %15 = alloca [33 x i8], align 16
  %16 = alloca %struct.curve_point, align 4
  %17 = alloca %struct.bignum256, align 4
  %18 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store ptr %2, ptr %10, align 8, !tbaa !5
  store ptr %3, ptr %11, align 8, !tbaa !5
  store i32 %4, ptr %12, align 4, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 36, ptr %14) #4
  %19 = load ptr, ptr %9, align 8, !tbaa !5
  call void @bn_read_be(ptr noundef %19, ptr noundef %14)
  call void @llvm.lifetime.start.p0(i64 33, ptr %15) #4
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = getelementptr inbounds [33 x i8], ptr %15, i64 0, i64 0
  call void @ecdsa_get_public_key33(ptr noundef %20, ptr noundef %21, ptr noundef %22)
  call void @llvm.lifetime.start.p0(i64 72, ptr %16) #4
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  call void @scalar_multiply(ptr noundef %23, ptr noundef %24, ptr noundef %16)
  %25 = getelementptr inbounds [33 x i8], ptr %15, i64 0, i64 0
  %26 = load ptr, ptr %11, align 8, !tbaa !5
  %27 = load i32, ptr %12, align 4, !tbaa !9
  %28 = load ptr, ptr %13, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.schnorr_sign_pair, ptr %28, i32 0, i32 0
  call void @calc_r(ptr noundef %16, ptr noundef %25, ptr noundef %26, i32 noundef %27, ptr noundef %29)
  call void @llvm.lifetime.start.p0(i64 36, ptr %17) #4
  %30 = load ptr, ptr %13, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.schnorr_sign_pair, ptr %30, i32 0, i32 0
  call void @bn_copy(ptr noundef %31, ptr noundef %17)
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ecdsa_curve, ptr %32, i32 0, i32 2
  call void @bn_multiply(ptr noundef %14, ptr noundef %17, ptr noundef %33)
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = load ptr, ptr %13, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.schnorr_sign_pair, ptr %35, i32 0, i32 1
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.ecdsa_curve, ptr %37, i32 0, i32 2
  call void @bn_subtractmod(ptr noundef %34, ptr noundef %17, ptr noundef %36, ptr noundef %38)
  call void @memzero(ptr noundef %14, i64 noundef 36)
  br label %39

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

46:                                               ; preds = %39
  %47 = load ptr, ptr %13, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.schnorr_sign_pair, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.ecdsa_curve, ptr %49, i32 0, i32 2
  call void @bn_mod(ptr noundef %48, ptr noundef %50)
  br label %39, !llvm.loop !11

51:                                               ; preds = %39
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.schnorr_sign_pair, ptr %52, i32 0, i32 1
  %54 = call i32 @bn_is_zero(ptr noundef %53)
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %61, label %56

56:                                               ; preds = %51
  %57 = load ptr, ptr %13, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.schnorr_sign_pair, ptr %57, i32 0, i32 0
  %59 = call i32 @bn_is_zero(ptr noundef %58)
  %60 = icmp ne i32 %59, 0
  br i1 %60, label %61, label %62

61:                                               ; preds = %56, %51
  store i32 1, ptr %7, align 4
  store i32 1, ptr %18, align 4
  br label %63

62:                                               ; preds = %56
  store i32 0, ptr %7, align 4
  store i32 1, ptr %18, align 4
  br label %63

63:                                               ; preds = %62, %61
  call void @llvm.lifetime.end.p0(i64 36, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 72, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 33, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %14) #4
  %64 = load i32, ptr %7, align 4
  ret i32 %64
}

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

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

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

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

; Function Attrs: nounwind uwtable
declare hidden void @calc_r(ptr noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef) #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @bn_copy(ptr noundef, ptr noundef) #3

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

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

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

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

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

declare i32 @bn_is_zero(ptr noundef) #2

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

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 = { inlinehint 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 #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}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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] }
%struct.curve_point = type { %struct.bignum256, %struct.bignum256 }

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @schnorr_sign(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, i32 noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %7 = alloca %struct.bignum256, align 4
  %8 = alloca [33 x i8], align 16
  %9 = alloca %struct.curve_point, align 4
  %10 = alloca %struct.bignum256, align 4
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %7) #4
  call void @bn_read_be(ptr noundef %1, ptr noundef nonnull %7) #4
  call void @llvm.lifetime.start.p0(i64 33, ptr nonnull %8) #4
  call void @ecdsa_get_public_key33(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %8) #4
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %9) #4
  call void @scalar_multiply(ptr noundef %0, ptr noundef %2, ptr noundef nonnull %9) #4
  call void @calc_r(ptr noundef nonnull %9, ptr noundef nonnull %8, ptr noundef %3, i32 noundef %4, ptr noundef %5)
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %10) #4
  call void @bn_copy(ptr noundef %5, ptr noundef nonnull %10)
  %11 = getelementptr inbounds i8, ptr %0, i64 108
  call void @bn_multiply(ptr noundef nonnull %7, ptr noundef nonnull %10, ptr noundef nonnull %11) #4
  %12 = getelementptr inbounds i8, ptr %5, i64 36
  call void @bn_subtractmod(ptr noundef %2, ptr noundef nonnull %10, ptr noundef nonnull %12, ptr noundef nonnull %11) #4
  call void @memzero(ptr noundef nonnull %7, i64 noundef 36) #4
  %13 = call i32 @bn_is_less(ptr noundef nonnull %11, ptr noundef nonnull %12) #4
  %.not21 = icmp eq i32 %13, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %.lr.ph
  call void @bn_mod(ptr noundef nonnull %12, ptr noundef nonnull %11) #4
  %14 = call i32 @bn_is_less(ptr noundef nonnull %11, ptr noundef nonnull %12) #4
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %6
  %15 = call i32 @bn_is_zero(ptr noundef nonnull %12) #4
  %.not19 = icmp eq i32 %15, 0
  br i1 %.not19, label %16, label %18

16:                                               ; preds = %._crit_edge
  %17 = call i32 @bn_is_zero(ptr noundef %5) #4
  %.not20 = icmp ne i32 %17, 0
  %spec.select = zext i1 %.not20 to i32
  br label %18

18:                                               ; preds = %16, %._crit_edge
  %.0 = phi i32 [ 1, %._crit_edge ], [ %spec.select, %16 ]
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %10) #4
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %9) #4
  call void @llvm.lifetime.end.p0(i64 33, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %7) #4
  ret i32 %.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_read_be(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

; Function Attrs: nounwind uwtable
declare hidden void @calc_r(ptr noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @bn_copy(ptr noundef, ptr noundef) local_unnamed_addr #3

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

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

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

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

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

declare i32 @bn_is_zero(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

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 = { inlinehint 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 #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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass