; LLVM IR from trustwallet/wallet-core :: trezor-crypto/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: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @bn_power_mod(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #4 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca %struct.bignum256, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = 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 36, ptr %9) #5
  call void @llvm.memset.p0.i64(ptr align 4 %9, i8 0, i64 36, i1 false)
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  call void @bn_copy(ptr noundef %14, ptr noundef %9)
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  call void @bn_one(ptr noundef %15)
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %54, %4
  %17 = load i32, ptr %10, align 4, !tbaa !9
  %18 = icmp slt i32 %17, 9
  br i1 %18, label %20, label %19

19:                                               ; preds = %16
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  br label %57

20:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.bignum256, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %10, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [9 x i32], ptr %22, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !9
  store i32 %26, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %49, %20
  %28 = load i32, ptr %13, align 4, !tbaa !9
  %29 = icmp slt i32 %28, 29
  br i1 %29, label %31, label %30

30:                                               ; preds = %27
  store i32 5, ptr %11, align 4
  br label %52

31:                                               ; preds = %27
  %32 = load i32, ptr %10, align 4, !tbaa !9
  %33 = icmp eq i32 %32, 8
  br i1 %33, label %34, label %38

34:                                               ; preds = %31
  %35 = load i32, ptr %12, align 4, !tbaa !9
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %34
  store i32 5, ptr %11, align 4
  br label %52

38:                                               ; preds = %34, %31
  %39 = load i32, ptr %12, align 4, !tbaa !9
  %40 = and i32 %39, 1
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %45

42:                                               ; preds = %38
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  call void @bn_multiply(ptr noundef %9, ptr noundef %43, ptr noundef %44)
  br label %45

45:                                               ; preds = %42, %38
  %46 = load i32, ptr %12, align 4, !tbaa !9
  %47 = lshr i32 %46, 1
  store i32 %47, ptr %12, align 4, !tbaa !9
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  call void @bn_multiply(ptr noundef %9, ptr noundef %9, ptr noundef %48)
  br label %49

49:                                               ; preds = %45
  %50 = load i32, ptr %13, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %13, align 4, !tbaa !9
  br label %27, !llvm.loop !11

52:                                               ; preds = %37, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  br label %53

53:                                               ; preds = %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %10, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %10, align 4, !tbaa !9
  br label %16, !llvm.loop !13

57:                                               ; preds = %19
  call void @memzero(ptr noundef %9, i64 noundef 36)
  call void @llvm.lifetime.end.p0(i64 36, ptr %9) #5
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #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 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 #5 = { 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: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @bn_power_mod(ptr noundef %0, ptr nocapture noundef readonly %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #4 {
  %5 = alloca %struct.bignum256, align 4
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %5) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %5, i8 0, i64 36, i1 false)
  call void @bn_copy(ptr noundef %0, ptr noundef nonnull %5)
  call void @bn_one(ptr noundef %3)
  br label %7

6:                                                ; preds = %.split20.us
  call void @memzero(ptr noundef nonnull %5, i64 noundef 36) #5
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %5) #5
  ret void

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

.split.us:                                        ; preds = %7, %13
  %.018.us = phi i32 [ %15, %13 ], [ 0, %7 ]
  %.01517.us = phi i32 [ %14, %13 ], [ %9, %7 ]
  %11 = and i32 %.01517.us, 1
  %.not.us = icmp eq i32 %11, 0
  br i1 %.not.us, label %13, label %12

12:                                               ; preds = %.split.us
  call void @bn_multiply(ptr noundef nonnull %5, ptr noundef %3, ptr noundef %2)
  br label %13

13:                                               ; preds = %12, %.split.us
  %14 = lshr i32 %.01517.us, 1
  call void @bn_multiply(ptr noundef nonnull %5, ptr noundef nonnull %5, ptr noundef %2)
  %15 = add nuw nsw i32 %.018.us, 1
  %exitcond.not = icmp eq i32 %15, 29
  br i1 %exitcond.not, label %.split20.us, label %.split.us, !llvm.loop !9

.split:                                           ; preds = %7, %20
  %.018 = phi i32 [ %22, %20 ], [ 0, %7 ]
  %.01517 = phi i32 [ %21, %20 ], [ %9, %7 ]
  %16 = icmp eq i32 %.01517, 0
  br i1 %16, label %.split20.us, label %17

17:                                               ; preds = %.split
  %18 = and i32 %.01517, 1
  %.not = icmp eq i32 %18, 0
  br i1 %.not, label %20, label %19

19:                                               ; preds = %17
  call void @bn_multiply(ptr noundef nonnull %5, ptr noundef %3, ptr noundef %2)
  br label %20

20:                                               ; preds = %19, %17
  %21 = lshr i32 %.01517, 1
  call void @bn_multiply(ptr noundef nonnull %5, ptr noundef nonnull %5, ptr noundef %2)
  %22 = add nuw nsw i32 %.018, 1
  %exitcond23.not = icmp eq i32 %22, 29
  br i1 %exitcond23.not, label %.split20.us, label %.split, !llvm.loop !9

.split20.us:                                      ; preds = %13, %20, %.split
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond25.not = icmp eq i64 %indvars.iv.next, 9
  br i1 %exitcond25.not, label %6, label %7, !llvm.loop !11
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #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 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 #5 = { 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,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass