; LLVM IR from tangjz/acm-icpc :: usaco/Training/Chapter 2 Bigger challenges (subset)

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

@n = external dso_local global i32, align 4
@sum = external dso_local global i32, align 4
@f = external dso_local global [781 x i32], align 16
@.str = external hidden unnamed_addr constant [10 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %5 = load ptr, ptr @stdin, align 8, !tbaa !5
  %6 = call ptr @freopen(ptr noundef @.str, ptr noundef @.str.1, ptr noundef %5)
  %7 = load ptr, ptr @stdout, align 8, !tbaa !5
  %8 = call ptr @freopen(ptr noundef @.str.2, ptr noundef @.str.3, ptr noundef %7)
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef @n)
  %10 = load i32, ptr @n, align 4, !tbaa !9
  %11 = load i32, ptr @n, align 4, !tbaa !9
  %12 = add nsw i32 %11, 1
  %13 = mul nsw i32 %10, %12
  %14 = ashr i32 %13, 1
  store i32 %14, ptr @sum, align 4, !tbaa !9
  %15 = load i32, ptr @sum, align 4, !tbaa !9
  %16 = and i32 %15, 1
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %20

18:                                               ; preds = %0
  %19 = call i32 @puts(ptr noundef @.str.5)
  br label %61

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

21:                                               ; preds = %50, %20
  %22 = load i32, ptr %2, align 4, !tbaa !9
  %23 = load i32, ptr @n, align 4, !tbaa !9
  %24 = icmp sle i32 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 2, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %53

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %27 = load i32, ptr @sum, align 4, !tbaa !9
  %28 = ashr i32 %27, 1
  store i32 %28, ptr %4, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %46, %26
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = load i32, ptr %2, align 4, !tbaa !9
  %32 = icmp sge i32 %30, %31
  br i1 %32, label %34, label %33

33:                                               ; preds = %29
  store i32 5, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %49

34:                                               ; preds = %29
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = load i32, ptr %2, align 4, !tbaa !9
  %37 = sub nsw i32 %35, %36
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [781 x i32], ptr @f, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [781 x i32], ptr @f, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = add i32 %44, %40
  store i32 %45, ptr %43, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %34
  %47 = load i32, ptr %4, align 4, !tbaa !9
  %48 = add nsw i32 %47, -1
  store i32 %48, ptr %4, align 4, !tbaa !9
  br label %29, !llvm.loop !11

49:                                               ; preds = %33
  br label %50

50:                                               ; preds = %49
  %51 = load i32, ptr %2, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %2, align 4, !tbaa !9
  br label %21, !llvm.loop !13

53:                                               ; preds = %25
  %54 = load i32, ptr @sum, align 4, !tbaa !9
  %55 = ashr i32 %54, 1
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [781 x i32], ptr @f, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !9
  %59 = lshr i32 %58, 1
  %60 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, i32 noundef %59)
  br label %61

61:                                               ; preds = %53, %18
  %62 = load ptr, ptr @stdin, align 8, !tbaa !5
  %63 = call i32 @fclose(ptr noundef %62)
  %64 = load ptr, ptr @stdout, align 8, !tbaa !5
  %65 = call i32 @fclose(ptr noundef %64)
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) #1

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

declare i32 @puts(ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

declare i32 @printf(ptr noundef, ...) #1

declare i32 @fclose(ptr noundef) #1

attributes #0 = { mustprogress norecurse 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 = { "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 #2 = { nocallback nofree nosync 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 = !{!"any pointer", !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 = distinct !{!13, !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"

@n = external dso_local global i32, align 4
@sum = external dso_local local_unnamed_addr global i32, align 4
@f = external dso_local local_unnamed_addr global [781 x i32], align 16
@.str = external hidden unnamed_addr constant [10 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = load ptr, ptr @stdin, align 8, !tbaa !5
  %2 = tail call ptr @freopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, ptr noundef %1)
  %3 = load ptr, ptr @stdout, align 8, !tbaa !5
  %4 = tail call ptr @freopen(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3, ptr noundef %3)
  %5 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull @n)
  %6 = load i32, ptr @n, align 4, !tbaa !9
  %7 = add nsw i32 %6, 1
  %8 = mul nsw i32 %7, %6
  %9 = ashr i32 %8, 1
  store i32 %9, ptr @sum, align 4, !tbaa !9
  %10 = and i32 %8, 2
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %13, label %11

11:                                               ; preds = %0
  %12 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.5)
  br label %42

13:                                               ; preds = %0
  store i32 1, ptr @f, align 16, !tbaa !9
  %.not913 = icmp slt i32 %6, 1
  br i1 %.not913, label %._crit_edge17, label %.lr.ph16

.lr.ph16:                                         ; preds = %13
  %14 = lshr i32 %8, 2
  %15 = lshr i32 %8, 2
  %16 = zext nneg i32 %15 to i64
  %17 = zext nneg i32 %14 to i64
  %wide.trip.count = zext nneg i32 %7 to i64
  br label %24

._crit_edge17:                                    ; preds = %._crit_edge, %13
  %18 = ashr i32 %8, 2
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [781 x i32], ptr @f, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = lshr i32 %21, 1
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6, i32 noundef %22)
  br label %42

24:                                               ; preds = %.lr.ph16, %._crit_edge
  %indvar = phi i64 [ 0, %.lr.ph16 ], [ %indvar.next, %._crit_edge ]
  %indvars.iv18 = phi i64 [ 1, %.lr.ph16 ], [ %indvars.iv.next19, %._crit_edge ]
  %25 = sub i64 %16, %indvar
  %.not1011 = icmp ugt i64 %indvars.iv18, %17
  br i1 %.not1011, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %24
  %min.iters.check = icmp ult i64 %25, 8
  br i1 %min.iters.check, label %.lr.ph.preheader29, label %vector.ph

.lr.ph.preheader29:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ %16, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %25, -8
  %ind.end = sub i64 %16, %n.vec
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = sub i64 %16, %index
  %26 = sub nsw i64 %offset.idx, %indvars.iv18
  %27 = getelementptr inbounds [781 x i32], ptr @f, i64 0, i64 %26
  %28 = getelementptr inbounds i8, ptr %27, i64 -12
  %29 = getelementptr inbounds i8, ptr %27, i64 -28
  %wide.load = load <4 x i32>, ptr %28, align 4, !tbaa !9
  %wide.load21 = load <4 x i32>, ptr %29, align 4, !tbaa !9
  %30 = getelementptr inbounds [781 x i32], ptr @f, i64 0, i64 %offset.idx
  %31 = getelementptr inbounds i8, ptr %30, i64 -12
  %32 = getelementptr inbounds i8, ptr %30, i64 -28
  %wide.load23 = load <4 x i32>, ptr %31, align 4, !tbaa !9
  %wide.load25 = load <4 x i32>, ptr %32, align 4, !tbaa !9
  %33 = add <4 x i32> %wide.load23, %wide.load
  %34 = add <4 x i32> %wide.load25, %wide.load21
  store <4 x i32> %33, ptr %31, align 4, !tbaa !9
  store <4 x i32> %34, ptr %32, align 4, !tbaa !9
  %index.next = add nuw i64 %index, 8
  %35 = icmp eq i64 %index.next, %n.vec
  br i1 %35, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %25, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader29

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %24
  %indvars.iv.next19 = add nuw nsw i64 %indvars.iv18, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next19, %wide.trip.count
  %indvar.next = add i64 %indvar, 1
  br i1 %exitcond.not, label %._crit_edge17, label %24, !llvm.loop !15

.lr.ph:                                           ; preds = %.lr.ph.preheader29, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader29 ]
  %36 = sub nsw i64 %indvars.iv, %indvars.iv18
  %37 = getelementptr inbounds [781 x i32], ptr @f, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = getelementptr inbounds [781 x i32], ptr @f, i64 0, i64 %indvars.iv
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = add i32 %40, %38
  store i32 %41, ptr %39, align 4, !tbaa !9
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %.not10.not = icmp sgt i64 %indvars.iv, %indvars.iv18
  br i1 %.not10.not, label %.lr.ph, label %._crit_edge, !llvm.loop !16

42:                                               ; preds = %._crit_edge17, %11
  %43 = load ptr, ptr @stdin, align 8, !tbaa !5
  %44 = tail call i32 @fclose(ptr noundef %43)
  %45 = load ptr, ptr @stdout, align 8, !tbaa !5
  %46 = tail call i32 @fclose(ptr noundef %45)
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @puts(ptr nocapture noundef readonly) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #2

attributes #0 = { mustprogress norecurse 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 = { "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 #2 = { nofree nounwind "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 = !{!"any pointer", !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, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !12, !14, !13}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass