; 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: nounwind uwtable
define dso_local i32 @bn_bitcount(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 8, ptr %4, align 4, !tbaa !9
  br label %7

7:                                                ; preds = %30, %1
  %8 = load i32, ptr %4, align 4, !tbaa !9
  %9 = icmp sge i32 %8, 0
  br i1 %9, label %10, label %33

10:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.bignum256, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [9 x i32], ptr %12, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !9
  store i32 %16, ptr %5, align 4, !tbaa !9
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %26

19:                                               ; preds = %10
  %20 = load i32, ptr %4, align 4, !tbaa !9
  %21 = mul nsw i32 %20, 30
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = call i32 @llvm.ctlz.i32(i32 %22, i1 true)
  %24 = sub nsw i32 32, %23
  %25 = add nsw i32 %21, %24
  store i32 %25, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %27

26:                                               ; preds = %10
  store i32 0, ptr %6, align 4
  br label %27

27:                                               ; preds = %26, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %28 = load i32, ptr %6, align 4
  switch i32 %28, label %34 [
    i32 0, label %29
  ]

29:                                               ; preds = %27
  br label %30

30:                                               ; preds = %29
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = add nsw i32 %31, -1
  store i32 %32, ptr %4, align 4, !tbaa !9
  br label %7, !llvm.loop !11

33:                                               ; preds = %7
  store i32 0, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %34

34:                                               ; preds = %33, %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %35 = load i32, ptr %2, align 4
  ret i32 %35
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctlz.i32(i32, i1 immarg) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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}
!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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local i32 @bn_bitcount(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %.split.loop.exit11

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 28
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not.1 = icmp eq i32 %6, 0
  br i1 %.not.1, label %7, label %.split.loop.exit11

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 24
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not.2 = icmp eq i32 %9, 0
  br i1 %.not.2, label %10, label %.split.loop.exit11

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %0, i64 20
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %.not.3 = icmp eq i32 %12, 0
  br i1 %.not.3, label %13, label %.split.loop.exit11

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %.not.4 = icmp eq i32 %15, 0
  br i1 %.not.4, label %16, label %.split.loop.exit11

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %0, i64 12
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %.not.5 = icmp eq i32 %18, 0
  br i1 %.not.5, label %19, label %.split.loop.exit11

19:                                               ; preds = %16
  %20 = getelementptr inbounds i8, ptr %0, i64 8
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %.not.6 = icmp eq i32 %21, 0
  br i1 %.not.6, label %22, label %.split.loop.exit11

22:                                               ; preds = %19
  %23 = getelementptr inbounds i8, ptr %0, i64 4
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %.not.7 = icmp eq i32 %24, 0
  br i1 %.not.7, label %25, label %.split.loop.exit11

25:                                               ; preds = %22
  %26 = load i32, ptr %0, align 4, !tbaa !5
  %.not.8 = icmp eq i32 %26, 0
  br i1 %.not.8, label %.split.loop.exit, label %.split.loop.exit11

.split.loop.exit11:                               ; preds = %25, %22, %19, %16, %13, %10, %7, %4, %1
  %.0915.lcssa = phi i32 [ 272, %1 ], [ 242, %4 ], [ 212, %7 ], [ 182, %10 ], [ 152, %13 ], [ 122, %16 ], [ 92, %19 ], [ 62, %22 ], [ 32, %25 ]
  %.lcssa = phi i32 [ %3, %1 ], [ %6, %4 ], [ %9, %7 ], [ %12, %10 ], [ %15, %13 ], [ %18, %16 ], [ %21, %19 ], [ %24, %22 ], [ %26, %25 ]
  %27 = tail call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 %.lcssa, i1 true)
  %28 = sub nuw nsw i32 %.0915.lcssa, %27
  br label %.split.loop.exit

.split.loop.exit:                                 ; preds = %25, %.split.loop.exit11
  %.2 = phi i32 [ %28, %.split.loop.exit11 ], [ 0, %25 ]
  ret i32 %.2
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctlz.i32(i32, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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 speculatable willreturn memory(none) }

!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"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass