; LLVM IR from ayush-tulsyan/ACM-ICPC-Handbook :: code/MillerRabin.ll (Z7Witnessxx)

; 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 nounwind uwtable
declare dso_local noundef i64 @_Z21ModularMultiplicationxxx(i64 noundef, i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z21ModularExponentiationxxx(i64 noundef, i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z7Witnessxx(i64 noundef %0, i64 noundef %1) #1 {
  %3 = alloca i1, align 1
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = sub nsw i64 %12, 1
  store i64 %13, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %19, %2
  %15 = load i64, ptr %6, align 8, !tbaa !5
  %16 = and i64 %15, 1
  %17 = icmp ne i64 %16, 0
  %18 = xor i1 %17, true
  br i1 %18, label %19, label %24

19:                                               ; preds = %14
  %20 = load i64, ptr %6, align 8, !tbaa !5
  %21 = ashr i64 %20, 1
  store i64 %21, ptr %6, align 8, !tbaa !5
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %7, align 4, !tbaa !9
  br label %14, !llvm.loop !11

24:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %25 = load i64, ptr %4, align 8, !tbaa !5
  %26 = load i64, ptr %6, align 8, !tbaa !5
  %27 = load i64, ptr %5, align 8, !tbaa !5
  %28 = call noundef i64 @_Z21ModularExponentiationxxx(i64 noundef %25, i64 noundef %26, i64 noundef %27)
  store i64 %28, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 1, ptr %10, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %52, %24
  %30 = load i32, ptr %10, align 4, !tbaa !9
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = icmp sle i32 %30, %31
  br i1 %32, label %34, label %33

33:                                               ; preds = %29
  store i32 4, ptr %11, align 4
  br label %55

34:                                               ; preds = %29
  %35 = load i64, ptr %8, align 8, !tbaa !5
  %36 = load i64, ptr %8, align 8, !tbaa !5
  %37 = load i64, ptr %5, align 8, !tbaa !5
  %38 = call noundef i64 @_Z21ModularMultiplicationxxx(i64 noundef %35, i64 noundef %36, i64 noundef %37)
  store i64 %38, ptr %9, align 8, !tbaa !5
  %39 = load i64, ptr %9, align 8, !tbaa !5
  %40 = icmp eq i64 %39, 1
  br i1 %40, label %41, label %50

41:                                               ; preds = %34
  %42 = load i64, ptr %8, align 8, !tbaa !5
  %43 = icmp ne i64 %42, 1
  br i1 %43, label %44, label %50

44:                                               ; preds = %41
  %45 = load i64, ptr %8, align 8, !tbaa !5
  %46 = load i64, ptr %5, align 8, !tbaa !5
  %47 = sub nsw i64 %46, 1
  %48 = icmp ne i64 %45, %47
  br i1 %48, label %49, label %50

49:                                               ; preds = %44
  store i1 true, ptr %3, align 1
  store i32 1, ptr %11, align 4
  br label %55

50:                                               ; preds = %44, %41, %34
  %51 = load i64, ptr %9, align 8, !tbaa !5
  store i64 %51, ptr %8, align 8, !tbaa !5
  br label %52

52:                                               ; preds = %50
  %53 = load i32, ptr %10, align 4, !tbaa !9
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %10, align 4, !tbaa !9
  br label %29, !llvm.loop !13

55:                                               ; preds = %49, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %56 = load i32, ptr %11, align 4
  switch i32 %56, label %62 [
    i32 4, label %57
  ]

57:                                               ; preds = %55
  %58 = load i64, ptr %8, align 8, !tbaa !5
  %59 = icmp ne i64 %58, 1
  br i1 %59, label %60, label %61

60:                                               ; preds = %57
  store i1 true, ptr %3, align 1
  store i32 1, ptr %11, align 4
  br label %62

61:                                               ; preds = %57
  store i1 false, ptr %3, align 1
  store i32 1, ptr %11, align 4
  br label %62

62:                                               ; preds = %61, %60, %55
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %63 = load i1, ptr %3, align 1
  ret i1 %63
}

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


; 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
declare dso_local noundef i64 @_Z21ModularMultiplicationxxx(i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z21ModularExponentiationxxx(i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z7Witnessxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = add nsw i64 %1, -1
  %4 = and i64 %3, 1
  %.not34 = icmp eq i64 %4, 0
  br i1 %.not34, label %.lr.ph, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %2
  %5 = tail call noundef i64 @_Z21ModularExponentiationxxx(i64 noundef %0, i64 noundef %3, i64 noundef %1)
  br label %.critedge

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.02436 = phi i32 [ %7, %.lr.ph ], [ 0, %2 ]
  %.02535 = phi i64 [ %6, %.lr.ph ], [ %3, %2 ]
  %6 = ashr exact i64 %.02535, 1
  %7 = add nuw nsw i32 %.02436, 1
  %8 = and i64 %.02535, 2
  %.not = icmp eq i64 %8, 0
  br i1 %.not, label %.lr.ph, label %.lr.ph42.preheader, !llvm.loop !5

.lr.ph42.preheader:                               ; preds = %.lr.ph
  %9 = tail call noundef i64 @_Z21ModularExponentiationxxx(i64 noundef %0, i64 noundef %6, i64 noundef %1)
  br label %.lr.ph42

10:                                               ; preds = %.lr.ph42
  %11 = add nuw i32 %.02140, 1
  %exitcond.not = icmp eq i32 %.02140, %7
  br i1 %exitcond.not, label %.critedge, label %.lr.ph42, !llvm.loop !7

.lr.ph42:                                         ; preds = %.lr.ph42.preheader, %10
  %.02140 = phi i32 [ %11, %10 ], [ 1, %.lr.ph42.preheader ]
  %.02339 = phi i64 [ %12, %10 ], [ %9, %.lr.ph42.preheader ]
  %12 = tail call noundef i64 @_Z21ModularMultiplicationxxx(i64 noundef %.02339, i64 noundef %.02339, i64 noundef %1)
  %13 = icmp ne i64 %12, 1
  %14 = icmp eq i64 %.02339, 1
  %or.cond.not32 = or i1 %14, %13
  %.not27 = icmp eq i64 %.02339, %3
  %or.cond29 = or i1 %or.cond.not32, %.not27
  br i1 %or.cond29, label %10, label %.loopexit

.critedge:                                        ; preds = %10, %._crit_edge.thread
  %.023.lcssa = phi i64 [ %5, %._crit_edge.thread ], [ %12, %10 ]
  %.not28 = icmp ne i64 %.023.lcssa, 1
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph42, %.critedge
  %.1 = phi i1 [ %.not28, %.critedge ], [ true, %.lr.ph42 ]
  ret i1 %.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" }

!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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass