; LLVM IR from abeaumont/competitive-programming :: cses/1739.ll (Z5countiiii)

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

@k = external dso_local global i32, align 4

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5countiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !5
  %10 = load i32, ptr @k, align 4, !tbaa !5
  %11 = sub nsw i32 %10, 1
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = add nsw i32 %12, %11
  store i32 %13, ptr %5, align 4, !tbaa !5
  %14 = load i32, ptr @k, align 4, !tbaa !5
  %15 = sub nsw i32 %14, 1
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = add nsw i32 %16, %15
  store i32 %17, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %46, %4
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = icmp sle i32 %19, %20
  br i1 %21, label %22, label %51

22:                                               ; preds = %18
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = srem i32 %23, 2
  %25 = icmp eq i32 %24, 1
  br i1 %25, label %26, label %34

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

34:                                               ; preds = %26, %22
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = srem i32 %35, 2
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %46

38:                                               ; preds = %34
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = add nsw i32 %39, -1
  store i32 %40, ptr %6, align 4, !tbaa !5
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = call noundef i32 @_Z6countxiii(i32 noundef %39, i32 noundef %41, i32 noundef %42)
  %44 = load i32, ptr %9, align 4, !tbaa !5
  %45 = add nsw i32 %44, %43
  store i32 %45, ptr %9, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %38, %34
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sdiv i32 %47, 2
  store i32 %48, ptr %5, align 4, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = sdiv i32 %49, 2
  store i32 %50, ptr %6, align 4, !tbaa !5
  br label %18, !llvm.loop !9

51:                                               ; preds = %18
  %52 = load i32, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret i32 %52
}

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


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

@k = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5countiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %.not21 = icmp sgt i32 %0, %1
  br i1 %.not21, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %5 = load i32, ptr @k, align 4, !tbaa !5
  %6 = add nsw i32 %5, -1
  %7 = add nsw i32 %6, %1
  %8 = add nsw i32 %6, %0
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %22
  %.024 = phi i32 [ %.2, %22 ], [ 0, %.lr.ph.preheader ]
  %.01723 = phi i32 [ %23, %22 ], [ %8, %.lr.ph.preheader ]
  %.01922 = phi i32 [ %24, %22 ], [ %7, %.lr.ph.preheader ]
  %9 = and i32 %.01723, -2147483647
  %10 = icmp eq i32 %9, 1
  br i1 %10, label %11, label %15

11:                                               ; preds = %.lr.ph
  %12 = add nuw nsw i32 %.01723, 1
  %13 = tail call noundef i32 @_Z6countxiii(i32 noundef %.01723, i32 noundef %2, i32 noundef %3)
  %14 = add nsw i32 %13, %.024
  br label %15

15:                                               ; preds = %11, %.lr.ph
  %.118 = phi i32 [ %12, %11 ], [ %.01723, %.lr.ph ]
  %.1 = phi i32 [ %14, %11 ], [ %.024, %.lr.ph ]
  %16 = and i32 %.01922, 1
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %22

18:                                               ; preds = %15
  %19 = add nsw i32 %.01922, -1
  %20 = tail call noundef i32 @_Z6countxiii(i32 noundef %.01922, i32 noundef %2, i32 noundef %3)
  %21 = add nsw i32 %20, %.1
  br label %22

22:                                               ; preds = %18, %15
  %.120 = phi i32 [ %19, %18 ], [ %.01922, %15 ]
  %.2 = phi i32 [ %21, %18 ], [ %.1, %15 ]
  %23 = sdiv i32 %.118, 2
  %24 = sdiv i32 %.120, 2
  %.not = icmp sgt i32 %23, %24
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %22, %4
  %.0.lcssa = phi i32 [ 0, %4 ], [ %.2, %22 ]
  ret i32 %.0.lcssa
}

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


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