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

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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @get_curve_by_name(ptr noundef) #1

; 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) #2

declare void @memzero(ptr noundef, i64 noundef) #3

; 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 i32 @hdnode_from_xprv(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5) #1 {
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i8, align 1
  %15 = alloca ptr, align 8
  %16 = alloca %struct.bignum256, align 4
  %17 = alloca i32, align 4
  store i32 %0, ptr %8, align 4, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !5
  store ptr %2, ptr %10, align 8, !tbaa !9
  store ptr %3, ptr %11, align 8, !tbaa !9
  store ptr %4, ptr %12, align 8, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #4
  store i8 0, ptr %14, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %18 = load ptr, ptr %12, align 8, !tbaa !9
  %19 = call ptr @get_curve_by_name(ptr noundef %18)
  store ptr %19, ptr %15, align 8, !tbaa !9
  %20 = load ptr, ptr %15, align 8, !tbaa !9
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %23

22:                                               ; preds = %6
  store i8 1, ptr %14, align 1, !tbaa !11
  br label %44

23:                                               ; preds = %6
  %24 = load ptr, ptr %15, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.curve_info, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %43

28:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 36, ptr %16) #4
  %29 = load ptr, ptr %11, align 8, !tbaa !9
  call void @bn_read_be(ptr noundef %29, ptr noundef %16)
  %30 = call i32 @bn_is_zero(ptr noundef %16)
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %28
  store i8 1, ptr %14, align 1, !tbaa !11
  br label %42

33:                                               ; preds = %28
  %34 = load ptr, ptr %15, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.curve_info, ptr %34, i32 0, i32 1
  %36 = load ptr, ptr %35, align 8, !tbaa !13
  %37 = getelementptr inbounds %struct.ecdsa_curve, ptr %36, i32 0, i32 2
  %38 = call i32 @bn_is_less(ptr noundef %16, ptr noundef %37)
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %41, label %40

40:                                               ; preds = %33
  store i8 1, ptr %14, align 1, !tbaa !11
  br label %41

41:                                               ; preds = %40, %33
  br label %42

42:                                               ; preds = %41, %32
  call void @memzero(ptr noundef %16, i64 noundef 36)
  call void @llvm.lifetime.end.p0(i64 36, ptr %16) #4
  br label %43

43:                                               ; preds = %42, %23
  br label %44

44:                                               ; preds = %43, %22
  %45 = load i8, ptr %14, align 1, !tbaa !11, !range !15, !noundef !16
  %46 = trunc i8 %45 to i1
  br i1 %46, label %47, label %48

47:                                               ; preds = %44
  store i32 0, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %72

48:                                               ; preds = %44
  %49 = load ptr, ptr %15, align 8, !tbaa !9
  %50 = load ptr, ptr %13, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.HDNode, ptr %50, i32 0, i32 6
  store ptr %49, ptr %51, align 8, !tbaa !17
  %52 = load i32, ptr %8, align 4, !tbaa !5
  %53 = load ptr, ptr %13, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.HDNode, ptr %53, i32 0, i32 0
  store i32 %52, ptr %54, align 8, !tbaa !19
  %55 = load i32, ptr %9, align 4, !tbaa !5
  %56 = load ptr, ptr %13, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.HDNode, ptr %56, i32 0, i32 1
  store i32 %55, ptr %57, align 4, !tbaa !20
  %58 = load ptr, ptr %13, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.HDNode, ptr %58, i32 0, i32 2
  %60 = getelementptr inbounds [32 x i8], ptr %59, i64 0, i64 0
  %61 = load ptr, ptr %10, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %60, ptr align 1 %61, i64 32, i1 false)
  %62 = load ptr, ptr %13, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.HDNode, ptr %62, i32 0, i32 3
  %64 = getelementptr inbounds [32 x i8], ptr %63, i64 0, i64 0
  %65 = load ptr, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %64, ptr align 1 %65, i64 32, i1 false)
  %66 = load ptr, ptr %13, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.HDNode, ptr %66, i32 0, i32 5
  %68 = getelementptr inbounds [33 x i8], ptr %67, i64 0, i64 0
  call void @memzero(ptr noundef %68, i64 noundef 33)
  %69 = load ptr, ptr %13, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.HDNode, ptr %69, i32 0, i32 4
  %71 = getelementptr inbounds [32 x i8], ptr %70, i64 0, i64 0
  call void @memzero(ptr noundef %71, i64 noundef 32)
  store i32 1, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %72

72:                                               ; preds = %48, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #4
  %73 = load i32, ptr %7, align 4
  ret i32 %73
}

declare void @bn_read_be(ptr noundef, ptr noundef) #3

declare i32 @bn_is_zero(ptr noundef) #3

declare i32 @bn_is_less(ptr noundef, ptr noundef) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!18, !10, i64 144}
!18 = !{!"", !6, i64 0, !6, i64 4, !7, i64 8, !7, i64 40, !7, i64 72, !7, i64 104, !10, i64 144}
!19 = !{!18, !6, i64 0}
!20 = !{!18, !6, i64 4}


; 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.bignum256 = type { [9 x i32] }

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @get_curve_by_name(ptr noundef) local_unnamed_addr #1

; 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) #2

declare void @memzero(ptr noundef, i64 noundef) local_unnamed_addr #3

; 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 range(i32 0, 2) i32 @hdnode_from_xprv(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2, ptr noundef %3, ptr noundef %4, ptr noundef %5) local_unnamed_addr #1 {
  %7 = alloca %struct.bignum256, align 4
  %8 = tail call ptr @get_curve_by_name(ptr noundef %4)
  %9 = icmp eq ptr %8, null
  br i1 %9, label %.critedge23, label %10

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %8, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %.critedge, label %13

13:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %7) #4
  call void @bn_read_be(ptr noundef %3, ptr noundef nonnull %7) #4
  %14 = call i32 @bn_is_zero(ptr noundef nonnull %7) #4
  %.not20 = icmp eq i32 %14, 0
  br i1 %.not20, label %15, label %.critedge25

15:                                               ; preds = %13
  %16 = load ptr, ptr %11, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 108
  %18 = call i32 @bn_is_less(ptr noundef nonnull %7, ptr noundef nonnull %17) #4
  %.not21 = icmp eq i32 %18, 0
  call void @memzero(ptr noundef nonnull %7, i64 noundef 36) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %7) #4
  br i1 %.not21, label %.critedge23, label %.critedge

.critedge:                                        ; preds = %10, %15
  %19 = getelementptr inbounds i8, ptr %5, i64 144
  store ptr %8, ptr %19, align 8, !tbaa !11
  store i32 %0, ptr %5, align 8, !tbaa !13
  %20 = getelementptr inbounds i8, ptr %5, i64 4
  store i32 %1, ptr %20, align 4, !tbaa !14
  %21 = getelementptr inbounds i8, ptr %5, i64 8
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %21, ptr noundef nonnull align 1 dereferenceable(32) %2, i64 32, i1 false)
  %22 = getelementptr inbounds i8, ptr %5, i64 40
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %22, ptr noundef nonnull align 1 dereferenceable(32) %3, i64 32, i1 false)
  %23 = getelementptr inbounds i8, ptr %5, i64 104
  call void @memzero(ptr noundef nonnull %23, i64 noundef 33) #4
  %24 = getelementptr inbounds i8, ptr %5, i64 72
  call void @memzero(ptr noundef nonnull %24, i64 noundef 32) #4
  br label %.critedge23

.critedge25:                                      ; preds = %13
  call void @memzero(ptr noundef nonnull %7, i64 noundef 36) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %7) #4
  br label %.critedge23

.critedge23:                                      ; preds = %6, %.critedge25, %15, %.critedge
  %.0 = phi i32 [ 1, %.critedge ], [ 0, %15 ], [ 0, %.critedge25 ], [ 0, %6 ]
  ret i32 %.0
}

declare void @bn_read_be(ptr noundef, ptr noundef) local_unnamed_addr #3

declare i32 @bn_is_zero(ptr noundef) local_unnamed_addr #3

declare i32 @bn_is_less(ptr noundef, ptr noundef) local_unnamed_addr #3

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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, !7, i64 8}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !7, i64 144}
!12 = !{!"", !10, i64 0, !10, i64 4, !8, i64 8, !8, i64 40, !8, i64 72, !8, i64 104, !7, i64 144}
!13 = !{!12, !10, i64 0}
!14 = !{!12, !10, i64 4}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass