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

; Function Attrs: nounwind uwtable
define dso_local i32 @hdnode_from_xpub(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5) #0 {
  %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 ptr, align 8
  %15 = 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 8, ptr %14) #4
  %16 = load ptr, ptr %12, align 8, !tbaa !9
  %17 = call ptr @get_curve_by_name(ptr noundef %16)
  store ptr %17, ptr %14, align 8, !tbaa !9
  %18 = load ptr, ptr %14, align 8, !tbaa !9
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %6
  store i32 0, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %58

21:                                               ; preds = %6
  %22 = load ptr, ptr %11, align 8, !tbaa !9
  %23 = getelementptr inbounds i8, ptr %22, i64 0
  %24 = load i8, ptr %23, align 1, !tbaa !11
  %25 = zext i8 %24 to i32
  %26 = icmp ne i32 %25, 2
  br i1 %26, label %27, label %34

27:                                               ; preds = %21
  %28 = load ptr, ptr %11, align 8, !tbaa !9
  %29 = getelementptr inbounds i8, ptr %28, i64 0
  %30 = load i8, ptr %29, align 1, !tbaa !11
  %31 = zext i8 %30 to i32
  %32 = icmp ne i32 %31, 3
  br i1 %32, label %33, label %34

33:                                               ; preds = %27
  store i32 0, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %58

34:                                               ; preds = %27, %21
  %35 = load ptr, ptr %14, align 8, !tbaa !9
  %36 = load ptr, ptr %13, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.HDNode, ptr %36, i32 0, i32 6
  store ptr %35, ptr %37, align 8, !tbaa !12
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = load ptr, ptr %13, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.HDNode, ptr %39, i32 0, i32 0
  store i32 %38, ptr %40, align 8, !tbaa !14
  %41 = load i32, ptr %9, align 4, !tbaa !5
  %42 = load ptr, ptr %13, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.HDNode, ptr %42, i32 0, i32 1
  store i32 %41, ptr %43, align 4, !tbaa !15
  %44 = load ptr, ptr %13, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.HDNode, ptr %44, i32 0, i32 2
  %46 = getelementptr inbounds [32 x i8], ptr %45, i64 0, i64 0
  %47 = load ptr, ptr %10, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %46, ptr align 1 %47, i64 32, i1 false)
  %48 = load ptr, ptr %13, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.HDNode, ptr %48, i32 0, i32 3
  %50 = getelementptr inbounds [32 x i8], ptr %49, i64 0, i64 0
  call void @memzero(ptr noundef %50, i64 noundef 32)
  %51 = load ptr, ptr %13, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.HDNode, ptr %51, i32 0, i32 4
  %53 = getelementptr inbounds [32 x i8], ptr %52, i64 0, i64 0
  call void @memzero(ptr noundef %53, i64 noundef 32)
  %54 = load ptr, ptr %13, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.HDNode, ptr %54, i32 0, i32 5
  %56 = getelementptr inbounds [33 x i8], ptr %55, i64 0, i64 0
  %57 = load ptr, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %56, ptr align 1 %57, i64 33, i1 false)
  store i32 1, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %58

58:                                               ; preds = %34, %33, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  %59 = load i32, ptr %7, align 4
  ret i32 %59
}

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @get_curve_by_name(ptr noundef) #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) #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) #1

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 144}
!13 = !{!"", !6, i64 0, !6, i64 4, !7, i64 8, !7, i64 40, !7, i64 72, !7, i64 104, !10, i64 144}
!14 = !{!13, !6, i64 0}
!15 = !{!13, !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"

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @hdnode_from_xpub(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2, ptr nocapture noundef readonly %3, ptr noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %7 = tail call ptr @get_curve_by_name(ptr noundef %4)
  %8 = icmp eq ptr %7, null
  br i1 %8, label %19, label %9

9:                                                ; preds = %6
  %10 = load i8, ptr %3, align 1, !tbaa !5
  %11 = and i8 %10, -2
  %switch = icmp eq i8 %11, 2
  br i1 %switch, label %12, label %19

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %5, i64 144
  store ptr %7, ptr %13, align 8, !tbaa !8
  store i32 %0, ptr %5, align 8, !tbaa !12
  %14 = getelementptr inbounds i8, ptr %5, i64 4
  store i32 %1, ptr %14, align 4, !tbaa !13
  %15 = getelementptr inbounds i8, ptr %5, i64 8
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %15, ptr noundef nonnull align 1 dereferenceable(32) %2, i64 32, i1 false)
  %16 = getelementptr inbounds i8, ptr %5, i64 40
  tail call void @memzero(ptr noundef nonnull %16, i64 noundef 32) #3
  %17 = getelementptr inbounds i8, ptr %5, i64 72
  tail call void @memzero(ptr noundef nonnull %17, i64 noundef 32) #3
  %18 = getelementptr inbounds i8, ptr %5, i64 104
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(33) %18, ptr noundef nonnull align 1 dereferenceable(33) %3, i64 33, i1 false)
  br label %19

19:                                               ; preds = %9, %6, %12
  %.0 = phi i32 [ 1, %12 ], [ 0, %6 ], [ 0, %9 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local ptr @get_curve_by_name(ptr noundef) local_unnamed_addr #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

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


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