; LLVM IR from tangjz/acm-icpc :: hdu/6220.ll (Z8evaluateiP4Node)

; 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.Node = type { i64, i64 }

; Function Attrs: mustprogress uwtable
define dso_local noundef { i64, i64 } @_Z8evaluateiP4Node(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i128, align 16
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i128, align 16
  %7 = alloca i128, align 16
  %8 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #3
  store i128 0, ptr %6, align 16, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #3
  store i128 0, ptr %7, align 16, !tbaa !11
  %9 = load ptr, ptr %5, align 8, !tbaa !9
  %10 = load ptr, ptr %5, align 8, !tbaa !9
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds %struct.Node, ptr %10, i64 %12
  call void @_ZSt4sortIP4NodeEvT_S2_(ptr noundef %9, ptr noundef %13)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %40, %2
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

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

19:                                               ; preds = %14
  %20 = load ptr, ptr %5, align 8, !tbaa !9
  %21 = load i32, ptr %8, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds %struct.Node, ptr %20, i64 %22
  %24 = getelementptr inbounds %struct.Node, ptr %23, i32 0, i32 0
  %25 = load i64, ptr %24, align 8, !tbaa !13
  %26 = zext i64 %25 to i128
  %27 = load i128, ptr %7, align 16, !tbaa !11
  %28 = add nsw i128 %27, %26
  store i128 %28, ptr %7, align 16, !tbaa !11
  %29 = load i128, ptr %7, align 16, !tbaa !11
  %30 = load ptr, ptr %5, align 8, !tbaa !9
  %31 = load i32, ptr %8, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds %struct.Node, ptr %30, i64 %32
  %34 = getelementptr inbounds %struct.Node, ptr %33, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !16
  %36 = zext i64 %35 to i128
  %37 = mul nsw i128 %29, %36
  %38 = load i128, ptr %6, align 16, !tbaa !11
  %39 = add nsw i128 %38, %37
  store i128 %39, ptr %6, align 16, !tbaa !11
  br label %40

40:                                               ; preds = %19
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %8, align 4, !tbaa !5
  br label %14, !llvm.loop !17

43:                                               ; preds = %18
  %44 = load i128, ptr %6, align 16, !tbaa !11
  store i128 %44, ptr %3, align 16
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #3
  %45 = load { i64, i64 }, ptr %3, align 16
  ret { i64, i64 } %45
}

; 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 @_ZSt4sortIP4NodeEvT_S2_(ptr noundef, ptr noundef) #2

; 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 = { 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"__int128", !7, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"_ZTS4Node", !15, i64 0, !15, i64 8}
!15 = !{!"long long", !7, i64 0}
!16 = !{!14, !15, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%struct.Node = type { i64, i64 }

; Function Attrs: mustprogress uwtable
define dso_local noundef { i64, i64 } @_Z8evaluateiP4Node(i32 noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds %struct.Node, ptr %1, i64 %3
  tail call void @_ZSt4sortIP4NodeEvT_S2_(ptr noundef %1, ptr noundef %4)
  %5 = icmp sgt i32 %0, 0
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %0 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %6 = icmp eq i32 %0, 1
  br i1 %6, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa.loopexit:          ; preds = %.lr.ph
  %extract.t27 = trunc i128 %33 to i64
  %extract28 = lshr i128 %33, 64
  %extract.t29 = trunc nuw i128 %extract28 to i64
  br label %._crit_edge.loopexit.unr-lcssa

._crit_edge.loopexit.unr-lcssa:                   ; preds = %._crit_edge.loopexit.unr-lcssa.loopexit, %.lr.ph.preheader
  %.lcssa.ph.off0 = phi i64 [ poison, %.lr.ph.preheader ], [ %extract.t27, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.lcssa.ph.off64 = phi i64 [ poison, %.lr.ph.preheader ], [ %extract.t29, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.01316.unr = phi i128 [ 0, %.lr.ph.preheader ], [ %28, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.01415.unr = phi i128 [ 0, %.lr.ph.preheader ], [ %33, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %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
  %7 = getelementptr inbounds %struct.Node, ptr %1, i64 %indvars.iv.unr
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = zext i64 %8 to i128
  %10 = add nuw nsw i128 %.01316.unr, %9
  %11 = getelementptr inbounds i8, ptr %7, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !10
  %13 = zext i64 %12 to i128
  %14 = mul nuw nsw i128 %10, %13
  %15 = add nuw nsw i128 %14, %.01415.unr
  %extract.t22 = trunc i128 %15 to i64
  %extract25 = lshr i128 %15, 64
  %extract.t26 = trunc nuw i128 %extract25 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %2
  %.014.lcssa.off0 = phi i64 [ 0, %2 ], [ %.lcssa.ph.off0, %._crit_edge.loopexit.unr-lcssa ], [ %extract.t22, %.lr.ph.epil ]
  %.014.lcssa.off64 = phi i64 [ 0, %2 ], [ %.lcssa.ph.off64, %._crit_edge.loopexit.unr-lcssa ], [ %extract.t26, %.lr.ph.epil ]
  %.fca.0.insert = insertvalue { i64, i64 } poison, i64 %.014.lcssa.off0, 0
  %.fca.1.insert = insertvalue { i64, i64 } %.fca.0.insert, i64 %.014.lcssa.off64, 1
  ret { i64, i64 } %.fca.1.insert

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %.01316 = phi i128 [ 0, %.lr.ph.preheader.new ], [ %28, %.lr.ph ]
  %.01415 = phi i128 [ 0, %.lr.ph.preheader.new ], [ %33, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %16 = getelementptr inbounds %struct.Node, ptr %1, i64 %indvars.iv
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = zext i64 %17 to i128
  %19 = add nuw nsw i128 %.01316, %18
  %20 = getelementptr inbounds i8, ptr %16, i64 8
  %21 = load i64, ptr %20, align 8, !tbaa !10
  %22 = zext i64 %21 to i128
  %23 = mul nuw nsw i128 %19, %22
  %24 = add nuw nsw i128 %23, %.01415
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %25 = getelementptr inbounds %struct.Node, ptr %1, i64 %indvars.iv.next
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = zext i64 %26 to i128
  %28 = add nuw nsw i128 %19, %27
  %29 = getelementptr inbounds i8, ptr %25, i64 8
  %30 = load i64, ptr %29, align 8, !tbaa !10
  %31 = zext i64 %30 to i128
  %32 = mul nuw nsw i128 %28, %31
  %33 = add nuw nsw i128 %32, %24
  %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.loopexit, label %.lr.ph, !llvm.loop !11
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIP4NodeEvT_S2_(ptr noundef, ptr noundef) local_unnamed_addr #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 = { 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" }

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


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