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

@.str = external hidden unnamed_addr constant [70 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [78 x i8], align 1

; 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

declare i32 @printf(ptr noundef, ...) #1

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @Hacl_Bignum_Montgomery_bn_check_modulus_u32(i32 noundef %0, ptr noundef %1) #5 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %2
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = zext i32 %15 to i64
  %17 = icmp ugt i64 %16, 4611686018427387903
  br i1 %17, label %18, label %20

18:                                               ; preds = %14
  %19 = call i32 (ptr, ...) @printf(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 823)
  call void @exit(i32 noundef 253) #7
  unreachable

20:                                               ; preds = %14
  br label %21

21:                                               ; preds = %20
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = zext i32 %22 to i64
  %24 = call ptr @llvm.stacksave.p0()
  store ptr %24, ptr %5, align 8
  %25 = alloca i32, i64 %23, align 16
  store i64 %23, ptr %6, align 8
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = zext i32 %26 to i64
  %28 = mul i64 %27, 4
  call void @llvm.memset.p0.i64(ptr align 16 %25, i8 0, i64 %28, i1 false)
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = zext i32 %29 to i64
  %31 = mul i64 %30, 4
  call void @llvm.memset.p0.i64(ptr align 16 %25, i8 0, i64 %31, i1 false)
  %32 = getelementptr inbounds i32, ptr %25, i64 0
  store i32 1, ptr %32, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #8
  %33 = load ptr, ptr %4, align 8, !tbaa !9
  %34 = getelementptr inbounds i32, ptr %33, i64 0
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = and i32 %35, 1
  store i32 %36, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #8
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = sub i32 0, %37
  store i32 %38, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #8
  store i32 0, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #8
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %79, %21
  %40 = load i32, ptr %10, align 4, !tbaa !5
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = icmp ult i32 %40, %41
  br i1 %42, label %44, label %43

43:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #8
  br label %82

44:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #8
  %45 = load i32, ptr %10, align 4, !tbaa !5
  %46 = zext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %25, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !9
  %50 = load i32, ptr %10, align 4, !tbaa !5
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds i32, ptr %49, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = call i32 @FStar_UInt32_eq_mask(i32 noundef %48, i32 noundef %53)
  store i32 %54, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #8
  %55 = load i32, ptr %10, align 4, !tbaa !5
  %56 = zext i32 %55 to i64
  %57 = getelementptr inbounds i32, ptr %25, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = load ptr, ptr %4, align 8, !tbaa !9
  %60 = load i32, ptr %10, align 4, !tbaa !5
  %61 = zext i32 %60 to i64
  %62 = getelementptr inbounds i32, ptr %59, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !5
  %64 = call i32 @FStar_UInt32_gte_mask(i32 noundef %58, i32 noundef %63)
  %65 = xor i32 %64, -1
  store i32 %65, ptr %12, align 4, !tbaa !5
  %66 = load i32, ptr %11, align 4, !tbaa !5
  %67 = load i32, ptr %9, align 4, !tbaa !5
  %68 = and i32 %66, %67
  %69 = load i32, ptr %11, align 4, !tbaa !5
  %70 = xor i32 %69, -1
  %71 = load i32, ptr %12, align 4, !tbaa !5
  %72 = and i32 %71, -1
  %73 = load i32, ptr %12, align 4, !tbaa !5
  %74 = xor i32 %73, -1
  %75 = and i32 %74, 0
  %76 = or i32 %72, %75
  %77 = and i32 %70, %76
  %78 = or i32 %68, %77
  store i32 %78, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #8
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #8
  br label %79

79:                                               ; preds = %44
  %80 = load i32, ptr %10, align 4, !tbaa !5
  %81 = add i32 %80, 1
  store i32 %81, ptr %10, align 4, !tbaa !5
  br label %39, !llvm.loop !11

82:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #8
  %83 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %83, ptr %13, align 4, !tbaa !5
  %84 = load i32, ptr %8, align 4, !tbaa !5
  %85 = load i32, ptr %13, align 4, !tbaa !5
  %86 = and i32 %84, %85
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #8
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #8
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #8
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #8
  %87 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %87)
  ret i32 %86
}

; Function Attrs: noinline nounwind uwtable
declare hidden i32 @FStar_UInt32_eq_mask(i32 noundef, i32 noundef) #6

; Function Attrs: noinline nounwind uwtable
declare hidden i32 @FStar_UInt32_gte_mask(i32 noundef, i32 noundef) #6

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 = { noreturn nounwind "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 = { nocallback nofree nosync nounwind willreturn }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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 #6 = { noinline 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 #7 = { noreturn nounwind }
attributes #8 = { 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 = distinct !{!11, !12}
!12 = !{!"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
define dso_local i32 @Hacl_Bignum_Montgomery_bn_check_modulus_u32(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #1 {
  %3 = zext i32 %0 to i64
  %4 = alloca i32, i64 %3, align 16
  %5 = shl nuw nsw i64 %3, 2
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %4, i8 0, i64 %5, i1 false)
  store i32 1, ptr %4, align 16, !tbaa !5
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %.not = icmp eq i32 %0, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %2
  %.0.lcssa = phi i32 [ 0, %2 ], [ %19, %.lr.ph ]
  %7 = and i32 %6, 1
  %8 = icmp eq i32 %7, 0
  %9 = select i1 %8, i32 0, i32 %.0.lcssa
  ret i32 %9

.lr.ph:                                           ; preds = %2, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %2 ]
  %.025 = phi i32 [ %19, %.lr.ph ], [ 0, %2 ]
  %10 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = tail call i32 @FStar_UInt32_eq_mask(i32 noundef %11, i32 noundef %13)
  %15 = load i32, ptr %12, align 4, !tbaa !5
  %16 = tail call i32 @FStar_UInt32_gte_mask(i32 noundef %11, i32 noundef %15)
  %17 = and i32 %14, %.025
  %.demorgan = or i32 %16, %14
  %18 = xor i32 %.demorgan, -1
  %19 = or i32 %17, %18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %3
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9
}

; Function Attrs: noinline nounwind uwtable
declare hidden i32 @FStar_UInt32_eq_mask(i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: noinline nounwind uwtable
declare hidden i32 @FStar_UInt32_gte_mask(i32 noundef, i32 noundef) local_unnamed_addr #2

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" }
attributes #2 = { noinline 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 = !{!"int", !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,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass