; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto (cardano)

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

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

; 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) #0

declare i32 @hdnode_fill_public_key(ptr noundef) #1

declare void @memzero(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @hdnode_from_secret_cardano(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  call void @memzero(ptr noundef %6, i64 noundef 152)
  %7 = load ptr, ptr %5, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.HDNode, ptr %7, i32 0, i32 0
  store i32 0, ptr %8, align 8, !tbaa !9
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.HDNode, ptr %9, i32 0, i32 1
  store i32 0, ptr %10, align 4, !tbaa !12
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.HDNode, ptr %11, i32 0, i32 6
  store ptr @ed25519_cardano_info, ptr %12, align 8, !tbaa !13
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.HDNode, ptr %13, i32 0, i32 3
  %15 = getelementptr inbounds [32 x i8], ptr %14, i64 0, i64 0
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %15, ptr align 1 %16, i64 32, i1 false)
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.HDNode, ptr %17, i32 0, i32 4
  %19 = getelementptr inbounds [32 x i8], ptr %18, i64 0, i64 0
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 32
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %19, ptr align 1 %21, i64 32, i1 false)
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.HDNode, ptr %22, i32 0, i32 2
  %24 = getelementptr inbounds [32 x i8], ptr %23, i64 0, i64 0
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 64
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %24, ptr align 1 %26, i64 32, i1 false)
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.HDNode, ptr %27, i32 0, i32 3
  %29 = getelementptr inbounds [32 x i8], ptr %28, i64 0, i64 0
  call void @cardano_ed25519_tweak_bits(ptr noundef %29)
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.HDNode, ptr %30, i32 0, i32 5
  %32 = getelementptr inbounds [33 x i8], ptr %31, i64 0, i64 0
  store i8 0, ptr %32, align 8, !tbaa !14
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = call i32 @hdnode_fill_public_key(ptr noundef %33)
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  br label %38

37:                                               ; preds = %2
  store i32 1, ptr %3, align 4
  br label %38

38:                                               ; preds = %37, %36
  %39 = load i32, ptr %3, align 4
  ret i32 %39
}

; Function Attrs: nounwind uwtable
declare hidden void @cardano_ed25519_tweak_bits(ptr noundef) #2

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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" }

!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, !11, i64 0}
!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 = !{!10, !11, i64 4}
!13 = !{!10, !6, i64 144}
!14 = !{!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 }

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

; 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) #0

declare i32 @hdnode_fill_public_key(ptr noundef) local_unnamed_addr #1

declare void @memzero(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @hdnode_from_secret_cardano(ptr nocapture noundef readonly %0, ptr noundef %1) local_unnamed_addr #2 {
  tail call void @memzero(ptr noundef %1, i64 noundef 152) #3
  store i32 0, ptr %1, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %1, i64 4
  store i32 0, ptr %3, align 4, !tbaa !11
  %4 = getelementptr inbounds i8, ptr %1, i64 144
  store ptr @ed25519_cardano_info, ptr %4, align 8, !tbaa !12
  %5 = getelementptr inbounds i8, ptr %1, i64 40
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %5, ptr noundef nonnull align 1 dereferenceable(32) %0, i64 32, i1 false)
  %6 = getelementptr inbounds i8, ptr %1, i64 72
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %6, ptr noundef nonnull align 1 dereferenceable(32) %7, i64 32, i1 false)
  %8 = getelementptr inbounds i8, ptr %1, i64 8
  %9 = getelementptr inbounds i8, ptr %0, i64 64
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %8, ptr noundef nonnull align 1 dereferenceable(32) %9, i64 32, i1 false)
  tail call void @cardano_ed25519_tweak_bits(ptr noundef nonnull %5)
  %10 = getelementptr inbounds i8, ptr %1, i64 104
  store i8 0, ptr %10, align 8, !tbaa !13
  %11 = tail call i32 @hdnode_fill_public_key(ptr noundef nonnull %1) #3
  %.not = icmp eq i32 %11, 0
  %. = zext i1 %.not to i32
  ret i32 %.
}

; Function Attrs: nounwind uwtable
declare hidden void @cardano_ed25519_tweak_bits(ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !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 = !{!8, !8, i64 0}


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