; LLVM IR from smv1999/CompetitiveProgrammingQuestionBank :: 2D Arrays/matrix_rotation.ll (Z6rotatei)

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

@matrix = external dso_local global [100 x [100 x i32]], align 16
@temp = external dso_local global [100 x [100 x i32]], align 16
@n = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6rotatei(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = sdiv i32 %6, 90
  store i32 %7, ptr %5, align 4, !tbaa !5
  %8 = load i32, ptr %5, align 4, !tbaa !5
  %9 = srem i32 %8, 4
  store i32 %9, ptr %5, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %79, %1
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = add nsw i32 %11, -1
  store i32 %12, ptr %5, align 4, !tbaa !5
  %13 = icmp ne i32 %11, 0
  br i1 %13, label %14, label %80

14:                                               ; preds = %10
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %45, %14
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = load i32, ptr @n, align 4, !tbaa !5
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %19, label %48

19:                                               ; preds = %15
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %41, %19
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = load i32, ptr @n, align 4, !tbaa !5
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %24, label %44

24:                                               ; preds = %20
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100 x [100 x i32]], ptr @matrix, i64 0, i64 %26
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100 x i32], ptr %27, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [100 x [100 x i32]], ptr @temp, i64 0, i64 %33
  %35 = load i32, ptr @n, align 4, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sub nsw i32 %35, %36
  %38 = sub nsw i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [100 x i32], ptr %34, i64 0, i64 %39
  store i32 %31, ptr %40, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %24
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %4, align 4, !tbaa !5
  br label %20, !llvm.loop !9

44:                                               ; preds = %20
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %3, align 4, !tbaa !5
  br label %15, !llvm.loop !11

48:                                               ; preds = %15
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %76, %48
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = load i32, ptr @n, align 4, !tbaa !5
  %52 = icmp slt i32 %50, %51
  br i1 %52, label %53, label %79

53:                                               ; preds = %49
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %72, %53
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = load i32, ptr @n, align 4, !tbaa !5
  %57 = icmp slt i32 %55, %56
  br i1 %57, label %58, label %75

58:                                               ; preds = %54
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [100 x [100 x i32]], ptr @temp, i64 0, i64 %60
  %62 = load i32, ptr %4, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [100 x i32], ptr %61, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = load i32, ptr %3, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [100 x [100 x i32]], ptr @matrix, i64 0, i64 %67
  %69 = load i32, ptr %4, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [100 x i32], ptr %68, i64 0, i64 %70
  store i32 %65, ptr %71, align 4, !tbaa !5
  br label %72

72:                                               ; preds = %58
  %73 = load i32, ptr %4, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %4, align 4, !tbaa !5
  br label %54, !llvm.loop !12

75:                                               ; preds = %54
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %3, align 4, !tbaa !5
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %3, align 4, !tbaa !5
  br label %49, !llvm.loop !13

79:                                               ; preds = %49
  br label %10, !llvm.loop !14

80:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #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}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !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"

@matrix = external dso_local local_unnamed_addr global [100 x [100 x i32]], align 16
@temp = external dso_local local_unnamed_addr global [100 x [100 x i32]], align 16
@n = 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 @_Z6rotatei(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sdiv i32 %0, 90
  %3 = srem i32 %2, 4
  %.not31 = icmp eq i32 %3, 0
  br i1 %.not31, label %._crit_edge, label %.preheader25.lr.ph

.preheader25.lr.ph:                               ; preds = %1
  %4 = load i32, ptr @n, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.preheader25.us.us.preheader, label %._crit_edge

.preheader25.us.us.preheader:                     ; preds = %.preheader25.lr.ph
  %6 = zext nneg i32 %4 to i64
  %7 = zext nneg i32 %4 to i64
  %8 = shl nuw nsw i64 %7, 2
  %xtraiter = and i64 %7, 3
  %9 = icmp ult i32 %4, 4
  %unroll_iter = and i64 %7, 2147483644
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  %xtraiter55 = and i64 %7, 3
  %10 = icmp ult i32 %4, 4
  %unroll_iter58 = and i64 %7, 2147483644
  %lcmp.mod57.not = icmp eq i64 %xtraiter55, 0
  br label %.preheader25.us.us

.preheader25.us.us:                               ; preds = %.preheader25.us.us.preheader, %..loopexit_crit_edge.split.us.us.us
  %.in = phi i32 [ %11, %..loopexit_crit_edge.split.us.us.us ], [ %3, %.preheader25.us.us.preheader ]
  %11 = add nsw i32 %.in, -1
  br label %.preheader23.us.us.us

.preheader23.us.us.us:                            ; preds = %._crit_edge.us.us.us, %.preheader25.us.us
  %indvars.iv43 = phi i64 [ %indvars.iv.next44, %._crit_edge.us.us.us ], [ 0, %.preheader25.us.us ]
  %12 = xor i64 %indvars.iv43, -1
  %13 = add nsw i64 %6, %12
  br i1 %9, label %._crit_edge.us.us.us.unr-lcssa, label %.preheader23.us.us.us.new

.preheader23.us.us.us.new:                        ; preds = %.preheader23.us.us.us, %.preheader23.us.us.us.new
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.preheader23.us.us.us.new ], [ 0, %.preheader23.us.us.us ]
  %niter = phi i64 [ %niter.next.3, %.preheader23.us.us.us.new ], [ 0, %.preheader23.us.us.us ]
  %14 = getelementptr inbounds [100 x [100 x i32]], ptr @matrix, i64 0, i64 %indvars.iv43, i64 %indvars.iv
  %15 = load i32, ptr %14, align 16, !tbaa !5
  %16 = getelementptr inbounds [100 x [100 x i32]], ptr @temp, i64 0, i64 %indvars.iv, i64 %13
  store i32 %15, ptr %16, align 4, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %17 = getelementptr inbounds [100 x [100 x i32]], ptr @matrix, i64 0, i64 %indvars.iv43, i64 %indvars.iv.next
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = getelementptr inbounds [100 x [100 x i32]], ptr @temp, i64 0, i64 %indvars.iv.next, i64 %13
  store i32 %18, ptr %19, align 4, !tbaa !5
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %20 = getelementptr inbounds [100 x [100 x i32]], ptr @matrix, i64 0, i64 %indvars.iv43, i64 %indvars.iv.next.1
  %21 = load i32, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds [100 x [100 x i32]], ptr @temp, i64 0, i64 %indvars.iv.next.1, i64 %13
  store i32 %21, ptr %22, align 4, !tbaa !5
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %23 = getelementptr inbounds [100 x [100 x i32]], ptr @matrix, i64 0, i64 %indvars.iv43, i64 %indvars.iv.next.2
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = getelementptr inbounds [100 x [100 x i32]], ptr @temp, i64 0, i64 %indvars.iv.next.2, i64 %13
  store i32 %24, ptr %25, align 4, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.us.us.us.unr-lcssa, label %.preheader23.us.us.us.new, !llvm.loop !9

._crit_edge.us.us.us.unr-lcssa:                   ; preds = %.preheader23.us.us.us.new, %.preheader23.us.us.us
  %indvars.iv.unr = phi i64 [ 0, %.preheader23.us.us.us ], [ %indvars.iv.next.3, %.preheader23.us.us.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us.us.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.us.us.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %._crit_edge.us.us.us.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.us.us.us.unr-lcssa ]
  %26 = getelementptr inbounds [100 x [100 x i32]], ptr @matrix, i64 0, i64 %indvars.iv43, i64 %indvars.iv.epil
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = getelementptr inbounds [100 x [100 x i32]], ptr @temp, i64 0, i64 %indvars.iv.epil, i64 %13
  store i32 %27, ptr %28, align 4, !tbaa !5
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge.us.us.us, label %.epil.preheader, !llvm.loop !11

._crit_edge.us.us.us:                             ; preds = %.epil.preheader, %._crit_edge.us.us.us.unr-lcssa
  %indvars.iv.next44 = add nuw nsw i64 %indvars.iv43, 1
  %exitcond47.not = icmp eq i64 %indvars.iv.next44, %7
  br i1 %exitcond47.not, label %.preheader.us.us.us.preheader, label %.preheader23.us.us.us, !llvm.loop !13

.preheader.us.us.us.preheader:                    ; preds = %._crit_edge.us.us.us
  br i1 %10, label %..loopexit_crit_edge.split.us.us.us.unr-lcssa, label %.preheader.us.us.us

.preheader.us.us.us:                              ; preds = %.preheader.us.us.us.preheader, %.preheader.us.us.us
  %indvar = phi i64 [ %indvar.next.3, %.preheader.us.us.us ], [ 0, %.preheader.us.us.us.preheader ]
  %niter59 = phi i64 [ %niter59.next.3, %.preheader.us.us.us ], [ 0, %.preheader.us.us.us.preheader ]
  %29 = mul nuw nsw i64 %indvar, 400
  %scevgep = getelementptr i8, ptr @matrix, i64 %29
  %scevgep48 = getelementptr i8, ptr @temp, i64 %29
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 16 %scevgep, ptr align 16 %scevgep48, i64 %8, i1 false), !tbaa !5
  %30 = mul nuw i64 %indvar, 400
  %31 = add nuw i64 %30, 400
  %scevgep.1 = getelementptr i8, ptr @matrix, i64 %31
  %scevgep48.1 = getelementptr i8, ptr @temp, i64 %31
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 16 %scevgep.1, ptr align 16 %scevgep48.1, i64 %8, i1 false), !tbaa !5
  %32 = mul nuw i64 %indvar, 400
  %33 = add nuw i64 %32, 800
  %scevgep.2 = getelementptr i8, ptr @matrix, i64 %33
  %scevgep48.2 = getelementptr i8, ptr @temp, i64 %33
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 16 %scevgep.2, ptr align 16 %scevgep48.2, i64 %8, i1 false), !tbaa !5
  %34 = mul nuw i64 %indvar, 400
  %35 = add nuw i64 %34, 1200
  %scevgep.3 = getelementptr i8, ptr @matrix, i64 %35
  %scevgep48.3 = getelementptr i8, ptr @temp, i64 %35
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 16 %scevgep.3, ptr align 16 %scevgep48.3, i64 %8, i1 false), !tbaa !5
  %indvar.next.3 = add nuw nsw i64 %indvar, 4
  %niter59.next.3 = add i64 %niter59, 4
  %niter59.ncmp.3 = icmp eq i64 %niter59.next.3, %unroll_iter58
  br i1 %niter59.ncmp.3, label %..loopexit_crit_edge.split.us.us.us.unr-lcssa, label %.preheader.us.us.us, !llvm.loop !14

..loopexit_crit_edge.split.us.us.us.unr-lcssa:    ; preds = %.preheader.us.us.us, %.preheader.us.us.us.preheader
  %indvar.unr = phi i64 [ 0, %.preheader.us.us.us.preheader ], [ %indvar.next.3, %.preheader.us.us.us ]
  br i1 %lcmp.mod57.not, label %..loopexit_crit_edge.split.us.us.us, label %.preheader.us.us.us.epil

.preheader.us.us.us.epil:                         ; preds = %..loopexit_crit_edge.split.us.us.us.unr-lcssa, %.preheader.us.us.us.epil
  %indvar.epil = phi i64 [ %indvar.next.epil, %.preheader.us.us.us.epil ], [ %indvar.unr, %..loopexit_crit_edge.split.us.us.us.unr-lcssa ]
  %epil.iter56 = phi i64 [ %epil.iter56.next, %.preheader.us.us.us.epil ], [ 0, %..loopexit_crit_edge.split.us.us.us.unr-lcssa ]
  %36 = mul nuw nsw i64 %indvar.epil, 400
  %scevgep.epil = getelementptr i8, ptr @matrix, i64 %36
  %scevgep48.epil = getelementptr i8, ptr @temp, i64 %36
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 16 %scevgep.epil, ptr align 16 %scevgep48.epil, i64 %8, i1 false), !tbaa !5
  %indvar.next.epil = add nuw nsw i64 %indvar.epil, 1
  %epil.iter56.next = add i64 %epil.iter56, 1
  %epil.iter56.cmp.not = icmp eq i64 %epil.iter56.next, %xtraiter55
  br i1 %epil.iter56.cmp.not, label %..loopexit_crit_edge.split.us.us.us, label %.preheader.us.us.us.epil, !llvm.loop !15

..loopexit_crit_edge.split.us.us.us:              ; preds = %.preheader.us.us.us.epil, %..loopexit_crit_edge.split.us.us.us.unr-lcssa
  %.not.us.us = icmp eq i32 %11, 0
  br i1 %.not.us.us, label %._crit_edge, label %.preheader25.us.us, !llvm.loop !16

._crit_edge:                                      ; preds = %..loopexit_crit_edge.split.us.us.us, %.preheader25.lr.ph, %1
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

!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, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass