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

; 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 { i32, i32 }

@b = external dso_local global i32, align 4
@p = external dso_local global i32, align 4

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

10:                                               ; preds = %1
  %11 = load i32, ptr @p, align 4, !tbaa !5
  %12 = icmp sle i32 %11, 2
  br i1 %12, label %13, label %15

13:                                               ; preds = %10, %1
  %14 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %14, ptr %2, align 4
  br label %71

15:                                               ; preds = %10
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = load i32, ptr @p, align 4, !tbaa !5
  %18 = sub nsw i32 %17, 1
  %19 = ashr i32 %18, 1
  %20 = call noundef i32 @_Z7mod_powii(i32 noundef %16, i32 noundef %19)
  %21 = icmp ne i32 %20, 1
  br i1 %21, label %22, label %23

22:                                               ; preds = %15
  store i32 -1, ptr %2, align 4
  br label %71

23:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  br label %24

24:                                               ; preds = %53, %23
  %25 = call i32 @rand() #5
  %26 = sext i32 %25 to i64
  %27 = shl i64 %26, 15
  %28 = call i32 @rand() #5
  %29 = sext i32 %28 to i64
  %30 = or i64 %27, %29
  %31 = load i32, ptr @p, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = srem i64 %30, %32
  %34 = trunc i64 %33 to i32
  store i32 %34, ptr %4, align 4, !tbaa !5
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = mul nsw i64 %36, %38
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = sub nsw i64 %39, %41
  %43 = load i32, ptr @p, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = srem i64 %42, %44
  %46 = trunc i64 %45 to i32
  store i32 %46, ptr %5, align 4, !tbaa !5
  %47 = icmp slt i32 %46, 0
  br i1 %47, label %48, label %52

48:                                               ; preds = %24
  %49 = load i32, ptr @p, align 4, !tbaa !5
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = add nsw i32 %50, %49
  store i32 %51, ptr %5, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %48, %24
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = load i32, ptr @p, align 4, !tbaa !5
  %56 = sub nsw i32 %55, 1
  %57 = ashr i32 %56, 1
  %58 = call noundef i32 @_Z7mod_powii(i32 noundef %54, i32 noundef %57)
  %59 = icmp eq i32 %58, 1
  br i1 %59, label %24, label %60, !llvm.loop !9

60:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %61 = getelementptr inbounds %struct.Pair, ptr %6, i32 0, i32 0
  %62 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %62, ptr %61, align 4, !tbaa !11
  %63 = getelementptr inbounds %struct.Pair, ptr %6, i32 0, i32 1
  store i32 1, ptr %63, align 4, !tbaa !13
  %64 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %64, ptr @b, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %65 = load i32, ptr @p, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  %67 = ashr i32 %66, 1
  %68 = call i64 @_ZN4Pair3powEi(ptr noundef nonnull align 4 dereferenceable(8) %6, i32 noundef %67)
  store i64 %68, ptr %7, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %7, i64 8, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  %69 = getelementptr inbounds %struct.Pair, ptr %6, i32 0, i32 0
  %70 = load i32, ptr %69, align 4, !tbaa !11
  store i32 %70, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %71

71:                                               ; preds = %60, %22, %13
  %72 = load i32, ptr %2, align 4
  ret i32 %72
}

; Function Attrs: nounwind
declare i32 @rand() #3

; Function Attrs: mustprogress uwtable
declare dso_local i64 @_ZN4Pair3powEi(ptr noundef nonnull align 4 dereferenceable(8), i32 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) #4

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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS4Pair", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 4}
!14 = !{i64 0, i64 4, !5, i64 4, i64 4, !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 { i32, i32 }

@b = external dso_local local_unnamed_addr global i32, align 4
@p = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7mod_powii(i32 noundef, i32 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 i32 @_Z8mod_sqrti(i32 noundef %0) local_unnamed_addr #2 {
  %2 = alloca %struct.Pair, align 4
  %3 = icmp eq i32 %0, 0
  %4 = load i32, ptr @p, align 4
  %5 = icmp slt i32 %4, 3
  %or.cond = select i1 %3, i1 true, i1 %5
  br i1 %or.cond, label %39, label %6

6:                                                ; preds = %1
  %7 = add nsw i32 %4, -1
  %8 = lshr i32 %7, 1
  %9 = tail call noundef i32 @_Z7mod_powii(i32 noundef %0, i32 noundef %8)
  %.not = icmp eq i32 %9, 1
  br i1 %.not, label %.preheader, label %39

.preheader:                                       ; preds = %6
  %10 = sext i32 %0 to i64
  br label %11

11:                                               ; preds = %.preheader, %11
  %12 = tail call i32 @rand() #4
  %13 = sext i32 %12 to i64
  %14 = shl nsw i64 %13, 15
  %15 = tail call i32 @rand() #4
  %16 = sext i32 %15 to i64
  %17 = or i64 %14, %16
  %18 = load i32, ptr @p, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = srem i64 %17, %19
  %21 = mul nsw i64 %20, %20
  %22 = sub nsw i64 %21, %10
  %23 = srem i64 %22, %19
  %24 = trunc nsw i64 %23 to i32
  %25 = icmp slt i64 %23, 0
  %26 = select i1 %25, i32 %18, i32 0
  %spec.select = add nsw i32 %26, %24
  %27 = add nsw i32 %18, -1
  %28 = ashr i32 %27, 1
  %29 = tail call noundef i32 @_Z7mod_powii(i32 noundef %spec.select, i32 noundef %28)
  %30 = icmp eq i32 %29, 1
  br i1 %30, label %11, label %31, !llvm.loop !9

31:                                               ; preds = %11
  %32 = trunc nsw i64 %20 to i32
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4
  store i32 %32, ptr %2, align 4, !tbaa !11
  %33 = getelementptr inbounds i8, ptr %2, i64 4
  store i32 1, ptr %33, align 4, !tbaa !13
  store i32 %spec.select, ptr @b, align 4, !tbaa !5
  %34 = load i32, ptr @p, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  %36 = ashr i32 %35, 1
  %37 = call i64 @_ZN4Pair3powEi(ptr noundef nonnull align 4 dereferenceable(8) %2, i32 noundef %36)
  %38 = trunc i64 %37 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4
  br label %39

39:                                               ; preds = %6, %1, %31
  %.0 = phi i32 [ %38, %31 ], [ %0, %1 ], [ -1, %6 ]
  ret i32 %.0
}

; Function Attrs: nounwind
declare i32 @rand() local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
declare dso_local i64 @_ZN4Pair3powEi(ptr noundef nonnull align 4 dereferenceable(8), i32 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 "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 = !{!"int", !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 4}
!13 = !{!12, !6, i64 4}


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