; 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._HMAC_SHA256_CTX = type { [64 x i8], %struct._SHA256_CTX }
%struct._SHA256_CTX = type { [8 x i32], i64, [16 x i32] }
%struct._HMAC_SHA512_CTX = type { [128 x i8], %struct._SHA512_CTX }
%struct._SHA512_CTX = type { [8 x i64], [2 x i64], [16 x i64] }

@ED25519_SEED_NAME = external constant [0 x i8], align 1
@.str = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #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) #1

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

declare void @hmac_sha512_Update(ptr noundef, ptr noundef, i32 noundef) #2

declare void @hmac_sha512_Final(ptr noundef, ptr noundef) #2

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

; 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
declare hidden void @cardano_ed25519_tweak_bits(ptr noundef) #3

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

; Function Attrs: nounwind uwtable
define dso_local i32 @secret_from_seed_cardano_ledger(ptr noundef %0, i32 noundef %1, ptr noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca [32 x i8], align 16
  %8 = alloca [64 x i8], align 16
  %9 = alloca %struct._HMAC_SHA256_CTX, align 8
  %10 = alloca %struct._HMAC_SHA512_CTX, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 64, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 168, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 336, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %13, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %14 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %14, ptr %12, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %22, %3
  %16 = call i64 @strlen(ptr noundef @ED25519_SEED_NAME) #6
  %17 = trunc i64 %16 to i32
  call void @hmac_sha512_Init(ptr noundef %10, ptr noundef @ED25519_SEED_NAME, i32 noundef %17)
  %18 = load ptr, ptr %11, align 8, !tbaa !5
  %19 = load i32, ptr %12, align 4, !tbaa !9
  call void @hmac_sha512_Update(ptr noundef %10, ptr noundef %18, i32 noundef %19)
  %20 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  call void @hmac_sha512_Final(ptr noundef %10, ptr noundef %20)
  %21 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  store ptr %21, ptr %11, align 8, !tbaa !5
  store i32 64, ptr %12, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %15
  %23 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 31
  %24 = load i8, ptr %23, align 1, !tbaa !11
  %25 = zext i8 %24 to i32
  %26 = and i32 %25, 32
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %15, label %28, !llvm.loop !12

28:                                               ; preds = %22
  %29 = call i64 @strlen(ptr noundef @ED25519_SEED_NAME) #6
  %30 = trunc i64 %29 to i32
  call void @hmac_sha256_Init(ptr noundef %9, ptr noundef @ED25519_SEED_NAME, i32 noundef %30)
  call void @hmac_sha256_Update(ptr noundef %9, ptr noundef @.str, i32 noundef 1)
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = load i32, ptr %5, align 4, !tbaa !9
  call void @hmac_sha256_Update(ptr noundef %9, ptr noundef %31, i32 noundef %32)
  %33 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  call void @hmac_sha256_Final(ptr noundef %9, ptr noundef %33)
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %34, ptr align 16 %35, i64 64, i1 false)
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i64 64
  %38 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %37, ptr align 16 %38, i64 32, i1 false)
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  call void @cardano_ed25519_tweak_bits(ptr noundef %39)
  call void @memzero(ptr noundef %9, i64 noundef 168)
  call void @memzero(ptr noundef %10, i64 noundef 336)
  %40 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  call void @memzero(ptr noundef %40, i64 noundef 64)
  %41 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  call void @memzero(ptr noundef %41, i64 noundef 32)
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 336, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 168, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr %7) #5
  ret i32 1
}

declare void @hmac_sha256_Init(ptr noundef, ptr noundef, i32 noundef) #2

declare void @hmac_sha256_Update(ptr noundef, ptr noundef, i32 noundef) #2

declare void @hmac_sha256_Final(ptr noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 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 #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 }
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, !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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; 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_SHA256_CTX = type { [64 x i8], %struct._SHA256_CTX }
%struct._SHA256_CTX = type { [8 x i32], i64, [16 x i32] }
%struct._HMAC_SHA512_CTX = type { [128 x i8], %struct._SHA512_CTX }
%struct._SHA512_CTX = type { [8 x i64], [2 x i64], [16 x i64] }

@ED25519_SEED_NAME = external constant [0 x i8], align 1
@.str = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #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 @hmac_sha512_Init(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare void @hmac_sha512_Update(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare void @hmac_sha512_Final(ptr noundef, ptr noundef) local_unnamed_addr #2

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

; 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
declare hidden void @cardano_ed25519_tweak_bits(ptr noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @secret_from_seed_cardano_ledger(ptr noundef %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #3 {
.peel.begin:
  %3 = alloca [32 x i8], align 16
  %4 = alloca [64 x i8], align 16
  %5 = alloca %struct._HMAC_SHA256_CTX, align 8
  %6 = alloca %struct._HMAC_SHA512_CTX, align 8
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %3) #5
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %4) #5
  call void @llvm.lifetime.start.p0(i64 168, ptr nonnull %5) #5
  call void @llvm.lifetime.start.p0(i64 336, ptr nonnull %6) #5
  %7 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) @ED25519_SEED_NAME) #6
  %8 = trunc i64 %7 to i32
  %9 = getelementptr inbounds i8, ptr %4, i64 31
  call void @hmac_sha512_Init(ptr noundef nonnull %6, ptr noundef nonnull @ED25519_SEED_NAME, i32 noundef %8) #5
  call void @hmac_sha512_Update(ptr noundef nonnull %6, ptr noundef %0, i32 noundef %1) #5
  call void @hmac_sha512_Final(ptr noundef nonnull %6, ptr noundef nonnull %4) #5
  %10 = load i8, ptr %9, align 1, !tbaa !5
  %11 = and i8 %10, 32
  %.not.peel = icmp eq i8 %11, 0
  br i1 %.not.peel, label %.loopexit, label %.peel.next

.peel.next:                                       ; preds = %.peel.begin, %.peel.next
  call void @hmac_sha512_Init(ptr noundef nonnull %6, ptr noundef nonnull @ED25519_SEED_NAME, i32 noundef %8) #5
  call void @hmac_sha512_Update(ptr noundef nonnull %6, ptr noundef nonnull %4, i32 noundef 64) #5
  call void @hmac_sha512_Final(ptr noundef nonnull %6, ptr noundef nonnull %4) #5
  %12 = load i8, ptr %9, align 1, !tbaa !5
  %13 = and i8 %12, 32
  %.not = icmp eq i8 %13, 0
  br i1 %.not, label %.loopexit, label %.peel.next, !llvm.loop !8

.loopexit:                                        ; preds = %.peel.next, %.peel.begin
  call void @hmac_sha256_Init(ptr noundef nonnull %5, ptr noundef nonnull @ED25519_SEED_NAME, i32 noundef %8) #5
  call void @hmac_sha256_Update(ptr noundef nonnull %5, ptr noundef nonnull @.str, i32 noundef 1) #5
  call void @hmac_sha256_Update(ptr noundef nonnull %5, ptr noundef %0, i32 noundef %1) #5
  call void @hmac_sha256_Final(ptr noundef nonnull %5, ptr noundef nonnull %3) #5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(64) %2, ptr noundef nonnull align 16 dereferenceable(64) %4, i64 64, i1 false)
  %14 = getelementptr inbounds i8, ptr %2, i64 64
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %14, ptr noundef nonnull align 16 dereferenceable(32) %3, i64 32, i1 false)
  call void @cardano_ed25519_tweak_bits(ptr noundef %2)
  call void @memzero(ptr noundef nonnull %5, i64 noundef 168) #5
  call void @memzero(ptr noundef nonnull %6, i64 noundef 336) #5
  call void @memzero(ptr noundef nonnull %4, i64 noundef 64) #5
  call void @memzero(ptr noundef nonnull %3, i64 noundef 32) #5
  call void @llvm.lifetime.end.p0(i64 336, ptr nonnull %6) #5
  call void @llvm.lifetime.end.p0(i64 168, ptr nonnull %5) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %4) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %3) #5
  ret i32 1
}

declare void @hmac_sha256_Init(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare void @hmac_sha256_Update(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare void @hmac_sha256_Final(ptr noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9, !10}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.peeled.count", i32 1}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopFullUnrollPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass