; LLVM IR from IntersectMBO/cardano-crypto :: cbits/ed25519 (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 { [5 x i64], [5 x i64], [5 x i64], [5 x i64] }

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

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

; Function Attrs: nounwind uwtable
declare hidden 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) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @cardano_crypto_ed25519_sign_open(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %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 [5 x i64], align 16
  %14 = alloca [5 x i64], 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) #2
  call void @llvm.lifetime.start.p0(i64 160, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 64, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 40, ptr %13) #2
  call void @llvm.lifetime.start.p0(i64 40, ptr %14) #2
  call void @llvm.lifetime.start.p0(i64 32, ptr %15) #2
  %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 %48

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 [5 x i64], 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 [5 x i64], 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 @expand256_modm(ptr noundef %36, ptr noundef %38, i64 noundef 32)
  %39 = getelementptr inbounds [5 x i64], ptr %13, i64 0, i64 0
  %40 = getelementptr inbounds [5 x i64], ptr %14, i64 0, i64 0
  call void @ge25519_double_scalarmult_vartime(ptr noundef %10, ptr noundef %11, ptr noundef %39, ptr noundef %40)
  %41 = getelementptr inbounds [32 x i8], ptr %15, i64 0, i64 0
  call void @ge25519_pack(ptr noundef %41, ptr noundef %10)
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = getelementptr inbounds [32 x i8], ptr %15, i64 0, i64 0
  %44 = call i32 @ed25519_verify(ptr noundef %42, ptr noundef %43, i64 noundef 32)
  %45 = icmp ne i32 %44, 0
  %46 = zext i1 %45 to i64
  %47 = select i1 %45, i32 0, i32 -1
  store i32 %47, ptr %5, align 4
  store i32 1, ptr %16, align 4
  br label %48

48:                                               ; preds = %28, %27
  call void @llvm.lifetime.end.p0(i64 32, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 40, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 40, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 64, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 160, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 160, ptr %10) #2
  %49 = load i32, ptr %5, align 4
  ret i32 %49
}

; Function Attrs: nounwind uwtable
declare hidden i32 @ge25519_unpack_negative_vartime(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @ge25519_double_scalarmult_vartime(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @ed25519_verify(ptr noundef, ptr noundef, i64 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 { [5 x i64], [5 x i64], [5 x i64], [5 x i64] }

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

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

; Function Attrs: nounwind uwtable
declare hidden 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 #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @cardano_crypto_ed25519_sign_open(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = alloca %struct.ge25519_t, align 16
  %6 = alloca %struct.ge25519_t, align 16
  %7 = alloca [64 x i8], align 16
  %8 = alloca [5 x i64], align 16
  %9 = alloca [5 x i64], align 16
  %10 = alloca [32 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %5) #2
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %6) #2
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %7) #2
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %8) #2
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %9) #2
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %10) #2
  %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 %19

13:                                               ; preds = %4
  %14 = call i32 @ge25519_unpack_negative_vartime(ptr noundef nonnull %6, ptr noundef %2)
  %.not8 = icmp eq i32 %14, 0
  br i1 %.not8, label %19, 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)
  %16 = getelementptr inbounds i8, ptr %3, i64 32
  call void @expand256_modm(ptr noundef nonnull %9, ptr noundef nonnull %16, i64 noundef 32)
  call void @ge25519_double_scalarmult_vartime(ptr noundef nonnull %5, ptr noundef nonnull %6, ptr noundef nonnull %8, ptr noundef nonnull %9)
  call void @ge25519_pack(ptr noundef nonnull %10, ptr noundef nonnull %5)
  %17 = call i32 @ed25519_verify(ptr noundef nonnull %3, ptr noundef nonnull %10, i64 noundef 32)
  %.not9 = icmp eq i32 %17, 0
  %18 = sext i1 %.not9 to i32
  br label %19

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

; Function Attrs: nounwind uwtable
declare hidden i32 @ge25519_unpack_negative_vartime(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @ge25519_double_scalarmult_vartime(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden 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 = { 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 #2 = { 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