; 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._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 }

@hdnode_from_seed.I = external hidden global [64 x i8], align 16
@hdnode_from_seed.ctx = external hidden global %struct._HMAC_SHA512_CTX, 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: 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

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 %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
  %11 = load ptr, ptr %9, align 8, !tbaa !5
  call void @memzero(ptr noundef %11, i64 noundef 152)
  %12 = load ptr, ptr %9, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.HDNode, ptr %12, i32 0, i32 0
  store i32 0, ptr %13, align 8, !tbaa !11
  %14 = load ptr, ptr %9, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.HDNode, ptr %14, i32 0, i32 1
  store i32 0, ptr %15, align 4, !tbaa !13
  %16 = load ptr, ptr %8, align 8, !tbaa !5
  %17 = call ptr @get_curve_by_name(ptr noundef %16)
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.HDNode, ptr %18, i32 0, i32 6
  store ptr %17, ptr %19, align 8, !tbaa !14
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.HDNode, ptr %20, i32 0, i32 6
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %25

24:                                               ; preds = %4
  store i32 0, ptr %5, align 4
  br label %85

25:                                               ; preds = %4
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.HDNode, ptr %26, i32 0, i32 6
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = getelementptr inbounds %struct.curve_info, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.HDNode, ptr %31, i32 0, i32 6
  %33 = load ptr, ptr %32, align 8, !tbaa !14
  %34 = getelementptr inbounds %struct.curve_info, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !15
  %36 = call i64 @strlen(ptr noundef %35) #5
  %37 = trunc i64 %36 to i32
  call void @hmac_sha512_Init(ptr noundef @hdnode_from_seed.ctx, ptr noundef %30, i32 noundef %37)
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = load i32, ptr %7, align 4, !tbaa !9
  call void @hmac_sha512_Update(ptr noundef @hdnode_from_seed.ctx, ptr noundef %38, i32 noundef %39)
  call void @hmac_sha512_Final(ptr noundef @hdnode_from_seed.ctx, ptr noundef @hdnode_from_seed.I)
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.HDNode, ptr %40, i32 0, i32 6
  %42 = load ptr, ptr %41, align 8, !tbaa !14
  %43 = getelementptr inbounds %struct.curve_info, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !17
  %45 = icmp ne ptr %44, null
  br i1 %45, label %46, label %75

46:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 36, ptr %10) #6
  br label %47

47:                                               ; preds = %61, %46
  br label %48

48:                                               ; preds = %47
  call void @bn_read_be(ptr noundef @hdnode_from_seed.I, ptr noundef %10)
  %49 = call i32 @bn_is_zero(ptr noundef %10)
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %61, label %51

51:                                               ; preds = %48
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.HDNode, ptr %52, i32 0, i32 6
  %54 = load ptr, ptr %53, align 8, !tbaa !14
  %55 = getelementptr inbounds %struct.curve_info, ptr %54, i32 0, i32 1
  %56 = load ptr, ptr %55, align 8, !tbaa !17
  %57 = getelementptr inbounds %struct.ecdsa_curve, ptr %56, i32 0, i32 2
  %58 = call i32 @bn_is_less(ptr noundef %10, ptr noundef %57)
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %61

60:                                               ; preds = %51
  br label %74

61:                                               ; preds = %51, %48
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.HDNode, ptr %62, i32 0, i32 6
  %64 = load ptr, ptr %63, align 8, !tbaa !14
  %65 = getelementptr inbounds %struct.curve_info, ptr %64, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !15
  %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 = call i64 @strlen(ptr noundef %71) #5
  %73 = trunc i64 %72 to i32
  call void @hmac_sha512_Init(ptr noundef @hdnode_from_seed.ctx, ptr noundef %66, i32 noundef %73)
  call void @hmac_sha512_Update(ptr noundef @hdnode_from_seed.ctx, ptr noundef @hdnode_from_seed.I, i32 noundef 64)
  call void @hmac_sha512_Final(ptr noundef @hdnode_from_seed.ctx, ptr noundef @hdnode_from_seed.I)
  br label %47

74:                                               ; preds = %60
  call void @memzero(ptr noundef %10, i64 noundef 36)
  call void @llvm.lifetime.end.p0(i64 36, ptr %10) #6
  br label %75

75:                                               ; preds = %74, %25
  %76 = load ptr, ptr %9, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.HDNode, ptr %76, i32 0, i32 3
  %78 = getelementptr inbounds [32 x i8], ptr %77, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %78, ptr align 16 @hdnode_from_seed.I, i64 32, i1 false)
  %79 = load ptr, ptr %9, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.HDNode, ptr %79, i32 0, i32 2
  %81 = getelementptr inbounds [32 x i8], ptr %80, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %81, ptr align 1 getelementptr inbounds (i8, ptr @hdnode_from_seed.I, i64 32), i64 32, i1 false)
  %82 = load ptr, ptr %9, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.HDNode, ptr %82, i32 0, i32 5
  %84 = getelementptr inbounds [33 x i8], ptr %83, i64 0, i64 0
  call void @memzero(ptr noundef %84, i64 noundef 33)
  call void @memzero(ptr noundef @hdnode_from_seed.I, i64 noundef 64)
  store i32 1, ptr %5, align 4
  br label %85

85:                                               ; preds = %75, %24
  %86 = load i32, ptr %5, align 4
  ret i32 %86
}

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

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

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 = { 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 #5 = { nounwind willreturn memory(read) }
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 = !{!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] }

@hdnode_from_seed.I = external hidden global [64 x i8], align 16
@hdnode_from_seed.ctx = external hidden global %struct._HMAC_SHA512_CTX, 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: 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

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 %struct.bignum256, align 4
  tail call void @memzero(ptr noundef %3, i64 noundef 152) #5
  store i32 0, ptr %3, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %3, i64 4
  store i32 0, ptr %6, align 4, !tbaa !11
  %7 = tail call ptr @get_curve_by_name(ptr noundef %2)
  %8 = getelementptr inbounds i8, ptr %3, i64 144
  store ptr %7, ptr %8, align 8, !tbaa !12
  %9 = icmp eq ptr %7, null
  br i1 %9, label %36, label %10

10:                                               ; preds = %4
  %11 = load ptr, ptr %7, align 8, !tbaa !13
  %12 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %11) #6
  %13 = trunc i64 %12 to i32
  tail call void @hmac_sha512_Init(ptr noundef nonnull @hdnode_from_seed.ctx, ptr noundef %11, i32 noundef %13) #5
  tail call void @hmac_sha512_Update(ptr noundef nonnull @hdnode_from_seed.ctx, ptr noundef %0, i32 noundef %1) #5
  tail call void @hmac_sha512_Final(ptr noundef nonnull @hdnode_from_seed.ctx, ptr noundef nonnull @hdnode_from_seed.I) #5
  %14 = load ptr, ptr %8, align 8, !tbaa !12
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  %.not = icmp eq ptr %16, null
  br i1 %.not, label %32, label %17

17:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %5) #5
  br label %18

18:                                               ; preds = %26, %17
  call void @bn_read_be(ptr noundef nonnull @hdnode_from_seed.I, ptr noundef nonnull %5) #5
  %19 = call i32 @bn_is_zero(ptr noundef nonnull %5) #5
  %.not18 = icmp eq i32 %19, 0
  br i1 %.not18, label %20, label %26

20:                                               ; preds = %18
  %21 = load ptr, ptr %8, align 8, !tbaa !12
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %23, i64 108
  %25 = call i32 @bn_is_less(ptr noundef nonnull %5, ptr noundef nonnull %24) #5
  %.not19 = icmp eq i32 %25, 0
  br i1 %.not19, label %26, label %31

26:                                               ; preds = %20, %18
  %27 = load ptr, ptr %8, align 8, !tbaa !12
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  %29 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %28) #6
  %30 = trunc i64 %29 to i32
  call void @hmac_sha512_Init(ptr noundef nonnull @hdnode_from_seed.ctx, ptr noundef %28, i32 noundef %30) #5
  call void @hmac_sha512_Update(ptr noundef nonnull @hdnode_from_seed.ctx, ptr noundef nonnull @hdnode_from_seed.I, i32 noundef 64) #5
  call void @hmac_sha512_Final(ptr noundef nonnull @hdnode_from_seed.ctx, ptr noundef nonnull @hdnode_from_seed.I) #5
  br label %18

31:                                               ; preds = %20
  call void @memzero(ptr noundef nonnull %5, i64 noundef 36) #5
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %5) #5
  br label %32

32:                                               ; preds = %31, %10
  %33 = getelementptr inbounds i8, ptr %3, i64 40
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %33, ptr noundef nonnull align 16 dereferenceable(32) @hdnode_from_seed.I, i64 32, i1 false)
  %34 = getelementptr inbounds i8, ptr %3, i64 8
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %34, ptr noundef nonnull align 16 dereferenceable(32) getelementptr inbounds (i8, ptr @hdnode_from_seed.I, i64 32), i64 32, i1 false)
  %35 = getelementptr inbounds i8, ptr %3, i64 104
  call void @memzero(ptr noundef nonnull %35, i64 noundef 33) #5
  call void @memzero(ptr noundef nonnull @hdnode_from_seed.I, i64 noundef 64) #5
  br label %36

36:                                               ; preds = %4, %32
  %.0 = phi i32 [ 1, %32 ], [ 0, %4 ]
  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 #4

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 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 #5 = { nounwind }
attributes #6 = { 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