; LLVM IR from tangjz/acm-icpc :: 51nod/1727.ll (Z10getFactExpx)

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

%struct.FactExp = type { i32, [12 x i32] }

@prime = external dso_local global [125000 x i32], align 16

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; 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
define dso_local void @_Z10getFactExpx(ptr dead_on_unwind noalias writable sret(%struct.FactExp) align 4 %0, i64 noundef %1) #2 {
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i64 %1, ptr %3, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 4 %0, i8 0, i64 52, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %6 = load i64, ptr %3, align 8, !tbaa !5
  %7 = sitofp i64 %6 to double
  %8 = call double @sqrt(double noundef %7) #4
  %9 = fptosi double %8 to i32
  store i32 %9, ptr %5, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %62, %2
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [125000 x i32], ptr @prime, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = icmp sle i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %65

18:                                               ; preds = %10
  %19 = load i64, ptr %3, align 8, !tbaa !5
  %20 = load i32, ptr %4, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [125000 x i32], ptr @prime, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = srem i64 %19, %24
  %26 = icmp eq i64 %25, 0
  br i1 %26, label %27, label %61

27:                                               ; preds = %18
  br label %28

28:                                               ; preds = %38, %27
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [125000 x i32], ptr @prime, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = srem i64 %29, %34
  %36 = icmp eq i64 %35, 0
  br i1 %36, label %37, label %53

37:                                               ; preds = %28
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %4, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [125000 x i32], ptr @prime, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = load i64, ptr %3, align 8, !tbaa !5
  %45 = sdiv i64 %44, %43
  store i64 %45, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.FactExp, ptr %0, i32 0, i32 1
  %47 = getelementptr inbounds %struct.FactExp, ptr %0, i32 0, i32 0
  %48 = load i32, ptr %47, align 4, !tbaa !11
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [12 x i32], ptr %46, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %50, align 4, !tbaa !9
  br label %28, !llvm.loop !13

53:                                               ; preds = %28
  %54 = getelementptr inbounds %struct.FactExp, ptr %0, i32 0, i32 0
  %55 = load i32, ptr %54, align 4, !tbaa !11
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %54, align 4, !tbaa !11
  %57 = load i64, ptr %3, align 8, !tbaa !5
  %58 = sitofp i64 %57 to double
  %59 = call double @sqrt(double noundef %58) #4
  %60 = fptosi double %59 to i32
  store i32 %60, ptr %5, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %53, %18
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %4, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %4, align 4, !tbaa !9
  br label %10, !llvm.loop !15

65:                                               ; preds = %17
  %66 = load i64, ptr %3, align 8, !tbaa !5
  %67 = icmp sgt i64 %66, 1
  br i1 %67, label %68, label %75

68:                                               ; preds = %65
  %69 = getelementptr inbounds %struct.FactExp, ptr %0, i32 0, i32 1
  %70 = getelementptr inbounds %struct.FactExp, ptr %0, i32 0, i32 0
  %71 = load i32, ptr %70, align 4, !tbaa !11
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %70, align 4, !tbaa !11
  %73 = sext i32 %71 to i64
  %74 = getelementptr inbounds [12 x i32], ptr %69, i64 0, i64 %73
  store i32 1, ptr %74, align 4, !tbaa !9
  br label %75

75:                                               ; preds = %68, %65
  ret void
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 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 = !{!12, !10, i64 0}
!12 = !{!"_ZTS7FactExp", !10, i64 0, !7, i64 4}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

%struct.FactExp = type { i32, [12 x i32] }

@prime = external dso_local local_unnamed_addr global [125000 x i32], align 16

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: mustprogress nofree nounwind memory(readwrite, inaccessiblemem: write) uwtable
define dso_local void @_Z10getFactExpx(ptr dead_on_unwind noalias nocapture writable sret(%struct.FactExp) align 4 %0, i64 noundef %1) local_unnamed_addr #1 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(52) %0, i8 0, i64 52, i1 false)
  %3 = sitofp i64 %1 to double
  %4 = tail call double @sqrt(double noundef %3) #3
  %5 = fptosi double %4 to i32
  %6 = load i32, ptr @prime, align 16, !tbaa !5
  %.not17 = icmp sgt i32 %6, %5
  br i1 %.not17, label %._crit_edge23, label %.lr.ph22

.lr.ph22:                                         ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  br label %10

._crit_edge23:                                    ; preds = %28, %2
  %8 = phi i32 [ 0, %2 ], [ %29, %28 ]
  %.013.lcssa = phi i64 [ %1, %2 ], [ %.2, %28 ]
  %9 = icmp sgt i64 %.013.lcssa, 1
  br i1 %9, label %32, label %37

10:                                               ; preds = %.lr.ph22, %28
  %11 = phi i32 [ 0, %.lr.ph22 ], [ %29, %28 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph22 ], [ %indvars.iv.next, %28 ]
  %12 = phi i32 [ %6, %.lr.ph22 ], [ %31, %28 ]
  %.020 = phi i32 [ %5, %.lr.ph22 ], [ %.1, %28 ]
  %.01318 = phi i64 [ %1, %.lr.ph22 ], [ %.2, %28 ]
  %13 = sext i32 %12 to i64
  %14 = srem i64 %.01318, %13
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %.lr.ph, label %28

.lr.ph:                                           ; preds = %10, %.lr.ph
  %.11416 = phi i64 [ %16, %.lr.ph ], [ %.01318, %10 ]
  %16 = sdiv i64 %.11416, %13
  %17 = load i32, ptr %0, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [12 x i32], ptr %7, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %19, align 4, !tbaa !5
  %22 = srem i64 %16, %13
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph
  %.pre = load i32, ptr %0, align 4, !tbaa !9
  %24 = add nsw i32 %.pre, 1
  store i32 %24, ptr %0, align 4, !tbaa !9
  %25 = sitofp i64 %16 to double
  %26 = tail call double @sqrt(double noundef %25) #3
  %27 = fptosi double %26 to i32
  br label %28

28:                                               ; preds = %10, %._crit_edge
  %29 = phi i32 [ %24, %._crit_edge ], [ %11, %10 ]
  %.2 = phi i64 [ %16, %._crit_edge ], [ %.01318, %10 ]
  %.1 = phi i32 [ %27, %._crit_edge ], [ %.020, %10 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %30 = getelementptr inbounds [125000 x i32], ptr @prime, i64 0, i64 %indvars.iv.next
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %.not = icmp sgt i32 %31, %.1
  br i1 %.not, label %._crit_edge23, label %10, !llvm.loop !13

32:                                               ; preds = %._crit_edge23
  %33 = getelementptr inbounds i8, ptr %0, i64 4
  %34 = add nsw i32 %8, 1
  store i32 %34, ptr %0, align 4, !tbaa !9
  %35 = sext i32 %8 to i64
  %36 = getelementptr inbounds [12 x i32], ptr %33, i64 0, i64 %35
  store i32 1, ptr %36, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %32, %._crit_edge23
  ret void
}

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { mustprogress nofree nounwind memory(readwrite, inaccessiblemem: write) 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 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTS7FactExp", !6, i64 0, !7, i64 4}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass