; LLVM IR from IanHarvey/minicrypt :: src (f25519util_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] }

@F25519_P_mini_ = external dso_local constant %struct.F25519_Mini, align 4

; 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: nounwind uwtable
declare dso_local i32 @F25519_cmp_mini_(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @F25519_set_mini(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i8, align 1
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  call void @F25519_setK_mini(ptr noundef %12, i32 noundef 0)
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = icmp ugt i64 %13, 32
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store i32 2, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %63

16:                                               ; preds = %3
  store i32 0, ptr %9, align 4, !tbaa !11
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %17

17:                                               ; preds = %56, %16
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = add i64 %18, -1
  store i64 %19, ptr %7, align 8, !tbaa !9
  %20 = icmp ugt i64 %18, 0
  br i1 %20, label %21, label %57

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #2
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i32 1
  store ptr %23, ptr %6, align 8, !tbaa !5
  %24 = load i8, ptr %22, align 1, !tbaa !13
  store i8 %24, ptr %11, align 1, !tbaa !13
  %25 = load i8, ptr %11, align 1, !tbaa !13
  %26 = zext i8 %25 to i32
  %27 = load i32, ptr %8, align 4, !tbaa !11
  %28 = shl i32 %26, %27
  %29 = and i32 %28, 536870911
  %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 %9, align 4, !tbaa !11
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [9 x i32], ptr %31, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !11
  %36 = or i32 %35, %29
  store i32 %36, ptr %34, align 4, !tbaa !11
  %37 = load i32, ptr %8, align 4, !tbaa !11
  %38 = add nsw i32 %37, 8
  store i32 %38, ptr %8, align 4, !tbaa !11
  %39 = load i32, ptr %8, align 4, !tbaa !11
  %40 = icmp sgt i32 %39, 29
  br i1 %40, label %41, label %56

41:                                               ; preds = %21
  %42 = load i32, ptr %8, align 4, !tbaa !11
  %43 = sub nsw i32 %42, 29
  store i32 %43, ptr %8, align 4, !tbaa !11
  %44 = load i32, ptr %9, align 4, !tbaa !11
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %9, align 4, !tbaa !11
  %46 = load i8, ptr %11, align 1, !tbaa !13
  %47 = zext i8 %46 to i32
  %48 = load i32, ptr %8, align 4, !tbaa !11
  %49 = sub nsw i32 8, %48
  %50 = ashr i32 %47, %49
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.F25519_Mini, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %9, align 4, !tbaa !11
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [9 x i32], ptr %52, i64 0, i64 %54
  store i32 %50, ptr %55, align 4, !tbaa !11
  br label %56

56:                                               ; preds = %41, %21
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #2
  br label %17, !llvm.loop !14

57:                                               ; preds = %17
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = call i32 @F25519_cmp_mini_(ptr noundef %58, ptr noundef @F25519_P_mini_)
  %60 = icmp sge i32 %59, 0
  br i1 %60, label %61, label %62

61:                                               ; preds = %57
  store i32 3, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %63

62:                                               ; preds = %57
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %63

63:                                               ; preds = %62, %61, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %64 = load i32, ptr %4, align 4
  ret i32 %64
}

; Function Attrs: nounwind uwtable
declare dso_local void @F25519_setK_mini(ptr noundef, i32 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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, 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.F25519_Mini = type { [9 x i32] }

@F25519_P_mini_ = external dso_local constant %struct.F25519_Mini, align 4

; Function Attrs: nounwind uwtable
declare dso_local i32 @F25519_cmp_mini_(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 4) i32 @F25519_set_mini(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  tail call void @F25519_setK_mini(ptr noundef %0, i32 noundef 0)
  %4 = icmp ugt i64 %2, 32
  br i1 %4, label %26, label %.preheader

.preheader:                                       ; preds = %3
  %.not25 = icmp eq i64 %2, 0
  br i1 %.not25, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %24
  %.in = phi i64 [ %5, %24 ], [ %2, %.preheader ]
  %.01728 = phi i32 [ %.1, %24 ], [ 0, %.preheader ]
  %.01827 = phi i32 [ %.119, %24 ], [ 0, %.preheader ]
  %.02126 = phi ptr [ %6, %24 ], [ %1, %.preheader ]
  %5 = add i64 %.in, -1
  %6 = getelementptr inbounds i8, ptr %.02126, i64 1
  %7 = load i8, ptr %.02126, align 1, !tbaa !5
  %8 = zext i8 %7 to i32
  %9 = shl i32 %8, %.01827
  %10 = and i32 %9, 536870911
  %11 = sext i32 %.01728 to i64
  %12 = getelementptr inbounds [9 x i32], ptr %0, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !8
  %14 = or i32 %10, %13
  store i32 %14, ptr %12, align 4, !tbaa !8
  %15 = add nsw i32 %.01827, 8
  %16 = icmp sgt i32 %.01827, 21
  br i1 %16, label %17, label %24

17:                                               ; preds = %.lr.ph
  %18 = add nsw i32 %.01827, -21
  %19 = add nsw i32 %.01728, 1
  %20 = sub nsw i32 29, %.01827
  %21 = lshr i32 %8, %20
  %22 = sext i32 %19 to i64
  %23 = getelementptr inbounds [9 x i32], ptr %0, i64 0, i64 %22
  store i32 %21, ptr %23, align 4, !tbaa !8
  br label %24

24:                                               ; preds = %17, %.lr.ph
  %.119 = phi i32 [ %18, %17 ], [ %15, %.lr.ph ]
  %.1 = phi i32 [ %19, %17 ], [ %.01728, %.lr.ph ]
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %24, %.preheader
  %25 = tail call i32 @F25519_cmp_mini_(ptr noundef %0, ptr noundef nonnull @F25519_P_mini_)
  %.inv = icmp slt i32 %25, 0
  %. = select i1 %.inv, i32 0, i32 3
  br label %26

26:                                               ; preds = %._crit_edge, %3
  %.0 = phi i32 [ 2, %3 ], [ %., %._crit_edge ]
  ret i32 %.0
}

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

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

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass