; LLVM IR from reyadussalahin/problem-solving :: search/ternary_search/codeforces/F1355E/Solution.ll (Z4calcx)

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

@h = external dso_local global [100010 x i32], align 16
@n = external dso_local global i32, align 4
@a = external dso_local global i32, align 4
@r = external dso_local global i32, align 4
@m = external dso_local global i32, align 4

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

8:                                                ; preds = %42, %1
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = load i32, ptr @n, align 4, !tbaa !9
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %45

13:                                               ; preds = %8
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = load i64, ptr %2, align 8, !tbaa !5
  %20 = icmp sle i64 %18, %19
  br i1 %20, label %21, label %31

21:                                               ; preds = %13
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = load i64, ptr %2, align 8, !tbaa !5
  %24 = add nsw i64 %22, %23
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = sub nsw i64 %24, %29
  store i64 %30, ptr %3, align 8, !tbaa !5
  br label %41

31:                                               ; preds = %13
  %32 = load i64, ptr %4, align 8, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = add nsw i64 %32, %37
  %39 = load i64, ptr %2, align 8, !tbaa !5
  %40 = sub nsw i64 %38, %39
  store i64 %40, ptr %4, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %31, %21
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %5, align 4, !tbaa !9
  br label %8, !llvm.loop !11

45:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %46 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %3, ptr noundef nonnull align 8 dereferenceable(8) %4)
  %47 = load i64, ptr %46, align 8, !tbaa !5
  store i64 %47, ptr %6, align 8, !tbaa !5
  %48 = load i64, ptr %6, align 8, !tbaa !5
  %49 = load i64, ptr %3, align 8, !tbaa !5
  %50 = sub nsw i64 %49, %48
  store i64 %50, ptr %3, align 8, !tbaa !5
  %51 = load i64, ptr %6, align 8, !tbaa !5
  %52 = load i64, ptr %4, align 8, !tbaa !5
  %53 = sub nsw i64 %52, %51
  store i64 %53, ptr %4, align 8, !tbaa !5
  %54 = load i64, ptr %3, align 8, !tbaa !5
  %55 = load i32, ptr @a, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = mul nsw i64 %54, %56
  %58 = load i64, ptr %4, align 8, !tbaa !5
  %59 = load i32, ptr @r, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = mul nsw i64 %58, %60
  %62 = add nsw i64 %57, %61
  %63 = load i64, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %64 = load i32, ptr @a, align 4, !tbaa !9
  %65 = load i32, ptr @r, align 4, !tbaa !9
  %66 = add nsw i32 %64, %65
  store i32 %66, ptr %7, align 4, !tbaa !9
  %67 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %7, ptr noundef nonnull align 4 dereferenceable(4) @m)
  %68 = load i32, ptr %67, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  %70 = mul nsw i64 %63, %69
  %71 = add nsw i64 %62, %70
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i64 %71
}

; 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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

attributes #0 = { mustprogress 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 = { inlinehint 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 #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 = !{!"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"

@h = external dso_local local_unnamed_addr global [100010 x i32], align 16
@n = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global i32, align 4
@r = external dso_local local_unnamed_addr global i32, align 4
@m = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calcx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %wide.trip.count = zext nneg i32 %5 to i64
  %min.iters.check = icmp ult i32 %5, 4
  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 ]
  %.ph = phi i64 [ 0, %.lr.ph ], [ %26, %middle.block ]
  %.ph32 = phi i64 [ 0, %.lr.ph ], [ %27, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %wide.trip.count, 2147483644
  %broadcast.splatinsert = insertelement <2 x i64> poison, i64 %0, i64 0
  %broadcast.splat = shufflevector <2 x i64> %broadcast.splatinsert, <2 x i64> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %23, %vector.body ]
  %vec.phi26 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %24, %vector.body ]
  %vec.phi27 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %19, %vector.body ]
  %vec.phi28 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %20, %vector.body ]
  %7 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %index
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  %wide.load = load <2 x i32>, ptr %7, align 16, !tbaa !5
  %wide.load29 = load <2 x i32>, ptr %8, align 8, !tbaa !5
  %9 = sext <2 x i32> %wide.load to <2 x i64>
  %10 = sext <2 x i32> %wide.load29 to <2 x i64>
  %11 = icmp slt <2 x i64> %broadcast.splat, %9
  %12 = icmp slt <2 x i64> %broadcast.splat, %10
  %13 = sub <2 x i64> %broadcast.splat, %9
  %14 = sub <2 x i64> %broadcast.splat, %10
  %15 = sub <2 x i64> %9, %broadcast.splat
  %16 = sub <2 x i64> %10, %broadcast.splat
  %17 = select <2 x i1> %11, <2 x i64> %15, <2 x i64> zeroinitializer
  %18 = select <2 x i1> %12, <2 x i64> %16, <2 x i64> zeroinitializer
  %19 = add <2 x i64> %vec.phi27, %17
  %20 = add <2 x i64> %vec.phi28, %18
  %21 = select <2 x i1> %11, <2 x i64> zeroinitializer, <2 x i64> %13
  %22 = select <2 x i1> %12, <2 x i64> zeroinitializer, <2 x i64> %14
  %23 = add <2 x i64> %vec.phi, %21
  %24 = add <2 x i64> %vec.phi26, %22
  %index.next = add nuw i64 %index, 4
  %25 = icmp eq i64 %index.next, %n.vec
  br i1 %25, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %24, %23
  %26 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %bin.rdx30 = add <2 x i64> %20, %19
  %27 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx30)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %1
  %.lcssa16 = phi i64 [ 0, %1 ], [ %27, %middle.block ], [ %55, %scalar.ph ]
  %.lcssa = phi i64 [ 0, %1 ], [ %26, %middle.block ], [ %57, %scalar.ph ]
  store i64 %.lcssa, ptr %2, align 8, !tbaa !13
  store i64 %.lcssa16, ptr %3, align 8, !tbaa !13
  %28 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %29 = load i64, ptr %28, align 8, !tbaa !13
  %30 = load i64, ptr %2, align 8, !tbaa !13
  %31 = sub nsw i64 %30, %29
  store i64 %31, ptr %2, align 8, !tbaa !13
  %32 = load i64, ptr %3, align 8, !tbaa !13
  %33 = sub nsw i64 %32, %29
  store i64 %33, ptr %3, align 8, !tbaa !13
  %34 = load i32, ptr @a, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = mul nsw i64 %31, %35
  %37 = load i32, ptr @r, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = mul nsw i64 %33, %38
  %40 = add nsw i64 %39, %36
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #4
  %41 = add nsw i32 %37, %34
  store i32 %41, ptr %4, align 4, !tbaa !5
  %42 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %4, ptr noundef nonnull align 4 dereferenceable(4) @m)
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = mul nsw i64 %29, %44
  %46 = add nsw i64 %40, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4
  ret i64 %46

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %47 = phi i64 [ %57, %scalar.ph ], [ %.ph, %scalar.ph.preheader ]
  %48 = phi i64 [ %55, %scalar.ph ], [ %.ph32, %scalar.ph.preheader ]
  %49 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %indvars.iv
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %.not = icmp sgt i64 %51, %0
  %52 = sub i64 %0, %51
  %53 = sub i64 %51, %0
  %54 = select i1 %.not, i64 %53, i64 0
  %55 = add i64 %48, %54
  %56 = select i1 %.not, i64 0, i64 %52
  %57 = add i64 %47, %56
  %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 !15
}

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = !{!14, !14, i64 0}
!14 = !{!"long long", !7, i64 0}
!15 = distinct !{!15, !10, !12, !11}


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