; LLVM IR from openssl/openssl :: crypto/ec (ec_curve)

; 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._ec_list_element_st = type { i32, ptr, ptr, ptr }
%struct.EC_builtin_curve = type { i32, ptr }

@curve_list = external hidden constant [82 x %struct._ec_list_element_st], align 16

; 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
define dso_local i64 @EC_get_builtin_curves(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %14, label %11

11:                                               ; preds = %2
  %12 = load i64, ptr %5, align 8, !tbaa !9
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %11, %2
  store i64 82, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %48

15:                                               ; preds = %11
  %16 = load i64, ptr %5, align 8, !tbaa !9
  %17 = icmp ult i64 %16, 82
  br i1 %17, label %18, label %20

18:                                               ; preds = %15
  %19 = load i64, ptr %5, align 8, !tbaa !9
  br label %21

20:                                               ; preds = %15
  br label %21

21:                                               ; preds = %20, %18
  %22 = phi i64 [ %19, %18 ], [ 82, %20 ]
  store i64 %22, ptr %7, align 8, !tbaa !9
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %44, %21
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = load i64, ptr %7, align 8, !tbaa !9
  %26 = icmp ult i64 %24, %25
  br i1 %26, label %27, label %47

27:                                               ; preds = %23
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = getelementptr inbounds [82 x %struct._ec_list_element_st], ptr @curve_list, i64 0, i64 %28
  %30 = getelementptr inbounds %struct._ec_list_element_st, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 16, !tbaa !11
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.EC_builtin_curve, ptr %32, i64 %33
  %35 = getelementptr inbounds %struct.EC_builtin_curve, ptr %34, i32 0, i32 0
  store i32 %31, ptr %35, align 8, !tbaa !14
  %36 = load i64, ptr %6, align 8, !tbaa !9
  %37 = getelementptr inbounds [82 x %struct._ec_list_element_st], ptr @curve_list, i64 0, i64 %36
  %38 = getelementptr inbounds %struct._ec_list_element_st, ptr %37, i32 0, i32 3
  %39 = load ptr, ptr %38, align 8, !tbaa !16
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = load i64, ptr %6, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.EC_builtin_curve, ptr %40, i64 %41
  %43 = getelementptr inbounds %struct.EC_builtin_curve, ptr %42, i32 0, i32 1
  store ptr %39, ptr %43, align 8, !tbaa !17
  br label %44

44:                                               ; preds = %27
  %45 = load i64, ptr %6, align 8, !tbaa !9
  %46 = add i64 %45, 1
  store i64 %46, ptr %6, align 8, !tbaa !9
  br label %23, !llvm.loop !18

47:                                               ; preds = %23
  store i64 82, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %48

48:                                               ; preds = %47, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %49 = load i64, ptr %3, align 8
  ret i64 %49
}

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, !13, i64 0}
!12 = !{!"_ec_list_element_st", !13, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !13, i64 0}
!15 = !{!"", !13, i64 0, !6, i64 8}
!16 = !{!12, !6, i64 24}
!17 = !{!15, !6, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"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._ec_list_element_st = type { i32, ptr, ptr, ptr }
%struct.EC_builtin_curve = type { i32, ptr }

@curve_list = external hidden local_unnamed_addr constant [82 x %struct._ec_list_element_st], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: write) uwtable
define dso_local noundef i64 @EC_get_builtin_curves(ptr noundef writeonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq i64 %1, 0
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %5 = tail call i64 @llvm.umin.i64(i64 %1, i64 82)
  %xtraiter = and i64 %5, 1
  %6 = icmp eq i64 %1, 1
  br i1 %6, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %5, 126
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.01416 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %20, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %7 = getelementptr inbounds [82 x %struct._ec_list_element_st], ptr @curve_list, i64 0, i64 %.01416
  %8 = load i32, ptr %7, align 16, !tbaa !5
  %9 = getelementptr inbounds %struct.EC_builtin_curve, ptr %0, i64 %.01416
  store i32 %8, ptr %9, align 8, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %7, i64 24
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %9, i64 8
  store ptr %11, ptr %12, align 8, !tbaa !14
  %13 = or disjoint i64 %.01416, 1
  %14 = getelementptr inbounds [82 x %struct._ec_list_element_st], ptr @curve_list, i64 0, i64 %13
  %15 = load i32, ptr %14, align 16, !tbaa !5
  %16 = getelementptr inbounds %struct.EC_builtin_curve, ptr %0, i64 %13
  store i32 %15, ptr %16, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %14, i64 24
  %18 = load ptr, ptr %17, align 8, !tbaa !13
  %19 = getelementptr inbounds i8, ptr %16, i64 8
  store ptr %18, ptr %19, align 8, !tbaa !14
  %20 = add nuw nsw i64 %.01416, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !15

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %.01416.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %20, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa
  %21 = getelementptr inbounds [82 x %struct._ec_list_element_st], ptr @curve_list, i64 0, i64 %.01416.unr
  %22 = load i32, ptr %21, align 16, !tbaa !5
  %23 = getelementptr inbounds %struct.EC_builtin_curve, ptr %0, i64 %.01416.unr
  store i32 %22, ptr %23, align 8, !tbaa !11
  %24 = getelementptr inbounds i8, ptr %21, i64 24
  %25 = load ptr, ptr %24, align 8, !tbaa !13
  %26 = getelementptr inbounds i8, ptr %23, i64 8
  store ptr %25, ptr %26, align 8, !tbaa !14
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.epil, %.loopexit.loopexit.unr-lcssa, %2
  ret i64 82
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #1

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: write) 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 speculatable willreturn memory(none) }

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


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