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

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

@a = external dso_local global [1000009 x i8], align 16
@prime = external dso_local global [80000 x i64], align 16
@total = external dso_local global i32, align 4

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

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

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

9:                                                ; preds = %5
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [1000009 x i8], ptr @a, i64 0, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !9, !range !11, !noundef !12
  %14 = trunc i8 %13 to i1
  br i1 %14, label %32, label %15

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

19:                                               ; preds = %27, %15
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = icmp sle i32 %20, 1000000
  br i1 %21, label %23, label %22

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

23:                                               ; preds = %19
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [1000009 x i8], ptr @a, i64 0, i64 %25
  store i8 1, ptr %26, align 1, !tbaa !9
  br label %27

27:                                               ; preds = %23
  %28 = load i32, ptr %1, align 4, !tbaa !5
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = add nsw i32 %29, %28
  store i32 %30, ptr %3, align 4, !tbaa !5
  br label %19, !llvm.loop !13

31:                                               ; preds = %22
  br label %32

32:                                               ; preds = %31, %9
  br label %33

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

36:                                               ; preds = %8
  store i32 0, ptr @total, align 4, !tbaa !5
  %37 = load i32, ptr @total, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr @total, align 4, !tbaa !5
  %39 = sext i32 %37 to i64
  %40 = getelementptr inbounds [80000 x i64], ptr @prime, i64 0, i64 %39
  store i64 2, ptr %40, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 3, ptr %4, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %59, %36
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = icmp sle i32 %42, 1000000
  br i1 %43, label %45, label %44

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

45:                                               ; preds = %41
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [1000009 x i8], ptr @a, i64 0, i64 %47
  %49 = load i8, ptr %48, align 1, !tbaa !9, !range !11, !noundef !12
  %50 = trunc i8 %49 to i1
  br i1 %50, label %58, label %51

51:                                               ; preds = %45
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = load i32, ptr @total, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr @total, align 4, !tbaa !5
  %56 = sext i32 %54 to i64
  %57 = getelementptr inbounds [80000 x i64], ptr @prime, i64 0, i64 %56
  store i64 %53, ptr %57, align 8, !tbaa !16
  br label %58

58:                                               ; preds = %51, %45
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %4, align 4, !tbaa !5
  %61 = add nsw i32 %60, 2
  store i32 %61, ptr %4, align 4, !tbaa !5
  br label %41, !llvm.loop !18

62:                                               ; preds = %44
  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 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = !{!17, !17, i64 0}
!17 = !{!"long long", !7, i64 0}
!18 = distinct !{!18, !14}


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

@a = external dso_local local_unnamed_addr global [1000009 x i8], align 16
@prime = external dso_local local_unnamed_addr global [80000 x i64], align 16
@total = external dso_local local_unnamed_addr global i32, align 4

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

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

2:                                                ; preds = %0, %.loopexit
  %indvars.iv27 = phi i64 [ 5, %0 ], [ %indvars.iv.next28, %.loopexit ]
  %indvars.iv22 = phi i64 [ 2, %0 ], [ %indvars.iv.next23, %.loopexit ]
  %indvars.iv20 = phi i32 [ 4, %0 ], [ %indvars.iv.next21, %.loopexit ]
  %3 = getelementptr inbounds [1000009 x i8], ptr @a, i64 0, i64 %indvars.iv22
  %4 = load i8, ptr %3, align 1, !tbaa !11, !range !13, !noundef !14
  %5 = trunc nuw i8 %4 to i1
  %6 = mul nuw nsw i64 %indvars.iv22, %indvars.iv22
  %7 = icmp ugt i64 %6, 1000000
  %or.cond.not = select i1 %5, i1 true, i1 %7
  br i1 %or.cond.not, label %.loopexit, label %.lr.ph.preheader

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

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv24 = phi i64 [ %8, %.lr.ph.preheader ], [ %indvars.iv.next25, %.lr.ph ]
  %9 = getelementptr inbounds [1000009 x i8], ptr @a, i64 0, i64 %indvars.iv24
  store i8 1, ptr %9, align 1, !tbaa !11
  %indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, %indvars.iv22
  %10 = icmp ult i64 %indvars.iv.next25, 1000001
  br i1 %10, label %.lr.ph, label %.loopexit, !llvm.loop !15

.loopexit:                                        ; preds = %.lr.ph, %2
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 1
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 2
  %11 = trunc nuw nsw i64 %indvars.iv27 to i32
  %indvars.iv.next21 = add i32 %indvars.iv20, %11
  %exitcond.not = icmp eq i64 %indvars.iv.next23, 1001
  br i1 %exitcond.not, label %1, label %2, !llvm.loop !17

12:                                               ; preds = %22
  ret void

13:                                               ; preds = %33, %1
  %indvars.iv = phi i64 [ 3, %1 ], [ %indvars.iv.next.1, %33 ]
  %14 = phi i32 [ 1, %1 ], [ %34, %33 ]
  %15 = getelementptr inbounds [1000009 x i8], ptr @a, i64 0, i64 %indvars.iv
  %16 = load i8, ptr %15, align 1, !tbaa !11, !range !13, !noundef !14
  %17 = trunc nuw i8 %16 to i1
  br i1 %17, label %22, label %18

18:                                               ; preds = %13
  %19 = add nsw i32 %14, 1
  store i32 %19, ptr @total, align 4, !tbaa !5
  %20 = sext i32 %14 to i64
  %21 = getelementptr inbounds [80000 x i64], ptr @prime, i64 0, i64 %20
  store i64 %indvars.iv, ptr %21, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %13, %18
  %23 = phi i32 [ %14, %13 ], [ %19, %18 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 2
  %24 = icmp ult i64 %indvars.iv, 999999
  br i1 %24, label %25, label %12, !llvm.loop !18

25:                                               ; preds = %22
  %26 = getelementptr inbounds [1000009 x i8], ptr @a, i64 0, i64 %indvars.iv.next
  %27 = load i8, ptr %26, align 1, !tbaa !11, !range !13, !noundef !14
  %28 = trunc nuw i8 %27 to i1
  br i1 %28, label %33, label %29

29:                                               ; preds = %25
  %30 = add nsw i32 %23, 1
  store i32 %30, ptr @total, align 4, !tbaa !5
  %31 = sext i32 %23 to i64
  %32 = getelementptr inbounds [80000 x i64], ptr @prime, i64 0, i64 %31
  store i64 %indvars.iv.next, ptr %32, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %29, %25
  %34 = phi i32 [ %23, %25 ], [ %30, %29 ]
  %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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}
!18 = distinct !{!18, !16}


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