; LLVM IR from DionysiosB/CodeForces :: 0500-0599/573A-BearAndPoker.ll (Z12getMinFactorl)

; 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: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z12getMinFactorl(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  br label %3

3:                                                ; preds = %12, %1
  %4 = load i64, ptr %2, align 8, !tbaa !5
  %5 = icmp ne i64 %4, 0
  br i1 %5, label %6, label %10

6:                                                ; preds = %3
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %8 = srem i64 %7, 2
  %9 = icmp eq i64 %8, 0
  br label %10

10:                                               ; preds = %6, %3
  %11 = phi i1 [ false, %3 ], [ %9, %6 ]
  br i1 %11, label %12, label %15

12:                                               ; preds = %10
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = sdiv i64 %13, 2
  store i64 %14, ptr %2, align 8, !tbaa !5
  br label %3, !llvm.loop !9

15:                                               ; preds = %10
  br label %16

16:                                               ; preds = %25, %15
  %17 = load i64, ptr %2, align 8, !tbaa !5
  %18 = icmp ne i64 %17, 0
  br i1 %18, label %19, label %23

19:                                               ; preds = %16
  %20 = load i64, ptr %2, align 8, !tbaa !5
  %21 = srem i64 %20, 3
  %22 = icmp eq i64 %21, 0
  br label %23

23:                                               ; preds = %19, %16
  %24 = phi i1 [ false, %16 ], [ %22, %19 ]
  br i1 %24, label %25, label %28

25:                                               ; preds = %23
  %26 = load i64, ptr %2, align 8, !tbaa !5
  %27 = sdiv i64 %26, 3
  store i64 %27, ptr %2, align 8, !tbaa !5
  br label %16, !llvm.loop !11

28:                                               ; preds = %23
  %29 = load i64, ptr %2, align 8, !tbaa !5
  ret i64 %29
}

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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple 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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i64 @_Z12getMinFactorl(i64 noundef %0) local_unnamed_addr #0 {
  br label %2

2:                                                ; preds = %2, %1
  %.0 = phi i64 [ %0, %1 ], [ %5, %2 ]
  %.not = icmp ne i64 %.0, 0
  %3 = and i64 %.0, 1
  %4 = icmp eq i64 %3, 0
  %or.cond = and i1 %.not, %4
  %5 = ashr exact i64 %.0, 1
  br i1 %or.cond, label %2, label %.critedge.preheader, !llvm.loop !5

.critedge.preheader:                              ; preds = %2
  %6 = srem i64 %.0, 3
  %7 = icmp eq i64 %6, 0
  %or.cond1214 = and i1 %.not, %7
  br i1 %or.cond1214, label %.critedge, label %.critedge2

.critedge:                                        ; preds = %.critedge.preheader, %.critedge
  %.115 = phi i64 [ %8, %.critedge ], [ %.0, %.critedge.preheader ]
  %8 = sdiv i64 %.115, 3
  %9 = add i64 %.115, -3
  %.not9 = icmp ult i64 %9, -5
  %10 = srem i64 %8, 3
  %11 = icmp eq i64 %10, 0
  %or.cond12 = and i1 %.not9, %11
  br i1 %or.cond12, label %.critedge, label %.critedge2, !llvm.loop !7

.critedge2:                                       ; preds = %.critedge, %.critedge.preheader
  %.1.lcssa = phi i64 [ %.0, %.critedge.preheader ], [ %8, %.critedge ]
  ret i64 %.1.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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)"}
!5 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


; Effective O3 passes: GVNPass,GlobalOptPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass