; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/Codeagon 2017/Problem E/E.ll (Z15primalIterationRi)

; 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"

@matchingCount = external dso_local global i32, align 4
@match = external dso_local global [2100 x [2100 x i32]], align 16
@matchx = external dso_local global [2100 x i32], align 16
@matchy = external dso_local global [2100 x i32], align 16
@pre1 = external dso_local global [2100 x i32], align 16
@pre2 = external dso_local global [2100 x i32], align 16
@res = external dso_local global i64, align 8
@cost = external dso_local global [2100 x [2100 x i64]], align 16

; 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: mustprogress nounwind uwtable
define dso_local void @_Z15primalIterationRi(ptr noundef nonnull align 4 dereferenceable(4) %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %6, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  br label %7

7:                                                ; preds = %64, %1
  %8 = load i32, ptr %3, align 4, !tbaa !9
  %9 = icmp ne i32 %8, 0
  br i1 %9, label %10, label %65

10:                                               ; preds = %7
  %11 = load i32, ptr %3, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [2100 x i32], ptr @pre2, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %14, ptr %4, align 4, !tbaa !9
  %15 = load i32, ptr %3, align 4, !tbaa !9
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [2100 x i32], ptr @matchx, i64 0, i64 %17
  store i32 %15, ptr %18, align 4, !tbaa !9
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = load i32, ptr %3, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [2100 x i32], ptr @matchy, i64 0, i64 %21
  store i32 %19, ptr %22, align 4, !tbaa !9
  %23 = load i32, ptr %4, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [2100 x [2100 x i32]], ptr @match, i64 0, i64 %24
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [2100 x i32], ptr %25, i64 0, i64 %27
  store i32 1, ptr %28, align 4, !tbaa !9
  %29 = load i32, ptr @matchingCount, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr @matchingCount, align 4, !tbaa !9
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [2100 x [2100 x i64]], ptr @cost, i64 0, i64 %32
  %34 = load i32, ptr %3, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [2100 x i64], ptr %33, i64 0, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !11
  %38 = load i64, ptr @res, align 8, !tbaa !11
  %39 = add nsw i64 %38, %37
  store i64 %39, ptr @res, align 8, !tbaa !11
  %40 = load i32, ptr %4, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [2100 x i32], ptr @pre1, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !9
  store i32 %43, ptr %3, align 4, !tbaa !9
  %44 = load i32, ptr %3, align 4, !tbaa !9
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %64

46:                                               ; preds = %10
  %47 = load i32, ptr %4, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [2100 x [2100 x i32]], ptr @match, i64 0, i64 %48
  %50 = load i32, ptr %3, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [2100 x i32], ptr %49, i64 0, i64 %51
  store i32 0, ptr %52, align 4, !tbaa !9
  %53 = load i32, ptr @matchingCount, align 4, !tbaa !9
  %54 = add nsw i32 %53, -1
  store i32 %54, ptr @matchingCount, align 4, !tbaa !9
  %55 = load i32, ptr %4, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [2100 x [2100 x i64]], ptr @cost, i64 0, i64 %56
  %58 = load i32, ptr %3, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [2100 x i64], ptr %57, i64 0, i64 %59
  %61 = load i64, ptr %60, align 8, !tbaa !11
  %62 = load i64, ptr @res, align 8, !tbaa !11
  %63 = sub nsw i64 %62, %61
  store i64 %63, ptr @res, align 8, !tbaa !11
  br label %64

64:                                               ; preds = %46, %10
  br label %7, !llvm.loop !13

65:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@matchingCount = external dso_local local_unnamed_addr global i32, align 4
@match = external dso_local local_unnamed_addr global [2100 x [2100 x i32]], align 16
@matchx = external dso_local local_unnamed_addr global [2100 x i32], align 16
@matchy = external dso_local local_unnamed_addr global [2100 x i32], align 16
@pre1 = external dso_local local_unnamed_addr global [2100 x i32], align 16
@pre2 = external dso_local local_unnamed_addr global [2100 x i32], align 16
@res = external dso_local local_unnamed_addr global i64, align 8
@cost = external dso_local local_unnamed_addr global [2100 x [2100 x i64]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local void @_Z15primalIterationRi(ptr nocapture noundef nonnull readonly align 4 dereferenceable(4) %0) local_unnamed_addr #0 {
  %2 = load i32, ptr %0, align 4, !tbaa !5
  %.not18 = icmp eq i32 %2, 0
  br i1 %.not18, label %36, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %res.promoted = load i64, ptr @res, align 8, !tbaa !9
  %matchingCount.promoted = load i32, ptr @matchingCount, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = getelementptr inbounds [2100 x i32], ptr @pre2, i64 0, i64 %3
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [2100 x i32], ptr @matchx, i64 0, i64 %6
  store i32 %2, ptr %7, align 4, !tbaa !5
  %8 = getelementptr inbounds [2100 x i32], ptr @matchy, i64 0, i64 %3
  store i32 %5, ptr %8, align 4, !tbaa !5
  %9 = getelementptr inbounds [2100 x [2100 x i32]], ptr @match, i64 0, i64 %6, i64 %3
  store i32 1, ptr %9, align 4, !tbaa !5
  %10 = getelementptr inbounds [2100 x [2100 x i64]], ptr @cost, i64 0, i64 %6, i64 %3
  %11 = load i64, ptr %10, align 8, !tbaa !9
  %12 = add nsw i64 %res.promoted, %11
  %13 = getelementptr inbounds [2100 x i32], ptr @pre1, i64 0, i64 %6
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %.not1723 = icmp eq i32 %14, 0
  br i1 %.not1723, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %15 = phi i32 [ %34, %.lr.ph ], [ %14, %.lr.ph.preheader ]
  %16 = phi i64 [ %32, %.lr.ph ], [ %12, %.lr.ph.preheader ]
  %17 = phi i64 [ %26, %.lr.ph ], [ %6, %.lr.ph.preheader ]
  %18 = sext i32 %15 to i64
  %19 = getelementptr inbounds [2100 x [2100 x i32]], ptr @match, i64 0, i64 %17, i64 %18
  store i32 0, ptr %19, align 4, !tbaa !5
  %20 = getelementptr inbounds [2100 x [2100 x i64]], ptr @cost, i64 0, i64 %17, i64 %18
  %21 = load i64, ptr %20, align 8, !tbaa !9
  %22 = sub nsw i64 %16, %21
  %23 = sext i32 %15 to i64
  %24 = getelementptr inbounds [2100 x i32], ptr @pre2, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [2100 x i32], ptr @matchx, i64 0, i64 %26
  store i32 %15, ptr %27, align 4, !tbaa !5
  %28 = getelementptr inbounds [2100 x i32], ptr @matchy, i64 0, i64 %23
  store i32 %25, ptr %28, align 4, !tbaa !5
  %29 = getelementptr inbounds [2100 x [2100 x i32]], ptr @match, i64 0, i64 %26, i64 %23
  store i32 1, ptr %29, align 4, !tbaa !5
  %30 = getelementptr inbounds [2100 x [2100 x i64]], ptr @cost, i64 0, i64 %26, i64 %23
  %31 = load i64, ptr %30, align 8, !tbaa !9
  %32 = add nsw i64 %22, %31
  %33 = getelementptr inbounds [2100 x i32], ptr @pre1, i64 0, i64 %26
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %.not17 = icmp eq i32 %34, 0
  br i1 %.not17, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa = phi i64 [ %12, %.lr.ph.preheader ], [ %32, %.lr.ph ]
  %35 = add nsw i32 %matchingCount.promoted, 1
  store i32 %35, ptr @matchingCount, align 4, !tbaa !5
  store i64 %.lcssa, ptr @res, align 8, !tbaa !9
  br label %36

36:                                               ; preds = %._crit_edge, %1
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: read, 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" }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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