; LLVM IR from ravireddy07/cpp-book :: src/Interview-Problems/n-red-m-blue-candies-arrangements.ll (Z4testiiiii)

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

@limit_red = external dso_local global i32, align 4
@limit_blue = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4testiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %7, align 4, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !5
  %14 = load i32, ptr %8, align 4, !tbaa !5
  %15 = load i32, ptr %9, align 4, !tbaa !5
  %16 = add nsw i32 %14, %15
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %5
  store i32 1, ptr %6, align 4
  br label %55

19:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !5
  %20 = load i32, ptr %8, align 4, !tbaa !5
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %22, label %35

22:                                               ; preds = %19
  %23 = load i32, ptr %10, align 4, !tbaa !5
  %24 = icmp sgt i32 %23, 0
  br i1 %24, label %25, label %35

25:                                               ; preds = %22
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = sub nsw i32 %26, 1
  %28 = load i32, ptr %8, align 4, !tbaa !5
  %29 = sub nsw i32 %28, 1
  %30 = load i32, ptr %9, align 4, !tbaa !5
  %31 = load i32, ptr %10, align 4, !tbaa !5
  %32 = sub nsw i32 %31, 1
  %33 = load i32, ptr @limit_blue, align 4, !tbaa !5
  %34 = call noundef i32 @_Z4testiiiii(i32 noundef %27, i32 noundef %29, i32 noundef %30, i32 noundef %32, i32 noundef %33)
  store i32 %34, ptr %12, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %25, %22, %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !5
  %36 = load i32, ptr %9, align 4, !tbaa !5
  %37 = icmp sgt i32 %36, 0
  br i1 %37, label %38, label %51

38:                                               ; preds = %35
  %39 = load i32, ptr %11, align 4, !tbaa !5
  %40 = icmp sgt i32 %39, 0
  br i1 %40, label %41, label %51

41:                                               ; preds = %38
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = sub nsw i32 %42, 1
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = load i32, ptr %9, align 4, !tbaa !5
  %46 = sub nsw i32 %45, 1
  %47 = load i32, ptr @limit_red, align 4, !tbaa !5
  %48 = load i32, ptr %11, align 4, !tbaa !5
  %49 = sub nsw i32 %48, 1
  %50 = call noundef i32 @_Z4testiiiii(i32 noundef %43, i32 noundef %44, i32 noundef %46, i32 noundef %47, i32 noundef %49)
  store i32 %50, ptr %13, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %41, %38, %35
  %52 = load i32, ptr %12, align 4, !tbaa !5
  %53 = load i32, ptr %13, align 4, !tbaa !5
  %54 = add nsw i32 %52, %53
  store i32 %54, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %55

55:                                               ; preds = %51, %18
  %56 = load i32, ptr %6, align 4
  ret i32 %56
}

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

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

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 = { 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 = !{!"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"

@limit_red = external dso_local local_unnamed_addr global i32, align 4
@limit_blue = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z4testiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = sub i32 0, %2
  %7 = icmp eq i32 %6, %1
  br i1 %7, label %28, label %8

8:                                                ; preds = %5
  %9 = icmp sgt i32 %1, 0
  %10 = icmp sgt i32 %3, 0
  %or.cond = and i1 %9, %10
  br i1 %or.cond, label %11, label %17

11:                                               ; preds = %8
  %12 = add nsw i32 %0, -1
  %13 = add nsw i32 %1, -1
  %14 = add nsw i32 %3, -1
  %15 = load i32, ptr @limit_blue, align 4, !tbaa !5
  %16 = tail call noundef i32 @_Z4testiiiii(i32 noundef %12, i32 noundef %13, i32 noundef %2, i32 noundef %14, i32 noundef %15)
  br label %17

17:                                               ; preds = %11, %8
  %.020 = phi i32 [ %16, %11 ], [ 0, %8 ]
  %18 = icmp sgt i32 %2, 0
  %19 = icmp sgt i32 %4, 0
  %or.cond3 = and i1 %18, %19
  br i1 %or.cond3, label %20, label %26

20:                                               ; preds = %17
  %21 = add nsw i32 %0, -1
  %22 = add nsw i32 %2, -1
  %23 = load i32, ptr @limit_red, align 4, !tbaa !5
  %24 = add nsw i32 %4, -1
  %25 = tail call noundef i32 @_Z4testiiiii(i32 noundef %21, i32 noundef %1, i32 noundef %22, i32 noundef %23, i32 noundef %24)
  br label %26

26:                                               ; preds = %20, %17
  %.0 = phi i32 [ %25, %20 ], [ 0, %17 ]
  %27 = add nsw i32 %.0, %.020
  br label %28

28:                                               ; preds = %5, %26
  %.021 = phi i32 [ %27, %26 ], [ 1, %5 ]
  ret i32 %.021
}

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass