; LLVM IR from tangjz/acm-icpc :: codeforces/complete/1034D.ll (Z4calci)

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

@n = external dso_local global i32, align 4
@etot = external dso_local global i32, align 4
@eve = external dso_local global [1200005 x %struct.Event], align 16
@_ZZ4calciE2dt = external hidden global [300001 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calci(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !9
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = mul i64 %9, 4
  call void @llvm.memset.p0.i64(ptr align 4 getelementptr inbounds (i32, ptr @_ZZ4calciE2dt, i64 1), i8 0, i64 %10, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 1, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %92, %1
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = load i32, ptr @n, align 4, !tbaa !5
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  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
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %95

16:                                               ; preds = %11
  br label %17

17:                                               ; preds = %62, %16
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = load i32, ptr @etot, align 4, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %21, label %29

21:                                               ; preds = %17
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1200005 x %struct.Event], ptr @eve, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.Event, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = icmp eq i32 %26, %27
  br label %29

29:                                               ; preds = %21, %17
  %30 = phi i1 [ false, %17 ], [ %28, %21 ]
  br i1 %30, label %31, label %65

31:                                               ; preds = %29
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [1200005 x %struct.Event], ptr @eve, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.Event, ptr %34, i32 0, i32 2
  %36 = load i32, ptr %35, align 4, !tbaa !13
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [1200005 x %struct.Event], ptr @eve, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.Event, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %40, align 4, !tbaa !14
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [300001 x i32], ptr @_ZZ4calciE2dt, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = add nsw i32 %44, %36
  store i32 %45, ptr %43, align 4, !tbaa !5
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [1200005 x %struct.Event], ptr @eve, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.Event, ptr %49, i32 0, i32 0
  %51 = load i32, ptr %50, align 4, !tbaa !14
  %52 = icmp sle i32 %46, %51
  br i1 %52, label %53, label %61

53:                                               ; preds = %31
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [1200005 x %struct.Event], ptr @eve, i64 0, i64 %55
  %57 = getelementptr inbounds %struct.Event, ptr %56, i32 0, i32 2
  %58 = load i32, ptr %57, align 4, !tbaa !13
  %59 = load i32, ptr %7, align 4, !tbaa !5
  %60 = add nsw i32 %59, %58
  store i32 %60, ptr %7, align 4, !tbaa !5
  br label %61

61:                                               ; preds = %53, %31
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %6, align 4, !tbaa !5
  br label %17, !llvm.loop !15

65:                                               ; preds = %29
  br label %66

66:                                               ; preds = %83, %65
  %67 = load i32, ptr %4, align 4, !tbaa !5
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = icmp sle i32 %67, %68
  br i1 %69, label %70, label %74

70:                                               ; preds = %66
  %71 = load i32, ptr %7, align 4, !tbaa !5
  %72 = load i32, ptr %2, align 4, !tbaa !5
  %73 = icmp sge i32 %71, %72
  br label %74

74:                                               ; preds = %70, %66
  %75 = phi i1 [ false, %66 ], [ %73, %70 ]
  br i1 %75, label %76, label %86

76:                                               ; preds = %74
  %77 = load i32, ptr %4, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [300001 x i32], ptr @_ZZ4calciE2dt, i64 0, i64 %78
  %80 = load i32, ptr %79, align 4, !tbaa !5
  %81 = load i32, ptr %7, align 4, !tbaa !5
  %82 = sub nsw i32 %81, %80
  store i32 %82, ptr %7, align 4, !tbaa !5
  br label %83

83:                                               ; preds = %76
  %84 = load i32, ptr %4, align 4, !tbaa !5
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %4, align 4, !tbaa !5
  br label %66, !llvm.loop !17

86:                                               ; preds = %74
  %87 = load i32, ptr %4, align 4, !tbaa !5
  %88 = sub nsw i32 %87, 1
  %89 = sext i32 %88 to i64
  %90 = load i64, ptr %3, align 8, !tbaa !9
  %91 = add nsw i64 %90, %89
  store i64 %91, ptr %3, align 8, !tbaa !9
  br label %92

92:                                               ; preds = %86
  %93 = load i32, ptr %5, align 4, !tbaa !5
  %94 = add nsw i32 %93, 1
  store i32 %94, ptr %5, align 4, !tbaa !5
  br label %11, !llvm.loop !18

95:                                               ; preds = %15
  %96 = load i64, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i64 %96
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !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, !6, i64 4}
!12 = !{!"_ZTS5Event", !6, i64 0, !6, i64 4, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !6, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}
!18 = distinct !{!18, !16}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@etot = external dso_local local_unnamed_addr global i32, align 4
@eve = external dso_local local_unnamed_addr global [1200005 x %struct.Event], align 16
@_ZZ4calciE2dt = external hidden local_unnamed_addr global [300001 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z4calci(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = shl nsw i64 %3, 2
  tail call void @llvm.memset.p0.i64(ptr nonnull align 4 getelementptr inbounds (i8, ptr @_ZZ4calciE2dt, i64 4), i8 0, i64 %4, i1 false)
  %.not40 = icmp slt i32 %2, 1
  br i1 %.not40, label %._crit_edge46, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %1
  %5 = load i32, ptr @etot, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = add nuw i32 %2, 1
  %wide.trip.count = zext i32 %7 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %indvars.iv54 = phi i64 [ 1, %.preheader.lr.ph ], [ %indvars.iv.next55, %._crit_edge ]
  %.045 = phi i32 [ 0, %.preheader.lr.ph ], [ %.3.lcssa, %._crit_edge ]
  %.02244 = phi i32 [ 0, %.preheader.lr.ph ], [ %.123.lcssa, %._crit_edge ]
  %.02542 = phi i32 [ 1, %.preheader.lr.ph ], [ %.126.lcssa, %._crit_edge ]
  %.02741 = phi i64 [ 0, %.preheader.lr.ph ], [ %38, %._crit_edge ]
  %8 = icmp slt i32 %.02244, %5
  br i1 %8, label %.lr.ph.preheader, label %.critedge

.lr.ph.preheader:                                 ; preds = %.preheader
  %9 = sext i32 %.02244 to i64
  br label %.lr.ph

._crit_edge46:                                    ; preds = %._crit_edge, %1
  %.027.lcssa = phi i64 [ 0, %1 ], [ %38, %._crit_edge ]
  ret i64 %.027.lcssa

.lr.ph:                                           ; preds = %.lr.ph.preheader, %15
  %indvars.iv = phi i64 [ %9, %.lr.ph.preheader ], [ %indvars.iv.next, %15 ]
  %.130 = phi i32 [ %.045, %.lr.ph.preheader ], [ %spec.select, %15 ]
  %10 = getelementptr inbounds [1200005 x %struct.Event], ptr @eve, i64 0, i64 %indvars.iv
  %11 = getelementptr inbounds i8, ptr %10, i64 4
  %12 = load i32, ptr %11, align 4, !tbaa !9
  %13 = zext i32 %12 to i64
  %14 = icmp eq i64 %indvars.iv54, %13
  br i1 %14, label %15, label %.critedge.loopexit.split.loop.exit59

15:                                               ; preds = %.lr.ph
  %16 = getelementptr inbounds i8, ptr %10, i64 8
  %17 = load i32, ptr %16, align 4, !tbaa !11
  %18 = load i32, ptr %10, align 4, !tbaa !12
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [300001 x i32], ptr @_ZZ4calciE2dt, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = add nsw i32 %21, %17
  store i32 %22, ptr %20, align 4, !tbaa !5
  %.not28 = icmp sgt i32 %.02542, %18
  %23 = select i1 %.not28, i32 0, i32 %17
  %spec.select = add nsw i32 %23, %.130
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %6
  br i1 %exitcond.not, label %.critedge, label %.lr.ph, !llvm.loop !13

.critedge.loopexit.split.loop.exit59:             ; preds = %.lr.ph
  %24 = trunc nsw i64 %indvars.iv to i32
  br label %.critedge

.critedge:                                        ; preds = %15, %.critedge.loopexit.split.loop.exit59, %.preheader
  %.123.lcssa = phi i32 [ %.02244, %.preheader ], [ %24, %.critedge.loopexit.split.loop.exit59 ], [ %5, %15 ]
  %.1.lcssa = phi i32 [ %.045, %.preheader ], [ %.130, %.critedge.loopexit.split.loop.exit59 ], [ %spec.select, %15 ]
  %25 = sext i32 %.02542 to i64
  %26 = icmp sge i64 %indvars.iv54, %25
  %27 = icmp sge i32 %.1.lcssa, %0
  %28 = select i1 %26, i1 %27, i1 false
  br i1 %28, label %.lr.ph37, label %._crit_edge

.lr.ph37:                                         ; preds = %.critedge, %.lr.ph37
  %indvars.iv51 = phi i64 [ %indvars.iv.next52, %.lr.ph37 ], [ %25, %.critedge ]
  %.336 = phi i32 [ %31, %.lr.ph37 ], [ %.1.lcssa, %.critedge ]
  %29 = getelementptr inbounds [300001 x i32], ptr @_ZZ4calciE2dt, i64 0, i64 %indvars.iv51
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = sub nsw i32 %.336, %30
  %indvars.iv.next52 = add nsw i64 %indvars.iv51, 1
  %32 = icmp slt i64 %indvars.iv51, %indvars.iv54
  %33 = icmp sge i32 %31, %0
  %34 = select i1 %32, i1 %33, i1 false
  br i1 %34, label %.lr.ph37, label %._crit_edge.loopexit, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph37
  %35 = trunc nsw i64 %indvars.iv.next52 to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.critedge
  %.126.lcssa = phi i32 [ %.02542, %.critedge ], [ %35, %._crit_edge.loopexit ]
  %.3.lcssa = phi i32 [ %.1.lcssa, %.critedge ], [ %31, %._crit_edge.loopexit ]
  %36 = add nsw i32 %.126.lcssa, -1
  %37 = sext i32 %36 to i64
  %38 = add nsw i64 %.02741, %37
  %indvars.iv.next55 = add nuw nsw i64 %indvars.iv54, 1
  %exitcond57.not = icmp eq i64 %indvars.iv.next55, %wide.trip.count
  br i1 %exitcond57.not, label %._crit_edge46, label %.preheader, !llvm.loop !16
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

!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++ TBAA"}
!9 = !{!10, !6, i64 4}
!10 = !{!"_ZTS5Event", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = !{!10, !6, i64 8}
!12 = !{!10, !6, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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