; LLVM IR from amritansh22/Data-Structures-and-Algorithms-in-cpp :: activity_selection.ll (Z10activitiesPSt4pairIxxEx)

; 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.std::pair" = type { i64, i64 }

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

12:                                               ; preds = %34, %2
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = load i64, ptr %4, align 8, !tbaa !9
  %15 = icmp slt i64 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %37

17:                                               ; preds = %12
  %18 = load i64, ptr %5, align 8, !tbaa !9
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = load i64, ptr %7, align 8, !tbaa !9
  %21 = getelementptr inbounds %"struct.std::pair", ptr %19, i64 %20
  %22 = getelementptr inbounds %"struct.std::pair", ptr %21, i32 0, i32 0
  %23 = load i64, ptr %22, align 8, !tbaa !13
  %24 = icmp sle i64 %18, %23
  br i1 %24, label %25, label %33

25:                                               ; preds = %17
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = add nsw i64 %26, 1
  store i64 %27, ptr %6, align 8, !tbaa !9
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = getelementptr inbounds %"struct.std::pair", ptr %28, i64 %29
  %31 = getelementptr inbounds %"struct.std::pair", ptr %30, i32 0, i32 1
  %32 = load i64, ptr %31, align 8, !tbaa !11
  store i64 %32, ptr %5, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %25, %17
  br label %34

34:                                               ; preds = %33
  %35 = load i64, ptr %7, align 8, !tbaa !9
  %36 = add nsw i64 %35, 1
  store i64 %36, ptr %7, align 8, !tbaa !9
  br label %12, !llvm.loop !14

37:                                               ; preds = %16
  %38 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i64 %38
}

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

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

attributes #0 = { mustprogress 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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTSSt4pairIxxE", !10, i64 0, !10, i64 8}
!13 = !{!12, !10, 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.std::pair" = type { i64, i64 }

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i64 @_Z10activitiesPSt4pairIxxEx(ptr nocapture noundef readonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i64 %1, 1
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = add nsw i64 %1, -1
  %xtraiter = and i64 %6, 1
  %7 = icmp eq i64 %1, 2
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %6, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %24, %.lr.ph.preheader
  %.1.lcssa.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %.1.1, %24 ]
  %.015.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %25, %24 ]
  %.01014.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %.1.1, %24 ]
  %.01113.unr = phi i64 [ %5, %.lr.ph.preheader ], [ %.112.1, %24 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %8 = getelementptr inbounds %"struct.std::pair", ptr %0, i64 %.015.unr
  %9 = load i64, ptr %8, align 8, !tbaa !10
  %.not.epil = icmp sgt i64 %.01113.unr, %9
  %10 = add nsw i64 %.01014.unr, 1
  %spec.select = select i1 %.not.epil, i64 %.01014.unr, i64 %10
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %2
  %.010.lcssa = phi i64 [ 1, %2 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %spec.select, %.lr.ph.epil ]
  ret i64 %.010.lcssa

.lr.ph:                                           ; preds = %24, %.lr.ph.preheader.new
  %.015 = phi i64 [ 1, %.lr.ph.preheader.new ], [ %25, %24 ]
  %.01014 = phi i64 [ 1, %.lr.ph.preheader.new ], [ %.1.1, %24 ]
  %.01113 = phi i64 [ %5, %.lr.ph.preheader.new ], [ %.112.1, %24 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %24 ]
  %11 = getelementptr inbounds %"struct.std::pair", ptr %0, i64 %.015
  %12 = load i64, ptr %11, align 8, !tbaa !10
  %.not = icmp sgt i64 %.01113, %12
  br i1 %.not, label %.lr.ph.1, label %13

13:                                               ; preds = %.lr.ph
  %14 = add nsw i64 %.01014, 1
  %15 = getelementptr inbounds i8, ptr %11, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !5
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %13
  %.112 = phi i64 [ %16, %13 ], [ %.01113, %.lr.ph ]
  %.1 = phi i64 [ %14, %13 ], [ %.01014, %.lr.ph ]
  %17 = getelementptr inbounds %"struct.std::pair", ptr %0, i64 %.015
  %18 = getelementptr inbounds i8, ptr %17, i64 16
  %19 = load i64, ptr %18, align 8, !tbaa !10
  %.not.1 = icmp sgt i64 %.112, %19
  br i1 %.not.1, label %24, label %20

20:                                               ; preds = %.lr.ph.1
  %21 = add nsw i64 %.1, 1
  %22 = getelementptr inbounds i8, ptr %17, i64 24
  %23 = load i64, ptr %22, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %20, %.lr.ph.1
  %.112.1 = phi i64 [ %23, %20 ], [ %.112, %.lr.ph.1 ]
  %.1.1 = phi i64 [ %21, %20 ], [ %.1, %.lr.ph.1 ]
  %25 = add nuw nsw i64 %.015, 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 %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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, !7, i64 8}
!6 = !{!"_ZTSSt4pairIxxE", !7, i64 0, !7, i64 8}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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