; LLVM IR from tangjz/acm-icpc :: projecteuler/540.ll (Z5iSqrtx)

; 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: mustprogress uwtable
define dso_local noundef i64 @_Z5iSqrtx(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %4 = load i64, ptr %2, align 8, !tbaa !5
  %5 = sitofp i64 %4 to x86_fp80
  %6 = call x86_fp80 @sqrtl(x86_fp80 noundef %5) #4
  %7 = call noundef x86_fp80 @_ZSt4ceile(x86_fp80 noundef %6)
  %8 = fptosi x86_fp80 %7 to i64
  store i64 %8, ptr %3, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %16, %1
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = load i64, ptr %3, align 8, !tbaa !5
  %12 = mul nsw i64 %10, %11
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = icmp sle i64 %12, %13
  br i1 %14, label %15, label %19

15:                                               ; preds = %9
  br label %16

16:                                               ; preds = %15
  %17 = load i64, ptr %3, align 8, !tbaa !5
  %18 = add nsw i64 %17, 1
  store i64 %18, ptr %3, align 8, !tbaa !5
  br label %9, !llvm.loop !9

19:                                               ; preds = %9
  br label %20

20:                                               ; preds = %27, %19
  %21 = load i64, ptr %3, align 8, !tbaa !5
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = mul nsw i64 %21, %22
  %24 = load i64, ptr %2, align 8, !tbaa !5
  %25 = icmp sgt i64 %23, %24
  br i1 %25, label %26, label %30

26:                                               ; preds = %20
  br label %27

27:                                               ; preds = %26
  %28 = load i64, ptr %3, align 8, !tbaa !5
  %29 = add nsw i64 %28, -1
  store i64 %29, ptr %3, align 8, !tbaa !5
  br label %20, !llvm.loop !11

30:                                               ; preds = %20
  %31 = load i64, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret i64 %31
}

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

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

; Function Attrs: nounwind
declare x86_fp80 @sqrtl(x86_fp80 noundef) #3

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

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


; 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 i64 @_Z5iSqrtx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = sitofp i64 %0 to x86_fp80
  %3 = tail call x86_fp80 @sqrtl(x86_fp80 noundef %2) #3
  %4 = tail call noundef x86_fp80 @_ZSt4ceile(x86_fp80 noundef %3)
  %5 = fptosi x86_fp80 %4 to i64
  br label %6

6:                                                ; preds = %6, %1
  %.0 = phi i64 [ %5, %1 ], [ %8, %6 ]
  %7 = mul nsw i64 %.0, %.0
  %.not = icmp sgt i64 %7, %0
  %8 = add nsw i64 %.0, 1
  br i1 %.not, label %.preheader, label %6, !llvm.loop !5

.preheader:                                       ; preds = %6, %.preheader
  %.1 = phi i64 [ %11, %.preheader ], [ %.0, %6 ]
  %9 = mul nsw i64 %.1, %.1
  %10 = icmp sgt i64 %9, %0
  %11 = add nsw i64 %.1, -1
  br i1 %10, label %.preheader, label %12, !llvm.loop !7

12:                                               ; preds = %.preheader
  ret i64 %.1
}

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare x86_fp80 @sqrtl(x86_fp80 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"}
!7 = distinct !{!7, !6}


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