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

; 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] }

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @bn_one(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @bn_rshift(ptr noundef) #2

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

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

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

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

11:                                               ; preds = %47, %2
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = icmp ult i32 %12, 9
  br i1 %13, label %14, label %50

14:                                               ; preds = %11
  %15 = getelementptr inbounds %struct.bignum256, ptr %9, i32 0, i32 0
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = zext i32 %16 to i64
  %18 = getelementptr inbounds [9 x i32], ptr %15, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !9
  store i32 %19, ptr %7, align 4, !tbaa !9
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %43, %14
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = icmp ult i32 %21, 30
  br i1 %22, label %23, label %46

23:                                               ; preds = %20
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = icmp eq i32 %24, 8
  br i1 %25, label %26, label %30

26:                                               ; preds = %23
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %26
  br label %46

30:                                               ; preds = %26, %23
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = and i32 %31, 1
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %30
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  call void @bn_multiply(ptr noundef %35, ptr noundef %8, ptr noundef %36)
  br label %37

37:                                               ; preds = %34, %30
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = lshr i32 %38, 1
  store i32 %39, ptr %7, align 4, !tbaa !9
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  call void @bn_multiply(ptr noundef %40, ptr noundef %41, ptr noundef %42)
  br label %43

43:                                               ; preds = %37
  %44 = load i32, ptr %6, align 4, !tbaa !9
  %45 = add i32 %44, 1
  store i32 %45, ptr %6, align 4, !tbaa !9
  br label %20, !llvm.loop !11

46:                                               ; preds = %29, %20
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %5, align 4, !tbaa !9
  %49 = add i32 %48, 1
  store i32 %49, ptr %5, align 4, !tbaa !9
  br label %11, !llvm.loop !13

50:                                               ; preds = %11
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  call void @bn_mod(ptr noundef %8, ptr noundef %51)
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %52, ptr align 4 %8, i64 36, i1 false)
  call void @memzero(ptr noundef %8, i64 noundef 36)
  call void @memzero(ptr noundef %9, i64 noundef 36)
  call void @llvm.lifetime.end.p0(i64 36, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @bn_one(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @bn_rshift(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @bn_mod(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @bn_multiply(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local void @bn_sqrt(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
  call void @bn_one(ptr noundef nonnull %3)
  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_addi(ptr noundef nonnull %4, i32 noundef 1)
  call void @bn_rshift(ptr noundef nonnull %4)
  call void @bn_rshift(ptr noundef nonnull %4)
  br label %5

5:                                                ; preds = %2, %.split22.us
  %indvars.iv = phi i64 [ 0, %2 ], [ %indvars.iv.next, %.split22.us ]
  %6 = getelementptr inbounds [9 x i32], ptr %4, i64 0, i64 %indvars.iv
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = icmp eq i64 %indvars.iv, 8
  br i1 %8, label %.split, label %.split.us

.split.us:                                        ; preds = %5, %11
  %.020.us = phi i32 [ %12, %11 ], [ %7, %5 ]
  %.01719.us = phi i32 [ %13, %11 ], [ 0, %5 ]
  %9 = and i32 %.020.us, 1
  %.not.us = icmp eq i32 %9, 0
  br i1 %.not.us, label %11, label %10

10:                                               ; preds = %.split.us
  call void @bn_multiply(ptr noundef %0, ptr noundef nonnull %3, ptr noundef %1)
  br label %11

11:                                               ; preds = %10, %.split.us
  %12 = lshr i32 %.020.us, 1
  call void @bn_multiply(ptr noundef %0, ptr noundef %0, ptr noundef %1)
  %13 = add nuw nsw i32 %.01719.us, 1
  %exitcond.not = icmp eq i32 %13, 30
  br i1 %exitcond.not, label %.split22.us, label %.split.us, !llvm.loop !9

.split:                                           ; preds = %5, %18
  %.020 = phi i32 [ %19, %18 ], [ %7, %5 ]
  %.01719 = phi i32 [ %20, %18 ], [ 0, %5 ]
  %14 = icmp eq i32 %.020, 0
  br i1 %14, label %.split22.us, label %15

15:                                               ; preds = %.split
  %16 = and i32 %.020, 1
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %18, label %17

17:                                               ; preds = %15
  call void @bn_multiply(ptr noundef %0, ptr noundef nonnull %3, ptr noundef %1)
  br label %18

18:                                               ; preds = %17, %15
  %19 = lshr i32 %.020, 1
  call void @bn_multiply(ptr noundef %0, ptr noundef %0, ptr noundef %1)
  %20 = add nuw nsw i32 %.01719, 1
  %exitcond25.not = icmp eq i32 %20, 30
  br i1 %exitcond25.not, label %.split22.us, label %.split, !llvm.loop !9

.split22.us:                                      ; preds = %11, %.split, %18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond27.not = icmp eq i64 %indvars.iv.next, 9
  br i1 %exitcond27.not, label %21, label %5, !llvm.loop !11

21:                                               ; preds = %.split22.us
  call void @bn_mod(ptr noundef nonnull %3, ptr noundef %1)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %0, ptr noundef nonnull align 4 dereferenceable(36) %3, i64 36, i1 false)
  call void @memzero(ptr noundef nonnull %3, i64 noundef 36) #4
  call void @memzero(ptr noundef nonnull %4, i64 noundef 36) #4
  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 void
}

; Function Attrs: nounwind uwtable
declare dso_local void @bn_addi(ptr noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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