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

; 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_deserialize(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5) #1 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca [78 x i8], align 16
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !9
  store i32 %2, ptr %10, align 4, !tbaa !9
  store ptr %3, ptr %11, align 8, !tbaa !5
  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 78, ptr %14) #4
  %17 = load ptr, ptr %12, align 8, !tbaa !5
  call void @memzero(ptr noundef %17, i64 noundef 152)
  %18 = load ptr, ptr %11, align 8, !tbaa !5
  %19 = call ptr @get_curve_by_name(ptr noundef %18)
  %20 = load ptr, ptr %12, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.HDNode, ptr %20, i32 0, i32 6
  store ptr %19, ptr %21, align 8, !tbaa !11
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = load ptr, ptr %12, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.HDNode, ptr %23, i32 0, i32 6
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = getelementptr inbounds %struct.curve_info, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 8, !tbaa !13
  %28 = getelementptr inbounds [78 x i8], ptr %14, i64 0, i64 0
  %29 = call i32 @base58_decode_check(ptr noundef %22, i32 noundef %27, ptr noundef %28, i32 noundef 78)
  %30 = sext i32 %29 to i64
  %31 = icmp ne i64 %30, 78
  br i1 %31, label %32, label %33

32:                                               ; preds = %6
  store i32 -1, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %93

33:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  %34 = getelementptr inbounds [78 x i8], ptr %14, i64 0, i64 0
  %35 = call i32 @read_be(ptr noundef %34)
  store i32 %35, ptr %16, align 4, !tbaa !9
  %36 = load i32, ptr %16, align 4, !tbaa !9
  %37 = load i32, ptr %9, align 4, !tbaa !9
  %38 = icmp eq i32 %36, %37
  br i1 %38, label %39, label %48

39:                                               ; preds = %33
  %40 = load ptr, ptr %12, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.HDNode, ptr %40, i32 0, i32 3
  %42 = getelementptr inbounds [32 x i8], ptr %41, i64 0, i64 0
  call void @memzero(ptr noundef %42, i64 noundef 32)
  %43 = load ptr, ptr %12, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.HDNode, ptr %43, i32 0, i32 5
  %45 = getelementptr inbounds [33 x i8], ptr %44, i64 0, i64 0
  %46 = getelementptr inbounds [78 x i8], ptr %14, i64 0, i64 0
  %47 = getelementptr inbounds i8, ptr %46, i64 45
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %45, ptr align 1 %47, i64 33, i1 false)
  br label %68

48:                                               ; preds = %33
  %49 = load i32, ptr %16, align 4, !tbaa !9
  %50 = load i32, ptr %10, align 4, !tbaa !9
  %51 = icmp eq i32 %49, %50
  br i1 %51, label %52, label %66

52:                                               ; preds = %48
  %53 = getelementptr inbounds [78 x i8], ptr %14, i64 0, i64 45
  %54 = load i8, ptr %53, align 1, !tbaa !15
  %55 = icmp ne i8 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %52
  store i32 -2, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %92

57:                                               ; preds = %52
  %58 = load ptr, ptr %12, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.HDNode, ptr %58, i32 0, i32 3
  %60 = getelementptr inbounds [32 x i8], ptr %59, i64 0, i64 0
  %61 = getelementptr inbounds [78 x i8], ptr %14, i64 0, i64 0
  %62 = getelementptr inbounds i8, ptr %61, i64 46
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %60, ptr align 1 %62, i64 32, i1 false)
  %63 = load ptr, ptr %12, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.HDNode, ptr %63, i32 0, i32 5
  %65 = getelementptr inbounds [33 x i8], ptr %64, i64 0, i64 0
  call void @memzero(ptr noundef %65, i64 noundef 33)
  br label %67

66:                                               ; preds = %48
  store i32 -3, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %92

67:                                               ; preds = %57
  br label %68

68:                                               ; preds = %67, %39
  %69 = getelementptr inbounds [78 x i8], ptr %14, i64 0, i64 4
  %70 = load i8, ptr %69, align 4, !tbaa !15
  %71 = zext i8 %70 to i32
  %72 = load ptr, ptr %12, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.HDNode, ptr %72, i32 0, i32 0
  store i32 %71, ptr %73, align 8, !tbaa !16
  %74 = load ptr, ptr %13, align 8, !tbaa !5
  %75 = icmp ne ptr %74, null
  br i1 %75, label %76, label %81

76:                                               ; preds = %68
  %77 = getelementptr inbounds [78 x i8], ptr %14, i64 0, i64 0
  %78 = getelementptr inbounds i8, ptr %77, i64 5
  %79 = call i32 @read_be(ptr noundef %78)
  %80 = load ptr, ptr %13, align 8, !tbaa !5
  store i32 %79, ptr %80, align 4, !tbaa !9
  br label %81

81:                                               ; preds = %76, %68
  %82 = getelementptr inbounds [78 x i8], ptr %14, i64 0, i64 0
  %83 = getelementptr inbounds i8, ptr %82, i64 9
  %84 = call i32 @read_be(ptr noundef %83)
  %85 = load ptr, ptr %12, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.HDNode, ptr %85, i32 0, i32 1
  store i32 %84, ptr %86, align 4, !tbaa !17
  %87 = load ptr, ptr %12, 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 [78 x i8], ptr %14, i64 0, i64 0
  %91 = getelementptr inbounds i8, ptr %90, i64 13
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %89, ptr align 1 %91, i64 32, i1 false)
  store i32 0, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %92

92:                                               ; preds = %81, %66, %56
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  br label %93

93:                                               ; preds = %92, %32
  call void @llvm.lifetime.end.p0(i64 78, ptr %14) #4
  %94 = load i32, ptr %7, align 4
  ret i32 %94
}

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

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

; 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 -3, 1) i32 @hdnode_deserialize(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef writeonly %5) local_unnamed_addr #1 {
  %7 = alloca [78 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 78, ptr nonnull %7) #4
  tail call void @memzero(ptr noundef %4, i64 noundef 152) #4
  %8 = tail call ptr @get_curve_by_name(ptr noundef %3)
  %9 = getelementptr inbounds i8, ptr %4, i64 144
  store ptr %8, ptr %9, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %8, i64 16
  %11 = load i32, ptr %10, align 8, !tbaa !11
  %12 = call i32 @base58_decode_check(ptr noundef %0, i32 noundef %11, ptr noundef nonnull %7, i32 noundef 78) #4
  %.not = icmp eq i32 %12, 78
  br i1 %.not, label %13, label %42

13:                                               ; preds = %6
  %14 = call i32 @read_be(ptr noundef nonnull %7) #4
  %15 = icmp eq i32 %14, %1
  br i1 %15, label %16, label %20

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %4, i64 40
  call void @memzero(ptr noundef nonnull %17, i64 noundef 32) #4
  %18 = getelementptr inbounds i8, ptr %4, i64 104
  %19 = getelementptr inbounds i8, ptr %7, i64 45
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(33) %18, ptr noundef nonnull align 1 dereferenceable(33) %19, i64 33, i1 false)
  br label %29

20:                                               ; preds = %13
  %21 = icmp eq i32 %14, %2
  br i1 %21, label %22, label %42

22:                                               ; preds = %20
  %23 = getelementptr inbounds i8, ptr %7, i64 45
  %24 = load i8, ptr %23, align 1, !tbaa !13
  %.not20 = icmp eq i8 %24, 0
  br i1 %.not20, label %25, label %42

25:                                               ; preds = %22
  %26 = getelementptr inbounds i8, ptr %4, i64 40
  %27 = getelementptr inbounds i8, ptr %7, i64 46
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %26, ptr noundef nonnull align 2 dereferenceable(32) %27, i64 32, i1 false)
  %28 = getelementptr inbounds i8, ptr %4, i64 104
  call void @memzero(ptr noundef nonnull %28, i64 noundef 33) #4
  br label %29

29:                                               ; preds = %25, %16
  %30 = getelementptr inbounds i8, ptr %7, i64 4
  %31 = load i8, ptr %30, align 4, !tbaa !13
  %32 = zext i8 %31 to i32
  store i32 %32, ptr %4, align 8, !tbaa !14
  %.not21 = icmp eq ptr %5, null
  br i1 %.not21, label %36, label %33

33:                                               ; preds = %29
  %34 = getelementptr inbounds i8, ptr %7, i64 5
  %35 = call i32 @read_be(ptr noundef nonnull %34) #4
  store i32 %35, ptr %5, align 4, !tbaa !15
  br label %36

36:                                               ; preds = %33, %29
  %37 = getelementptr inbounds i8, ptr %7, i64 9
  %38 = call i32 @read_be(ptr noundef nonnull %37) #4
  %39 = getelementptr inbounds i8, ptr %4, i64 4
  store i32 %38, ptr %39, align 4, !tbaa !16
  %40 = getelementptr inbounds i8, ptr %4, i64 8
  %41 = getelementptr inbounds i8, ptr %7, i64 13
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %40, ptr noundef nonnull align 1 dereferenceable(32) %41, i64 32, i1 false)
  br label %42

42:                                               ; preds = %36, %22, %20, %6
  %.1 = phi i32 [ -1, %6 ], [ 0, %36 ], [ -2, %22 ], [ -3, %20 ]
  call void @llvm.lifetime.end.p0(i64 78, ptr nonnull %7) #4
  ret i32 %.1
}

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

declare i32 @read_be(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, !10, i64 144}
!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 = !{!12, !7, i64 16}
!12 = !{!"", !10, i64 0, !10, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28}
!13 = !{!8, !8, i64 0}
!14 = !{!6, !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = !{!6, !7, i64 4}


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