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

@.str = external hidden unnamed_addr constant [23 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [76 x i8], align 1
@__PRETTY_FUNCTION__.bn_cmov = external hidden unnamed_addr constant [69 x i8], align 1

; 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 void @bn_cmov(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  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 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = sub nsw i32 0, %12
  store i32 %13, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %14 = load i32, ptr %10, align 4, !tbaa !9
  %15 = xor i32 %14, -1
  store i32 %15, ptr %11, align 4, !tbaa !9
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = icmp eq i32 %16, 1
  br i1 %17, label %21, label %18

18:                                               ; preds = %4
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %18, %4
  br label %23

22:                                               ; preds = %18
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 275, ptr noundef @__PRETTY_FUNCTION__.bn_cmov) #4
  unreachable

23:                                               ; preds = %21
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %50, %23
  %25 = load i32, ptr %9, align 4, !tbaa !9
  %26 = icmp slt i32 %25, 9
  br i1 %26, label %27, label %53

27:                                               ; preds = %24
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.bignum256, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [9 x i32], ptr %29, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = load i32, ptr %10, align 4, !tbaa !9
  %35 = and i32 %33, %34
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.bignum256, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %9, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [9 x i32], ptr %37, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = and i32 %41, %42
  %44 = or i32 %35, %43
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.bignum256, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %9, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [9 x i32], ptr %46, i64 0, i64 %48
  store i32 %44, ptr %49, align 4, !tbaa !9
  br label %50

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

53:                                               ; preds = %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret void
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #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 = { noreturn nounwind "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 = { nounwind }
attributes #4 = { noreturn 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"

@.str = external hidden unnamed_addr constant [23 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [76 x i8], align 1
@__PRETTY_FUNCTION__.bn_cmov = external hidden unnamed_addr constant [69 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @bn_cmov(ptr nocapture noundef writeonly %0, i32 noundef %1, ptr nocapture noundef readonly %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = sub nsw i32 0, %1
  %or.cond = icmp ult i32 %1, 2
  br i1 %or.cond, label %.preheader, label %64

.preheader:                                       ; preds = %4
  %6 = icmp eq i32 %1, 0
  br i1 %6, label %.preheader.split.us.preheader, label %.preheader.split.preheader

.preheader.split.preheader:                       ; preds = %.preheader
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = and i32 %7, %5
  store i32 %8, ptr %0, align 4, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %2, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = and i32 %10, %5
  %12 = getelementptr inbounds i8, ptr %0, i64 4
  store i32 %11, ptr %12, align 4, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %2, i64 8
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = and i32 %14, %5
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %15, ptr %16, align 4, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %2, i64 12
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = and i32 %18, %5
  %20 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %19, ptr %20, align 4, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %2, i64 16
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = and i32 %22, %5
  %24 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 %23, ptr %24, align 4, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %2, i64 20
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = and i32 %26, %5
  %28 = getelementptr inbounds i8, ptr %0, i64 20
  store i32 %27, ptr %28, align 4, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %2, i64 24
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = and i32 %30, %5
  %32 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 %31, ptr %32, align 4, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %2, i64 28
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = and i32 %34, %5
  %36 = getelementptr inbounds i8, ptr %0, i64 28
  store i32 %35, ptr %36, align 4, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %2, i64 32
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = and i32 %38, %5
  br label %.split.us

.preheader.split.us.preheader:                    ; preds = %.preheader
  %40 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %40, ptr %0, align 4, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %3, i64 4
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %0, i64 4
  store i32 %42, ptr %43, align 4, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %3, i64 8
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %45, ptr %46, align 4, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %3, i64 12
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %48, ptr %49, align 4, !tbaa !5
  %50 = getelementptr inbounds i8, ptr %3, i64 16
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 %51, ptr %52, align 4, !tbaa !5
  %53 = getelementptr inbounds i8, ptr %3, i64 20
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = getelementptr inbounds i8, ptr %0, i64 20
  store i32 %54, ptr %55, align 4, !tbaa !5
  %56 = getelementptr inbounds i8, ptr %3, i64 24
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 %57, ptr %58, align 4, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %3, i64 28
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = getelementptr inbounds i8, ptr %0, i64 28
  store i32 %60, ptr %61, align 4, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %3, i64 32
  %63 = load i32, ptr %62, align 4, !tbaa !5
  br label %.split.us

64:                                               ; preds = %4
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 275, ptr noundef nonnull @__PRETTY_FUNCTION__.bn_cmov) #2
  unreachable

.split.us:                                        ; preds = %.preheader.split.preheader, %.preheader.split.us.preheader
  %.sink = phi i32 [ %39, %.preheader.split.preheader ], [ %63, %.preheader.split.us.preheader ]
  %65 = getelementptr inbounds i8, ptr %0, i64 32
  store i32 %.sink, ptr %65, align 4, !tbaa !5
  ret void
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #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 = { noreturn nounwind "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 = { noreturn 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"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass