; LLVM IR from tangjz/acm-icpc :: projecteuler/590.ll (Z7mod_powi3Exp)

; 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.Exp = type <{ i32, i8, [3 x i8] }>

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z7mod_powi3Exp(i32 noundef %0, i64 %1) #0 {
  %3 = alloca %struct.Exp, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i64 %1, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %7 = getelementptr inbounds %struct.Exp, ptr %3, i32 0, i32 1
  %8 = load i8, ptr %7, align 4, !tbaa !9, !range !12, !noundef !13
  %9 = trunc i8 %8 to i1
  br i1 %9, label %10, label %14

10:                                               ; preds = %2
  %11 = getelementptr inbounds %struct.Exp, ptr %3, i32 0, i32 0
  %12 = load i32, ptr %11, align 4, !tbaa !14
  %13 = add nsw i32 %12, 400000000
  br label %17

14:                                               ; preds = %2
  %15 = getelementptr inbounds %struct.Exp, ptr %3, i32 0, i32 0
  %16 = load i32, ptr %15, align 4, !tbaa !14
  br label %17

17:                                               ; preds = %14, %10
  %18 = phi i32 [ %13, %10 ], [ %16, %14 ]
  store i32 %18, ptr %6, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %37, %17
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %22, label %47

22:                                               ; preds = %19
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = and i32 %23, 1
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %35

26:                                               ; preds = %22
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = mul nsw i64 %28, %30
  %32 = srem i64 %31, 1000000000
  %33 = trunc i64 %32 to i32
  store i32 %33, ptr %5, align 4, !tbaa !5
  %34 = icmp ne i32 %33, 0
  br label %35

35:                                               ; preds = %26, %22
  %36 = phi i1 [ false, %22 ], [ %34, %26 ]
  br label %37

37:                                               ; preds = %35
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = ashr i32 %38, 1
  store i32 %39, ptr %6, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = mul nsw i64 %41, %43
  %45 = srem i64 %44, 1000000000
  %46 = trunc i64 %45 to i32
  store i32 %46, ptr %4, align 4, !tbaa !5
  br label %19, !llvm.loop !15

47:                                               ; preds = %19
  %48 = load i32, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %48
}

; 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

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 = { 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, !11, i64 4}
!10 = !{!"_ZTS3Exp", !6, i64 0, !11, i64 4}
!11 = !{!"bool", !7, i64 0}
!12 = !{i8 0, i8 2}
!13 = !{}
!14 = !{!10, !6, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; 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 nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef range(i32 -999999999, 1000000000) i32 @_Z7mod_powi3Exp(i32 noundef %0, i64 %1) local_unnamed_addr #0 {
  %.sroa.0.0.extract.trunc = trunc i64 %1 to i32
  %3 = and i64 %1, 4294967296
  %.not = icmp eq i64 %3, 0
  %4 = add nsw i32 %.sroa.0.0.extract.trunc, 400000000
  %5 = select i1 %.not, i32 %.sroa.0.0.extract.trunc, i32 %4
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2, %14
  %.014 = phi i32 [ %15, %14 ], [ %5, %2 ]
  %.0913 = phi i32 [ %.1, %14 ], [ 1, %2 ]
  %.01012 = phi i32 [ %18, %14 ], [ %0, %2 ]
  %7 = and i32 %.014, 1
  %.not11 = icmp eq i32 %7, 0
  br i1 %.not11, label %.lr.ph._crit_edge, label %8

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %.pre = sext i32 %.01012 to i64
  br label %14

8:                                                ; preds = %.lr.ph
  %9 = sext i32 %.0913 to i64
  %10 = sext i32 %.01012 to i64
  %11 = mul nsw i64 %9, %10
  %12 = srem i64 %11, 1000000000
  %13 = trunc nsw i64 %12 to i32
  br label %14

14:                                               ; preds = %.lr.ph._crit_edge, %8
  %.pre-phi = phi i64 [ %.pre, %.lr.ph._crit_edge ], [ %10, %8 ]
  %.1 = phi i32 [ %.0913, %.lr.ph._crit_edge ], [ %13, %8 ]
  %15 = lshr i32 %.014, 1
  %16 = mul nsw i64 %.pre-phi, %.pre-phi
  %17 = urem i64 %16, 1000000000
  %18 = trunc nuw nsw i64 %17 to i32
  %.not15 = icmp ult i32 %.014, 2
  br i1 %.not15, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %14, %2
  %.09.lcssa = phi i32 [ 1, %2 ], [ %.1, %14 ]
  ret i32 %.09.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) 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"}


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