; LLVM IR from fwip/fastqz :: libzpaq.ll (ZN7libzpaq10StateTable10next_stateERiS1_i)

; 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 uwtable
declare dso_local noundef i32 @_ZN7libzpaq10StateTable10num_statesEii(ptr noundef nonnull align 1 dereferenceable(1024), i32 noundef, i32 noundef) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN7libzpaq10StateTable8discountERi(ptr noundef nonnull align 1 dereferenceable(1024), ptr noundef nonnull align 4 dereferenceable(4)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7libzpaq10StateTable10next_stateERiS1_i(ptr noundef nonnull align 1 dereferenceable(1024) %0, ptr noundef nonnull align 4 dereferenceable(4) %1, ptr noundef nonnull align 4 dereferenceable(4) %2, i32 noundef %3) #0 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !9
  %9 = load ptr, ptr %5, align 8
  %10 = load ptr, ptr %6, align 8, !tbaa !5
  %11 = load i32, ptr %10, align 4, !tbaa !9
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %14 = icmp slt i32 %11, %13
  br i1 %14, label %15, label %20

15:                                               ; preds = %4
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = sub nsw i32 1, %18
  call void @_ZN7libzpaq10StateTable10next_stateERiS1_i(ptr noundef nonnull align 1 dereferenceable(1024) %9, ptr noundef nonnull align 4 dereferenceable(4) %16, ptr noundef nonnull align 4 dereferenceable(4) %17, i32 noundef %19)
  br label %70

20:                                               ; preds = %4
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %28

23:                                               ; preds = %20
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %24, align 4, !tbaa !9
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  call void @_ZN7libzpaq10StateTable8discountERi(ptr noundef nonnull align 1 dereferenceable(1024) %9, ptr noundef nonnull align 4 dereferenceable(4) %27)
  br label %33

28:                                               ; preds = %20
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %29, align 4, !tbaa !9
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  call void @_ZN7libzpaq10StateTable8discountERi(ptr noundef nonnull align 1 dereferenceable(1024) %9, ptr noundef nonnull align 4 dereferenceable(4) %32)
  br label %33

33:                                               ; preds = %28, %23
  br label %34

34:                                               ; preds = %68, %33
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = call noundef i32 @_ZN7libzpaq10StateTable10num_statesEii(ptr noundef nonnull align 1 dereferenceable(1024) %9, i32 noundef %36, i32 noundef %38)
  %40 = icmp ne i32 %39, 0
  %41 = xor i1 %40, true
  br i1 %41, label %42, label %69

42:                                               ; preds = %34
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = icmp slt i32 %44, 2
  br i1 %45, label %46, label %50

46:                                               ; preds = %42
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load i32, ptr %47, align 4, !tbaa !9
  %49 = add nsw i32 %48, -1
  store i32 %49, ptr %47, align 4, !tbaa !9
  br label %68

50:                                               ; preds = %42
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = load i32, ptr %51, align 4, !tbaa !9
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = load i32, ptr %53, align 4, !tbaa !9
  %55 = sub nsw i32 %54, 1
  %56 = mul nsw i32 %52, %55
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = load i32, ptr %57, align 4, !tbaa !9
  %59 = sdiv i32 %58, 2
  %60 = add nsw i32 %56, %59
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = load i32, ptr %61, align 4, !tbaa !9
  %63 = sdiv i32 %60, %62
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %63, ptr %64, align 4, !tbaa !9
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = load i32, ptr %65, align 4, !tbaa !9
  %67 = add nsw i32 %66, -1
  store i32 %67, ptr %65, align 4, !tbaa !9
  br label %68

68:                                               ; preds = %50, %46
  br label %34, !llvm.loop !11

69:                                               ; preds = %34
  br label %70

70:                                               ; preds = %69, %15
  ret void
}

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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN7libzpaq10StateTable10num_statesEii(ptr noundef nonnull align 1 dereferenceable(1024), i32 noundef, i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN7libzpaq10StateTable8discountERi(ptr noundef nonnull align 1 dereferenceable(1024), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7libzpaq10StateTable10next_stateERiS1_i(ptr noundef nonnull align 1 dereferenceable(1024) %0, ptr noundef nonnull align 4 dereferenceable(4) %1, ptr noundef nonnull align 4 dereferenceable(4) %2, i32 noundef %3) local_unnamed_addr #0 align 2 {
  %.pre = load i32, ptr %1, align 4, !tbaa !5
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse, %4
  %5 = phi i32 [ %.pre, %4 ], [ %6, %tailrecurse ]
  %.tr23 = phi ptr [ %1, %4 ], [ %.tr24, %tailrecurse ]
  %.tr24 = phi ptr [ %2, %4 ], [ %.tr23, %tailrecurse ]
  %.tr25 = phi i32 [ %3, %4 ], [ %8, %tailrecurse ]
  %6 = load i32, ptr %.tr24, align 4, !tbaa !5
  %7 = icmp slt i32 %5, %6
  %8 = sub nsw i32 1, %.tr25
  br i1 %7, label %tailrecurse, label %9

9:                                                ; preds = %tailrecurse
  %.not = icmp eq i32 %.tr25, 0
  %.lcssa31..lcssa = select i1 %.not, i32 %5, i32 %6
  %.tr23.lcssa..tr24.lcssa = select i1 %.not, ptr %.tr23, ptr %.tr24
  %.tr24.lcssa..tr23.lcssa = select i1 %.not, ptr %.tr24, ptr %.tr23
  %10 = add nsw i32 %.lcssa31..lcssa, 1
  store i32 %10, ptr %.tr23.lcssa..tr24.lcssa, align 4, !tbaa !5
  tail call void @_ZN7libzpaq10StateTable8discountERi(ptr noundef nonnull align 1 dereferenceable(1024) %0, ptr noundef nonnull align 4 dereferenceable(4) %.tr24.lcssa..tr23.lcssa)
  %11 = load i32, ptr %.tr23, align 4, !tbaa !5
  %12 = load i32, ptr %.tr24, align 4, !tbaa !5
  %13 = tail call noundef i32 @_ZN7libzpaq10StateTable10num_statesEii(ptr noundef nonnull align 1 dereferenceable(1024) %0, i32 noundef %11, i32 noundef %12)
  %.not2227 = icmp eq i32 %13, 0
  br i1 %.not2227, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %9, %27
  %14 = load i32, ptr %.tr24, align 4, !tbaa !5
  %15 = icmp slt i32 %14, 2
  %16 = load i32, ptr %.tr23, align 4, !tbaa !5
  br i1 %15, label %17, label %19

17:                                               ; preds = %.lr.ph
  %18 = add nsw i32 %16, -1
  store i32 %18, ptr %.tr23, align 4, !tbaa !5
  %.pre30 = load i32, ptr %.tr24, align 4, !tbaa !5
  br label %27

19:                                               ; preds = %.lr.ph
  %20 = add nsw i32 %14, -1
  %21 = mul nsw i32 %16, %20
  %22 = lshr i32 %14, 1
  %23 = add nsw i32 %21, %22
  %24 = sdiv i32 %23, %14
  store i32 %24, ptr %.tr23, align 4, !tbaa !5
  %25 = load i32, ptr %.tr24, align 4, !tbaa !5
  %26 = add nsw i32 %25, -1
  store i32 %26, ptr %.tr24, align 4, !tbaa !5
  %.pre29 = load i32, ptr %.tr23, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %19, %17
  %28 = phi i32 [ %26, %19 ], [ %.pre30, %17 ]
  %29 = phi i32 [ %.pre29, %19 ], [ %18, %17 ]
  %30 = tail call noundef i32 @_ZN7libzpaq10StateTable10num_statesEii(ptr noundef nonnull align 1 dereferenceable(1024) %0, i32 noundef %29, i32 noundef %28)
  %.not22 = icmp eq i32 %30, 0
  br i1 %.not22, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %27, %9
  ret void
}

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 = { 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,IPSCCPPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass