; LLVM IR from Hunterdii/GeeksforGeeks-POTD :: December 2024 GFG SOLUTION (December-09)

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

; Function Attrs: nounwind uwtable
define dso_local ptr @insertInterval(ptr noundef %0, i32 noundef %1, i64 %2, ptr noundef %3) #0 {
  %5 = alloca %struct.Interval, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i64 %2, ptr %5, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = add nsw i32 %12, 1
  %14 = sext i32 %13 to i64
  %15 = mul i64 %14, 8
  %16 = call noalias ptr @malloc(i64 noundef %15) #5
  store ptr %16, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %109, %4
  %18 = load i32, ptr %11, align 4, !tbaa !9
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %112

22:                                               ; preds = %17
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load i32, ptr %11, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds %struct.Interval, ptr %23, i64 %25
  %27 = getelementptr inbounds %struct.Interval, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !11
  %29 = getelementptr inbounds %struct.Interval, ptr %5, i32 0, i32 0
  %30 = load i32, ptr %29, align 4, !tbaa !13
  %31 = icmp slt i32 %28, %30
  br i1 %31, label %32, label %42

32:                                               ; preds = %22
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = load i32, ptr %10, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %10, align 4, !tbaa !9
  %36 = sext i32 %34 to i64
  %37 = getelementptr inbounds %struct.Interval, ptr %33, i64 %36
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = load i32, ptr %11, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds %struct.Interval, ptr %38, i64 %40
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %37, ptr align 4 %41, i64 8, i1 false), !tbaa.struct !14
  br label %108

42:                                               ; preds = %22
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = load i32, ptr %11, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds %struct.Interval, ptr %43, i64 %45
  %47 = getelementptr inbounds %struct.Interval, ptr %46, i32 0, i32 0
  %48 = load i32, ptr %47, align 4, !tbaa !13
  %49 = getelementptr inbounds %struct.Interval, ptr %5, i32 0, i32 1
  %50 = load i32, ptr %49, align 4, !tbaa !11
  %51 = icmp sle i32 %48, %50
  br i1 %51, label %52, label %97

52:                                               ; preds = %42
  %53 = getelementptr inbounds %struct.Interval, ptr %5, i32 0, i32 0
  %54 = load i32, ptr %53, align 4, !tbaa !13
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load i32, ptr %11, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds %struct.Interval, ptr %55, i64 %57
  %59 = getelementptr inbounds %struct.Interval, ptr %58, i32 0, i32 0
  %60 = load i32, ptr %59, align 4, !tbaa !13
  %61 = icmp slt i32 %54, %60
  br i1 %61, label %62, label %65

62:                                               ; preds = %52
  %63 = getelementptr inbounds %struct.Interval, ptr %5, i32 0, i32 0
  %64 = load i32, ptr %63, align 4, !tbaa !13
  br label %72

65:                                               ; preds = %52
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = load i32, ptr %11, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds %struct.Interval, ptr %66, i64 %68
  %70 = getelementptr inbounds %struct.Interval, ptr %69, i32 0, i32 0
  %71 = load i32, ptr %70, align 4, !tbaa !13
  br label %72

72:                                               ; preds = %65, %62
  %73 = phi i32 [ %64, %62 ], [ %71, %65 ]
  %74 = getelementptr inbounds %struct.Interval, ptr %5, i32 0, i32 0
  store i32 %73, ptr %74, align 4, !tbaa !13
  %75 = getelementptr inbounds %struct.Interval, ptr %5, i32 0, i32 1
  %76 = load i32, ptr %75, align 4, !tbaa !11
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = load i32, ptr %11, align 4, !tbaa !9
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds %struct.Interval, ptr %77, i64 %79
  %81 = getelementptr inbounds %struct.Interval, ptr %80, i32 0, i32 1
  %82 = load i32, ptr %81, align 4, !tbaa !11
  %83 = icmp sgt i32 %76, %82
  br i1 %83, label %84, label %87

84:                                               ; preds = %72
  %85 = getelementptr inbounds %struct.Interval, ptr %5, i32 0, i32 1
  %86 = load i32, ptr %85, align 4, !tbaa !11
  br label %94

87:                                               ; preds = %72
  %88 = load ptr, ptr %6, align 8, !tbaa !5
  %89 = load i32, ptr %11, align 4, !tbaa !9
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds %struct.Interval, ptr %88, i64 %90
  %92 = getelementptr inbounds %struct.Interval, ptr %91, i32 0, i32 1
  %93 = load i32, ptr %92, align 4, !tbaa !11
  br label %94

94:                                               ; preds = %87, %84
  %95 = phi i32 [ %86, %84 ], [ %93, %87 ]
  %96 = getelementptr inbounds %struct.Interval, ptr %5, i32 0, i32 1
  store i32 %95, ptr %96, align 4, !tbaa !11
  br label %107

97:                                               ; preds = %42
  %98 = load ptr, ptr %9, align 8, !tbaa !5
  %99 = load i32, ptr %10, align 4, !tbaa !9
  %100 = add nsw i32 %99, 1
  store i32 %100, ptr %10, align 4, !tbaa !9
  %101 = sext i32 %99 to i64
  %102 = getelementptr inbounds %struct.Interval, ptr %98, i64 %101
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %102, ptr align 4 %5, i64 8, i1 false), !tbaa.struct !14
  %103 = load ptr, ptr %6, align 8, !tbaa !5
  %104 = load i32, ptr %11, align 4, !tbaa !9
  %105 = sext i32 %104 to i64
  %106 = getelementptr inbounds %struct.Interval, ptr %103, i64 %105
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %5, ptr align 4 %106, i64 8, i1 false), !tbaa.struct !14
  br label %107

107:                                              ; preds = %97, %94
  br label %108

108:                                              ; preds = %107, %32
  br label %109

109:                                              ; preds = %108
  %110 = load i32, ptr %11, align 4, !tbaa !9
  %111 = add nsw i32 %110, 1
  store i32 %111, ptr %11, align 4, !tbaa !9
  br label %17, !llvm.loop !15

112:                                              ; preds = %21
  %113 = load ptr, ptr %9, align 8, !tbaa !5
  %114 = load i32, ptr %10, align 4, !tbaa !9
  %115 = add nsw i32 %114, 1
  store i32 %115, ptr %10, align 4, !tbaa !9
  %116 = sext i32 %114 to i64
  %117 = getelementptr inbounds %struct.Interval, ptr %113, i64 %116
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %117, ptr align 4 %5, i64 8, i1 false), !tbaa.struct !14
  %118 = load i32, ptr %10, align 4, !tbaa !9
  %119 = load ptr, ptr %8, align 8, !tbaa !5
  store i32 %118, ptr %119, align 4, !tbaa !9
  %120 = load ptr, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret ptr %120
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; 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 = { nounwind allocsize(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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

!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 = !{!"", !10, i64 0, !10, i64 4}
!13 = !{!12, !10, i64 0}
!14 = !{i64 0, i64 4, !9, i64 4, i64 4, !9}
!15 = distinct !{!15, !16}
!16 = !{!"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.Interval = type { i32, i32 }

; Function Attrs: nofree nounwind memory(write, argmem: readwrite, inaccessiblemem: readwrite) uwtable
define dso_local noalias noundef ptr @insertInterval(ptr nocapture noundef readonly %0, i32 noundef %1, i64 %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %.sroa.0.0.extract.trunc = trunc i64 %2 to i32
  %.sroa.8.0.extract.shift = lshr i64 %2, 32
  %.sroa.8.0.extract.trunc = trunc nuw i64 %.sroa.8.0.extract.shift to i32
  %5 = add nsw i32 %1, 1
  %6 = sext i32 %5 to i64
  %7 = shl nsw i64 %6, 3
  %8 = tail call noalias ptr @malloc(i64 noundef %7) #3
  %9 = icmp sgt i32 %1, 0
  br i1 %9, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %4
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %30, %4
  %.sroa.0.0.lcssa = phi i32 [ %.sroa.0.0.extract.trunc, %4 ], [ %.sroa.0.1, %30 ]
  %.sroa.8.0.lcssa = phi i32 [ %.sroa.8.0.extract.trunc, %4 ], [ %.sroa.8.1, %30 ]
  %.038.lcssa = phi i32 [ 0, %4 ], [ %.1, %30 ]
  %10 = add nsw i32 %.038.lcssa, 1
  %11 = sext i32 %.038.lcssa to i64
  %12 = getelementptr inbounds %struct.Interval, ptr %8, i64 %11
  store i32 %.sroa.0.0.lcssa, ptr %12, align 4, !tbaa !5
  %.sroa.8.0..sroa_idx34 = getelementptr inbounds i8, ptr %12, i64 4
  store i32 %.sroa.8.0.lcssa, ptr %.sroa.8.0..sroa_idx34, align 4, !tbaa !5
  store i32 %10, ptr %3, align 4, !tbaa !5
  ret ptr %8

.lr.ph:                                           ; preds = %.lr.ph.preheader, %30
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %30 ]
  %.03843 = phi i32 [ 0, %.lr.ph.preheader ], [ %.1, %30 ]
  %.sroa.8.042 = phi i32 [ %.sroa.8.0.extract.trunc, %.lr.ph.preheader ], [ %.sroa.8.1, %30 ]
  %.sroa.0.041 = phi i32 [ %.sroa.0.0.extract.trunc, %.lr.ph.preheader ], [ %.sroa.0.1, %30 ]
  %13 = getelementptr inbounds %struct.Interval, ptr %0, i64 %indvars.iv
  %14 = getelementptr inbounds i8, ptr %13, i64 4
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp slt i32 %15, %.sroa.0.041
  br i1 %16, label %17, label %22

17:                                               ; preds = %.lr.ph
  %18 = add nsw i32 %.03843, 1
  %19 = sext i32 %.03843 to i64
  %20 = getelementptr inbounds %struct.Interval, ptr %8, i64 %19
  %21 = load i64, ptr %13, align 4
  store i64 %21, ptr %20, align 4
  br label %30

22:                                               ; preds = %.lr.ph
  %23 = load i32, ptr %13, align 4, !tbaa !5
  %.not = icmp sgt i32 %23, %.sroa.8.042
  br i1 %.not, label %26, label %24

24:                                               ; preds = %22
  %.sroa.0.0. = tail call i32 @llvm.smin.i32(i32 %.sroa.0.041, i32 %23)
  %25 = tail call i32 @llvm.smax.i32(i32 %.sroa.8.042, i32 %15)
  br label %30

26:                                               ; preds = %22
  %27 = add nsw i32 %.03843, 1
  %28 = sext i32 %.03843 to i64
  %29 = getelementptr inbounds %struct.Interval, ptr %8, i64 %28
  store i32 %.sroa.0.041, ptr %29, align 4, !tbaa !5
  %.sroa.8.0..sroa_idx = getelementptr inbounds i8, ptr %29, i64 4
  store i32 %.sroa.8.042, ptr %.sroa.8.0..sroa_idx, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %17, %26, %24
  %.sroa.0.1 = phi i32 [ %.sroa.0.041, %17 ], [ %.sroa.0.0., %24 ], [ %23, %26 ]
  %.sroa.8.1 = phi i32 [ %.sroa.8.042, %17 ], [ %25, %24 ], [ %15, %26 ]
  %.1 = phi i32 [ %18, %17 ], [ %.03843, %24 ], [ %27, %26 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

attributes #0 = { nofree nounwind memory(write, argmem: readwrite, inaccessiblemem: readwrite) 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nounwind allocsize(0) }

!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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass