; LLVM IR from UtkarshPathrabe/Competitive-Coding :: SPOJ Solutions/NDIV.ll (Z6shievev)

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

@check = external dso_local global [32000 x i32], align 16
@prime = external dso_local global [10000 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6shievev() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 3, ptr %1, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %34, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = icmp sle i32 %7, 180
  br i1 %8, label %10, label %9

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

10:                                               ; preds = %6
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [32000 x i32], ptr @check, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %33, label %16

16:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = mul nsw i32 %17, %18
  store i32 %19, ptr %3, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %28, %16
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = icmp sle i32 %21, 32000
  br i1 %22, label %24, label %23

23:                                               ; preds = %20
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %32

24:                                               ; preds = %20
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [32000 x i32], ptr @check, i64 0, i64 %26
  store i32 1, ptr %27, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %24
  %29 = load i32, ptr %1, align 4, !tbaa !5
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = add nsw i32 %30, %29
  store i32 %31, ptr %3, align 4, !tbaa !5
  br label %20, !llvm.loop !9

32:                                               ; preds = %23
  br label %33

33:                                               ; preds = %32, %10
  br label %34

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

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

38:                                               ; preds = %55, %37
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = icmp sle i32 %39, 32000
  br i1 %40, label %42, label %41

41:                                               ; preds = %38
  store i32 8, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %58

42:                                               ; preds = %38
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [32000 x i32], ptr @check, i64 0, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %54, label %48

48:                                               ; preds = %42
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = load i32, ptr %4, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %4, align 4, !tbaa !5
  %52 = sext i32 %50 to i64
  %53 = getelementptr inbounds [10000 x i32], ptr @prime, i64 0, i64 %52
  store i32 %49, ptr %53, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %48, %42
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %5, align 4, !tbaa !5
  %57 = add nsw i32 %56, 2
  store i32 %57, ptr %5, align 4, !tbaa !5
  br label %38, !llvm.loop !12

58:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  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 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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

@check = external dso_local local_unnamed_addr global [32000 x i32], align 16
@prime = external dso_local local_unnamed_addr global [10000 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z6shievev() local_unnamed_addr #0 {
  br label %2

1:                                                ; preds = %.loopexit
  store i32 2, ptr @prime, align 16, !tbaa !5
  br label %13

2:                                                ; preds = %0, %.loopexit
  %indvars.iv30 = phi i64 [ 16, %0 ], [ %indvars.iv.next31, %.loopexit ]
  %indvars.iv25 = phi i64 [ 3, %0 ], [ %indvars.iv.next26, %.loopexit ]
  %indvars.iv23 = phi i32 [ 9, %0 ], [ %indvars.iv.next24, %.loopexit ]
  %3 = getelementptr inbounds [32000 x i32], ptr @check, i64 0, i64 %indvars.iv25
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %.not17 = icmp eq i32 %4, 0
  %5 = mul nuw nsw i64 %indvars.iv25, %indvars.iv25
  %6 = icmp ult i64 %5, 32001
  %or.cond = select i1 %.not17, i1 %6, i1 false
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %2
  %7 = zext i32 %indvars.iv23 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv27 = phi i64 [ %7, %.lr.ph.preheader ], [ %indvars.iv.next28, %.lr.ph ]
  %8 = getelementptr inbounds [32000 x i32], ptr @check, i64 0, i64 %indvars.iv27
  store i32 1, ptr %8, align 4, !tbaa !5
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, %indvars.iv25
  %9 = icmp ult i64 %indvars.iv.next28, 32001
  br i1 %9, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %2
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, 2
  %10 = icmp ult i64 %indvars.iv25, 179
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 8
  %11 = trunc nuw nsw i64 %indvars.iv30 to i32
  %indvars.iv.next24 = add i32 %indvars.iv23, %11
  br i1 %10, label %2, label %1, !llvm.loop !11

12:                                               ; preds = %21
  ret void

13:                                               ; preds = %31, %1
  %indvars.iv = phi i64 [ 3, %1 ], [ %indvars.iv.next.1, %31 ]
  %.01420 = phi i32 [ 1, %1 ], [ %.1.1, %31 ]
  %14 = getelementptr inbounds [32000 x i32], ptr @check, i64 0, i64 %indvars.iv
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %16, label %21

16:                                               ; preds = %13
  %17 = add nsw i32 %.01420, 1
  %18 = sext i32 %.01420 to i64
  %19 = getelementptr inbounds [10000 x i32], ptr @prime, i64 0, i64 %18
  %20 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %20, ptr %19, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %13, %16
  %.1 = phi i32 [ %.01420, %13 ], [ %17, %16 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 2
  %22 = icmp ult i64 %indvars.iv, 31999
  br i1 %22, label %23, label %12, !llvm.loop !12

23:                                               ; preds = %21
  %24 = getelementptr inbounds [32000 x i32], ptr @check, i64 0, i64 %indvars.iv.next
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %.not.1 = icmp eq i32 %25, 0
  br i1 %.not.1, label %26, label %31

26:                                               ; preds = %23
  %27 = add nsw i32 %.1, 1
  %28 = sext i32 %.1 to i64
  %29 = getelementptr inbounds [10000 x i32], ptr @prime, i64 0, i64 %28
  %30 = trunc nuw nsw i64 %indvars.iv.next to i32
  store i32 %30, ptr %29, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %26, %23
  %.1.1 = phi i32 [ %.1, %23 ], [ %27, %26 ]
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 4
  br label %13
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; Effective O3 passes: GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass