; LLVM IR from tangjz/acm-icpc :: hdu/6128.ll (Z8mod_sqrtxx)

; 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.Pair = type { i64, i64 }

@B = external dso_local global i64, align 8
@P = external dso_local global i64, align 8

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

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z7mod_powxxx(i64 noundef, i64 noundef, i64 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 noundef i64 @_Z8mod_sqrtxx(i64 noundef %0, i64 noundef %1) #2 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca %struct.Pair, align 8
  %9 = alloca %struct.Pair, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = icmp ne i64 %10, 0
  br i1 %11, label %12, label %15

12:                                               ; preds = %2
  %13 = load i64, ptr %5, align 8, !tbaa !5
  %14 = icmp sle i64 %13, 2
  br i1 %14, label %15, label %17

15:                                               ; preds = %12, %2
  %16 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %16, ptr %3, align 8
  br label %63

17:                                               ; preds = %12
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = load i64, ptr %5, align 8, !tbaa !5
  %20 = sub nsw i64 %19, 1
  %21 = ashr i64 %20, 1
  %22 = load i64, ptr %5, align 8, !tbaa !5
  %23 = call noundef i64 @_Z7mod_powxxx(i64 noundef %18, i64 noundef %21, i64 noundef %22)
  %24 = icmp ne i64 %23, 1
  br i1 %24, label %25, label %26

25:                                               ; preds = %17
  store i64 -1, ptr %3, align 8
  br label %63

26:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  br label %27

27:                                               ; preds = %39, %26
  %28 = call noundef i32 @_Z6randLLv()
  %29 = sext i32 %28 to i64
  %30 = load i64, ptr %5, align 8, !tbaa !5
  %31 = srem i64 %29, %30
  store i64 %31, ptr %6, align 8, !tbaa !5
  %32 = load i64, ptr %6, align 8, !tbaa !5
  %33 = load i64, ptr %6, align 8, !tbaa !5
  %34 = load i64, ptr %5, align 8, !tbaa !5
  %35 = call noundef i64 @_Z7mod_mulxxx(i64 noundef %32, i64 noundef %33, i64 noundef %34)
  %36 = load i64, ptr %4, align 8, !tbaa !5
  %37 = load i64, ptr %5, align 8, !tbaa !5
  %38 = call noundef i64 @_Z7mod_subxxx(i64 noundef %35, i64 noundef %36, i64 noundef %37)
  store i64 %38, ptr %7, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %27
  %40 = load i64, ptr %7, align 8, !tbaa !5
  %41 = load i64, ptr %5, align 8, !tbaa !5
  %42 = sub nsw i64 %41, 1
  %43 = ashr i64 %42, 1
  %44 = load i64, ptr %5, align 8, !tbaa !5
  %45 = call noundef i64 @_Z7mod_powxxx(i64 noundef %40, i64 noundef %43, i64 noundef %44)
  %46 = icmp eq i64 %45, 1
  br i1 %46, label %27, label %47, !llvm.loop !9

47:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #4
  %48 = getelementptr inbounds %struct.Pair, ptr %8, i32 0, i32 0
  %49 = load i64, ptr %6, align 8, !tbaa !5
  store i64 %49, ptr %48, align 8, !tbaa !11
  %50 = getelementptr inbounds %struct.Pair, ptr %8, i32 0, i32 1
  store i64 1, ptr %50, align 8, !tbaa !13
  %51 = load i64, ptr %7, align 8, !tbaa !5
  store i64 %51, ptr @B, align 8, !tbaa !5
  %52 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %52, ptr @P, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %9) #4
  %53 = load i64, ptr %5, align 8, !tbaa !5
  %54 = add nsw i64 %53, 1
  %55 = ashr i64 %54, 1
  %56 = call { i64, i64 } @_ZN4Pair3powEx(ptr noundef nonnull align 8 dereferenceable(16) %8, i64 noundef %55)
  %57 = getelementptr inbounds { i64, i64 }, ptr %9, i32 0, i32 0
  %58 = extractvalue { i64, i64 } %56, 0
  store i64 %58, ptr %57, align 8
  %59 = getelementptr inbounds { i64, i64 }, ptr %9, i32 0, i32 1
  %60 = extractvalue { i64, i64 } %56, 1
  store i64 %60, ptr %59, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %8, ptr align 8 %9, i64 16, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 16, ptr %9) #4
  %61 = getelementptr inbounds %struct.Pair, ptr %8, i32 0, i32 0
  %62 = load i64, ptr %61, align 8, !tbaa !11
  store i64 %62, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %63

63:                                               ; preds = %47, %25, %15
  %64 = load i64, ptr %3, align 8
  ret i64 %64
}

; Function Attrs: mustprogress uwtable
declare dso_local { i64, i64 } @_ZN4Pair3powEx(ptr noundef nonnull align 8 dereferenceable(16), i64 noundef) #2 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!12, !6, i64 0}
!12 = !{!"_ZTS4Pair", !6, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = !{i64 0, i64 8, !5, i64 8, i64 8, !5}


; 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.Pair = type { i64, i64 }

@B = external dso_local local_unnamed_addr global i64, align 8
@P = external dso_local local_unnamed_addr global i64, align 8

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

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

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

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

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

; Function Attrs: mustprogress 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 noundef i64 @_Z8mod_sqrtxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.Pair, align 8
  %4 = icmp eq i64 %0, 0
  %5 = icmp slt i64 %1, 3
  %or.cond = or i1 %4, %5
  br i1 %or.cond, label %23, label %6

6:                                                ; preds = %2
  %7 = add nsw i64 %1, -1
  %8 = lshr i64 %7, 1
  %9 = tail call noundef i64 @_Z7mod_powxxx(i64 noundef %0, i64 noundef %8, i64 noundef %1)
  %.not = icmp eq i64 %9, 1
  br i1 %.not, label %.preheader, label %23

.preheader:                                       ; preds = %6, %.preheader
  %10 = tail call noundef i32 @_Z6randLLv()
  %11 = sext i32 %10 to i64
  %12 = srem i64 %11, %1
  %13 = tail call noundef i64 @_Z7mod_mulxxx(i64 noundef %12, i64 noundef %12, i64 noundef %1)
  %14 = tail call noundef i64 @_Z7mod_subxxx(i64 noundef %13, i64 noundef %0, i64 noundef %1)
  %15 = tail call noundef i64 @_Z7mod_powxxx(i64 noundef %14, i64 noundef %8, i64 noundef %1)
  %16 = icmp eq i64 %15, 1
  br i1 %16, label %.preheader, label %17, !llvm.loop !5

17:                                               ; preds = %.preheader
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  store i64 %12, ptr %3, align 8, !tbaa !7
  %18 = getelementptr inbounds i8, ptr %3, i64 8
  store i64 1, ptr %18, align 8, !tbaa !12
  store i64 %14, ptr @B, align 8, !tbaa !13
  store i64 %1, ptr @P, align 8, !tbaa !13
  %19 = add nuw nsw i64 %1, 1
  %20 = lshr i64 %19, 1
  %21 = call { i64, i64 } @_ZN4Pair3powEx(ptr noundef nonnull align 8 dereferenceable(16) %3, i64 noundef %20)
  %22 = extractvalue { i64, i64 } %21, 0
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  br label %23

23:                                               ; preds = %6, %2, %17
  %.0 = phi i64 [ %22, %17 ], [ %0, %2 ], [ -1, %6 ]
  ret i64 %.0
}

; Function Attrs: mustprogress uwtable
declare dso_local { i64, i64 } @_ZN4Pair3powEx(ptr noundef nonnull align 8 dereferenceable(16), i64 noundef) local_unnamed_addr #2 align 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 = { mustprogress 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 = { 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 = !{!8, !9, i64 0}
!8 = !{!"_ZTS4Pair", !9, i64 0, !9, i64 8}
!9 = !{!"long long", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!8, !9, i64 8}
!13 = !{!9, !9, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass