; LLVM IR from tangjz/acm-icpc :: buaa/oj3rd/1090.ll (Z4factv)

; 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"

@X = external dso_local global i64, align 8
@a = external dso_local global [4 x i64], align 16
@flag = external dso_local global i8, align 1
@__const._Z4factv.s = external hidden unnamed_addr constant [4 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4factv() #0 {
  %1 = alloca [4 x i32], align 16
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 16, ptr %1) #3
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %1, ptr align 16 @__const._Z4factv.s, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %34, %0
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = icmp slt i32 %4, 4
  br i1 %5, label %7, label %6

6:                                                ; preds = %3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %37

7:                                                ; preds = %3
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [4 x i64], ptr @a, i64 0, i64 %9
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %20, %7
  %12 = load i64, ptr @X, align 8, !tbaa !9
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [4 x i32], ptr %1, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = srem i64 %12, %17
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %20, label %33

20:                                               ; preds = %11
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [4 x i64], ptr @a, i64 0, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = add nsw i64 %24, 1
  store i64 %25, ptr %23, align 8, !tbaa !9
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [4 x i32], ptr %1, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = load i64, ptr @X, align 8, !tbaa !9
  %32 = sdiv i64 %31, %30
  store i64 %32, ptr @X, align 8, !tbaa !9
  br label %11, !llvm.loop !11

33:                                               ; preds = %11
  br label %34

34:                                               ; preds = %33
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %2, align 4, !tbaa !5
  br label %3, !llvm.loop !13

37:                                               ; preds = %6
  %38 = load i64, ptr @X, align 8, !tbaa !9
  %39 = icmp sgt i64 %38, 1
  br i1 %39, label %52, label %40

40:                                               ; preds = %37
  %41 = load i64, ptr @a, align 16, !tbaa !9
  %42 = icmp sgt i64 %41, 54
  br i1 %42, label %52, label %43

43:                                               ; preds = %40
  %44 = load i64, ptr getelementptr inbounds ([4 x i64], ptr @a, i64 0, i64 1), align 8, !tbaa !9
  %45 = icmp sgt i64 %44, 36
  br i1 %45, label %52, label %46

46:                                               ; preds = %43
  %47 = load i64, ptr getelementptr inbounds ([4 x i64], ptr @a, i64 0, i64 2), align 16, !tbaa !9
  %48 = icmp sgt i64 %47, 18
  br i1 %48, label %52, label %49

49:                                               ; preds = %46
  %50 = load i64, ptr getelementptr inbounds ([4 x i64], ptr @a, i64 0, i64 3), align 8, !tbaa !9
  %51 = icmp sgt i64 %50, 18
  br label %52

52:                                               ; preds = %49, %46, %43, %40, %37
  %53 = phi i1 [ true, %46 ], [ true, %43 ], [ true, %40 ], [ true, %37 ], [ %51, %49 ]
  %54 = zext i1 %53 to i8
  store i8 %54, ptr @flag, align 1, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 16, ptr %1) #3
  ret void
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = !{!15, !15, i64 0}
!15 = !{!"bool", !7, i64 0}


; 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"

@X = external dso_local local_unnamed_addr global i64, align 8
@a = external dso_local local_unnamed_addr global [4 x i64], align 16
@flag = external dso_local local_unnamed_addr global i8, align 1
@__const._Z4factv.s = external hidden unnamed_addr constant [4 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z4factv() local_unnamed_addr #0 {
  %X.promoted12 = load i64, ptr @X, align 8, !tbaa !5
  store i64 0, ptr @a, align 16, !tbaa !5
  %1 = load i32, ptr @__const._Z4factv.s, align 16, !tbaa !9
  %2 = sext i32 %1 to i64
  %3 = srem i64 %X.promoted12, %2
  %4 = icmp eq i64 %3, 0
  br i1 %4, label %.lr.ph, label %12

.lr.ph:                                           ; preds = %0, %.lr.ph
  %5 = phi i64 [ %7, %.lr.ph ], [ 0, %0 ]
  %6 = phi i64 [ %8, %.lr.ph ], [ %X.promoted12, %0 ]
  %7 = add nuw nsw i64 %5, 1
  %8 = sdiv i64 %6, %2
  %9 = srem i64 %8, %2
  %10 = icmp eq i64 %9, 0
  br i1 %10, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph
  store i64 %7, ptr @a, align 16, !tbaa !5
  store i64 %8, ptr @X, align 8, !tbaa !5
  %11 = icmp ugt i64 %5, 53
  br label %12

12:                                               ; preds = %._crit_edge, %0
  %13 = phi i1 [ %11, %._crit_edge ], [ false, %0 ]
  %.lcssa1113 = phi i64 [ %8, %._crit_edge ], [ %X.promoted12, %0 ]
  store i64 0, ptr getelementptr inbounds (i8, ptr @a, i64 8), align 8, !tbaa !5
  %14 = load i32, ptr getelementptr inbounds (i8, ptr @__const._Z4factv.s, i64 4), align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = srem i64 %.lcssa1113, %15
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %.lr.ph.1, label %25

.lr.ph.1:                                         ; preds = %12, %.lr.ph.1
  %18 = phi i64 [ %20, %.lr.ph.1 ], [ 0, %12 ]
  %19 = phi i64 [ %21, %.lr.ph.1 ], [ %.lcssa1113, %12 ]
  %20 = add nuw nsw i64 %18, 1
  %21 = sdiv i64 %19, %15
  %22 = srem i64 %21, %15
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %.lr.ph.1, label %._crit_edge.1, !llvm.loop !11

._crit_edge.1:                                    ; preds = %.lr.ph.1
  store i64 %20, ptr getelementptr inbounds (i8, ptr @a, i64 8), align 8, !tbaa !5
  store i64 %21, ptr @X, align 8, !tbaa !5
  %24 = icmp ugt i64 %18, 35
  br label %25

25:                                               ; preds = %._crit_edge.1, %12
  %26 = phi i1 [ %24, %._crit_edge.1 ], [ false, %12 ]
  %.lcssa1113.1 = phi i64 [ %21, %._crit_edge.1 ], [ %.lcssa1113, %12 ]
  store i64 0, ptr getelementptr inbounds (i8, ptr @a, i64 16), align 16, !tbaa !5
  %27 = load i32, ptr getelementptr inbounds (i8, ptr @__const._Z4factv.s, i64 8), align 8, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = srem i64 %.lcssa1113.1, %28
  %30 = icmp eq i64 %29, 0
  br i1 %30, label %.lr.ph.2, label %38

.lr.ph.2:                                         ; preds = %25, %.lr.ph.2
  %31 = phi i64 [ %33, %.lr.ph.2 ], [ 0, %25 ]
  %32 = phi i64 [ %34, %.lr.ph.2 ], [ %.lcssa1113.1, %25 ]
  %33 = add nuw nsw i64 %31, 1
  %34 = sdiv i64 %32, %28
  %35 = srem i64 %34, %28
  %36 = icmp eq i64 %35, 0
  br i1 %36, label %.lr.ph.2, label %._crit_edge.2, !llvm.loop !11

._crit_edge.2:                                    ; preds = %.lr.ph.2
  store i64 %33, ptr getelementptr inbounds (i8, ptr @a, i64 16), align 16, !tbaa !5
  store i64 %34, ptr @X, align 8, !tbaa !5
  %37 = icmp ugt i64 %31, 17
  br label %38

38:                                               ; preds = %._crit_edge.2, %25
  %39 = phi i1 [ %37, %._crit_edge.2 ], [ false, %25 ]
  %.lcssa1113.2 = phi i64 [ %34, %._crit_edge.2 ], [ %.lcssa1113.1, %25 ]
  store i64 0, ptr getelementptr inbounds (i8, ptr @a, i64 24), align 8, !tbaa !5
  %40 = load i32, ptr getelementptr inbounds (i8, ptr @__const._Z4factv.s, i64 12), align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = srem i64 %.lcssa1113.2, %41
  %43 = icmp eq i64 %42, 0
  br i1 %43, label %.lr.ph.3, label %51

.lr.ph.3:                                         ; preds = %38, %.lr.ph.3
  %44 = phi i64 [ %46, %.lr.ph.3 ], [ 0, %38 ]
  %45 = phi i64 [ %47, %.lr.ph.3 ], [ %.lcssa1113.2, %38 ]
  %46 = add nuw nsw i64 %44, 1
  %47 = sdiv i64 %45, %41
  %48 = srem i64 %47, %41
  %49 = icmp eq i64 %48, 0
  br i1 %49, label %.lr.ph.3, label %._crit_edge.3, !llvm.loop !11

._crit_edge.3:                                    ; preds = %.lr.ph.3
  store i64 %46, ptr getelementptr inbounds (i8, ptr @a, i64 24), align 8, !tbaa !5
  store i64 %47, ptr @X, align 8, !tbaa !5
  %50 = icmp ugt i64 %44, 17
  br label %51

51:                                               ; preds = %._crit_edge.3, %38
  %52 = phi i1 [ %50, %._crit_edge.3 ], [ false, %38 ]
  %53 = phi i64 [ %47, %._crit_edge.3 ], [ %.lcssa1113.2, %38 ]
  %54 = icmp sgt i64 %53, 1
  %or.cond = select i1 %54, i1 true, i1 %13
  %or.cond3 = select i1 %or.cond, i1 true, i1 %26
  %or.cond5 = select i1 %or.cond3, i1 true, i1 %39
  %narrow = select i1 %or.cond5, i1 true, i1 %52
  %55 = zext i1 %narrow to i8
  store i8 %55, ptr @flag, align 1, !tbaa !13
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: 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 = !{!6, !6, i64 0}
!6 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !14, i64 0}
!14 = !{!"bool", !7, i64 0}


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