; 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.HDNode = type { i32, i32, [32 x i8], [32 x i8], [32 x i8], [33 x i8], ptr }

@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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare void @hdnode_fill_public_key(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @bench_ckd_normal(i32 noundef %0) #3 {
  %2 = alloca i32, align 4
  %3 = alloca [130 x i8], align 16
  %4 = alloca %struct.HDNode, align 8
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 130, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 152, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %6

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

10:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %20

11:                                               ; preds = %6
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %4, ptr align 8 @root, i64 152, i1 false)
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = call i32 @hdnode_public_ckd(ptr noundef %4, i32 noundef %12)
  call void @hdnode_fill_public_key(ptr noundef %4)
  %14 = getelementptr inbounds %struct.HDNode, ptr %4, i32 0, i32 5
  %15 = getelementptr inbounds [33 x i8], ptr %14, i64 0, i64 0
  %16 = getelementptr inbounds [130 x i8], ptr %3, i64 0, i64 0
  call void @ecdsa_get_address(ptr noundef %15, i32 noundef 0, i32 noundef 1, i32 noundef 0, ptr noundef %16, i32 noundef 130)
  br label %17

17:                                               ; preds = %11
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %5, align 4, !tbaa !5
  br label %6, !llvm.loop !9

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

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

declare void @ecdsa_get_address(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 #4 = { 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.HDNode = type { i32, i32, [32 x i8], [32 x i8], [32 x i8], [33 x i8], ptr }

@root = external hidden local_unnamed_addr 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare void @hdnode_fill_public_key(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @bench_ckd_normal(i32 noundef %0) local_unnamed_addr #3 {
  %2 = alloca [130 x i8], align 16
  %3 = alloca %struct.HDNode, align 8
  call void @llvm.lifetime.start.p0(i64 130, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 152, ptr nonnull %3) #4
  %4 = icmp sgt i32 %0, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %5 = getelementptr inbounds i8, ptr %3, i64 104
  br label %6

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

6:                                                ; preds = %.lr.ph, %6
  %.04 = phi i32 [ 0, %.lr.ph ], [ %8, %6 ]
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(152) %3, ptr noundef nonnull align 8 dereferenceable(152) @root, i64 152, i1 false)
  %7 = call i32 @hdnode_public_ckd(ptr noundef nonnull %3, i32 noundef %.04) #4
  call void @hdnode_fill_public_key(ptr noundef nonnull %3) #4
  call void @ecdsa_get_address(ptr noundef nonnull %5, i32 noundef 0, i32 noundef 1, i32 noundef 0, ptr noundef nonnull %2, i32 noundef 130) #4
  %8 = add nuw nsw i32 %.04, 1
  %exitcond.not = icmp eq i32 %8, %0
  br i1 %exitcond.not, label %._crit_edge, label %6, !llvm.loop !5
}

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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 #4 = { 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,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass