; 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: nounwind uwtable
define dso_local void @bn_read_le(ptr noundef %0, ptr noundef %1) #1 {
  %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 = %38, %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 %41

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

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @read_le(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 = { 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_le(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = tail call i32 @read_le(ptr noundef %0)
  %4 = and i32 %3, 536870911
  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 3)
  %8 = and i32 %7, 536870911
  %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 6)
  %13 = and i32 %12, 536870911
  %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 9)
  %18 = and i32 %17, 536870911
  %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 12)
  %23 = and i32 %22, 536870911
  %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 15)
  %28 = and i32 %27, 536870911
  %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 18)
  %33 = and i32 %32, 536870911
  %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 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_le(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,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass