; LLVM IR from tangjz/acm-icpc :: leetcode/extra/Shopee_003-Order Delivery.ll (ZN4Flow7minCostEi)

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

@_ZN4Flow1NE = external dso_local global i32, align 4
@_ZN4Flow1SE = external dso_local global i32, align 4
@_ZN4Flow4flowE = external dso_local global i32, align 4
@_ZN4Flow3sumE = external dso_local global i64, align 8
@_ZN4Flow4costE = external dso_local global i64, align 8
@_ZN4Flow3visE = external dso_local global [45 x i8], align 16

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN4Flow4SPFAEv() #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN4Flow8DijkstraEv() #3

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN4Flow3dfsEii(i32 noundef, i32 noundef) #3

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4Flow7minCostEi(i32 noundef %0) #3 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %5 = call noundef zeroext i1 @_ZN4Flow4SPFAEv()
  br label %6

6:                                                ; preds = %37, %1
  br label %7

7:                                                ; preds = %35, %6
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %10, label %36

10:                                               ; preds = %7
  %11 = load i32, ptr @_ZN4Flow1NE, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = mul i64 %12, 1
  call void @llvm.memset.p0.i64(ptr align 16 @_ZN4Flow3visE, i8 0, i64 %13, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %14 = load i32, ptr @_ZN4Flow1SE, align 4, !tbaa !5
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = call noundef i32 @_ZN4Flow3dfsEii(i32 noundef %14, i32 noundef %15)
  store i32 %16, ptr %3, align 4, !tbaa !5
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %20, label %19

19:                                               ; preds = %10
  store i32 5, ptr %4, align 4
  br label %33

20:                                               ; preds = %10
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = sub nsw i32 %22, %21
  store i32 %23, ptr %2, align 4, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr @_ZN4Flow4flowE, align 4, !tbaa !5
  %26 = add nsw i32 %25, %24
  store i32 %26, ptr @_ZN4Flow4flowE, align 4, !tbaa !5
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = load i64, ptr @_ZN4Flow3sumE, align 8, !tbaa !9
  %30 = mul nsw i64 %28, %29
  %31 = load i64, ptr @_ZN4Flow4costE, align 8, !tbaa !9
  %32 = add nsw i64 %31, %30
  store i64 %32, ptr @_ZN4Flow4costE, align 8, !tbaa !9
  store i32 0, ptr %4, align 4
  br label %33

33:                                               ; preds = %20, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  %34 = load i32, ptr %4, align 4
  switch i32 %34, label %40 [
    i32 0, label %35
    i32 5, label %36
  ]

35:                                               ; preds = %33
  br label %7, !llvm.loop !11

36:                                               ; preds = %33, %7
  br label %37

37:                                               ; preds = %36
  %38 = call noundef zeroext i1 @_ZN4Flow8DijkstraEv()
  br i1 %38, label %6, label %39, !llvm.loop !13

39:                                               ; preds = %37
  ret void

40:                                               ; preds = %33
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@_ZN4Flow1NE = external dso_local local_unnamed_addr global i32, align 4
@_ZN4Flow1SE = external dso_local local_unnamed_addr global i32, align 4
@_ZN4Flow4flowE = external dso_local local_unnamed_addr global i32, align 4
@_ZN4Flow3sumE = external dso_local local_unnamed_addr global i64, align 8
@_ZN4Flow4costE = external dso_local local_unnamed_addr global i64, align 8
@_ZN4Flow3visE = external dso_local local_unnamed_addr global [45 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN4Flow4SPFAEv() local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN4Flow8DijkstraEv() local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN4Flow3dfsEii(i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4Flow7minCostEi(i32 noundef %0) local_unnamed_addr #2 {
  %2 = tail call noundef zeroext i1 @_ZN4Flow4SPFAEv()
  br label %3

3:                                                ; preds = %.thread, %1
  %.08 = phi i32 [ %0, %1 ], [ %.1.lcssa, %.thread ]
  %4 = icmp sgt i32 %.08, 0
  br i1 %4, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %3, %9
  %.113 = phi i32 [ %10, %9 ], [ %.08, %3 ]
  %5 = load i32, ptr @_ZN4Flow1NE, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  tail call void @llvm.memset.p0.i64(ptr nonnull align 16 @_ZN4Flow3visE, i8 0, i64 %6, i1 false)
  %7 = load i32, ptr @_ZN4Flow1SE, align 4, !tbaa !5
  %8 = tail call noundef i32 @_ZN4Flow3dfsEii(i32 noundef %7, i32 noundef %.113)
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %.thread, label %9

9:                                                ; preds = %.lr.ph
  %10 = sub nsw i32 %.113, %8
  %11 = load i32, ptr @_ZN4Flow4flowE, align 4, !tbaa !5
  %12 = add nsw i32 %11, %8
  store i32 %12, ptr @_ZN4Flow4flowE, align 4, !tbaa !5
  %13 = sext i32 %8 to i64
  %14 = load i64, ptr @_ZN4Flow3sumE, align 8, !tbaa !9
  %15 = mul nsw i64 %14, %13
  %16 = load i64, ptr @_ZN4Flow4costE, align 8, !tbaa !9
  %17 = add nsw i64 %16, %15
  store i64 %17, ptr @_ZN4Flow4costE, align 8, !tbaa !9
  %18 = icmp sgt i32 %10, 0
  br i1 %18, label %.lr.ph, label %.thread

.thread:                                          ; preds = %9, %.lr.ph, %3
  %.1.lcssa = phi i32 [ %.08, %3 ], [ %.113, %.lr.ph ], [ %10, %9 ]
  %19 = tail call noundef zeroext i1 @_ZN4Flow8DijkstraEv()
  br i1 %19, label %3, label %20, !llvm.loop !11

20:                                               ; preds = %.thread
  ret void
}

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 nounwind willreturn memory(argmem: write) }
attributes #2 = { 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass