; LLVM IR from square/subzero :: core/trezor-crypto/tests (test_speed)

; 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.ecdsa_curve = type { %struct.bignum256, %struct.curve_point, %struct.bignum256, %struct.bignum256, i32, %struct.bignum256, [64 x [8 x %struct.curve_point]] }
%struct.curve_point = type { %struct.bignum256, %struct.bignum256 }
%struct.bignum256 = type { [9 x i32] }
%struct.HDNode = type { i32, i32, [32 x i8], [32 x i8], [32 x i8], [33 x i8], ptr }

@secp256k1 = external constant %struct.ecdsa_curve, align 4
@root = external hidden global %struct.HDNode, align 8

; 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 @bench_ckd_optimized(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca [130 x i8], align 16
  %4 = alloca %struct.curve_point, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 130, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 72, ptr %4) #3
  %6 = call i32 @ecdsa_read_pubkey(ptr noundef @secp256k1, ptr noundef getelementptr inbounds (%struct.HDNode, ptr @root, i32 0, i32 5), ptr noundef %4)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %7

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

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

12:                                               ; preds = %7
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = getelementptr inbounds [130 x i8], ptr %3, i64 0, i64 0
  call void @hdnode_public_ckd_address_optimized(ptr noundef %4, ptr noundef getelementptr inbounds (%struct.HDNode, ptr @root, i32 0, i32 2), i32 noundef %13, i32 noundef 0, i32 noundef 0, i32 noundef 1, ptr noundef %14, i32 noundef 130, i32 noundef 0)
  br label %15

15:                                               ; preds = %12
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %5, align 4, !tbaa !5
  br label %7, !llvm.loop !9

18:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 72, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 130, ptr %3) #3
  ret void
}

declare i32 @ecdsa_read_pubkey(ptr noundef, ptr noundef, ptr noundef) #2

declare void @hdnode_public_ckd_address_optimized(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef, i32 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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

%struct.ecdsa_curve = type { %struct.bignum256, %struct.curve_point, %struct.bignum256, %struct.bignum256, i32, %struct.bignum256, [64 x [8 x %struct.curve_point]] }
%struct.curve_point = type { %struct.bignum256, %struct.bignum256 }
%struct.bignum256 = type { [9 x i32] }
%struct.HDNode = type { i32, i32, [32 x i8], [32 x i8], [32 x i8], [33 x i8], ptr }

@secp256k1 = external constant %struct.ecdsa_curve, align 4
@root = external hidden global %struct.HDNode, align 8

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

; Function Attrs: mustprogress 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 @bench_ckd_optimized(i32 noundef %0) local_unnamed_addr #1 {
  %2 = alloca [130 x i8], align 16
  %3 = alloca %struct.curve_point, align 4
  call void @llvm.lifetime.start.p0(i64 130, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %3) #3
  %4 = call i32 @ecdsa_read_pubkey(ptr noundef nonnull @secp256k1, ptr noundef nonnull getelementptr inbounds (i8, ptr @root, i64 104), ptr noundef nonnull %3) #3
  %5 = icmp sgt i32 %0, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %1
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 130, ptr nonnull %2) #3
  ret void

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.04 = phi i32 [ %6, %.lr.ph ], [ 0, %1 ]
  call void @hdnode_public_ckd_address_optimized(ptr noundef nonnull %3, ptr noundef nonnull getelementptr inbounds (i8, ptr @root, i64 8), i32 noundef %.04, i32 noundef 0, i32 noundef 0, i32 noundef 1, ptr noundef nonnull %2, i32 noundef 130, i32 noundef 0) #3
  %6 = add nuw nsw i32 %.04, 1
  %exitcond.not = icmp eq i32 %6, %0
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5
}

declare i32 @ecdsa_read_pubkey(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @hdnode_public_ckd_address_optimized(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { "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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass