; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Codechef/philips (1)

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

; Function Attrs: nounwind uwtable
define dso_local i32 @count(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !9
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %16, label %12

12:                                               ; preds = %4
  %13 = load i32, ptr %7, align 4, !tbaa !5
  %14 = load i32, ptr %8, align 4, !tbaa !5
  %15 = icmp sgt i32 %13, %14
  br i1 %15, label %16, label %17

16:                                               ; preds = %12, %4
  store i32 0, ptr %5, align 4
  br label %36

17:                                               ; preds = %12
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  store i32 1, ptr %5, align 4
  br label %36

21:                                               ; preds = %17
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  %25 = load i32, ptr %8, align 4, !tbaa !5
  %26 = load ptr, ptr %9, align 8, !tbaa !9
  %27 = call i32 @count(i32 noundef %22, i32 noundef %24, i32 noundef %25, ptr noundef %26)
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = sub nsw i32 %28, %29
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = load ptr, ptr %9, align 8, !tbaa !9
  %34 = call i32 @count(i32 noundef %30, i32 noundef %31, i32 noundef %32, ptr noundef %33)
  %35 = add nsw i32 %27, %34
  store i32 %35, ptr %5, align 4
  br label %36

36:                                               ; preds = %21, %20, %16
  %37 = load i32, ptr %5, align 4
  ret i32 %37
}

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

!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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}


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

; Function Attrs: nofree nosync nounwind memory(none) uwtable
define dso_local i32 @count(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef readnone %3) local_unnamed_addr #0 {
  %5 = icmp sgt i32 %1, %2
  %6 = icmp slt i32 %0, 0
  %or.cond19 = or i1 %5, %6
  br i1 %or.cond19, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %7 = add nsw i32 %1, 1
  br label %8

8:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr21 = phi i32 [ %0, %.lr.ph ], [ %11, %tailrecurse ]
  %accumulator.tr20 = phi i32 [ 0, %.lr.ph ], [ %12, %tailrecurse ]
  %9 = icmp eq i32 %.tr21, 0
  br i1 %9, label %._crit_edge.loopexit, label %tailrecurse

tailrecurse:                                      ; preds = %8
  %10 = tail call i32 @count(i32 noundef %.tr21, i32 noundef %7, i32 noundef %2, ptr noundef %3)
  %11 = sub nsw i32 %.tr21, %1
  %12 = add nsw i32 %10, %accumulator.tr20
  %13 = icmp slt i32 %11, 0
  br i1 %13, label %._crit_edge.loopexit, label %8

._crit_edge.loopexit:                             ; preds = %8, %tailrecurse
  %accumulator.tr.lcssa.ph = phi i32 [ %12, %tailrecurse ], [ %accumulator.tr20, %8 ]
  %.0.ph = phi i32 [ 0, %tailrecurse ], [ 1, %8 ]
  %14 = add nsw i32 %.0.ph, %accumulator.tr.lcssa.ph
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %4
  %accumulator.ret.tr = phi i32 [ 0, %4 ], [ %14, %._crit_edge.loopexit ]
  ret i32 %accumulator.ret.tr
}

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


; Effective O3 passes: GlobalOptPass,InstCombinePass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass