; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 18. Greedy (Ford-Fulkerson-Algorithm)

; 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
@capacity = external dso_local global [1000 x [1000 x i32]], align 16
@e = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [14 x i8], align 1

; 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: nounwind uwtable
declare dso_local i32 @fordFulkerson(i32 noundef, i32 noundef) #1

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

7:                                                ; preds = %29, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %7
  store i32 2, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %32

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %25, %12
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = load i32, ptr @n, align 4, !tbaa !5
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  store i32 5, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %28

18:                                               ; preds = %13
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1000 x [1000 x i32]], ptr @capacity, i64 0, i64 %20
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1000 x i32], ptr %21, i64 0, i64 %23
  store i32 0, ptr %24, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %18
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %4, align 4, !tbaa !5
  br label %13, !llvm.loop !9

28:                                               ; preds = %17
  br label %29

29:                                               ; preds = %28
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !11

32:                                               ; preds = %11
  store i32 6, ptr @n, align 4, !tbaa !5
  store i32 7, ptr @e, align 4, !tbaa !5
  store i32 8, ptr getelementptr inbounds ([1000 x i32], ptr @capacity, i64 0, i64 1), align 4, !tbaa !5
  store i32 3, ptr getelementptr inbounds ([1000 x i32], ptr @capacity, i64 0, i64 4), align 16, !tbaa !5
  store i32 9, ptr getelementptr inbounds ([1000 x i32], ptr getelementptr inbounds ([1000 x [1000 x i32]], ptr @capacity, i64 0, i64 1), i64 0, i64 2), align 8, !tbaa !5
  store i32 7, ptr getelementptr inbounds ([1000 x i32], ptr getelementptr inbounds ([1000 x [1000 x i32]], ptr @capacity, i64 0, i64 2), i64 0, i64 4), align 16, !tbaa !5
  store i32 2, ptr getelementptr inbounds ([1000 x i32], ptr getelementptr inbounds ([1000 x [1000 x i32]], ptr @capacity, i64 0, i64 2), i64 0, i64 5), align 4, !tbaa !5
  store i32 5, ptr getelementptr inbounds ([1000 x i32], ptr getelementptr inbounds ([1000 x [1000 x i32]], ptr @capacity, i64 0, i64 3), i64 0, i64 5), align 4, !tbaa !5
  store i32 7, ptr getelementptr inbounds ([1000 x i32], ptr getelementptr inbounds ([1000 x [1000 x i32]], ptr @capacity, i64 0, i64 4), i64 0, i64 2), align 8, !tbaa !5
  store i32 4, ptr getelementptr inbounds ([1000 x i32], ptr getelementptr inbounds ([1000 x [1000 x i32]], ptr @capacity, i64 0, i64 4), i64 0, i64 3), align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 5, ptr %6, align 4, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = call i32 @fordFulkerson(i32 noundef %33, i32 noundef %34)
  %36 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %35)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %37 = load i32, ptr %1, align 4
  ret i32 %37
}

declare i32 @printf(ptr noundef, ...) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { "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/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
@capacity = external dso_local local_unnamed_addr global [1000 x [1000 x i32]], align 16
@e = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @fordFulkerson(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.preheader.us.preheader, label %._crit_edge12

.preheader.us.preheader:                          ; preds = %0
  %3 = zext nneg i32 %1 to i64
  %4 = shl nuw nsw i64 %3, 2
  %xtraiter = and i64 %3, 7
  %5 = icmp ult i32 %1, 8
  br i1 %5, label %._crit_edge12.loopexit.unr-lcssa, label %.preheader.us.preheader.new

.preheader.us.preheader.new:                      ; preds = %.preheader.us.preheader
  %unroll_iter = and i64 %3, 2147483640
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us, %.preheader.us.preheader.new
  %indvar = phi i64 [ 0, %.preheader.us.preheader.new ], [ %indvar.next.7, %.preheader.us ]
  %niter = phi i64 [ 0, %.preheader.us.preheader.new ], [ %niter.next.7, %.preheader.us ]
  %6 = mul nuw nsw i64 %indvar, 4000
  %scevgep = getelementptr i8, ptr @capacity, i64 %6
  tail call void @llvm.memset.p0.i64(ptr align 16 %scevgep, i8 0, i64 %4, i1 false), !tbaa !5
  %7 = mul nuw i64 %indvar, 4000
  %gep = getelementptr i8, ptr getelementptr (i8, ptr @capacity, i64 4000), i64 %7
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep, i8 0, i64 %4, i1 false), !tbaa !5
  %8 = mul nuw i64 %indvar, 4000
  %gep15 = getelementptr i8, ptr getelementptr (i8, ptr @capacity, i64 8000), i64 %8
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep15, i8 0, i64 %4, i1 false), !tbaa !5
  %9 = mul nuw i64 %indvar, 4000
  %gep16 = getelementptr i8, ptr getelementptr (i8, ptr @capacity, i64 12000), i64 %9
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep16, i8 0, i64 %4, i1 false), !tbaa !5
  %10 = mul nuw i64 %indvar, 4000
  %gep17 = getelementptr i8, ptr getelementptr (i8, ptr @capacity, i64 16000), i64 %10
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep17, i8 0, i64 %4, i1 false), !tbaa !5
  %11 = mul nuw i64 %indvar, 4000
  %gep18 = getelementptr i8, ptr getelementptr (i8, ptr @capacity, i64 20000), i64 %11
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep18, i8 0, i64 %4, i1 false), !tbaa !5
  %12 = mul nuw i64 %indvar, 4000
  %gep19 = getelementptr i8, ptr getelementptr (i8, ptr @capacity, i64 24000), i64 %12
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep19, i8 0, i64 %4, i1 false), !tbaa !5
  %13 = mul nuw i64 %indvar, 4000
  %gep20 = getelementptr i8, ptr getelementptr (i8, ptr @capacity, i64 28000), i64 %13
  tail call void @llvm.memset.p0.i64(ptr align 16 %gep20, i8 0, i64 %4, i1 false), !tbaa !5
  %indvar.next.7 = add nuw nsw i64 %indvar, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge12.loopexit.unr-lcssa, label %.preheader.us, !llvm.loop !9

._crit_edge12.loopexit.unr-lcssa:                 ; preds = %.preheader.us, %.preheader.us.preheader
  %indvar.unr = phi i64 [ 0, %.preheader.us.preheader ], [ %indvar.next.7, %.preheader.us ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge12, label %.preheader.us.epil

.preheader.us.epil:                               ; preds = %._crit_edge12.loopexit.unr-lcssa, %.preheader.us.epil
  %indvar.epil = phi i64 [ %indvar.next.epil, %.preheader.us.epil ], [ %indvar.unr, %._crit_edge12.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader.us.epil ], [ 0, %._crit_edge12.loopexit.unr-lcssa ]
  %14 = mul nuw nsw i64 %indvar.epil, 4000
  %scevgep.epil = getelementptr i8, ptr @capacity, i64 %14
  tail call void @llvm.memset.p0.i64(ptr align 16 %scevgep.epil, i8 0, i64 %4, i1 false), !tbaa !5
  %indvar.next.epil = add nuw nsw i64 %indvar.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge12, label %.preheader.us.epil, !llvm.loop !11

._crit_edge12:                                    ; preds = %._crit_edge12.loopexit.unr-lcssa, %.preheader.us.epil, %0
  store i32 6, ptr @n, align 4, !tbaa !5
  store i32 7, ptr @e, align 4, !tbaa !5
  store i32 8, ptr getelementptr inbounds (i8, ptr @capacity, i64 4), align 4, !tbaa !5
  store i32 3, ptr getelementptr inbounds (i8, ptr @capacity, i64 16), align 16, !tbaa !5
  store i32 9, ptr getelementptr inbounds (i8, ptr @capacity, i64 4008), align 8, !tbaa !5
  store i32 7, ptr getelementptr inbounds (i8, ptr @capacity, i64 8016), align 16, !tbaa !5
  store i32 2, ptr getelementptr inbounds (i8, ptr @capacity, i64 8020), align 4, !tbaa !5
  store i32 5, ptr getelementptr inbounds (i8, ptr @capacity, i64 12020), align 4, !tbaa !5
  store i32 7, ptr getelementptr inbounds (i8, ptr @capacity, i64 16008), align 8, !tbaa !5
  store i32 4, ptr getelementptr inbounds (i8, ptr @capacity, i64 16012), align 4, !tbaa !5
  %15 = tail call i32 @fordFulkerson(i32 noundef 0, i32 noundef 5)
  %16 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %15)
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

attributes #0 = { 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 = { nofree nounwind "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) }

!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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}


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