; LLVM IR from tangjz/acm-icpc :: lydsy/3032.ll (Z4calcPxi)

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

@f = external dso_local global [100001 x i64], align 16

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

7:                                                ; preds = %38, %2
  %8 = load i32, ptr %5, align 4, !tbaa !9
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = icmp sle i32 %8, %9
  br i1 %10, label %11, label %41

11:                                               ; preds = %7
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds i64, ptr %12, i64 0
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = sdiv i64 %14, %16
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i64, ptr %18, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = sub nsw i64 %22, %17
  store i64 %23, ptr %21, align 8, !tbaa !11
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = sub nsw i32 %24, 1
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100001 x i64], ptr @f, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !11
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i64, ptr %29, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !11
  %34 = add nsw i64 %28, %33
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [100001 x i64], ptr @f, i64 0, i64 %36
  store i64 %34, ptr %37, align 8, !tbaa !11
  br label %38

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

41:                                               ; preds = %7
  %42 = load i32, ptr %4, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i64, ptr @f, i64 %43
  %45 = getelementptr inbounds i64, ptr %44, i64 1
  call void @_ZSt4sortIPxEvT_S1_(ptr noundef getelementptr inbounds (i64, ptr @f, i64 1), ptr noundef %45)
  store i32 1, ptr %5, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %65, %41
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = load i32, ptr %4, align 4, !tbaa !9
  %49 = icmp sle i32 %47, %48
  br i1 %49, label %50, label %68

50:                                               ; preds = %46
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [100001 x i64], ptr @f, i64 0, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !11
  %55 = load i32, ptr %4, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  %57 = ashr i32 %56, 1
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [100001 x i64], ptr @f, i64 0, i64 %58
  %60 = load i64, ptr %59, align 8, !tbaa !11
  %61 = sub nsw i64 %54, %60
  %62 = call noundef i64 @_ZSt3absx(i64 noundef %61)
  %63 = load i64, ptr %6, align 8, !tbaa !11
  %64 = add nsw i64 %63, %62
  store i64 %64, ptr %6, align 8, !tbaa !11
  br label %65

65:                                               ; preds = %50
  %66 = load i32, ptr %5, align 4, !tbaa !9
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %5, align 4, !tbaa !9
  br label %46, !llvm.loop !15

68:                                               ; preds = %46
  %69 = load i64, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret i64 %69
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPxEvT_S1_(ptr noundef, ptr noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZSt3absx(i64 noundef) #3

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

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 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 = { 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 #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 = !{!"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"}
!15 = distinct !{!15, !14}


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

@f = external dso_local global [100001 x i64], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z4calcPxi(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not22 = icmp slt i32 %1, 1
  br i1 %.not22, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %2
  %3 = sext i32 %1 to i64
  %4 = getelementptr inbounds i64, ptr @f, i64 %3
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  tail call void @_ZSt4sortIPxEvT_S1_(ptr noundef nonnull getelementptr inbounds (i8, ptr @f, i64 8), ptr noundef nonnull %5)
  br label %._crit_edge29

.lr.ph:                                           ; preds = %2
  %6 = zext nneg i32 %1 to i64
  %.pre = load i64, ptr @f, align 16, !tbaa !5
  %7 = zext nneg i32 %1 to i64
  %xtraiter = and i64 %7, 1
  %8 = icmp eq i32 %1, 1
  br i1 %8, label %._crit_edge.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %7, 2147483646
  br label %9

9:                                                ; preds = %9, %.lr.ph.new
  %10 = phi i64 [ %.pre, %.lr.ph.new ], [ %23, %9 ]
  %indvars.iv = phi i64 [ 1, %.lr.ph.new ], [ %indvars.iv.next.1, %9 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %9 ]
  %11 = load i64, ptr %0, align 8, !tbaa !5
  %12 = sdiv i64 %11, %6
  %13 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = sub nsw i64 %14, %12
  store i64 %15, ptr %13, align 8, !tbaa !5
  %16 = add nsw i64 %10, %15
  %17 = getelementptr inbounds [100001 x i64], ptr @f, i64 0, i64 %indvars.iv
  store i64 %16, ptr %17, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %18 = load i64, ptr %0, align 8, !tbaa !5
  %19 = sdiv i64 %18, %6
  %20 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.next
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = sub nsw i64 %21, %19
  store i64 %22, ptr %20, align 8, !tbaa !5
  %23 = add nsw i64 %16, %22
  %24 = getelementptr inbounds [100001 x i64], ptr @f, i64 0, i64 %indvars.iv.next
  store i64 %23, ptr %24, align 8, !tbaa !5
  %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.unr-lcssa, label %9, !llvm.loop !9

._crit_edge.unr-lcssa:                            ; preds = %9, %.lr.ph
  %.unr = phi i64 [ %.pre, %.lr.ph ], [ %23, %9 ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next.1, %9 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %._crit_edge.epilog-lcssa

._crit_edge.epilog-lcssa:                         ; preds = %._crit_edge.unr-lcssa
  %25 = load i64, ptr %0, align 8, !tbaa !5
  %26 = sdiv i64 %25, %6
  %27 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.unr
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = sub nsw i64 %28, %26
  store i64 %29, ptr %27, align 8, !tbaa !5
  %30 = add nsw i64 %.unr, %29
  %31 = getelementptr inbounds [100001 x i64], ptr @f, i64 0, i64 %indvars.iv.unr
  store i64 %30, ptr %31, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.unr-lcssa, %._crit_edge.epilog-lcssa
  %32 = zext nneg i32 %1 to i64
  %33 = getelementptr inbounds i64, ptr @f, i64 %32
  %34 = getelementptr inbounds i8, ptr %33, i64 8
  tail call void @_ZSt4sortIPxEvT_S1_(ptr noundef nonnull getelementptr inbounds (i8, ptr @f, i64 8), ptr noundef nonnull %34)
  br i1 %.not22, label %._crit_edge29, label %.lr.ph28

.lr.ph28:                                         ; preds = %._crit_edge
  %35 = add nuw i32 %1, 1
  %36 = lshr i32 %35, 1
  %37 = zext nneg i32 %36 to i64
  %38 = getelementptr inbounds [100001 x i64], ptr @f, i64 0, i64 %37
  %wide.trip.count34 = zext i32 %35 to i64
  br label %39

39:                                               ; preds = %.lr.ph28, %39
  %indvars.iv31 = phi i64 [ 1, %.lr.ph28 ], [ %indvars.iv.next32, %39 ]
  %.026 = phi i64 [ 0, %.lr.ph28 ], [ %45, %39 ]
  %40 = getelementptr inbounds [100001 x i64], ptr @f, i64 0, i64 %indvars.iv31
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = load i64, ptr %38, align 8, !tbaa !5
  %43 = sub nsw i64 %41, %42
  %44 = tail call noundef i64 @_ZSt3absx(i64 noundef %43)
  %45 = add nsw i64 %44, %.026
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %exitcond35.not = icmp eq i64 %indvars.iv.next32, %wide.trip.count34
  br i1 %exitcond35.not, label %._crit_edge29, label %39, !llvm.loop !11

._crit_edge29:                                    ; preds = %39, %._crit_edge.thread, %._crit_edge
  %.0.lcssa = phi i64 [ 0, %._crit_edge ], [ 0, %._crit_edge.thread ], [ %45, %39 ]
  ret i64 %.0.lcssa
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPxEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZSt3absx(i64 noundef) local_unnamed_addr #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 = { inlinehint 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 #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" }

!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"}
!11 = distinct !{!11, !10}


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