; LLVM IR from daanx/mimalloc-bench :: bench/cfrac (pfactor)

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

@stdin = external global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @main(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  store i32 0, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store ptr null, ptr %6, align 8, !tbaa !9
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = add nsw i32 %7, -1
  store i32 %8, ptr %4, align 4, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %24

11:                                               ; preds = %2
  br label %12

12:                                               ; preds = %19, %11
  %13 = load ptr, ptr %5, align 8, !tbaa !9
  %14 = getelementptr inbounds ptr, ptr %13, i32 1
  store ptr %14, ptr %5, align 8, !tbaa !9
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = call ptr @atop(ptr noundef %15)
  %17 = call ptr @psetq(ptr noundef %6, ptr noundef %16)
  %18 = load ptr, ptr %6, align 8, !tbaa !9
  call void @showfactors(ptr noundef %18)
  br label %19

19:                                               ; preds = %12
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = add nsw i32 %20, -1
  store i32 %21, ptr %4, align 4, !tbaa !5
  %22 = icmp sgt i32 %21, 0
  br i1 %22, label %12, label %23, !llvm.loop !11

23:                                               ; preds = %19
  br label %36

24:                                               ; preds = %2
  br label %25

25:                                               ; preds = %34, %24
  %26 = load ptr, ptr @stdin, align 8, !tbaa !9
  %27 = call ptr @fgetp(ptr noundef %26)
  %28 = call ptr @psetq(ptr noundef %6, ptr noundef %27)
  %29 = load ptr, ptr %6, align 8, !tbaa !9
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %32

31:                                               ; preds = %25
  br label %35

32:                                               ; preds = %25
  %33 = load ptr, ptr %6, align 8, !tbaa !9
  call void @showfactors(ptr noundef %33)
  br label %34

34:                                               ; preds = %32
  br i1 true, label %25, label %35

35:                                               ; preds = %34, %31
  br label %36

36:                                               ; preds = %35, %23
  %37 = load ptr, ptr %6, align 8, !tbaa !9
  call void @pdestroy(ptr noundef %37)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  ret i32 0
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @psetq(ptr noundef, ptr noundef) #2

declare ptr @atop(ptr noundef) #3

declare ptr @fgetp(ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @pdestroy(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare dso_local void @showfactors(ptr noundef) #0

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { "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 #4 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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"

@stdin = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  store ptr null, ptr %3, align 8, !tbaa !5
  %4 = add nsw i32 %0, -1
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %.preheader, label %.preheader6

.preheader:                                       ; preds = %2
  %5 = load ptr, ptr @stdin, align 8, !tbaa !5
  %6 = tail call ptr @fgetp(ptr noundef %5) #4
  %7 = call ptr @psetq(ptr noundef nonnull %3, ptr noundef %6)
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %.loopexit, label %.lr.ph

.preheader6:                                      ; preds = %2, %.preheader6
  %.03 = phi i32 [ %15, %.preheader6 ], [ %4, %2 ]
  %.0 = phi ptr [ %10, %.preheader6 ], [ %1, %2 ]
  %10 = getelementptr inbounds i8, ptr %.0, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %12 = call ptr @atop(ptr noundef %11) #4
  %13 = call ptr @psetq(ptr noundef nonnull %3, ptr noundef %12)
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  call void @showfactors(ptr noundef %14)
  %15 = add nsw i32 %.03, -1
  %16 = icmp sgt i32 %.03, 1
  br i1 %16, label %.preheader6, label %.loopexit.loopexit8, !llvm.loop !9

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %17 = phi ptr [ %21, %.lr.ph ], [ %8, %.preheader ]
  call void @showfactors(ptr noundef nonnull %17)
  %18 = load ptr, ptr @stdin, align 8, !tbaa !5
  %19 = call ptr @fgetp(ptr noundef %18) #4
  %20 = call ptr @psetq(ptr noundef nonnull %3, ptr noundef %19)
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %.loopexit, label %.lr.ph

.loopexit.loopexit8:                              ; preds = %.preheader6
  %.pre = load ptr, ptr %3, align 8, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %.loopexit.loopexit8, %.preheader
  %23 = phi ptr [ %.pre, %.loopexit.loopexit8 ], [ null, %.preheader ], [ null, %.lr.ph ]
  call void @pdestroy(ptr noundef %23)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  ret i32 0
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @psetq(ptr noundef, ptr noundef) local_unnamed_addr #2

declare ptr @atop(ptr noundef) local_unnamed_addr #3

declare ptr @fgetp(ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @pdestroy(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
declare dso_local void @showfactors(ptr noundef) local_unnamed_addr #0

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { "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 #4 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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