; LLVM IR from TrainingByPackt/CPP-Data-Structures-and-Algorithm-Design-Principles :: Lesson4/Exercise23/mapreduce_primecheck.ll (ZN16prime_calculator8is_primeEl)

; 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: 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 uwtable
define dso_local noundef zeroext i1 @_ZN16prime_calculator8is_primeEl(i64 noundef %0) #1 {
  %2 = alloca i1, align 1
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = icmp sgt i64 %8, 2
  br i1 %9, label %10, label %43

10:                                               ; preds = %1
  %11 = load i64, ptr %3, align 8, !tbaa !5
  %12 = srem i64 %11, 2
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %10
  store i1 false, ptr %2, align 1
  br label %52

15:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = call noundef i64 @_ZSt3absl(i64 noundef %16)
  store i64 %17, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = sitofp i64 %18 to double
  %20 = call double @sqrt(double noundef %19) #4
  %21 = fptosi double %20 to i64
  store i64 %21, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 3, ptr %6, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %34, %15
  %23 = load i64, ptr %6, align 8, !tbaa !5
  %24 = load i64, ptr %5, align 8, !tbaa !5
  %25 = icmp sle i64 %23, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %22
  store i32 2, ptr %7, align 4
  br label %37

27:                                               ; preds = %22
  %28 = load i64, ptr %4, align 8, !tbaa !5
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = srem i64 %28, %29
  %31 = icmp eq i64 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %27
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %37

33:                                               ; preds = %27
  br label %34

34:                                               ; preds = %33
  %35 = load i64, ptr %6, align 8, !tbaa !5
  %36 = add nsw i64 %35, 2
  store i64 %36, ptr %6, align 8, !tbaa !5
  br label %22, !llvm.loop !9

37:                                               ; preds = %32, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %38 = load i32, ptr %7, align 4
  switch i32 %38, label %40 [
    i32 2, label %39
  ]

39:                                               ; preds = %37
  store i32 0, ptr %7, align 4
  br label %40

40:                                               ; preds = %39, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %41 = load i32, ptr %7, align 4
  switch i32 %41, label %54 [
    i32 0, label %42
    i32 1, label %52
  ]

42:                                               ; preds = %40
  br label %51

43:                                               ; preds = %1
  %44 = load i64, ptr %3, align 8, !tbaa !5
  %45 = icmp eq i64 %44, 0
  br i1 %45, label %49, label %46

46:                                               ; preds = %43
  %47 = load i64, ptr %3, align 8, !tbaa !5
  %48 = icmp eq i64 %47, 1
  br i1 %48, label %49, label %50

49:                                               ; preds = %46, %43
  store i1 false, ptr %2, align 1
  br label %52

50:                                               ; preds = %46
  br label %51

51:                                               ; preds = %50, %42
  store i1 true, ptr %2, align 1
  br label %52

52:                                               ; preds = %51, %49, %40, %14
  %53 = load i1, ptr %2, align 1
  ret i1 %53

54:                                               ; preds = %40
  unreachable
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZSt3absl(i64 noundef) #2

; Function Attrs: nounwind
declare double @sqrt(double noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint 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 #3 = { 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" }
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 = !{!"long", !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"

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN16prime_calculator8is_primeEl(i64 noundef %0) local_unnamed_addr #0 {
  %2 = icmp sgt i64 %0, 2
  br i1 %2, label %3, label %15

3:                                                ; preds = %1
  %4 = and i64 %0, 1
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %3
  %7 = tail call noundef i64 @_ZSt3absl(i64 noundef %0)
  %8 = sitofp i64 %7 to double
  %9 = tail call double @sqrt(double noundef %8) #3
  %10 = fptosi double %9 to i64
  %.not.not19 = icmp slt i64 %10, 3
  br i1 %.not.not19, label %.thread, label %.lr.ph

11:                                               ; preds = %.lr.ph
  %12 = add nuw nsw i64 %.01420, 2
  %.not.not = icmp sgt i64 %12, %10
  br i1 %.not.not, label %.thread, label %.lr.ph, !llvm.loop !5

.lr.ph:                                           ; preds = %6, %11
  %.01420 = phi i64 [ %12, %11 ], [ 3, %6 ]
  %13 = srem i64 %7, %.01420
  %14 = icmp eq i64 %13, 0
  br i1 %14, label %.loopexit, label %11

15:                                               ; preds = %1
  %or.cond = icmp ult i64 %0, 2
  br i1 %or.cond, label %.loopexit, label %.thread

.thread:                                          ; preds = %11, %6, %15
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %15, %3, %.thread
  %.1 = phi i1 [ true, %.thread ], [ false, %3 ], [ false, %15 ], [ false, %.lr.ph ]
  ret i1 %.1
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZSt3absl(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #2

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 = { inlinehint 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 = { mustprogress nofree nounwind willreturn memory(write) "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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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