; LLVM IR from cryspen/libcrux :: sys/hacl/c/src (Hacl_Bignum)

; 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"

; 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 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: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local void @Hacl_Bignum_bn_add_mod_n_u64(i32 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @Hacl_Bignum_Montgomery_bn_precomp_r2_mod_n_u64(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #2 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !9
  %12 = load ptr, ptr %8, align 8, !tbaa !9
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = zext i32 %13 to i64
  %15 = mul i64 %14, 8
  call void @llvm.memset.p0.i64(ptr align 8 %12, i8 0, i64 %15, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = udiv i32 %16, 64
  store i32 %17, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = urem i32 %18, 64
  store i32 %19, ptr %10, align 4, !tbaa !5
  %20 = load ptr, ptr %8, align 8, !tbaa !9
  %21 = load i32, ptr %9, align 4, !tbaa !5
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds i64, ptr %20, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !11
  %25 = load i32, ptr %10, align 4, !tbaa !5
  %26 = zext i32 %25 to i64
  %27 = shl i64 1, %26
  %28 = or i64 %24, %27
  %29 = load ptr, ptr %8, align 8, !tbaa !9
  %30 = load i32, ptr %9, align 4, !tbaa !5
  %31 = zext i32 %30 to i64
  %32 = getelementptr inbounds i64, ptr %29, i64 %31
  store i64 %28, ptr %32, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %47, %4
  %34 = load i32, ptr %11, align 4, !tbaa !5
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = mul i32 128, %35
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = sub i32 %36, %37
  %39 = icmp ult i32 %34, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %50

41:                                               ; preds = %33
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = load ptr, ptr %7, align 8, !tbaa !9
  %44 = load ptr, ptr %8, align 8, !tbaa !9
  %45 = load ptr, ptr %8, align 8, !tbaa !9
  %46 = load ptr, ptr %8, align 8, !tbaa !9
  call void @Hacl_Bignum_bn_add_mod_n_u64(i32 noundef %42, ptr noundef %43, ptr noundef %44, ptr noundef %45, ptr noundef %46)
  br label %47

47:                                               ; preds = %41
  %48 = load i32, ptr %11, align 4, !tbaa !5
  %49 = add i32 %48, 1
  store i32 %49, ptr %11, align 4, !tbaa !5
  br label %33, !llvm.loop !13

50:                                               ; preds = %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
declare dso_local void @Hacl_Bignum_bn_add_mod_n_u64(i32 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @Hacl_Bignum_Montgomery_bn_precomp_r2_mod_n_u64(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = zext i32 %0 to i64
  %6 = shl nuw nsw i64 %5, 3
  tail call void @llvm.memset.p0.i64(ptr align 8 %3, i8 0, i64 %6, i1 false)
  %7 = lshr i32 %1, 6
  %8 = and i32 %1, 63
  %9 = zext nneg i32 %7 to i64
  %10 = getelementptr inbounds i64, ptr %3, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = zext nneg i32 %8 to i64
  %13 = shl nuw i64 1, %12
  %14 = or i64 %11, %13
  store i64 %14, ptr %10, align 8, !tbaa !5
  %15 = shl i32 %0, 7
  %16 = sub i32 %15, %1
  %.not = icmp eq i32 %15, %1
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %4
  ret void

.lr.ph:                                           ; preds = %4, %.lr.ph
  %.017 = phi i32 [ %17, %.lr.ph ], [ 0, %4 ]
  tail call void @Hacl_Bignum_bn_add_mod_n_u64(i32 noundef %0, ptr noundef %2, ptr noundef %3, ptr noundef %3, ptr noundef %3)
  %17 = add nuw i32 %.017, 1
  %exitcond.not = icmp eq i32 %17, %16
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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" }

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


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