; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/Bayan 2015 Warmup/Problem C/C.ll (Z2goiii)

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

@cnt = external dso_local global i32, align 4
@g = external dso_local global [1002 x [1002 x i32]], align 16
@x = external dso_local global i32, align 4
@y = external dso_local global i32, align 4

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z2goiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !5
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr @x, align 4, !tbaa !5
  %9 = sub nsw i32 %7, %8
  %10 = add nsw i32 %9, 1
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = load i32, ptr @y, align 4, !tbaa !5
  %13 = sub nsw i32 %11, %12
  %14 = add nsw i32 %13, 1
  %15 = load i32, ptr @x, align 4, !tbaa !5
  %16 = load i32, ptr @y, align 4, !tbaa !5
  %17 = call noundef i32 @_Z4rectiiii(i32 noundef %10, i32 noundef %14, i32 noundef %15, i32 noundef %16)
  %18 = load i32, ptr @x, align 4, !tbaa !5
  %19 = load i32, ptr @y, align 4, !tbaa !5
  %20 = mul nsw i32 %18, %19
  %21 = icmp slt i32 %17, %20
  br i1 %21, label %22, label %23

22:                                               ; preds = %3
  br label %64

23:                                               ; preds = %3
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = icmp eq i32 %24, 1
  br i1 %25, label %26, label %30

26:                                               ; preds = %23
  %27 = load i32, ptr @x, align 4, !tbaa !5
  %28 = load i32, ptr @cnt, align 4, !tbaa !5
  %29 = sub nsw i32 %28, %27
  store i32 %29, ptr @cnt, align 4, !tbaa !5
  br label %34

30:                                               ; preds = %23
  %31 = load i32, ptr @y, align 4, !tbaa !5
  %32 = load i32, ptr @cnt, align 4, !tbaa !5
  %33 = sub nsw i32 %32, %31
  store i32 %33, ptr @cnt, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %30, %26
  %35 = load i32, ptr @x, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1002 x [1002 x i32]], ptr @g, i64 0, i64 %36
  %38 = load i32, ptr @y, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [1002 x i32], ptr %37, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %48

44:                                               ; preds = %34
  %45 = load i32, ptr @x, align 4, !tbaa !5
  %46 = load i32, ptr @y, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  call void @_Z2goiii(i32 noundef %45, i32 noundef %47, i32 noundef 1)
  br label %63

48:                                               ; preds = %34
  %49 = load i32, ptr @x, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [1002 x [1002 x i32]], ptr @g, i64 0, i64 %51
  %53 = load i32, ptr @y, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [1002 x i32], ptr %52, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %62

58:                                               ; preds = %48
  %59 = load i32, ptr @x, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  %61 = load i32, ptr @y, align 4, !tbaa !5
  call void @_Z2goiii(i32 noundef %60, i32 noundef %61, i32 noundef 2)
  br label %62

62:                                               ; preds = %58, %48
  br label %63

63:                                               ; preds = %62, %44
  br label %64

64:                                               ; preds = %63, %22
  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 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"}


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

@cnt = external dso_local local_unnamed_addr global i32, align 4
@g = external dso_local local_unnamed_addr global [1002 x [1002 x i32]], align 16
@x = external dso_local local_unnamed_addr global i32, align 4
@y = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z2goiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.pre = load i32, ptr @x, align 4, !tbaa !5
  %.pre7 = load i32, ptr @y, align 4, !tbaa !5
  %4 = add i32 %0, 1
  %5 = sub i32 %4, %.pre
  %6 = add i32 %1, 1
  %7 = sub i32 %6, %.pre7
  %8 = tail call noundef i32 @_Z4rectiiii(i32 noundef %5, i32 noundef %7, i32 noundef %.pre, i32 noundef %.pre7)
  %9 = load i32, ptr @x, align 4, !tbaa !5
  %10 = load i32, ptr @y, align 4, !tbaa !5
  %11 = mul nsw i32 %10, %9
  %12 = icmp slt i32 %8, %11
  br i1 %12, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %tailrecurse.backedge
  %13 = phi i32 [ %28, %tailrecurse.backedge ], [ %10, %3 ]
  %14 = phi i32 [ %27, %tailrecurse.backedge ], [ %9, %3 ]
  %.tr58 = phi i32 [ %.tr5.be, %tailrecurse.backedge ], [ %2, %3 ]
  %15 = icmp eq i32 %.tr58, 1
  %16 = load i32, ptr @cnt, align 4
  %storemerge.v = select i1 %15, i32 %14, i32 %13
  %storemerge = sub nsw i32 %16, %storemerge.v
  store i32 %storemerge, ptr @cnt, align 4, !tbaa !5
  %17 = sext i32 %14 to i64
  %18 = add nsw i32 %13, 1
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [1002 x [1002 x i32]], ptr @g, i64 0, i64 %17, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %.not = icmp eq i32 %21, 0
  br i1 %.not, label %31, label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %.lr.ph, %31
  %.tr.be = phi i32 [ %14, %.lr.ph ], [ %32, %31 ]
  %.tr4.be = phi i32 [ %18, %.lr.ph ], [ %13, %31 ]
  %.tr5.be = phi i32 [ 1, %.lr.ph ], [ 2, %31 ]
  %22 = add i32 %.tr.be, 1
  %23 = sub i32 %22, %14
  %24 = add i32 %.tr4.be, 1
  %25 = sub i32 %24, %13
  %26 = tail call noundef i32 @_Z4rectiiii(i32 noundef %23, i32 noundef %25, i32 noundef %14, i32 noundef %13)
  %27 = load i32, ptr @x, align 4, !tbaa !5
  %28 = load i32, ptr @y, align 4, !tbaa !5
  %29 = mul nsw i32 %28, %27
  %30 = icmp slt i32 %26, %29
  br i1 %30, label %._crit_edge, label %.lr.ph

31:                                               ; preds = %.lr.ph
  %32 = add nsw i32 %14, 1
  %33 = sext i32 %32 to i64
  %34 = sext i32 %13 to i64
  %35 = getelementptr inbounds [1002 x [1002 x i32]], ptr @g, i64 0, i64 %33, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %.not3 = icmp eq i32 %36, 0
  br i1 %.not3, label %._crit_edge, label %tailrecurse.backedge

._crit_edge:                                      ; preds = %tailrecurse.backedge, %31, %3
  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" }

!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"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass