; 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._HMAC_SHA512_CTX = type { [128 x i8], %struct._SHA512_CTX }
%struct._SHA512_CTX = type { [8 x i64], [2 x i64], [16 x i64] }
%struct.bignum256 = type { [9 x i32] }
%struct.HDNode = type { i32, i32, [32 x i8], [32 x i8], [32 x i8], [33 x i8], ptr }
%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 }

; 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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

; Function Attrs: nounwind uwtable
define dso_local i32 @hdnode_from_seed(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca [64 x i8], align 16
  %11 = alloca i32, align 4
  %12 = alloca %struct._HMAC_SHA512_CTX, align 8
  %13 = alloca %struct.bignum256, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 64, ptr %10) #6
  %14 = load ptr, ptr %9, align 8, !tbaa !5
  call void @memzero(ptr noundef %14, i64 noundef 152)
  %15 = load ptr, ptr %9, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.HDNode, ptr %15, i32 0, i32 0
  store i32 0, ptr %16, align 8, !tbaa !11
  %17 = load ptr, ptr %9, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.HDNode, ptr %17, i32 0, i32 1
  store i32 0, ptr %18, align 4, !tbaa !13
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = call ptr @get_curve_by_name(ptr noundef %19)
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.HDNode, ptr %21, i32 0, i32 6
  store ptr %20, ptr %22, align 8, !tbaa !14
  %23 = load ptr, ptr %9, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.HDNode, ptr %23, i32 0, i32 6
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  %26 = icmp eq ptr %25, null
  br i1 %26, label %27, label %28

27:                                               ; preds = %4
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %96

28:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 336, ptr %12) #6
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.HDNode, ptr %29, i32 0, i32 6
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  %32 = getelementptr inbounds %struct.curve_info, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !15
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.HDNode, ptr %34, i32 0, i32 6
  %36 = load ptr, ptr %35, align 8, !tbaa !14
  %37 = getelementptr inbounds %struct.curve_info, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  %39 = call i64 @strlen(ptr noundef %38) #7
  %40 = trunc i64 %39 to i32
  call void @hmac_sha512_Init(ptr noundef %12, ptr noundef %33, i32 noundef %40)
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i32, ptr %7, align 4, !tbaa !9
  call void @hmac_sha512_Update(ptr noundef %12, ptr noundef %41, i32 noundef %42)
  %43 = getelementptr inbounds [64 x i8], ptr %10, i64 0, i64 0
  call void @hmac_sha512_Final(ptr noundef %12, ptr noundef %43)
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.HDNode, ptr %44, i32 0, i32 6
  %46 = load ptr, ptr %45, align 8, !tbaa !14
  %47 = getelementptr inbounds %struct.curve_info, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !17
  %49 = icmp ne ptr %48, null
  br i1 %49, label %50, label %82

50:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 36, ptr %13) #6
  call void @llvm.memset.p0.i64(ptr align 4 %13, i8 0, i64 36, i1 false)
  br label %51

51:                                               ; preds = %66, %50
  br label %52

52:                                               ; preds = %51
  %53 = getelementptr inbounds [64 x i8], ptr %10, i64 0, i64 0
  call void @bn_read_be(ptr noundef %53, ptr noundef %13)
  %54 = call i32 @bn_is_zero(ptr noundef %13)
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %66, label %56

56:                                               ; preds = %52
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.HDNode, ptr %57, i32 0, i32 6
  %59 = load ptr, ptr %58, align 8, !tbaa !14
  %60 = getelementptr inbounds %struct.curve_info, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !17
  %62 = getelementptr inbounds %struct.ecdsa_curve, ptr %61, i32 0, i32 2
  %63 = call i32 @bn_is_less(ptr noundef %13, ptr noundef %62)
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %66

65:                                               ; preds = %56
  br label %81

66:                                               ; preds = %56, %52
  %67 = load ptr, ptr %9, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.HDNode, ptr %67, i32 0, i32 6
  %69 = load ptr, ptr %68, align 8, !tbaa !14
  %70 = getelementptr inbounds %struct.curve_info, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !15
  %72 = load ptr, ptr %9, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.HDNode, ptr %72, i32 0, i32 6
  %74 = load ptr, ptr %73, align 8, !tbaa !14
  %75 = getelementptr inbounds %struct.curve_info, ptr %74, i32 0, i32 0
  %76 = load ptr, ptr %75, align 8, !tbaa !15
  %77 = call i64 @strlen(ptr noundef %76) #7
  %78 = trunc i64 %77 to i32
  call void @hmac_sha512_Init(ptr noundef %12, ptr noundef %71, i32 noundef %78)
  %79 = getelementptr inbounds [64 x i8], ptr %10, i64 0, i64 0
  call void @hmac_sha512_Update(ptr noundef %12, ptr noundef %79, i32 noundef 64)
  %80 = getelementptr inbounds [64 x i8], ptr %10, i64 0, i64 0
  call void @hmac_sha512_Final(ptr noundef %12, ptr noundef %80)
  br label %51

81:                                               ; preds = %65
  call void @memzero(ptr noundef %13, i64 noundef 36)
  call void @llvm.lifetime.end.p0(i64 36, ptr %13) #6
  br label %82

82:                                               ; preds = %81, %28
  %83 = load ptr, ptr %9, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.HDNode, ptr %83, i32 0, i32 3
  %85 = getelementptr inbounds [32 x i8], ptr %84, i64 0, i64 0
  %86 = getelementptr inbounds [64 x i8], ptr %10, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %85, ptr align 16 %86, i64 32, i1 false)
  %87 = load ptr, ptr %9, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.HDNode, ptr %87, i32 0, i32 2
  %89 = getelementptr inbounds [32 x i8], ptr %88, i64 0, i64 0
  %90 = getelementptr inbounds [64 x i8], ptr %10, i64 0, i64 0
  %91 = getelementptr inbounds i8, ptr %90, i64 32
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %89, ptr align 1 %91, i64 32, i1 false)
  %92 = load ptr, ptr %9, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.HDNode, ptr %92, i32 0, i32 5
  %94 = getelementptr inbounds [33 x i8], ptr %93, i64 0, i64 0
  call void @memzero(ptr noundef %94, i64 noundef 33)
  %95 = getelementptr inbounds [64 x i8], ptr %10, i64 0, i64 0
  call void @memzero(ptr noundef %95, i64 noundef 64)
  store i32 1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 336, ptr %12) #6
  br label %96

96:                                               ; preds = %82, %27
  call void @llvm.lifetime.end.p0(i64 64, ptr %10) #6
  %97 = load i32, ptr %5, align 4
  ret i32 %97
}

declare void @hmac_sha512_Init(ptr noundef, ptr noundef, i32 noundef) #3

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #5

declare void @hmac_sha512_Update(ptr noundef, ptr noundef, i32 noundef) #3

declare void @hmac_sha512_Final(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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind willreturn memory(read) "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 }
attributes #7 = { nounwind willreturn memory(read) }

!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 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 40, !7, i64 72, !7, i64 104, !6, i64 144}
!13 = !{!12, !10, i64 4}
!14 = !{!12, !6, i64 144}
!15 = !{!16, !6, i64 0}
!16 = !{!"", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28}
!17 = !{!16, !6, i64 8}


; 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._HMAC_SHA512_CTX = type { [128 x i8], %struct._SHA512_CTX }
%struct._SHA512_CTX = type { [8 x i64], [2 x i64], [16 x i64] }
%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: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @hdnode_from_seed(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = alloca [64 x i8], align 16
  %6 = alloca %struct._HMAC_SHA512_CTX, align 8
  %7 = alloca %struct.bignum256, align 4
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #6
  tail call void @memzero(ptr noundef %3, i64 noundef 152) #6
  store i32 0, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %3, i64 4
  store i32 0, ptr %8, align 4, !tbaa !11
  %9 = tail call ptr @get_curve_by_name(ptr noundef %2)
  %10 = getelementptr inbounds i8, ptr %3, i64 144
  store ptr %9, ptr %10, align 8, !tbaa !12
  %11 = icmp eq ptr %9, null
  br i1 %11, label %39, label %12

12:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 336, ptr nonnull %6) #6
  %13 = load ptr, ptr %9, align 8, !tbaa !13
  %14 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %13) #7
  %15 = trunc i64 %14 to i32
  call void @hmac_sha512_Init(ptr noundef nonnull %6, ptr noundef %13, i32 noundef %15) #6
  call void @hmac_sha512_Update(ptr noundef nonnull %6, ptr noundef %0, i32 noundef %1) #6
  call void @hmac_sha512_Final(ptr noundef nonnull %6, ptr noundef nonnull %5) #6
  %16 = load ptr, ptr %10, align 8, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %16, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !15
  %.not = icmp eq ptr %18, null
  br i1 %.not, label %34, label %19

19:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %7) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(36) %7, i8 0, i64 36, i1 false)
  br label %20

20:                                               ; preds = %28, %19
  call void @bn_read_be(ptr noundef nonnull %5, ptr noundef nonnull %7) #6
  %21 = call i32 @bn_is_zero(ptr noundef nonnull %7) #6
  %.not17 = icmp eq i32 %21, 0
  br i1 %.not17, label %22, label %28

22:                                               ; preds = %20
  %23 = load ptr, ptr %10, align 8, !tbaa !12
  %24 = getelementptr inbounds i8, ptr %23, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = getelementptr inbounds i8, ptr %25, i64 108
  %27 = call i32 @bn_is_less(ptr noundef nonnull %7, ptr noundef nonnull %26) #6
  %.not18 = icmp eq i32 %27, 0
  br i1 %.not18, label %28, label %33

28:                                               ; preds = %22, %20
  %29 = load ptr, ptr %10, align 8, !tbaa !12
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %30) #7
  %32 = trunc i64 %31 to i32
  call void @hmac_sha512_Init(ptr noundef nonnull %6, ptr noundef %30, i32 noundef %32) #6
  call void @hmac_sha512_Update(ptr noundef nonnull %6, ptr noundef nonnull %5, i32 noundef 64) #6
  call void @hmac_sha512_Final(ptr noundef nonnull %6, ptr noundef nonnull %5) #6
  br label %20

33:                                               ; preds = %22
  call void @memzero(ptr noundef nonnull %7, i64 noundef 36) #6
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %7) #6
  br label %34

34:                                               ; preds = %33, %12
  %35 = getelementptr inbounds i8, ptr %3, i64 40
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %35, ptr noundef nonnull align 16 dereferenceable(32) %5, i64 32, i1 false)
  %36 = getelementptr inbounds i8, ptr %3, i64 8
  %37 = getelementptr inbounds i8, ptr %5, i64 32
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %36, ptr noundef nonnull align 16 dereferenceable(32) %37, i64 32, i1 false)
  %38 = getelementptr inbounds i8, ptr %3, i64 104
  call void @memzero(ptr noundef nonnull %38, i64 noundef 33) #6
  call void @memzero(ptr noundef nonnull %5, i64 noundef 64) #6
  call void @llvm.lifetime.end.p0(i64 336, ptr nonnull %6) #6
  br label %39

39:                                               ; preds = %4, %34
  %.0 = phi i32 [ 1, %34 ], [ 0, %4 ]
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #6
  ret i32 %.0
}

declare void @hmac_sha512_Init(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #5

declare void @hmac_sha512_Update(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

declare void @hmac_sha512_Final(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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 }
attributes #7 = { nounwind willreturn memory(read) }

!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 0}
!6 = !{!"", !7, i64 0, !7, i64 4, !8, i64 8, !8, i64 40, !8, i64 72, !8, i64 104, !10, i64 144}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !10, i64 144}
!13 = !{!14, !10, i64 0}
!14 = !{!"", !10, i64 0, !10, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28}
!15 = !{!14, !10, i64 8}


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