; LLVM IR from IanHarvey/minicrypt :: src (curve25519_mini)

; 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.F25519_Mini = type { [9 x i32] }
%struct.XZPoint = type { %struct.F25519_Mini, %struct.F25519_Mini }
%struct.Curve25519Msg_Mini = type { [32 x i8] }

; Function Attrs: nounwind uwtable
define dso_local i32 @Curve25519_mini(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca %struct.F25519_Mini, align 4
  %11 = alloca %struct.XZPoint, align 4
  %12 = alloca %struct.XZPoint, align 4
  %13 = alloca [2 x ptr], align 16
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 36, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 72, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 72, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr %13) #3
  store ptr %11, ptr %13, align 8, !tbaa !5
  %16 = getelementptr inbounds ptr, ptr %13, i64 1
  store ptr %12, ptr %16, align 8, !tbaa !5
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.Curve25519Msg_Mini, ptr %17, i32 0, i32 0
  %19 = getelementptr inbounds [32 x i8], ptr %18, i64 0, i64 0
  %20 = call i32 @F25519_set_mini(ptr noundef %10, ptr noundef %19, i64 noundef 32)
  store i32 %20, ptr %8, align 4, !tbaa !9
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %25

23:                                               ; preds = %3
  %24 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %24, ptr %4, align 4
  store i32 1, ptr %14, align 4
  br label %75

25:                                               ; preds = %3
  %26 = getelementptr inbounds %struct.XZPoint, ptr %11, i32 0, i32 0
  call void @F25519_copy_mini(ptr noundef %26, ptr noundef %10)
  %27 = getelementptr inbounds %struct.XZPoint, ptr %11, i32 0, i32 1
  call void @F25519_setK_mini(ptr noundef %27, i32 noundef 1)
  %28 = getelementptr inbounds %struct.XZPoint, ptr %12, i32 0, i32 0
  call void @F25519_setK_mini(ptr noundef %28, i32 noundef 1)
  %29 = getelementptr inbounds %struct.XZPoint, ptr %12, i32 0, i32 1
  call void @F25519_setK_mini(ptr noundef %29, i32 noundef 0)
  call void @monty_double_add(ptr noundef %11, ptr noundef %12, ptr noundef %10)
  store i32 253, ptr %9, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %55, %25
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = icmp sge i32 %31, 3
  br i1 %32, label %33, label %58

33:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Curve25519Msg_Mini, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = ashr i32 %36, 3
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [32 x i8], ptr %35, i64 0, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !11
  %41 = zext i8 %40 to i32
  %42 = load i32, ptr %9, align 4, !tbaa !9
  %43 = and i32 %42, 7
  %44 = ashr i32 %41, %43
  %45 = and i32 %44, 1
  store i32 %45, ptr %15, align 4, !tbaa !9
  %46 = load i32, ptr %15, align 4, !tbaa !9
  %47 = xor i32 %46, 1
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [2 x ptr], ptr %13, i64 0, i64 %48
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  %51 = load i32, ptr %15, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [2 x ptr], ptr %13, i64 0, i64 %52
  %54 = load ptr, ptr %53, align 8, !tbaa !5
  call void @monty_double_add(ptr noundef %50, ptr noundef %54, ptr noundef %10)
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  br label %55

55:                                               ; preds = %33
  %56 = load i32, ptr %9, align 4, !tbaa !9
  %57 = add nsw i32 %56, -1
  store i32 %57, ptr %9, align 4, !tbaa !9
  br label %30, !llvm.loop !12

58:                                               ; preds = %30
  br label %59

59:                                               ; preds = %63, %58
  %60 = load i32, ptr %9, align 4, !tbaa !9
  %61 = icmp sge i32 %60, 0
  br i1 %61, label %62, label %66

62:                                               ; preds = %59
  call void @monty_double_add(ptr noundef %12, ptr noundef %11, ptr noundef %10)
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %9, align 4, !tbaa !9
  %65 = add nsw i32 %64, -1
  store i32 %65, ptr %9, align 4, !tbaa !9
  br label %59, !llvm.loop !14

66:                                               ; preds = %59
  %67 = getelementptr inbounds %struct.XZPoint, ptr %12, i32 0, i32 1
  call void @F25519_modexp_inv_mini_(ptr noundef %10, ptr noundef %67)
  %68 = getelementptr inbounds %struct.XZPoint, ptr %12, i32 0, i32 0
  %69 = getelementptr inbounds %struct.XZPoint, ptr %12, i32 0, i32 0
  call void @F25519_mul3_mini(ptr noundef %68, ptr noundef %69, ptr noundef %10)
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.Curve25519Msg_Mini, ptr %70, i32 0, i32 0
  %72 = getelementptr inbounds [32 x i8], ptr %71, i64 0, i64 0
  %73 = getelementptr inbounds %struct.XZPoint, ptr %12, i32 0, i32 0
  %74 = call i32 @F25519_get_mini(ptr noundef %72, i64 noundef 32, ptr noundef %73)
  store i32 %74, ptr %4, align 4
  store i32 1, ptr %14, align 4
  br label %75

75:                                               ; preds = %66, %23
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %76 = load i32, ptr %4, align 4
  ret i32 %76
}

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

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

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

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

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

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

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

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

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

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

!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"}
!14 = distinct !{!14, !13}


; 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.F25519_Mini = type { [9 x i32] }
%struct.XZPoint = type { %struct.F25519_Mini, %struct.F25519_Mini }

; Function Attrs: nounwind uwtable
define dso_local i32 @Curve25519_mini(ptr noundef %0, ptr nocapture noundef readonly %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.F25519_Mini, align 4
  %5 = alloca %struct.XZPoint, align 4
  %6 = alloca %struct.XZPoint, align 4
  %7 = alloca [2 x ptr], align 16
  call void @llvm.lifetime.start.p0(i64 36, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %6) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #3
  store ptr %5, ptr %7, align 16, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  store ptr %6, ptr %8, align 8, !tbaa !5
  %9 = call i32 @F25519_set_mini(ptr noundef nonnull %4, ptr noundef %2, i64 noundef 32) #3
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %10, label %32

10:                                               ; preds = %3
  call void @F25519_copy_mini(ptr noundef nonnull %5, ptr noundef nonnull %4) #3
  %11 = getelementptr inbounds i8, ptr %5, i64 36
  call void @F25519_setK_mini(ptr noundef nonnull %11, i32 noundef 1) #3
  call void @F25519_setK_mini(ptr noundef nonnull %6, i32 noundef 1) #3
  %12 = getelementptr inbounds i8, ptr %6, i64 36
  call void @F25519_setK_mini(ptr noundef nonnull %12, i32 noundef 0) #3
  call void @monty_double_add(ptr noundef nonnull %5, ptr noundef nonnull %6, ptr noundef nonnull %4)
  br label %13

13:                                               ; preds = %10, %13
  %.01315 = phi i32 [ 253, %10 ], [ %29, %13 ]
  %14 = lshr i32 %.01315, 3
  %15 = zext nneg i32 %14 to i64
  %16 = getelementptr inbounds [32 x i8], ptr %1, i64 0, i64 %15
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = zext i8 %17 to i32
  %19 = and i32 %.01315, 7
  %20 = lshr i32 %18, %19
  %21 = and i32 %20, 1
  %22 = xor i32 %21, 1
  %23 = zext nneg i32 %22 to i64
  %24 = getelementptr inbounds [2 x ptr], ptr %7, i64 0, i64 %23
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  %26 = zext nneg i32 %21 to i64
  %27 = getelementptr inbounds [2 x ptr], ptr %7, i64 0, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  call void @monty_double_add(ptr noundef %25, ptr noundef %28, ptr noundef nonnull %4)
  %29 = add nsw i32 %.01315, -1
  %30 = icmp ugt i32 %.01315, 3
  br i1 %30, label %13, label %._crit_edge, !llvm.loop !10

._crit_edge:                                      ; preds = %13
  call void @monty_double_add(ptr noundef nonnull %6, ptr noundef nonnull %5, ptr noundef nonnull %4)
  call void @monty_double_add(ptr noundef nonnull %6, ptr noundef nonnull %5, ptr noundef nonnull %4)
  call void @monty_double_add(ptr noundef nonnull %6, ptr noundef nonnull %5, ptr noundef nonnull %4)
  call void @F25519_modexp_inv_mini_(ptr noundef nonnull %4, ptr noundef nonnull %12)
  call void @F25519_mul3_mini(ptr noundef nonnull %6, ptr noundef nonnull %6, ptr noundef nonnull %4) #3
  %31 = call i32 @F25519_get_mini(ptr noundef %0, i64 noundef 32, ptr noundef nonnull %6) #3
  br label %32

32:                                               ; preds = %3, %._crit_edge
  %.0 = phi i32 [ %31, %._crit_edge ], [ %9, %3 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 36, ptr nonnull %4) #3
  ret i32 %.0
}

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

declare i32 @F25519_set_mini(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

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

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

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

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

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

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

declare i32 @F25519_get_mini(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

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

!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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass