; LLVM IR from DionysiosB/CodeForces :: 0400-0499/436A-FeedWithCandy.ll (Z12findMaxCandySt6vectorI5candySaIS0_EEll)

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z12findMaxCandySt6vectorI5candySaIS0_EEll(ptr noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  store i8 0, ptr %7, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %13 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %13, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %14 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %14, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %70, %3
  %16 = load i8, ptr %7, align 1, !tbaa !11, !range !13, !noundef !14
  %17 = trunc i8 %16 to i1
  %18 = xor i1 %17, true
  br i1 %18, label %19, label %71

19:                                               ; preds = %15
  store i8 1, ptr %7, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !15
  br label %20

20:                                               ; preds = %66, %19
  %21 = load i32, ptr %11, align 4, !tbaa !15
  %22 = sext i32 %21 to i64
  %23 = call noundef i64 @_ZNKSt6vectorI5candySaIS0_EE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #2
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %20
  store i32 4, ptr %12, align 4
  br label %69

26:                                               ; preds = %20
  %27 = load i32, ptr %11, align 4, !tbaa !15
  %28 = sext i32 %27 to i64
  %29 = call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %28) #2
  %30 = getelementptr inbounds %struct.candy, ptr %29, i32 0, i32 3
  %31 = load i64, ptr %30, align 8, !tbaa !17
  %32 = icmp ne i64 %31, 0
  br i1 %32, label %65, label %33

33:                                               ; preds = %26
  %34 = load i32, ptr %11, align 4, !tbaa !15
  %35 = sext i32 %34 to i64
  %36 = call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %35) #2
  %37 = getelementptr inbounds %struct.candy, ptr %36, i32 0, i32 0
  %38 = load i64, ptr %37, align 8, !tbaa !19
  %39 = load i64, ptr %9, align 8, !tbaa !9
  %40 = icmp eq i64 %38, %39
  br i1 %40, label %41, label %65

41:                                               ; preds = %33
  %42 = load i32, ptr %11, align 4, !tbaa !15
  %43 = sext i32 %42 to i64
  %44 = call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %43) #2
  %45 = getelementptr inbounds %struct.candy, ptr %44, i32 0, i32 1
  %46 = load i64, ptr %45, align 8, !tbaa !20
  %47 = load i64, ptr %8, align 8, !tbaa !9
  %48 = icmp sle i64 %46, %47
  br i1 %48, label %49, label %65

49:                                               ; preds = %41
  %50 = load i64, ptr %10, align 8, !tbaa !9
  %51 = add nsw i64 %50, 1
  store i64 %51, ptr %10, align 8, !tbaa !9
  store i8 0, ptr %7, align 1, !tbaa !11
  %52 = load i64, ptr %9, align 8, !tbaa !9
  %53 = sub nsw i64 1, %52
  store i64 %53, ptr %9, align 8, !tbaa !9
  %54 = load i32, ptr %11, align 4, !tbaa !15
  %55 = sext i32 %54 to i64
  %56 = call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %55) #2
  %57 = getelementptr inbounds %struct.candy, ptr %56, i32 0, i32 2
  %58 = load i64, ptr %57, align 8, !tbaa !21
  %59 = load i64, ptr %8, align 8, !tbaa !9
  %60 = add nsw i64 %59, %58
  store i64 %60, ptr %8, align 8, !tbaa !9
  %61 = load i32, ptr %11, align 4, !tbaa !15
  %62 = sext i32 %61 to i64
  %63 = call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %62) #2
  %64 = getelementptr inbounds %struct.candy, ptr %63, i32 0, i32 3
  store i64 1, ptr %64, align 8, !tbaa !17
  store i32 4, ptr %12, align 4
  br label %69

65:                                               ; preds = %41, %33, %26
  br label %66

66:                                               ; preds = %65
  %67 = load i32, ptr %11, align 4, !tbaa !15
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %11, align 4, !tbaa !15
  br label %20, !llvm.loop !22

69:                                               ; preds = %49, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %70

70:                                               ; preds = %69
  br label %15, !llvm.loop !24

71:                                               ; preds = %15
  %72 = load i64, ptr %10, align 8, !tbaa !9
  store i32 1, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  ret i64 %72
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNKSt6vectorI5candySaIS0_EE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #0 align 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 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!18, !10, i64 24}
!18 = !{!"_ZTS5candy", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!19 = !{!18, !10, i64 0}
!20 = !{!18, !10, i64 8}
!21 = !{!18, !10, i64 16}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = distinct !{!24, !23}


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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z12findMaxCandySt6vectorI5candySaIS0_EEll(ptr noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = tail call noundef i64 @_ZNKSt6vectorI5candySaIS0_EE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #1
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %.loopexit41, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3, %.loopexit
  %.0192952 = phi i64 [ %24, %.loopexit ], [ %1, %3 ]
  %.0173051 = phi i64 [ %20, %.loopexit ], [ %2, %3 ]
  %.0163150 = phi i64 [ %19, %.loopexit ], [ 0, %3 ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %17
  %indvars.iv = phi i64 [ %indvars.iv.next, %17 ], [ 0, %.lr.ph.preheader ]
  %6 = tail call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %indvars.iv) #1
  %7 = getelementptr inbounds i8, ptr %6, i64 24
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %.not = icmp eq i64 %8, 0
  br i1 %.not, label %9, label %17

9:                                                ; preds = %.lr.ph
  %10 = tail call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %indvars.iv) #1
  %11 = load i64, ptr %10, align 8, !tbaa !10
  %12 = icmp eq i64 %11, %.0173051
  br i1 %12, label %13, label %17

13:                                               ; preds = %9
  %14 = tail call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %indvars.iv) #1
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %.not24 = icmp sgt i64 %16, %.0192952
  br i1 %.not24, label %17, label %.loopexit

17:                                               ; preds = %.lr.ph, %9, %13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %18 = tail call noundef i64 @_ZNKSt6vectorI5candySaIS0_EE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #1
  %.not36 = icmp ugt i64 %18, %indvars.iv.next
  br i1 %.not36, label %.lr.ph, label %.loopexit41, !llvm.loop !12

.loopexit:                                        ; preds = %13
  %19 = add nuw nsw i64 %.0163150, 1
  %20 = sub nsw i64 1, %.0173051
  %21 = tail call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %indvars.iv) #1
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  %23 = load i64, ptr %22, align 8, !tbaa !14
  %24 = add nsw i64 %23, %.0192952
  %25 = tail call noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %indvars.iv) #1
  %26 = getelementptr inbounds i8, ptr %25, i64 24
  store i64 1, ptr %26, align 8, !tbaa !5
  %27 = tail call noundef i64 @_ZNKSt6vectorI5candySaIS0_EE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #1
  %28 = icmp eq i64 %27, 0
  br i1 %28, label %.loopexit41, label %.lr.ph.preheader, !llvm.loop !15

.loopexit41:                                      ; preds = %.loopexit, %17, %3
  %.0163149 = phi i64 [ 0, %3 ], [ %.0163150, %17 ], [ %19, %.loopexit ]
  ret i64 %.0163149
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNKSt6vectorI5candySaIS0_EE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(32) ptr @_ZNSt6vectorI5candySaIS0_EEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 2

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


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