; LLVM IR from IanHarvey/minicrypt :: src (f25519mul_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.MulResult = type { [18 x i32] }
%struct.U64 = type { i32, i32 }
%struct.F25519_Mini = type { [9 x i32] }

; Function Attrs: nounwind uwtable
define dso_local void @F25519_mul3_mini(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca %struct.MulResult, align 4
  %8 = alloca i32, align 4
  %9 = alloca %struct.U64, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 72, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %10 = getelementptr inbounds %struct.U64, ptr %9, i32 0, i32 1
  store i32 0, ptr %10, align 4, !tbaa !9
  %11 = getelementptr inbounds %struct.U64, ptr %9, i32 0, i32 0
  store i32 0, ptr %11, align 4, !tbaa !12
  store i32 0, ptr %8, align 4, !tbaa !13
  br label %12

12:                                               ; preds = %50, %3
  %13 = load i32, ptr %8, align 4, !tbaa !13
  %14 = icmp slt i32 %13, 17
  br i1 %14, label %15, label %53

15:                                               ; preds = %12
  %16 = load i32, ptr %8, align 4, !tbaa !13
  %17 = icmp slt i32 %16, 9
  br i1 %17, label %18, label %29

18:                                               ; preds = %15
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.F25519_Mini, ptr %19, i32 0, i32 0
  %21 = getelementptr inbounds [9 x i32], ptr %20, i64 0, i64 0
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.F25519_Mini, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %8, align 4, !tbaa !13
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [9 x i32], ptr %23, i64 0, i64 %25
  %27 = load i32, ptr %8, align 4, !tbaa !13
  %28 = add nsw i32 %27, 1
  call void @u64_sum_row(ptr noundef %9, ptr noundef %21, ptr noundef %26, i32 noundef %28)
  br label %42

29:                                               ; preds = %15
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.F25519_Mini, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %8, align 4, !tbaa !13
  %33 = sub nsw i32 %32, 9
  %34 = add nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [9 x i32], ptr %31, i64 0, i64 %35
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.F25519_Mini, ptr %37, i32 0, i32 0
  %39 = getelementptr inbounds [9 x i32], ptr %38, i64 0, i64 8
  %40 = load i32, ptr %8, align 4, !tbaa !13
  %41 = sub nsw i32 17, %40
  call void @u64_sum_row(ptr noundef %9, ptr noundef %36, ptr noundef %39, i32 noundef %41)
  br label %42

42:                                               ; preds = %29, %18
  %43 = getelementptr inbounds %struct.U64, ptr %9, i32 0, i32 0
  %44 = load i32, ptr %43, align 4, !tbaa !12
  %45 = and i32 %44, 536870911
  %46 = getelementptr inbounds %struct.MulResult, ptr %7, i32 0, i32 0
  %47 = load i32, ptr %8, align 4, !tbaa !13
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [18 x i32], ptr %46, i64 0, i64 %48
  store i32 %45, ptr %49, align 4, !tbaa !13
  call void @u64_shift_bits(ptr noundef %9)
  br label %50

50:                                               ; preds = %42
  %51 = load i32, ptr %8, align 4, !tbaa !13
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %8, align 4, !tbaa !13
  br label %12, !llvm.loop !14

53:                                               ; preds = %12
  %54 = getelementptr inbounds %struct.U64, ptr %9, i32 0, i32 0
  %55 = load i32, ptr %54, align 4, !tbaa !12
  %56 = and i32 %55, 536870911
  %57 = getelementptr inbounds %struct.MulResult, ptr %7, i32 0, i32 0
  %58 = load i32, ptr %8, align 4, !tbaa !13
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [18 x i32], ptr %57, i64 0, i64 %59
  store i32 %56, ptr %60, align 4, !tbaa !13
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.F25519_Mini, ptr %61, i32 0, i32 0
  %63 = getelementptr inbounds [9 x i32], ptr %62, i64 0, i64 0
  %64 = getelementptr inbounds %struct.MulResult, ptr %7, i32 0, i32 0
  %65 = getelementptr inbounds [18 x i32], ptr %64, i64 0, i64 0
  call void @mul_reduce_approx(ptr noundef %63, ptr noundef %65)
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  call void @F25519_reduce_mini_(ptr noundef %66)
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr %7) #3
  ret void
}

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

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

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

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

declare void @F25519_reduce_mini_(ptr noundef) #2

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

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, !11, i64 4}
!10 = !{!"", !11, i64 0, !11, i64 4}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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.MulResult = type { [18 x i32] }
%struct.U64 = type { i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local void @F25519_mul3_mini(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.MulResult, align 4
  %5 = alloca %struct.U64, align 4
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  %6 = getelementptr inbounds i8, ptr %5, i64 4
  store i32 0, ptr %6, align 4, !tbaa !5
  store i32 0, ptr %5, align 4, !tbaa !10
  %7 = getelementptr inbounds i8, ptr %2, i64 32
  br label %8

8:                                                ; preds = %3, %19
  %indvars.iv = phi i64 [ 0, %3 ], [ %indvars.iv.next.pre-phi, %19 ]
  %9 = icmp ult i64 %indvars.iv, 9
  br i1 %9, label %10, label %14

10:                                               ; preds = %8
  %11 = getelementptr inbounds [9 x i32], ptr %2, i64 0, i64 %indvars.iv
  %12 = add nuw nsw i64 %indvars.iv, 1
  %13 = trunc nuw nsw i64 %12 to i32
  call void @u64_sum_row(ptr noundef nonnull %5, ptr noundef %1, ptr noundef %11, i32 noundef %13)
  br label %19

14:                                               ; preds = %8
  %15 = add nsw i64 %indvars.iv, -8
  %16 = getelementptr inbounds [9 x i32], ptr %1, i64 0, i64 %15
  %17 = trunc i64 %indvars.iv to i32
  %18 = sub i32 17, %17
  call void @u64_sum_row(ptr noundef nonnull %5, ptr noundef %16, ptr noundef nonnull %7, i32 noundef %18)
  %.pre = add nuw nsw i64 %indvars.iv, 1
  br label %19

19:                                               ; preds = %14, %10
  %indvars.iv.next.pre-phi = phi i64 [ %.pre, %14 ], [ %12, %10 ]
  %20 = load i32, ptr %5, align 4, !tbaa !10
  %21 = and i32 %20, 536870911
  %22 = getelementptr inbounds [18 x i32], ptr %4, i64 0, i64 %indvars.iv
  store i32 %21, ptr %22, align 4, !tbaa !11
  call void @u64_shift_bits(ptr noundef nonnull %5)
  %exitcond.not = icmp eq i64 %indvars.iv.next.pre-phi, 17
  br i1 %exitcond.not, label %23, label %8, !llvm.loop !12

23:                                               ; preds = %19
  %24 = load i32, ptr %5, align 4, !tbaa !10
  %25 = and i32 %24, 536870911
  %26 = getelementptr inbounds i8, ptr %4, i64 68
  store i32 %25, ptr %26, align 4, !tbaa !11
  call void @mul_reduce_approx(ptr noundef %0, ptr noundef nonnull %4)
  call void @F25519_reduce_mini_(ptr noundef %0) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %4) #3
  ret void
}

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

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

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

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

declare void @F25519_reduce_mini_(ptr noundef) local_unnamed_addr #2

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

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, !7, i64 4}
!6 = !{!"", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,LoopRotatePass,SROAPass,SimplifyCFGPass