; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto/ed25519-donna (ed25519-blake2b)

; 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.ge25519_t = type { [10 x i32], [10 x i32], [10 x i32], [10 x i32] }

; 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: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

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

; Function Attrs: nounwind uwtable
declare hidden void @ed25519_hram(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef) #3

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

; Function Attrs: nounwind uwtable
define dso_local i32 @ed25519_sign_open_blake2b(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #3 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %struct.ge25519_t, align 16
  %11 = alloca %struct.ge25519_t, align 16
  %12 = alloca [64 x i8], align 16
  %13 = alloca [9 x i32], align 16
  %14 = alloca [9 x i32], align 16
  %15 = alloca [32 x i8], align 16
  %16 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 160, ptr %10) #4
  call void @llvm.memset.p0.i64(ptr align 16 %10, i8 0, i64 160, i1 false)
  call void @llvm.lifetime.start.p0(i64 160, ptr %11) #4
  call void @llvm.memset.p0.i64(ptr align 16 %11, i8 0, i64 160, i1 false)
  call void @llvm.lifetime.start.p0(i64 64, ptr %12) #4
  call void @llvm.memset.p0.i64(ptr align 16 %12, i8 0, i64 64, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr %13) #4
  call void @llvm.memset.p0.i64(ptr align 16 %13, i8 0, i64 36, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr %14) #4
  call void @llvm.memset.p0.i64(ptr align 16 %14, i8 0, i64 36, i1 false)
  call void @llvm.lifetime.start.p0(i64 32, ptr %15) #4
  call void @llvm.memset.p0.i64(ptr align 16 %15, i8 0, i64 32, i1 false)
  %17 = load ptr, ptr %9, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 63
  %19 = load i8, ptr %18, align 1, !tbaa !11
  %20 = zext i8 %19 to i32
  %21 = and i32 %20, 224
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %27, label %23

23:                                               ; preds = %4
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = call i32 @ge25519_unpack_negative_vartime(ptr noundef %11, ptr noundef %24)
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %28, label %27

27:                                               ; preds = %23, %4
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %16, align 4
  br label %53

28:                                               ; preds = %23
  %29 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load i64, ptr %7, align 8, !tbaa !9
  call void @ed25519_hram(ptr noundef %29, ptr noundef %30, ptr noundef %31, ptr noundef %32, i64 noundef %33)
  %34 = getelementptr inbounds [9 x i32], ptr %13, i64 0, i64 0
  %35 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  call void @expand256_modm(ptr noundef %34, ptr noundef %35, i64 noundef 64)
  %36 = getelementptr inbounds [9 x i32], ptr %14, i64 0, i64 0
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i64 32
  call void @expand_raw256_modm(ptr noundef %36, ptr noundef %38)
  %39 = getelementptr inbounds [9 x i32], ptr %14, i64 0, i64 0
  %40 = call i32 @is_reduced256_modm(ptr noundef %39)
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %43, label %42

42:                                               ; preds = %28
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %16, align 4
  br label %53

43:                                               ; preds = %28
  %44 = getelementptr inbounds [9 x i32], ptr %13, i64 0, i64 0
  %45 = getelementptr inbounds [9 x i32], ptr %14, i64 0, i64 0
  call void @ge25519_double_scalarmult_vartime(ptr noundef %10, ptr noundef %11, ptr noundef %44, ptr noundef %45)
  %46 = getelementptr inbounds [32 x i8], ptr %15, i64 0, i64 0
  call void @ge25519_pack(ptr noundef %46, ptr noundef %10)
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = getelementptr inbounds [32 x i8], ptr %15, i64 0, i64 0
  %49 = call i32 @ed25519_verify(ptr noundef %47, ptr noundef %48, i64 noundef 32)
  %50 = icmp ne i32 %49, 0
  %51 = zext i1 %50 to i64
  %52 = select i1 %50, i32 0, i32 -1
  store i32 %52, ptr %5, align 4
  store i32 1, ptr %16, align 4
  br label %53

53:                                               ; preds = %43, %42, %27
  call void @llvm.lifetime.end.p0(i64 32, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 160, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 160, ptr %10) #4
  %54 = load i32, ptr %5, align 4
  ret i32 %54
}

declare i32 @ge25519_unpack_negative_vartime(ptr noundef, ptr noundef) #2

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

declare i32 @is_reduced256_modm(ptr noundef) #2

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

declare i32 @ed25519_verify(ptr noundef, ptr noundef, i64 noundef) #2

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

!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 = !{!"long", !7, i64 0}
!11 = !{!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.ge25519_t = type { [10 x i32], [10 x i32], [10 x i32], [10 x i32] }

; 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: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

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

; Function Attrs: nounwind uwtable
declare hidden void @ed25519_hram(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @ed25519_sign_open_blake2b(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #3 {
  %5 = alloca %struct.ge25519_t, align 16
  %6 = alloca %struct.ge25519_t, align 16
  %7 = alloca [64 x i8], align 16
  %8 = alloca [9 x i32], align 16
  %9 = alloca [9 x i32], align 16
  %10 = alloca [32 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %5) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(160) %5, i8 0, i64 160, i1 false)
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %6) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(160) %6, i8 0, i64 160, i1 false)
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %7) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %7, i8 0, i64 64, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %8) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(36) %8, i8 0, i64 36, i1 false)
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %9) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(36) %9, i8 0, i64 36, i1 false)
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %10) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %10, i8 0, i64 32, i1 false)
  %11 = getelementptr inbounds i8, ptr %3, i64 63
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %.not = icmp ult i8 %12, 32
  br i1 %.not, label %13, label %21

13:                                               ; preds = %4
  %14 = call i32 @ge25519_unpack_negative_vartime(ptr noundef nonnull %6, ptr noundef %2) #4
  %.not8 = icmp eq i32 %14, 0
  br i1 %.not8, label %21, label %15

15:                                               ; preds = %13
  call void @ed25519_hram(ptr noundef nonnull %7, ptr noundef nonnull %3, ptr noundef %2, ptr noundef %0, i64 noundef %1)
  call void @expand256_modm(ptr noundef nonnull %8, ptr noundef nonnull %7, i64 noundef 64) #4
  %16 = getelementptr inbounds i8, ptr %3, i64 32
  call void @expand_raw256_modm(ptr noundef nonnull %9, ptr noundef nonnull %16) #4
  %17 = call i32 @is_reduced256_modm(ptr noundef nonnull %9) #4
  %.not9 = icmp eq i32 %17, 0
  br i1 %.not9, label %21, label %18

18:                                               ; preds = %15
  call void @ge25519_double_scalarmult_vartime(ptr noundef nonnull %5, ptr noundef nonnull %6, ptr noundef nonnull %8, ptr noundef nonnull %9) #4
  call void @ge25519_pack(ptr noundef nonnull %10, ptr noundef nonnull %5) #4
  %19 = call i32 @ed25519_verify(ptr noundef nonnull %3, ptr noundef nonnull %10, i64 noundef 32) #4
  %.not10 = icmp eq i32 %19, 0
  %20 = sext i1 %.not10 to i32
  br label %21

21:                                               ; preds = %15, %4, %13, %18
  %.0 = phi i32 [ %20, %18 ], [ -1, %13 ], [ -1, %4 ], [ -1, %15 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %10) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %9) #4
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 160, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 160, ptr nonnull %5) #4
  ret i32 %.0
}

declare i32 @ge25519_unpack_negative_vartime(ptr noundef, ptr noundef) local_unnamed_addr #2

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

declare i32 @is_reduced256_modm(ptr noundef) local_unnamed_addr #2

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

declare i32 @ed25519_verify(ptr noundef, ptr noundef, i64 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: write) }
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 }

!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"}


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