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

; 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] }>

@c = external dso_local global [5087 x [5087 x i32]], align 16
@cnt = external dso_local global [17 x i32], align 16
@fct = external dso_local global [17 x [5087 x %struct.Exp]], align 16

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

13:                                               ; preds = %2
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %4, i64 8, i1 false), !tbaa.struct !9
  %14 = load i64, ptr %6, align 4
  %15 = call noundef i32 @_Z7mod_powi3Exp(i32 noundef 2, i64 %14)
  store i32 %15, ptr %3, align 4
  br label %74

16:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 1, ptr %9, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %59, %16
  %18 = load i32, ptr %8, align 4, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [17 x i32], ptr @cnt, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = icmp sle i32 %18, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %64

25:                                               ; preds = %17
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = load i32, ptr %9, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [17 x i32], ptr @cnt, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [5087 x [5087 x i32]], ptr @c, i64 0, i64 %34
  %36 = load i32, ptr %8, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [5087 x i32], ptr %35, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = mul nsw i64 %29, %40
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = sub nsw i32 %42, 1
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [17 x [5087 x %struct.Exp]], ptr @fct, i64 0, i64 %45
  %47 = load i32, ptr %8, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [5087 x %struct.Exp], ptr %46, i64 0, i64 %48
  %50 = call i64 @_ZNK3ExpmlERKS_(ptr noundef nonnull align 4 dereferenceable(5) %4, ptr noundef nonnull align 4 dereferenceable(5) %49)
  %51 = trunc i64 %50 to i40
  store i40 %51, ptr %10, align 4
  %52 = load i64, ptr %10, align 4
  %53 = call noundef i32 @_Z3dfsi3Exp(i32 noundef %43, i64 %52)
  %54 = sext i32 %53 to i64
  %55 = mul nsw i64 %41, %54
  %56 = add nsw i64 %27, %55
  %57 = srem i64 %56, 1000000000
  %58 = trunc i64 %57 to i32
  store i32 %58, ptr %7, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %25
  %60 = load i32, ptr %8, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %8, align 4, !tbaa !5
  %62 = load i32, ptr %9, align 4, !tbaa !5
  %63 = sub nsw i32 0, %62
  store i32 %63, ptr %9, align 4, !tbaa !5
  br label %17, !llvm.loop !12

64:                                               ; preds = %24
  %65 = load i32, ptr %7, align 4, !tbaa !5
  %66 = icmp slt i32 %65, 0
  br i1 %66, label %67, label %70

67:                                               ; preds = %64
  %68 = load i32, ptr %7, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1000000000
  br label %72

70:                                               ; preds = %64
  %71 = load i32, ptr %7, align 4, !tbaa !5
  br label %72

72:                                               ; preds = %70, %67
  %73 = phi i32 [ %69, %67 ], [ %71, %70 ]
  store i32 %73, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %74

74:                                               ; preds = %72, %13
  %75 = load i32, ptr %3, align 4
  ret i32 %75
}

; 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

; Function Attrs: mustprogress uwtable
declare dso_local i64 @_ZNK3ExpmlERKS_(ptr noundef nonnull align 4 dereferenceable(5), ptr noundef nonnull align 4 dereferenceable(5)) #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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i64 0, i64 4, !5, i64 4, i64 1, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"bool", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

%struct.Exp = type <{ i32, i8, [3 x i8] }>

@c = external dso_local local_unnamed_addr global [5087 x [5087 x i32]], align 16
@cnt = external dso_local local_unnamed_addr global [17 x i32], align 16
@fct = external dso_local global [17 x [5087 x %struct.Exp]], align 16

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z3dfsi3Exp(i32 noundef %0, i64 %1) local_unnamed_addr #1 {
  %3 = alloca %struct.Exp, align 8
  store i64 %1, ptr %3, align 8
  %4 = icmp slt i32 %0, 1
  br i1 %4, label %9, label %.preheader

.preheader:                                       ; preds = %2
  %5 = zext nneg i32 %0 to i64
  %6 = getelementptr inbounds [17 x i32], ptr @cnt, i64 0, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %.not22 = icmp slt i32 %7, 0
  br i1 %.not22, label %._crit_edge.thread, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %8 = add nsw i32 %0, -1
  br label %14

9:                                                ; preds = %2
  %10 = tail call noundef i32 @_Z7mod_powi3Exp(i32 noundef 2, i64 %1)
  br label %._crit_edge.thread

._crit_edge:                                      ; preds = %14
  %11 = trunc nsw i64 %28 to i32
  %12 = icmp slt i64 %28, 0
  %13 = add nsw i32 %11, 1000000000
  %spec.select = select i1 %12, i32 %13, i32 %11
  br label %._crit_edge.thread

14:                                               ; preds = %.lr.ph, %14
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %14 ]
  %15 = phi i32 [ %7, %.lr.ph ], [ %30, %14 ]
  %.01725 = phi i64 [ 0, %.lr.ph ], [ %28, %14 ]
  %.01824 = phi i32 [ 1, %.lr.ph ], [ %29, %14 ]
  %16 = sext i32 %.01824 to i64
  %17 = sext i32 %15 to i64
  %18 = getelementptr inbounds [5087 x [5087 x i32]], ptr @c, i64 0, i64 %17, i64 %indvars.iv
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = mul nsw i64 %20, %16
  %22 = getelementptr inbounds [17 x [5087 x %struct.Exp]], ptr @fct, i64 0, i64 %5, i64 %indvars.iv
  %23 = call i64 @_ZNK3ExpmlERKS_(ptr noundef nonnull align 4 dereferenceable(5) %3, ptr noundef nonnull align 4 dereferenceable(5) %22)
  %.sroa.0.0.insert.ext = and i64 %23, 1099511627775
  %24 = call noundef i32 @_Z3dfsi3Exp(i32 noundef %8, i64 %.sroa.0.0.insert.ext)
  %25 = sext i32 %24 to i64
  %.fr30 = freeze i64 %21
  %26 = mul i64 %.fr30, %25
  %27 = add i64 %26, %.01725
  %28 = srem i64 %27, 1000000000
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %29 = sub nsw i32 0, %.01824
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %.not.not = icmp slt i64 %indvars.iv, %31
  br i1 %.not.not, label %14, label %._crit_edge, !llvm.loop !9

._crit_edge.thread:                               ; preds = %._crit_edge, %.preheader, %9
  %.0 = phi i32 [ %10, %9 ], [ 0, %.preheader ], [ %spec.select, %._crit_edge ]
  ret i32 %.0
}

; Function Attrs: mustprogress uwtable
declare dso_local i64 @_ZNK3ExpmlERKS_(ptr noundef nonnull align 4 dereferenceable(5), ptr noundef nonnull align 4 dereferenceable(5)) local_unnamed_addr #1 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 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 = !{!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"}


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