; 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: inlinehint nounwind uwtable
declare dso_local i32 @read_le(ptr noundef) #0

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

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

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

8:                                                ; preds = %36, %2
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp slt i32 %9, 8
  br i1 %10, label %11, label %39

11:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = mul nsw i32 %13, 4
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds i8, ptr %12, i64 %15
  %17 = call i32 @read_le(ptr noundef %16)
  store i32 %17, ptr %7, align 4, !tbaa !9
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = mul nsw i32 2, %19
  %21 = shl i32 %18, %20
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = or i32 %22, %21
  store i32 %23, ptr %6, align 4, !tbaa !9
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = and i32 %24, 1073741823
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.bignum256, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [9 x i32], ptr %27, i64 0, i64 %29
  store i32 %25, ptr %30, align 4, !tbaa !9
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = load i32, ptr %5, align 4, !tbaa !9
  %33 = mul nsw i32 2, %32
  %34 = sub nsw i32 30, %33
  %35 = lshr i32 %31, %34
  store i32 %35, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %36

36:                                               ; preds = %11
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %5, align 4, !tbaa !9
  br label %8, !llvm.loop !11

39:                                               ; preds = %8
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.bignum256, ptr %41, i32 0, i32 0
  %43 = getelementptr inbounds [9 x i32], ptr %42, i64 0, i64 8
  store i32 %40, ptr %43, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

attributes #0 = { 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 #1 = { nocallback nofree nosync 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 = { 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: inlinehint nounwind uwtable
declare dso_local i32 @read_le(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @bn_read_le(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = tail call i32 @read_le(ptr noundef %0)
  %4 = and i32 %3, 1073741823
  store i32 %4, ptr %1, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = tail call i32 @read_le(ptr noundef nonnull %5)
  %7 = tail call i32 @llvm.fshl.i32(i32 %6, i32 %3, i32 2)
  %8 = and i32 %7, 1073741823
  %9 = getelementptr inbounds i8, ptr %1, i64 4
  store i32 %8, ptr %9, align 4, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = tail call i32 @read_le(ptr noundef nonnull %10)
  %12 = tail call i32 @llvm.fshl.i32(i32 %11, i32 %6, i32 4)
  %13 = and i32 %12, 1073741823
  %14 = getelementptr inbounds i8, ptr %1, i64 8
  store i32 %13, ptr %14, align 4, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %0, i64 12
  %16 = tail call i32 @read_le(ptr noundef nonnull %15)
  %17 = tail call i32 @llvm.fshl.i32(i32 %16, i32 %11, i32 6)
  %18 = and i32 %17, 1073741823
  %19 = getelementptr inbounds i8, ptr %1, i64 12
  store i32 %18, ptr %19, align 4, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %0, i64 16
  %21 = tail call i32 @read_le(ptr noundef nonnull %20)
  %22 = tail call i32 @llvm.fshl.i32(i32 %21, i32 %16, i32 8)
  %23 = and i32 %22, 1073741823
  %24 = getelementptr inbounds i8, ptr %1, i64 16
  store i32 %23, ptr %24, align 4, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %0, i64 20
  %26 = tail call i32 @read_le(ptr noundef nonnull %25)
  %27 = tail call i32 @llvm.fshl.i32(i32 %26, i32 %21, i32 10)
  %28 = and i32 %27, 1073741823
  %29 = getelementptr inbounds i8, ptr %1, i64 20
  store i32 %28, ptr %29, align 4, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %0, i64 24
  %31 = tail call i32 @read_le(ptr noundef nonnull %30)
  %32 = tail call i32 @llvm.fshl.i32(i32 %31, i32 %26, i32 12)
  %33 = and i32 %32, 1073741823
  %34 = getelementptr inbounds i8, ptr %1, i64 24
  store i32 %33, ptr %34, align 4, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %0, i64 28
  %36 = tail call i32 @read_le(ptr noundef nonnull %35)
  %37 = tail call i32 @llvm.fshl.i32(i32 %36, i32 %31, i32 14)
  %38 = and i32 %37, 1073741823
  %39 = getelementptr inbounds i8, ptr %1, i64 28
  store i32 %38, ptr %39, align 4, !tbaa !5
  %40 = lshr i32 %36, 16
  %41 = getelementptr inbounds i8, ptr %1, i64 32
  store i32 %40, ptr %41, align 4, !tbaa !5
  ret void
}

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

attributes #0 = { 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 #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) }

!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,LCSSAPass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass