; LLVM IR from UtkarshPathrabe/Competitive-Coding :: SPOJ Solutions (AFS)

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

@list = external dso_local global [1000000 x i64], align 16
@sequence = external dso_local global [1000000 x i64], align 16

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

4:                                                ; preds = %14, %0
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 1000000
  br i1 %6, label %7, label %17

7:                                                ; preds = %4
  %8 = load i32, ptr %1, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %9
  store i64 1, ptr %10, align 8, !tbaa !9
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [1000000 x i64], ptr @sequence, i64 0, i64 %12
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %7
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %1, align 4, !tbaa !5
  br label %4, !llvm.loop !11

17:                                               ; preds = %4
  store i32 2, ptr %1, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %38, %17
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = icmp slt i32 %19, 1000000
  br i1 %20, label %21, label %41

21:                                               ; preds = %18
  store i32 2, ptr %2, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %27, %21
  %23 = load i32, ptr %1, align 4, !tbaa !5
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = mul nsw i32 %23, %24
  store i32 %25, ptr %3, align 4, !tbaa !5
  %26 = icmp slt i32 %25, 1000000
  br i1 %26, label %27, label %37

27:                                               ; preds = %22
  %28 = load i32, ptr %1, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !9
  %34 = add nsw i64 %33, %29
  store i64 %34, ptr %32, align 8, !tbaa !9
  %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 %22, !llvm.loop !13

37:                                               ; preds = %22
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %1, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %1, align 4, !tbaa !5
  br label %18, !llvm.loop !14

41:                                               ; preds = %18
  store i32 2, ptr %1, align 4, !tbaa !5
  br label %42

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

45:                                               ; preds = %42
  %46 = load i32, ptr %1, align 4, !tbaa !5
  %47 = sub nsw i32 %46, 1
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [1000000 x i64], ptr @sequence, i64 0, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !9
  %51 = load i32, ptr %1, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !9
  %55 = add nsw i64 %50, %54
  %56 = load i32, ptr %1, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [1000000 x i64], ptr @sequence, i64 0, i64 %57
  store i64 %55, ptr %58, align 8, !tbaa !9
  br label %59

59:                                               ; preds = %45
  %60 = load i32, ptr %1, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %1, align 4, !tbaa !5
  br label %42, !llvm.loop !15

62:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #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 = { 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/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 = distinct !{!14, !12}
!15 = distinct !{!15, !12}


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

@list = external dso_local local_unnamed_addr global [1000000 x i64], align 16
@sequence = external dso_local local_unnamed_addr global [1000000 x i64], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @pre() local_unnamed_addr #0 {
vector.ph:
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8000000) @sequence, i8 0, i64 8000000, i1 false), !tbaa !5
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next.4, %vector.body ]
  %0 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %index
  %1 = getelementptr inbounds i8, ptr %0, i64 16
  store <2 x i64> <i64 1, i64 1>, ptr %0, align 16, !tbaa !5
  store <2 x i64> <i64 1, i64 1>, ptr %1, align 16, !tbaa !5
  %index.next = add nuw nsw i64 %index, 4
  %2 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %index.next
  %3 = getelementptr inbounds i8, ptr %2, i64 16
  store <2 x i64> <i64 1, i64 1>, ptr %2, align 16, !tbaa !5
  store <2 x i64> <i64 1, i64 1>, ptr %3, align 16, !tbaa !5
  %index.next.1 = add nuw nsw i64 %index, 8
  %4 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %index.next.1
  %5 = getelementptr inbounds i8, ptr %4, i64 16
  store <2 x i64> <i64 1, i64 1>, ptr %4, align 16, !tbaa !5
  store <2 x i64> <i64 1, i64 1>, ptr %5, align 16, !tbaa !5
  %index.next.2 = add nuw nsw i64 %index, 12
  %6 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %index.next.2
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  store <2 x i64> <i64 1, i64 1>, ptr %6, align 16, !tbaa !5
  store <2 x i64> <i64 1, i64 1>, ptr %7, align 16, !tbaa !5
  %index.next.3 = add nuw nsw i64 %index, 16
  %8 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %index.next.3
  %9 = getelementptr inbounds i8, ptr %8, i64 16
  store <2 x i64> <i64 1, i64 1>, ptr %8, align 16, !tbaa !5
  store <2 x i64> <i64 1, i64 1>, ptr %9, align 16, !tbaa !5
  %index.next.4 = add nuw nsw i64 %index, 20
  %10 = icmp eq i64 %index.next.4, 1000000
  br i1 %10, label %.preheader18, label %vector.body, !llvm.loop !9

.preheader18:                                     ; preds = %vector.body, %._crit_edge
  %indvars.iv27 = phi i64 [ %indvars.iv.next28, %._crit_edge ], [ 2, %vector.body ]
  %indvars.iv25 = phi i64 [ %indvars.iv.next26, %._crit_edge ], [ 4, %vector.body ]
  %11 = icmp ult i64 %indvars.iv27, 500000
  br i1 %11, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader18, %.lr.ph
  %indvars.iv31 = phi i64 [ %indvars.iv.next32, %.lr.ph ], [ 2, %.preheader18 ]
  %indvars.iv29 = phi i64 [ %indvars.iv.next30, %.lr.ph ], [ %indvars.iv25, %.preheader18 ]
  %12 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %indvars.iv29
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = add nsw i64 %13, %indvars.iv27
  store i64 %14, ptr %12, align 8, !tbaa !5
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %15 = mul nuw nsw i64 %indvars.iv.next32, %indvars.iv27
  %16 = icmp ult i64 %15, 1000000
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, %indvars.iv27
  br i1 %16, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %.preheader18
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, 2
  %exitcond37.not = icmp eq i64 %indvars.iv.next28, 1000000
  br i1 %exitcond37.not, label %.preheader, label %.preheader18, !llvm.loop !14

.preheader:                                       ; preds = %._crit_edge, %.preheader
  %17 = phi i64 [ %24, %.preheader ], [ 0, %._crit_edge ]
  %indvars.iv38 = phi i64 [ %indvars.iv.next39.1, %.preheader ], [ 2, %._crit_edge ]
  %18 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %indvars.iv38
  %19 = load i64, ptr %18, align 16, !tbaa !5
  %20 = add nsw i64 %19, %17
  %21 = getelementptr inbounds [1000000 x i64], ptr @sequence, i64 0, i64 %indvars.iv38
  store i64 %20, ptr %21, align 16, !tbaa !5
  %indvars.iv.next39 = or disjoint i64 %indvars.iv38, 1
  %22 = getelementptr inbounds [1000000 x i64], ptr @list, i64 0, i64 %indvars.iv.next39
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = add nsw i64 %23, %20
  %25 = getelementptr inbounds [1000000 x i64], ptr @sequence, i64 0, i64 %indvars.iv.next39
  store i64 %24, ptr %25, align 8, !tbaa !5
  %indvars.iv.next39.1 = add nuw nsw i64 %indvars.iv38, 2
  %exitcond41.not.1 = icmp eq i64 %indvars.iv.next39.1, 1000000
  br i1 %exitcond41.not.1, label %26, label %.preheader, !llvm.loop !15

26:                                               ; preds = %.preheader
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { 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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass