; LLVM IR from tangjz/acm-icpc :: hdu/5644.ll (Z14MinCostMaxFlowi)

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

@N = external dso_local global i32, align 4
@S = external dso_local global i32, align 4
@sum = external dso_local global i32, align 4
@flow = external dso_local global i32, align 4
@cost = external dso_local global i32, align 4
@vis = external dso_local global [403 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 uwtable
declare dso_local noundef zeroext i1 @_Z8Dijkstrav() #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 i32 @_Z3dfsii(i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z14MinCostMaxFlowi(i32 noundef %0) #1 {
  %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 @_Z8Dijkstrav()
  br label %6

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

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

10:                                               ; preds = %7
  %11 = load i32, ptr @N, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = mul i64 %12, 1
  call void @llvm.memset.p0.i64(ptr align 16 @vis, i8 0, i64 %13, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %14 = load i32, ptr @S, align 4, !tbaa !5
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = call noundef i32 @_Z3dfsii(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 %32

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 @flow, align 4, !tbaa !5
  %26 = add nsw i32 %25, %24
  store i32 %26, ptr @flow, align 4, !tbaa !5
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = load i32, ptr @sum, align 4, !tbaa !5
  %29 = mul nsw i32 %27, %28
  %30 = load i32, ptr @cost, align 4, !tbaa !5
  %31 = add nsw i32 %30, %29
  store i32 %31, ptr @cost, align 4, !tbaa !5
  store i32 0, ptr %4, align 4
  br label %32

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

34:                                               ; preds = %32
  br label %7, !llvm.loop !9

35:                                               ; preds = %32, %7
  br label %36

36:                                               ; preds = %35
  %37 = call noundef zeroext i1 @_Z8Dijkstrav()
  br i1 %37, label %6, label %38, !llvm.loop !11

38:                                               ; preds = %36
  ret void

39:                                               ; preds = %32
  unreachable
}

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


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

@N = external dso_local local_unnamed_addr global i32, align 4
@S = external dso_local local_unnamed_addr global i32, align 4
@sum = external dso_local local_unnamed_addr global i32, align 4
@flow = external dso_local local_unnamed_addr global i32, align 4
@cost = external dso_local local_unnamed_addr global i32, align 4
@vis = external dso_local local_unnamed_addr global [403 x i8], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_Z8Dijkstrav() 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 i32 @_Z3dfsii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z14MinCostMaxFlowi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef zeroext i1 @_Z8Dijkstrav()
  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 @N, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  tail call void @llvm.memset.p0.i64(ptr nonnull align 16 @vis, i8 0, i64 %6, i1 false)
  %7 = load i32, ptr @S, align 4, !tbaa !5
  %8 = tail call noundef i32 @_Z3dfsii(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 @flow, align 4, !tbaa !5
  %12 = add nsw i32 %11, %8
  store i32 %12, ptr @flow, align 4, !tbaa !5
  %13 = load i32, ptr @sum, align 4, !tbaa !5
  %14 = mul nsw i32 %13, %8
  %15 = load i32, ptr @cost, align 4, !tbaa !5
  %16 = add nsw i32 %15, %14
  store i32 %16, ptr @cost, align 4, !tbaa !5
  %17 = icmp sgt i32 %10, 0
  br i1 %17, label %.lr.ph, label %.thread

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

19:                                               ; preds = %.thread
  ret void
}

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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}
!10 = !{!"llvm.loop.mustprogress"}


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