; LLVM IR from square/subzero :: core/trezor-crypto/ed25519-donna (ed25519-keccak)

; 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

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

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

; 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 @ed25519_hram(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @ed25519_sign_open_keccak(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #2 {
  %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) #3
  call void @llvm.lifetime.start.p0(i64 160, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 64, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 32, ptr %15) #3
  %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) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 64, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 160, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 160, ptr %10) #3
  %54 = load i32, ptr %5, align 4
  ret i32 %54
}

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

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

declare i32 @is_reduced256_modm(ptr noundef) #1

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

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

attributes #0 = { nocallback nofree nosync 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, !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

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

declare void @ge25519_pack(ptr noundef, ptr noundef) local_unnamed_addr #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: nounwind uwtable
declare hidden void @ed25519_hram(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @ed25519_sign_open_keccak(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #2 {
  %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) #3
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %6) #3
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %7) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %8) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %9) #3
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %10) #3
  %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) #3
  %.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) #3
  %16 = getelementptr inbounds i8, ptr %3, i64 32
  call void @expand_raw256_modm(ptr noundef nonnull %9, ptr noundef nonnull %16) #3
  %17 = call i32 @is_reduced256_modm(ptr noundef nonnull %9) #3
  %.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) #3
  call void @ge25519_pack(ptr noundef nonnull %10, ptr noundef nonnull %5) #3
  %19 = call i32 @ed25519_verify(ptr noundef nonnull %3, ptr noundef nonnull %10, i64 noundef 32) #3
  %.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) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %9) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %8) #3
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %7) #3
  call void @llvm.lifetime.end.p0(i64 160, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 160, ptr nonnull %5) #3
  ret i32 %.0
}

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

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

declare i32 @is_reduced256_modm(ptr noundef) local_unnamed_addr #1

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

declare i32 @ed25519_verify(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync 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, !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