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

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

11:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %42

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

39:                                               ; preds = %12
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %6, align 4, !tbaa !9
  br label %8, !llvm.loop !11

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @read_be(ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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: nounwind uwtable
define dso_local void @bn_read_be(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 28
  %4 = tail call i32 @read_be(ptr noundef nonnull %3)
  %5 = and i32 %4, 536870911
  store i32 %5, ptr %1, align 4, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = tail call i32 @read_be(ptr noundef nonnull %6)
  %8 = tail call i32 @llvm.fshl.i32(i32 %7, i32 %4, i32 3)
  %9 = and i32 %8, 536870911
  %10 = getelementptr inbounds i8, ptr %1, i64 4
  store i32 %9, ptr %10, align 4, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %0, i64 20
  %12 = tail call i32 @read_be(ptr noundef nonnull %11)
  %13 = tail call i32 @llvm.fshl.i32(i32 %12, i32 %7, i32 6)
  %14 = and i32 %13, 536870911
  %15 = getelementptr inbounds i8, ptr %1, i64 8
  store i32 %14, ptr %15, align 4, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %0, i64 16
  %17 = tail call i32 @read_be(ptr noundef nonnull %16)
  %18 = tail call i32 @llvm.fshl.i32(i32 %17, i32 %12, i32 9)
  %19 = and i32 %18, 536870911
  %20 = getelementptr inbounds i8, ptr %1, i64 12
  store i32 %19, ptr %20, align 4, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %0, i64 12
  %22 = tail call i32 @read_be(ptr noundef nonnull %21)
  %23 = tail call i32 @llvm.fshl.i32(i32 %22, i32 %17, i32 12)
  %24 = and i32 %23, 536870911
  %25 = getelementptr inbounds i8, ptr %1, i64 16
  store i32 %24, ptr %25, align 4, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %0, i64 8
  %27 = tail call i32 @read_be(ptr noundef nonnull %26)
  %28 = tail call i32 @llvm.fshl.i32(i32 %27, i32 %22, i32 15)
  %29 = and i32 %28, 536870911
  %30 = getelementptr inbounds i8, ptr %1, i64 20
  store i32 %29, ptr %30, align 4, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %0, i64 4
  %32 = tail call i32 @read_be(ptr noundef nonnull %31)
  %33 = tail call i32 @llvm.fshl.i32(i32 %32, i32 %27, i32 18)
  %34 = and i32 %33, 536870911
  %35 = getelementptr inbounds i8, ptr %1, i64 24
  store i32 %34, ptr %35, align 4, !tbaa !5
  %36 = tail call i32 @read_be(ptr noundef %0)
  %37 = tail call i32 @llvm.fshl.i32(i32 %36, i32 %32, i32 21)
  %38 = and i32 %37, 536870911
  %39 = getelementptr inbounds i8, ptr %1, i64 28
  store i32 %38, ptr %39, align 4, !tbaa !5
  %40 = lshr i32 %36, 8
  %41 = getelementptr inbounds i8, ptr %1, i64 32
  store i32 %40, ptr %41, align 4, !tbaa !5
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @read_be(ptr noundef) local_unnamed_addr #1

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

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 = { 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 #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,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass