; LLVM IR from trustwallet/wallet-core :: trezor-crypto/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_point = type { %struct.bignum256, %struct.bignum256 }
%struct.ecdsa_curve = type { %struct.bignum256, %struct.curve_point, %struct.bignum256, %struct.bignum256, i32, %struct.bignum256, [64 x [8 x %struct.curve_point]] }

; 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 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 @memzero(ptr noundef, i64 noundef) #2

; 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: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @write_be(ptr noundef, i32 noundef) #4

declare void @bn_write_be(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @hdnode_public_ckd_cp(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef %5) #5 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca [37 x i8], align 16
  %15 = alloca [64 x i8], align 16
  %16 = alloca %struct.bignum256, align 4
  %17 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store ptr %2, ptr %10, align 8, !tbaa !5
  store i32 %3, ptr %11, align 4, !tbaa !9
  store ptr %4, ptr %12, align 8, !tbaa !5
  store ptr %5, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 37, ptr %14) #6
  call void @llvm.memset.p0.i64(ptr align 16 %14, i8 0, i64 37, i1 false)
  call void @llvm.lifetime.start.p0(i64 64, ptr %15) #6
  call void @llvm.memset.p0.i64(ptr align 16 %15, i8 0, i64 64, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr %16) #6
  call void @llvm.memset.p0.i64(ptr align 4 %16, i8 0, i64 36, i1 false)
  %18 = load i32, ptr %11, align 4, !tbaa !9
  %19 = and i32 %18, -2147483648
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %6
  store i32 0, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %76

22:                                               ; preds = %6
  %23 = load ptr, ptr %9, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.curve_point, ptr %23, i32 0, i32 1
  %25 = getelementptr inbounds %struct.bignum256, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds [9 x i32], ptr %25, i64 0, i64 0
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = and i32 %27, 1
  %29 = or i32 2, %28
  %30 = trunc i32 %29 to i8
  %31 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  store i8 %30, ptr %31, align 16, !tbaa !11
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.curve_point, ptr %32, i32 0, i32 0
  %34 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  %35 = getelementptr inbounds i8, ptr %34, i64 1
  call void @bn_write_be(ptr noundef %33, ptr noundef %35)
  %36 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  %37 = getelementptr inbounds i8, ptr %36, i64 33
  %38 = load i32, ptr %11, align 4, !tbaa !9
  call void @write_be(ptr noundef %37, i32 noundef %38)
  br label %39

39:                                               ; preds = %70, %22
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr %10, align 8, !tbaa !5
  %42 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  %43 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  call void @hmac_sha512(ptr noundef %41, i32 noundef 32, ptr noundef %42, i32 noundef 37, ptr noundef %43)
  %44 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  call void @bn_read_be(ptr noundef %44, ptr noundef %16)
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.ecdsa_curve, ptr %45, i32 0, i32 2
  %47 = call i32 @bn_is_less(ptr noundef %16, ptr noundef %46)
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %70

49:                                               ; preds = %40
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = load ptr, ptr %12, align 8, !tbaa !5
  %52 = call i32 @scalar_multiply(ptr noundef %50, ptr noundef %16, ptr noundef %51)
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  %55 = load ptr, ptr %12, align 8, !tbaa !5
  call void @point_add(ptr noundef %53, ptr noundef %54, ptr noundef %55)
  %56 = load ptr, ptr %12, align 8, !tbaa !5
  %57 = call i32 @point_is_infinity(ptr noundef %56)
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %69, label %59

59:                                               ; preds = %49
  %60 = load ptr, ptr %13, align 8, !tbaa !5
  %61 = icmp ne ptr %60, null
  br i1 %61, label %62, label %66

62:                                               ; preds = %59
  %63 = load ptr, ptr %13, align 8, !tbaa !5
  %64 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  %65 = getelementptr inbounds i8, ptr %64, i64 32
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %63, ptr align 1 %65, i64 32, i1 false)
  br label %66

66:                                               ; preds = %62, %59
  %67 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  call void @memzero(ptr noundef %67, i64 noundef 37)
  %68 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  call void @memzero(ptr noundef %68, i64 noundef 64)
  call void @memzero(ptr noundef %16, i64 noundef 36)
  store i32 1, ptr %7, align 4
  store i32 1, ptr %17, align 4
  br label %76

69:                                               ; preds = %49
  br label %70

70:                                               ; preds = %69, %40
  %71 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  store i8 1, ptr %71, align 16, !tbaa !11
  %72 = getelementptr inbounds [37 x i8], ptr %14, i64 0, i64 0
  %73 = getelementptr inbounds i8, ptr %72, i64 1
  %74 = getelementptr inbounds [64 x i8], ptr %15, i64 0, i64 0
  %75 = getelementptr inbounds i8, ptr %74, i64 32
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %73, ptr align 1 %75, i64 32, i1 false)
  br label %39

76:                                               ; preds = %66, %21
  call void @llvm.lifetime.end.p0(i64 36, ptr %16) #6
  call void @llvm.lifetime.end.p0(i64 64, ptr %15) #6
  call void @llvm.lifetime.end.p0(i64 37, ptr %14) #6
  %77 = load i32, ptr %7, align 4
  ret i32 %77
}

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

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

declare void @point_add(ptr noundef, ptr noundef, ptr noundef) #2

declare i32 @point_is_infinity(ptr 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #5 = { 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 #6 = { 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 = !{!7, !7, i64 0}


; 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: 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 @memzero(ptr noundef, i64 noundef) local_unnamed_addr #2

; 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: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @write_be(ptr noundef, i32 noundef) local_unnamed_addr #4

declare void @bn_write_be(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @hdnode_public_ckd_cp(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef writeonly %5) local_unnamed_addr #5 {
  %7 = alloca [37 x i8], align 16
  %8 = alloca [64 x i8], align 16
  %9 = alloca %struct.bignum256, align 4
  call void @llvm.lifetime.start.p0(i64 37, ptr nonnull %7) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(37) %7, i8 0, i64 37, i1 false)
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %8) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %8, i8 0, i64 64, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %9) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %9, i8 0, i64 36, i1 false)
  %.not = icmp sgt i32 %3, -1
  br i1 %.not, label %10, label %29

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %1, i64 36
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = trunc i32 %12 to i8
  %14 = and i8 %13, 1
  %15 = or disjoint i8 %14, 2
  store i8 %15, ptr %7, align 16, !tbaa !9
  %16 = getelementptr inbounds i8, ptr %7, i64 1
  call void @bn_write_be(ptr noundef %1, ptr noundef nonnull %16) #6
  %17 = getelementptr inbounds i8, ptr %7, i64 33
  call void @write_be(ptr noundef nonnull %17, i32 noundef %3)
  %18 = getelementptr inbounds i8, ptr %0, i64 108
  %19 = getelementptr inbounds i8, ptr %8, i64 32
  br label %20

20:                                               ; preds = %28, %10
  call void @hmac_sha512(ptr noundef %2, i32 noundef 32, ptr noundef nonnull %7, i32 noundef 37, ptr noundef nonnull %8) #6
  call void @bn_read_be(ptr noundef nonnull %8, ptr noundef nonnull %9) #6
  %21 = call i32 @bn_is_less(ptr noundef nonnull %9, ptr noundef nonnull %18) #6
  %.not15 = icmp eq i32 %21, 0
  br i1 %.not15, label %28, label %22

22:                                               ; preds = %20
  %23 = call i32 @scalar_multiply(ptr noundef %0, ptr noundef nonnull %9, ptr noundef %4) #6
  call void @point_add(ptr noundef %0, ptr noundef %1, ptr noundef %4) #6
  %24 = call i32 @point_is_infinity(ptr noundef %4) #6
  %.not16 = icmp eq i32 %24, 0
  br i1 %.not16, label %25, label %28

25:                                               ; preds = %22
  %.not17 = icmp eq ptr %5, null
  br i1 %.not17, label %27, label %26

26:                                               ; preds = %25
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %5, ptr noundef nonnull align 16 dereferenceable(32) %19, i64 32, i1 false)
  br label %27

27:                                               ; preds = %26, %25
  call void @memzero(ptr noundef nonnull %7, i64 noundef 37) #6
  call void @memzero(ptr noundef nonnull %8, i64 noundef 64) #6
  call void @memzero(ptr noundef nonnull %9, i64 noundef 36) #6
  br label %29

28:                                               ; preds = %22, %20
  store i8 1, ptr %7, align 16, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %16, ptr noundef nonnull align 16 dereferenceable(32) %19, i64 32, i1 false)
  br label %20

29:                                               ; preds = %6, %27
  %.0 = phi i32 [ 1, %27 ], [ 0, %6 ]
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %9) #6
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %8) #6
  call void @llvm.lifetime.end.p0(i64 37, ptr nonnull %7) #6
  ret i32 %.0
}

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

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

declare void @point_add(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @point_is_infinity(ptr 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #5 = { 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 #6 = { 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 = !{!7, !7, i64 0}


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