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

@curve25519_info = external dso_local constant %struct.curve_info, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @hdnode_get_shared_key(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  %10 = load ptr, ptr %6, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.HDNode, ptr %10, i32 0, i32 6
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct.curve_info, ptr %12, i32 0, i32 1
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %32

16:                                               ; preds = %4
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.HDNode, ptr %17, i32 0, i32 6
  %19 = load ptr, ptr %18, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.curve_info, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.HDNode, ptr %22, i32 0, i32 3
  %24 = getelementptr inbounds [32 x i8], ptr %23, i64 0, i64 0
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = call i32 @ecdh_multiply(ptr noundef %21, ptr noundef %24, ptr noundef %25, ptr noundef %26)
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %16
  store i32 1, ptr %5, align 4
  br label %57

30:                                               ; preds = %16
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 65, ptr %31, align 4, !tbaa !14
  store i32 0, ptr %5, align 4
  br label %57

32:                                               ; preds = %4
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.HDNode, ptr %33, i32 0, i32 6
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  %36 = icmp eq ptr %35, @curve25519_info
  br i1 %36, label %37, label %55

37:                                               ; preds = %32
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i64 0
  store i8 4, ptr %39, align 1, !tbaa !15
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i64 0
  %42 = load i8, ptr %41, align 1, !tbaa !15
  %43 = zext i8 %42 to i32
  %44 = icmp ne i32 %43, 64
  br i1 %44, label %45, label %46

45:                                               ; preds = %37
  store i32 1, ptr %5, align 4
  br label %57

46:                                               ; preds = %37
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %47, i64 1
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.HDNode, ptr %49, i32 0, i32 3
  %51 = getelementptr inbounds [32 x i8], ptr %50, i64 0, i64 0
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds i8, ptr %52, i64 1
  call void @curve25519_scalarmult(ptr noundef %48, ptr noundef %51, ptr noundef %53)
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 33, ptr %54, align 4, !tbaa !14
  store i32 0, ptr %5, align 4
  br label %57

55:                                               ; preds = %32
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %56, align 4, !tbaa !14
  store i32 1, ptr %5, align 4
  br label %57

57:                                               ; preds = %55, %46, %45, %30, %29
  %58 = load i32, ptr %5, align 4
  ret i32 %58
}

declare i32 @ecdh_multiply(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

declare void @curve25519_scalarmult(ptr noundef, ptr noundef, ptr noundef) #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 = { "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" }

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

@curve25519_info = external dso_local constant %struct.curve_info, align 8

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @hdnode_get_shared_key(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 144
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %.not = icmp eq ptr %8, null
  br i1 %.not, label %12, label %9

9:                                                ; preds = %4
  %10 = getelementptr inbounds i8, ptr %0, i64 40
  %11 = tail call i32 @ecdh_multiply(ptr noundef nonnull %8, ptr noundef nonnull %10, ptr noundef %1, ptr noundef %2) #2
  %.not16 = icmp eq i32 %11, 0
  br i1 %.not16, label %.sink.split, label %20

12:                                               ; preds = %4
  %13 = icmp eq ptr %6, @curve25519_info
  br i1 %13, label %14, label %.sink.split

14:                                               ; preds = %12
  store i8 4, ptr %2, align 1, !tbaa !13
  %15 = load i8, ptr %1, align 1, !tbaa !13
  %.not15 = icmp eq i8 %15, 64
  br i1 %.not15, label %16, label %20

16:                                               ; preds = %14
  %17 = getelementptr inbounds i8, ptr %2, i64 1
  %18 = getelementptr inbounds i8, ptr %0, i64 40
  %19 = getelementptr inbounds i8, ptr %1, i64 1
  tail call void @curve25519_scalarmult(ptr noundef nonnull %17, ptr noundef nonnull %18, ptr noundef nonnull %19) #2
  br label %.sink.split

.sink.split:                                      ; preds = %12, %9, %16
  %.sink = phi i32 [ 33, %16 ], [ 65, %9 ], [ 0, %12 ]
  %.0.ph = phi i32 [ 0, %16 ], [ 0, %9 ], [ 1, %12 ]
  store i32 %.sink, ptr %3, align 4, !tbaa !14
  br label %20

20:                                               ; preds = %.sink.split, %14, %9
  %.0 = phi i32 [ 1, %9 ], [ 1, %14 ], [ %.0.ph, %.sink.split ]
  ret i32 %.0
}

declare i32 @ecdh_multiply(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @curve25519_scalarmult(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #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 = { "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 = { 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, !10, i64 8}
!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 = !{!7, !7, i64 0}


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