; LLVM IR from tangjz/acm-icpc :: usaco/Training/Chapter 1 Getting started/sprime.ll (Z3dfsi)

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

@n = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z5primei(i32 noundef) #0

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3dfsi(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %6 = icmp sge i32 %4, %5
  br i1 %6, label %7, label %10

7:                                                ; preds = %1
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %8)
  br label %30

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %27, %10
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp sle i32 %12, 9
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %30

15:                                               ; preds = %11
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = mul nsw i32 %16, 10
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = add nsw i32 %17, %18
  %20 = call noundef zeroext i1 @_Z5primei(i32 noundef %19)
  br i1 %20, label %21, label %26

21:                                               ; preds = %15
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = mul nsw i32 %22, 10
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = add nsw i32 %23, %24
  call void @_Z3dfsi(i32 noundef %25)
  br label %26

26:                                               ; preds = %21, %15
  br label %27

27:                                               ; preds = %26
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %3, align 4, !tbaa !5
  br label %11, !llvm.loop !9

30:                                               ; preds = %14, %7
  ret void
}

declare i32 @printf(ptr noundef, ...) #3

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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++ 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"

@n = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z5primei(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3dfsi(i32 noundef %0) local_unnamed_addr #0 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %37, %1
  %.tr = phi i32 [ %0, %1 ], [ %38, %37 ]
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not = icmp sgt i32 %2, %.tr
  br i1 %.not, label %.preheader, label %6

.preheader:                                       ; preds = %tailrecurse
  %3 = mul nsw i32 %.tr, 10
  %4 = or disjoint i32 %3, 1
  %5 = tail call noundef zeroext i1 @_Z5primei(i32 noundef %4)
  br i1 %5, label %8, label %9

6:                                                ; preds = %tailrecurse
  %7 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %.tr)
  br label %.loopexit

8:                                                ; preds = %.preheader
  tail call void @_Z3dfsi(i32 noundef %4)
  br label %9

9:                                                ; preds = %.preheader, %8
  %10 = add nsw i32 %3, 2
  %11 = tail call noundef zeroext i1 @_Z5primei(i32 noundef %10)
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  tail call void @_Z3dfsi(i32 noundef %10)
  br label %13

13:                                               ; preds = %12, %9
  %14 = add nsw i32 %3, 3
  %15 = tail call noundef zeroext i1 @_Z5primei(i32 noundef %14)
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  tail call void @_Z3dfsi(i32 noundef %14)
  br label %17

17:                                               ; preds = %16, %13
  %18 = add nsw i32 %3, 4
  %19 = tail call noundef zeroext i1 @_Z5primei(i32 noundef %18)
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  tail call void @_Z3dfsi(i32 noundef %18)
  br label %21

21:                                               ; preds = %20, %17
  %22 = add nsw i32 %3, 5
  %23 = tail call noundef zeroext i1 @_Z5primei(i32 noundef %22)
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  tail call void @_Z3dfsi(i32 noundef %22)
  br label %25

25:                                               ; preds = %24, %21
  %26 = add nsw i32 %3, 6
  %27 = tail call noundef zeroext i1 @_Z5primei(i32 noundef %26)
  br i1 %27, label %28, label %29

28:                                               ; preds = %25
  tail call void @_Z3dfsi(i32 noundef %26)
  br label %29

29:                                               ; preds = %28, %25
  %30 = add nsw i32 %3, 7
  %31 = tail call noundef zeroext i1 @_Z5primei(i32 noundef %30)
  br i1 %31, label %32, label %33

32:                                               ; preds = %29
  tail call void @_Z3dfsi(i32 noundef %30)
  br label %33

33:                                               ; preds = %32, %29
  %34 = add nsw i32 %3, 8
  %35 = tail call noundef zeroext i1 @_Z5primei(i32 noundef %34)
  br i1 %35, label %36, label %37

36:                                               ; preds = %33
  tail call void @_Z3dfsi(i32 noundef %34)
  br label %37

37:                                               ; preds = %36, %33
  %38 = add nsw i32 %3, 9
  %39 = tail call noundef zeroext i1 @_Z5primei(i32 noundef %38)
  br i1 %39, label %tailrecurse, label %.loopexit

.loopexit:                                        ; preds = %37, %6
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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" }
attributes #1 = { nofree nounwind "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"}


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