; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/101 Hacks/101 Hack 2015/101 Hack Oct/Problem C/C.ll (Z1fx)

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

@n = external dso_local global i64, align 8
@a = external dso_local global [100100 x i64], align 16
@b = external dso_local global [100100 x i64], align 16

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

5:                                                ; preds = %38, %1
  %6 = load i32, ptr %4, align 4, !tbaa !9
  %7 = sext i32 %6 to i64
  %8 = load i64, ptr @n, align 8, !tbaa !5
  %9 = icmp slt i64 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %41

11:                                               ; preds = %5
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [100100 x i64], ptr @a, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [100100 x i64], ptr @b, i64 0, i64 %17
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = mul nsw i64 %15, %19
  %21 = load i64, ptr %2, align 8, !tbaa !5
  %22 = icmp sgt i64 %20, %21
  br i1 %22, label %23, label %37

23:                                               ; preds = %11
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [100100 x i64], ptr @a, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !5
  %28 = load i64, ptr %2, align 8, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100100 x i64], ptr @b, i64 0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = sdiv i64 %28, %32
  %34 = sub nsw i64 %27, %33
  %35 = load i64, ptr %3, align 8, !tbaa !5
  %36 = add nsw i64 %35, %34
  store i64 %36, ptr %3, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %23, %11
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %4, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %4, align 4, !tbaa !9
  br label %5, !llvm.loop !11

41:                                               ; preds = %10
  %42 = load i64, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i64 %42
}

; 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 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 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@n = external dso_local local_unnamed_addr global i64, align 8
@a = external dso_local local_unnamed_addr global [100100 x i64], align 16
@b = external dso_local local_unnamed_addr global [100100 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z1fx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @n, align 8, !tbaa !5
  %3 = icmp sgt i64 %2, 0
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %xtraiter = and i64 %2, 1
  %4 = icmp eq i64 %2, 1
  br i1 %4, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %2, 9223372036854775806
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %35, %.lr.ph.preheader
  %.1.lcssa.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %.1.1, %35 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %35 ]
  %.01013.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %.1.1, %35 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %5 = getelementptr inbounds [100100 x i64], ptr @a, i64 0, i64 %indvars.iv.unr
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds [100100 x i64], ptr @b, i64 0, i64 %indvars.iv.unr
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = mul nsw i64 %8, %6
  %10 = icmp sgt i64 %9, %0
  br i1 %10, label %11, label %._crit_edge

11:                                               ; preds = %.lr.ph.epil
  %12 = sdiv i64 %0, %8
  %13 = add i64 %6, %.01013.unr
  %14 = sub i64 %13, %12
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %11, %.lr.ph.epil, %1
  %.010.lcssa = phi i64 [ 0, %1 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %14, %11 ], [ %.01013.unr, %.lr.ph.epil ]
  ret i64 %.010.lcssa

.lr.ph:                                           ; preds = %35, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %35 ]
  %.01013 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %.1.1, %35 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %35 ]
  %15 = getelementptr inbounds [100100 x i64], ptr @a, i64 0, i64 %indvars.iv
  %16 = load i64, ptr %15, align 16, !tbaa !5
  %17 = getelementptr inbounds [100100 x i64], ptr @b, i64 0, i64 %indvars.iv
  %18 = load i64, ptr %17, align 16, !tbaa !5
  %19 = mul nsw i64 %18, %16
  %20 = icmp sgt i64 %19, %0
  br i1 %20, label %21, label %.lr.ph.1

21:                                               ; preds = %.lr.ph
  %22 = sdiv i64 %0, %18
  %23 = add i64 %16, %.01013
  %24 = sub i64 %23, %22
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %21
  %.1 = phi i64 [ %24, %21 ], [ %.01013, %.lr.ph ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %25 = getelementptr inbounds [100100 x i64], ptr @a, i64 0, i64 %indvars.iv.next
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = getelementptr inbounds [100100 x i64], ptr @b, i64 0, i64 %indvars.iv.next
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = mul nsw i64 %28, %26
  %30 = icmp sgt i64 %29, %0
  br i1 %30, label %31, label %35

31:                                               ; preds = %.lr.ph.1
  %32 = sdiv i64 %0, %28
  %33 = add i64 %26, %.1
  %34 = sub i64 %33, %32
  br label %35

35:                                               ; preds = %31, %.lr.ph.1
  %.1.1 = phi i64 [ %34, %31 ], [ %.1, %.lr.ph.1 ]
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9
}

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

!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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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