; LLVM IR from abeaumont/competitive-programming :: kattis/tritiling.ll (Z1fv)

; 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 [31 x i32], align 16
@c = external dso_local global [31 x i32], align 16
@d = external dso_local global [31 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z1fv() #0 {
  %1 = alloca i32, align 4
  store i32 1, ptr @d, align 16, !tbaa !5
  %2 = load i32, ptr @d, align 16, !tbaa !5
  %3 = load i32, ptr @c, align 16, !tbaa !5
  %4 = add nsw i32 %2, %3
  store i32 %4, ptr getelementptr inbounds ([31 x i32], ptr @a, i64 0, i64 1), align 4, !tbaa !5
  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 = %53, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 31
  br i1 %7, label %9, label %8

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

9:                                                ; preds = %5
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sub nsw i32 %10, 1
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [31 x i32], ptr @d, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = sub nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [31 x i32], ptr @c, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = add nsw i32 %14, %19
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [31 x i32], ptr @a, i64 0, i64 %22
  store i32 %20, ptr %23, align 4, !tbaa !5
  %24 = load i32, ptr %1, align 4, !tbaa !5
  %25 = sub nsw i32 %24, 2
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [31 x i32], ptr @c, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = load i32, ptr %1, align 4, !tbaa !5
  %30 = sub nsw i32 %29, 2
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [31 x i32], ptr @d, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %28, %33
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [31 x i32], ptr @c, i64 0, i64 %36
  store i32 %34, ptr %37, align 4, !tbaa !5
  %38 = load i32, ptr %1, align 4, !tbaa !5
  %39 = sub nsw i32 %38, 2
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [31 x i32], ptr @d, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = load i32, ptr %1, align 4, !tbaa !5
  %44 = sub nsw i32 %43, 1
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [31 x i32], ptr @a, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = mul nsw i32 2, %47
  %49 = add nsw i32 %42, %48
  %50 = load i32, ptr %1, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [31 x i32], ptr @d, i64 0, i64 %51
  store i32 %49, ptr %52, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %9
  %54 = load i32, ptr %1, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !9

56:                                               ; preds = %8
  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"}


; 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 [31 x i32], align 16
@c = external dso_local local_unnamed_addr global [31 x i32], align 16
@d = external dso_local local_unnamed_addr global [31 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z1fv() local_unnamed_addr #0 {
  store i32 1, ptr @d, align 16, !tbaa !5
  %1 = load i32, ptr @c, align 16, !tbaa !5
  %2 = add nsw i32 %1, 1
  store i32 %2, ptr getelementptr inbounds (i8, ptr @a, i64 4), align 4, !tbaa !5
  %.pre = load i32, ptr getelementptr inbounds (i8, ptr @d, i64 4), align 4, !tbaa !5
  %.pre13 = load i32, ptr getelementptr inbounds (i8, ptr @c, i64 4), align 4, !tbaa !5
  br label %4

3:                                                ; preds = %4
  ret void

4:                                                ; preds = %0, %4
  %5 = phi i32 [ %2, %0 ], [ %8, %4 ]
  %6 = phi i32 [ %.pre13, %0 ], [ %15, %4 ]
  %7 = phi i32 [ %.pre, %0 ], [ %18, %4 ]
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next, %4 ]
  %8 = add nsw i32 %6, %7
  %9 = getelementptr inbounds [31 x i32], ptr @a, i64 0, i64 %indvars.iv
  store i32 %8, ptr %9, align 4, !tbaa !5
  %10 = add nsw i64 %indvars.iv, -2
  %11 = getelementptr inbounds [31 x i32], ptr @c, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = getelementptr inbounds [31 x i32], ptr @d, i64 0, i64 %10
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = add nsw i32 %14, %12
  %16 = getelementptr inbounds [31 x i32], ptr @c, i64 0, i64 %indvars.iv
  store i32 %15, ptr %16, align 4, !tbaa !5
  %17 = shl nsw i32 %5, 1
  %18 = add nsw i32 %17, %14
  %19 = getelementptr inbounds [31 x i32], ptr @d, i64 0, i64 %indvars.iv
  store i32 %18, ptr %19, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 31
  br i1 %exitcond.not, label %3, label %4, !llvm.loop !9
}

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


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass