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

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

; 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 @ge25519_pack(ptr noundef, ptr noundef) #3

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

declare void @curve25519_neg(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @ed25519_cosi_combine_publickeys(ptr noundef %0, ptr noundef %1, i64 noundef %2) #4 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca %struct.ge25519_t, align 4
  %10 = alloca %struct.ge25519_pniels_t, align 4
  %11 = alloca %struct.ge25519_p1p1_t, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  store i64 0, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 160, ptr %9) #5
  call void @llvm.memset.p0.i64(ptr align 4 %9, i8 0, i64 160, i1 false)
  call void @llvm.lifetime.start.p0(i64 160, ptr %10) #5
  call void @llvm.memset.p0.i64(ptr align 4 %10, i8 0, i64 160, i1 false)
  call void @llvm.lifetime.start.p0(i64 160, ptr %11) #5
  call void @llvm.memset.p0.i64(ptr align 4 %11, i8 0, i64 160, i1 false)
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = icmp eq i64 %13, 1
  br i1 %14, label %15, label %18

15:                                               ; preds = %3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %16, ptr align 1 %17, i64 32, i1 false)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %58

18:                                               ; preds = %3
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = add i64 %20, 1
  store i64 %21, ptr %8, align 8, !tbaa !9
  %22 = getelementptr inbounds [32 x i8], ptr %19, i64 %20
  %23 = getelementptr inbounds [32 x i8], ptr %22, i64 0, i64 0
  %24 = call i32 @ge25519_unpack_negative_vartime(ptr noundef %9, ptr noundef %23)
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %27, label %26

26:                                               ; preds = %18
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %58

27:                                               ; preds = %18
  call void @ge25519_full_to_pniels(ptr noundef %10, ptr noundef %9)
  br label %28

28:                                               ; preds = %42, %27
  %29 = load i64, ptr %8, align 8, !tbaa !9
  %30 = load i64, ptr %7, align 8, !tbaa !9
  %31 = sub i64 %30, 1
  %32 = icmp ult i64 %29, %31
  br i1 %32, label %33, label %43

33:                                               ; preds = %28
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i64, ptr %8, align 8, !tbaa !9
  %36 = add i64 %35, 1
  store i64 %36, ptr %8, align 8, !tbaa !9
  %37 = getelementptr inbounds [32 x i8], ptr %34, i64 %35
  %38 = getelementptr inbounds [32 x i8], ptr %37, i64 0, i64 0
  %39 = call i32 @ge25519_unpack_negative_vartime(ptr noundef %9, ptr noundef %38)
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %42, label %41

41:                                               ; preds = %33
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %58

42:                                               ; preds = %33
  call void @ge25519_pnielsadd(ptr noundef %10, ptr noundef %9, ptr noundef %10)
  br label %28, !llvm.loop !11

43:                                               ; preds = %28
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load i64, ptr %8, align 8, !tbaa !9
  %46 = add i64 %45, 1
  store i64 %46, ptr %8, align 8, !tbaa !9
  %47 = getelementptr inbounds [32 x i8], ptr %44, i64 %45
  %48 = getelementptr inbounds [32 x i8], ptr %47, i64 0, i64 0
  %49 = call i32 @ge25519_unpack_negative_vartime(ptr noundef %9, ptr noundef %48)
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %52, label %51

51:                                               ; preds = %43
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %58

52:                                               ; preds = %43
  call void @ge25519_pnielsadd_p1p1(ptr noundef %11, ptr noundef %9, ptr noundef %10, i8 noundef zeroext 0)
  call void @ge25519_p1p1_to_partial(ptr noundef %9, ptr noundef %11)
  %53 = getelementptr inbounds %struct.ge25519_t, ptr %9, i32 0, i32 0
  %54 = getelementptr inbounds [10 x i32], ptr %53, i64 0, i64 0
  %55 = getelementptr inbounds %struct.ge25519_t, ptr %9, i32 0, i32 0
  %56 = getelementptr inbounds [10 x i32], ptr %55, i64 0, i64 0
  call void @curve25519_neg(ptr noundef %54, ptr noundef %56)
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  call void @ge25519_pack(ptr noundef %57, ptr noundef %9)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %58

58:                                               ; preds = %52, %51, %41, %26, %15
  call void @llvm.lifetime.end.p0(i64 160, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 160, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 160, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %59 = load i32, ptr %4, align 4
  ret i32 %59
}

declare void @ge25519_full_to_pniels(ptr noundef, ptr noundef) #3

declare void @ge25519_pnielsadd(ptr noundef, ptr noundef, ptr noundef) #3

declare void @ge25519_pnielsadd_p1p1(ptr noundef, ptr noundef, ptr noundef, i8 noundef zeroext) #3

declare void @ge25519_p1p1_to_partial(ptr noundef, ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 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 #5 = { 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 = distinct !{!11, !12}
!12 = !{!"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.ge25519_t = type { [10 x i32], [10 x i32], [10 x i32], [10 x i32] }
%struct.ge25519_pniels_t = type { [10 x i32], [10 x i32], [10 x i32], [10 x i32] }
%struct.ge25519_p1p1_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

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

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

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

declare void @curve25519_neg(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @ed25519_cosi_combine_publickeys(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #4 {
  %4 = alloca %struct.ge25519_t, align 4
  %5 = alloca %struct.ge25519_pniels_t, align 4
  %6 = alloca %struct.ge25519_p1p1_t, align 4
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %4) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(160) %4, i8 0, i64 160, i1 false)
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %5) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(160) %5, i8 0, i64 160, i1 false)
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %6) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(160) %6, i8 0, i64 160, i1 false)
  %7 = icmp eq i64 %2, 1
  br i1 %7, label %8, label %9

8:                                                ; preds = %3
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %0, ptr noundef nonnull align 1 dereferenceable(32) %1, i64 32, i1 false)
  br label %.loopexit

9:                                                ; preds = %3
  %10 = call i32 @ge25519_unpack_negative_vartime(ptr noundef nonnull %4, ptr noundef %1) #5
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %.loopexit, label %11

11:                                               ; preds = %9
  call void @ge25519_full_to_pniels(ptr noundef nonnull %5, ptr noundef nonnull %4) #5
  %12 = add i64 %2, -1
  %13 = icmp ugt i64 %12, 1
  br i1 %13, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %11, %16
  %.017 = phi i64 [ %17, %16 ], [ 1, %11 ]
  %14 = getelementptr inbounds [32 x i8], ptr %1, i64 %.017
  %15 = call i32 @ge25519_unpack_negative_vartime(ptr noundef nonnull %4, ptr noundef nonnull %14) #5
  %.not15 = icmp eq i32 %15, 0
  br i1 %.not15, label %.loopexit, label %16

16:                                               ; preds = %.lr.ph
  %17 = add nuw i64 %.017, 1
  call void @ge25519_pnielsadd(ptr noundef nonnull %5, ptr noundef nonnull %4, ptr noundef nonnull %5) #5
  %exitcond.not = icmp eq i64 %17, %12
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %16, %11
  %18 = getelementptr inbounds [32 x i8], ptr %1, i64 %12
  %19 = call i32 @ge25519_unpack_negative_vartime(ptr noundef nonnull %4, ptr noundef nonnull %18) #5
  %.not14 = icmp eq i32 %19, 0
  br i1 %.not14, label %.loopexit, label %20

20:                                               ; preds = %._crit_edge
  call void @ge25519_pnielsadd_p1p1(ptr noundef nonnull %6, ptr noundef nonnull %4, ptr noundef nonnull %5, i8 noundef zeroext 0) #5
  call void @ge25519_p1p1_to_partial(ptr noundef nonnull %4, ptr noundef nonnull %6) #5
  call void @curve25519_neg(ptr noundef nonnull %4, ptr noundef nonnull %4) #5
  call void @ge25519_pack(ptr noundef %0, ptr noundef nonnull %4) #5
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %._crit_edge, %9, %20, %8
  %.012 = phi i32 [ 0, %8 ], [ 0, %20 ], [ -1, %9 ], [ -1, %._crit_edge ], [ -1, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 160, ptr nonnull %6) #5
  call void @llvm.lifetime.end.p0(i64 160, ptr nonnull %5) #5
  call void @llvm.lifetime.end.p0(i64 160, ptr nonnull %4) #5
  ret i32 %.012
}

declare void @ge25519_full_to_pniels(ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @ge25519_pnielsadd(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @ge25519_pnielsadd_p1p1(ptr noundef, ptr noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #3

declare void @ge25519_p1p1_to_partial(ptr noundef, ptr noundef) local_unnamed_addr #3

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass