; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 18. Greedy (Activity_Selection_Greedy_algo)

; 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.Activity = type { i32, i32 }

@count = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [30 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [9 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @ActivitySelection(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %87, %2
  %10 = load i32, ptr %5, align 4, !tbaa !9
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %90

13:                                               ; preds = %9
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %6, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %83, %13
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = load i32, ptr %4, align 4, !tbaa !9
  %19 = sub nsw i32 %18, 1
  %20 = icmp slt i32 %17, %19
  br i1 %20, label %21, label %86

21:                                               ; preds = %16
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds %struct.Activity, ptr %22, i64 %24
  %26 = getelementptr inbounds %struct.Activity, ptr %25, i32 0, i32 1
  %27 = load i32, ptr %26, align 4, !tbaa !11
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds %struct.Activity, ptr %28, i64 %30
  %32 = getelementptr inbounds %struct.Activity, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !11
  %34 = icmp sgt i32 %27, %33
  br i1 %34, label %35, label %82

35:                                               ; preds = %21
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds %struct.Activity, ptr %36, i64 %38
  %40 = getelementptr inbounds %struct.Activity, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !11
  store i32 %41, ptr %7, align 4, !tbaa !9
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds %struct.Activity, ptr %42, i64 %44
  %46 = getelementptr inbounds %struct.Activity, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 4, !tbaa !13
  store i32 %47, ptr %8, align 4, !tbaa !9
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds %struct.Activity, ptr %48, i64 %50
  %52 = getelementptr inbounds %struct.Activity, ptr %51, i32 0, i32 1
  %53 = load i32, ptr %52, align 4, !tbaa !11
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds %struct.Activity, ptr %54, i64 %56
  %58 = getelementptr inbounds %struct.Activity, ptr %57, i32 0, i32 1
  store i32 %53, ptr %58, align 4, !tbaa !11
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = load i32, ptr %6, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds %struct.Activity, ptr %59, i64 %61
  %63 = getelementptr inbounds %struct.Activity, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %63, align 4, !tbaa !13
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = load i32, ptr %5, align 4, !tbaa !9
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds %struct.Activity, ptr %65, i64 %67
  %69 = getelementptr inbounds %struct.Activity, ptr %68, i32 0, i32 0
  store i32 %64, ptr %69, align 4, !tbaa !13
  %70 = load i32, ptr %7, align 4, !tbaa !9
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = load i32, ptr %6, align 4, !tbaa !9
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds %struct.Activity, ptr %71, i64 %73
  %75 = getelementptr inbounds %struct.Activity, ptr %74, i32 0, i32 1
  store i32 %70, ptr %75, align 4, !tbaa !11
  %76 = load i32, ptr %8, align 4, !tbaa !9
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = load i32, ptr %6, align 4, !tbaa !9
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds %struct.Activity, ptr %77, i64 %79
  %81 = getelementptr inbounds %struct.Activity, ptr %80, i32 0, i32 0
  store i32 %76, ptr %81, align 4, !tbaa !13
  br label %82

82:                                               ; preds = %35, %21
  br label %83

83:                                               ; preds = %82
  %84 = load i32, ptr %6, align 4, !tbaa !9
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %6, align 4, !tbaa !9
  br label %16, !llvm.loop !14

86:                                               ; preds = %16
  br label %87

87:                                               ; preds = %86
  %88 = load i32, ptr %5, align 4, !tbaa !9
  %89 = add nsw i32 %88, 1
  store i32 %89, ptr %5, align 4, !tbaa !9
  br label %9, !llvm.loop !16

90:                                               ; preds = %9
  store i32 0, ptr %5, align 4, !tbaa !9
  %91 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %92 = load ptr, ptr %3, align 8, !tbaa !5
  %93 = load i32, ptr %5, align 4, !tbaa !9
  %94 = sext i32 %93 to i64
  %95 = getelementptr inbounds %struct.Activity, ptr %92, i64 %94
  %96 = getelementptr inbounds %struct.Activity, ptr %95, i32 0, i32 0
  %97 = load i32, ptr %96, align 4, !tbaa !13
  %98 = load ptr, ptr %3, align 8, !tbaa !5
  %99 = load i32, ptr %5, align 4, !tbaa !9
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds %struct.Activity, ptr %98, i64 %100
  %102 = getelementptr inbounds %struct.Activity, ptr %101, i32 0, i32 1
  %103 = load i32, ptr %102, align 4, !tbaa !11
  %104 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %97, i32 noundef %103)
  %105 = load i32, ptr @count, align 4, !tbaa !9
  %106 = add nsw i32 %105, 1
  store i32 %106, ptr @count, align 4, !tbaa !9
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %107

107:                                              ; preds = %143, %90
  %108 = load i32, ptr %6, align 4, !tbaa !9
  %109 = load i32, ptr %4, align 4, !tbaa !9
  %110 = icmp slt i32 %108, %109
  br i1 %110, label %111, label %146

111:                                              ; preds = %107
  %112 = load ptr, ptr %3, align 8, !tbaa !5
  %113 = load i32, ptr %6, align 4, !tbaa !9
  %114 = sext i32 %113 to i64
  %115 = getelementptr inbounds %struct.Activity, ptr %112, i64 %114
  %116 = getelementptr inbounds %struct.Activity, ptr %115, i32 0, i32 0
  %117 = load i32, ptr %116, align 4, !tbaa !13
  %118 = load ptr, ptr %3, align 8, !tbaa !5
  %119 = load i32, ptr %5, align 4, !tbaa !9
  %120 = sext i32 %119 to i64
  %121 = getelementptr inbounds %struct.Activity, ptr %118, i64 %120
  %122 = getelementptr inbounds %struct.Activity, ptr %121, i32 0, i32 1
  %123 = load i32, ptr %122, align 4, !tbaa !11
  %124 = icmp sge i32 %117, %123
  br i1 %124, label %125, label %142

125:                                              ; preds = %111
  %126 = load i32, ptr @count, align 4, !tbaa !9
  %127 = add nsw i32 %126, 1
  store i32 %127, ptr @count, align 4, !tbaa !9
  %128 = load ptr, ptr %3, align 8, !tbaa !5
  %129 = load i32, ptr %6, align 4, !tbaa !9
  %130 = sext i32 %129 to i64
  %131 = getelementptr inbounds %struct.Activity, ptr %128, i64 %130
  %132 = getelementptr inbounds %struct.Activity, ptr %131, i32 0, i32 0
  %133 = load i32, ptr %132, align 4, !tbaa !13
  %134 = load ptr, ptr %3, align 8, !tbaa !5
  %135 = load i32, ptr %6, align 4, !tbaa !9
  %136 = sext i32 %135 to i64
  %137 = getelementptr inbounds %struct.Activity, ptr %134, i64 %136
  %138 = getelementptr inbounds %struct.Activity, ptr %137, i32 0, i32 1
  %139 = load i32, ptr %138, align 4, !tbaa !11
  %140 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %133, i32 noundef %139)
  %141 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %141, ptr %5, align 4, !tbaa !9
  br label %142

142:                                              ; preds = %125, %111
  br label %143

143:                                              ; preds = %142
  %144 = load i32, ptr %6, align 4, !tbaa !9
  %145 = add nsw i32 %144, 1
  store i32 %145, ptr %6, align 4, !tbaa !9
  br label %107, !llvm.loop !17

146:                                              ; preds = %107
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

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

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

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

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


; 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.Activity = type { i32, i32 }

@count = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [30 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [9 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local void @ActivitySelection(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph52, label %._crit_edge

.lr.ph52:                                         ; preds = %2
  %4 = add nsw i32 %1, -1
  %5 = zext nneg i32 %4 to i64
  %wide.trip.count64 = zext nneg i32 %1 to i64
  %wide.trip.count = zext i32 %4 to i64
  br label %6

.loopexit:                                        ; preds = %19, %6
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond65.not = icmp eq i64 %indvars.iv.next62, %wide.trip.count64
  br i1 %exitcond65.not, label %._crit_edge, label %6, !llvm.loop !5

6:                                                ; preds = %.lr.ph52, %.loopexit
  %indvars.iv61 = phi i64 [ 0, %.lr.ph52 ], [ %indvars.iv.next62, %.loopexit ]
  %indvars.iv = phi i64 [ 1, %.lr.ph52 ], [ %indvars.iv.next, %.loopexit ]
  %indvars.iv.next62 = add nuw nsw i64 %indvars.iv61, 1
  %7 = icmp ult i64 %indvars.iv.next62, %5
  br i1 %7, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %6
  %8 = getelementptr inbounds %struct.Activity, ptr %0, i64 %indvars.iv61
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  br label %10

10:                                               ; preds = %.lr.ph, %19
  %indvars.iv58 = phi i64 [ %indvars.iv, %.lr.ph ], [ %indvars.iv.next59, %19 ]
  %11 = load i32, ptr %9, align 4, !tbaa !7
  %12 = getelementptr inbounds %struct.Activity, ptr %0, i64 %indvars.iv58
  %13 = getelementptr inbounds i8, ptr %12, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !7
  %15 = icmp sgt i32 %11, %14
  br i1 %15, label %16, label %19

16:                                               ; preds = %10
  %17 = load i32, ptr %8, align 4, !tbaa !12
  store i32 %14, ptr %9, align 4, !tbaa !7
  %18 = load i32, ptr %12, align 4, !tbaa !12
  store i32 %18, ptr %8, align 4, !tbaa !12
  store i32 %11, ptr %13, align 4, !tbaa !7
  store i32 %17, ptr %12, align 4, !tbaa !12
  br label %19

19:                                               ; preds = %10, %16
  %indvars.iv.next59 = add nuw nsw i64 %indvars.iv58, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next59, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit, label %10, !llvm.loop !13

._crit_edge:                                      ; preds = %.loopexit, %2
  %20 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %21 = load i32, ptr %0, align 4, !tbaa !12
  %22 = getelementptr inbounds i8, ptr %0, i64 4
  %23 = load i32, ptr %22, align 4, !tbaa !7
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %21, i32 noundef %23)
  %25 = load i32, ptr @count, align 4, !tbaa !14
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr @count, align 4, !tbaa !14
  %27 = icmp sgt i32 %1, 1
  br i1 %27, label %.lr.ph56.preheader, label %._crit_edge57

.lr.ph56.preheader:                               ; preds = %._crit_edge
  %wide.trip.count69 = zext nneg i32 %1 to i64
  br label %.lr.ph56

.lr.ph56:                                         ; preds = %.lr.ph56.preheader, %41
  %indvars.iv66 = phi i64 [ 1, %.lr.ph56.preheader ], [ %indvars.iv.next67, %41 ]
  %.154 = phi i32 [ 0, %.lr.ph56.preheader ], [ %.2, %41 ]
  %28 = getelementptr inbounds %struct.Activity, ptr %0, i64 %indvars.iv66
  %29 = load i32, ptr %28, align 4, !tbaa !12
  %30 = sext i32 %.154 to i64
  %31 = getelementptr inbounds %struct.Activity, ptr %0, i64 %30, i32 1
  %32 = load i32, ptr %31, align 4, !tbaa !7
  %.not = icmp slt i32 %29, %32
  br i1 %.not, label %41, label %33

33:                                               ; preds = %.lr.ph56
  %34 = load i32, ptr @count, align 4, !tbaa !14
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr @count, align 4, !tbaa !14
  %36 = load i32, ptr %28, align 4, !tbaa !12
  %37 = getelementptr inbounds i8, ptr %28, i64 4
  %38 = load i32, ptr %37, align 4, !tbaa !7
  %39 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %36, i32 noundef %38)
  %40 = trunc nuw nsw i64 %indvars.iv66 to i32
  br label %41

41:                                               ; preds = %.lr.ph56, %33
  %.2 = phi i32 [ %40, %33 ], [ %.154, %.lr.ph56 ]
  %indvars.iv.next67 = add nuw nsw i64 %indvars.iv66, 1
  %exitcond70.not = icmp eq i64 %indvars.iv.next67, %wide.trip.count69
  br i1 %exitcond70.not, label %._crit_edge57, label %.lr.ph56, !llvm.loop !15

._crit_edge57:                                    ; preds = %41, %._crit_edge
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { nofree 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 = { nofree 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" }

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


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