; LLVM IR from pranjalwalia/Competitive-programming :: Hackerrank/language/C (Post Transition)

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

; 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: 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) #1

; Function Attrs: nounwind uwtable
define dso_local void @town_with_most_packages(ptr dead_on_unwind noalias writable sret(%struct.town) align 8 %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %1, ptr %4, align 8, !tbaa !5
  store i32 %2, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 -1, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  br label %10

10:                                               ; preds = %49, %3
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = add nsw i32 %11, -1
  store i32 %12, ptr %5, align 4, !tbaa !9
  %13 = icmp sgt i32 %12, -1
  br i1 %13, label %14, label %50

14:                                               ; preds = %10
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %39, %14
  %16 = load i32, ptr %9, align 4, !tbaa !9
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds %struct.town, ptr %17, i64 %19
  %21 = getelementptr inbounds %struct.town, ptr %20, i32 0, i32 2
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = icmp slt i32 %16, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %42

25:                                               ; preds = %15
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds %struct.town, ptr %26, i64 %28
  %30 = getelementptr inbounds %struct.town, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  %32 = load i32, ptr %9, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.post_office, ptr %31, i64 %33
  %35 = getelementptr inbounds %struct.post_office, ptr %34, i32 0, i32 3
  %36 = load i32, ptr %35, align 8, !tbaa !14
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = add nsw i32 %37, %36
  store i32 %38, ptr %7, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %25
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %9, align 4, !tbaa !9
  br label %15, !llvm.loop !16

42:                                               ; preds = %24
  %43 = load i32, ptr %7, align 4, !tbaa !9
  %44 = load i32, ptr %6, align 4, !tbaa !9
  %45 = icmp sgt i32 %43, %44
  br i1 %45, label %46, label %49

46:                                               ; preds = %42
  %47 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %47, ptr %6, align 4, !tbaa !9
  %48 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %48, ptr %8, align 4, !tbaa !9
  br label %49

49:                                               ; preds = %46, %42
  br label %10, !llvm.loop !18

50:                                               ; preds = %10
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds %struct.town, ptr %51, i64 %53
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 8 %54, i64 24, i1 false), !tbaa.struct !19
  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
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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 16}
!12 = !{!"town", !6, i64 0, !6, i64 8, !10, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !10, i64 16}
!15 = !{!"post_office", !10, i64 0, !10, i64 4, !6, i64 8, !10, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = !{i64 0, i64 8, !5, i64 8, i64 8, !5, i64 16, i64 4, !9}


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

; Function Attrs: mustprogress 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) #0

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @town_with_most_packages(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.town) align 8 %0, ptr nocapture noundef readonly %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = icmp sgt i32 %2, 0
  br i1 %4, label %.preheader.preheader, label %._crit_edge25

.preheader.preheader:                             ; preds = %3
  %5 = zext nneg i32 %2 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %indvars.iv28 = phi i64 [ %5, %.preheader.preheader ], [ %indvars.iv.next29, %._crit_edge ]
  %.01424 = phi i32 [ undef, %.preheader.preheader ], [ %spec.select20, %._crit_edge ]
  %.01623 = phi i32 [ -1, %.preheader.preheader ], [ %spec.select, %._crit_edge ]
  %indvars.iv.next29 = add nsw i64 %indvars.iv28, -1
  %6 = getelementptr inbounds %struct.town, ptr %1, i64 %indvars.iv.next29
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %10 = getelementptr inbounds i8, ptr %6, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %wide.trip.count = zext nneg i32 %8 to i64
  %min.iters.check = icmp ult i32 %8, 9
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  %.01521.ph = phi i32 [ 0, %.lr.ph ], [ %48, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.mod.vf = and i64 %wide.trip.count, 7
  %12 = icmp eq i64 %n.mod.vf, 0
  %13 = select i1 %12, i64 8, i64 %n.mod.vf
  %n.vec = sub nsw i64 %wide.trip.count, %13
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %45, %vector.body ]
  %vec.phi31 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %46, %vector.body ]
  %14 = or disjoint i64 %index, 1
  %15 = or disjoint i64 %index, 2
  %16 = or disjoint i64 %index, 3
  %17 = or disjoint i64 %index, 4
  %18 = or disjoint i64 %index, 5
  %19 = or disjoint i64 %index, 6
  %20 = or disjoint i64 %index, 7
  %21 = getelementptr inbounds %struct.post_office, ptr %11, i64 %index, i32 3
  %22 = getelementptr inbounds %struct.post_office, ptr %11, i64 %14, i32 3
  %23 = getelementptr inbounds %struct.post_office, ptr %11, i64 %15, i32 3
  %24 = getelementptr inbounds %struct.post_office, ptr %11, i64 %16, i32 3
  %25 = getelementptr inbounds %struct.post_office, ptr %11, i64 %17, i32 3
  %26 = getelementptr inbounds %struct.post_office, ptr %11, i64 %18, i32 3
  %27 = getelementptr inbounds %struct.post_office, ptr %11, i64 %19, i32 3
  %28 = getelementptr inbounds %struct.post_office, ptr %11, i64 %20, i32 3
  %29 = load i32, ptr %21, align 8, !tbaa !12
  %30 = load i32, ptr %22, align 8, !tbaa !12
  %31 = load i32, ptr %23, align 8, !tbaa !12
  %32 = load i32, ptr %24, align 8, !tbaa !12
  %33 = insertelement <4 x i32> poison, i32 %29, i64 0
  %34 = insertelement <4 x i32> %33, i32 %30, i64 1
  %35 = insertelement <4 x i32> %34, i32 %31, i64 2
  %36 = insertelement <4 x i32> %35, i32 %32, i64 3
  %37 = load i32, ptr %25, align 8, !tbaa !12
  %38 = load i32, ptr %26, align 8, !tbaa !12
  %39 = load i32, ptr %27, align 8, !tbaa !12
  %40 = load i32, ptr %28, align 8, !tbaa !12
  %41 = insertelement <4 x i32> poison, i32 %37, i64 0
  %42 = insertelement <4 x i32> %41, i32 %38, i64 1
  %43 = insertelement <4 x i32> %42, i32 %39, i64 2
  %44 = insertelement <4 x i32> %43, i32 %40, i64 3
  %45 = add <4 x i32> %36, %vec.phi
  %46 = add <4 x i32> %44, %vec.phi31
  %index.next = add nuw i64 %index, 8
  %47 = icmp eq i64 %index.next, %n.vec
  br i1 %47, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %46, %45
  %48 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  br label %scalar.ph.preheader

._crit_edge:                                      ; preds = %scalar.ph, %.preheader
  %.015.lcssa = phi i32 [ 0, %.preheader ], [ %54, %scalar.ph ]
  %49 = icmp sgt i32 %.015.lcssa, %.01623
  %spec.select = tail call i32 @llvm.smax.i32(i32 %.015.lcssa, i32 %.01623)
  %50 = trunc nuw nsw i64 %indvars.iv.next29 to i32
  %spec.select20 = select i1 %49, i32 %50, i32 %.01424
  %51 = icmp sgt i64 %indvars.iv28, 1
  br i1 %51, label %.preheader, label %._crit_edge25.loopexit, !llvm.loop !18

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %.01521 = phi i32 [ %54, %scalar.ph ], [ %.01521.ph, %scalar.ph.preheader ]
  %52 = getelementptr inbounds %struct.post_office, ptr %11, i64 %indvars.iv, i32 3
  %53 = load i32, ptr %52, align 8, !tbaa !12
  %54 = add nsw i32 %53, %.01521
  %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 %scalar.ph, !llvm.loop !19

._crit_edge25.loopexit:                           ; preds = %._crit_edge
  %55 = sext i32 %spec.select20 to i64
  br label %._crit_edge25

._crit_edge25:                                    ; preds = %._crit_edge25.loopexit, %3
  %.014.lcssa = phi i64 [ 0, %3 ], [ %55, %._crit_edge25.loopexit ]
  %56 = getelementptr inbounds %struct.town, ptr %1, i64 %.014.lcssa
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef nonnull align 8 dereferenceable(24) %56, i64 24, i1 false), !tbaa.struct !20
  ret void
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, 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 #2 = { 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, !10, i64 16}
!6 = !{!"town", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!13, !10, i64 16}
!13 = !{!"post_office", !10, i64 0, !10, i64 4, !7, i64 8, !10, i64 16}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}
!18 = distinct !{!18, !15}
!19 = distinct !{!19, !15, !17, !16}
!20 = !{i64 0, i64 8, !21, i64 8, i64 8, !21, i64 16, i64 4, !22}
!21 = !{!7, !7, i64 0}
!22 = !{!10, !10, i64 0}


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