; LLVM IR from tangjz/acm-icpc :: usaco/Training/Chapter 1 Getting started/transfrom.ll (Z10transform2v)

; 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
@init = external dso_local global [11 x [11 x i8]], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z5matchv() #0

; 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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z10transform2v() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca [11 x [11 x i8]], align 16
  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 121, ptr %3) #2
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %31, %0
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp slt i32 %5, %6
  br i1 %7, label %8, label %34

8:                                                ; preds = %4
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %27, %8
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %30

13:                                               ; preds = %9
  %14 = load i32, ptr %1, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [11 x [11 x i8]], ptr @init, i64 0, i64 %15
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [11 x i8], ptr %16, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [11 x [11 x i8]], ptr %3, i64 0, i64 %22
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [11 x i8], ptr %23, i64 0, i64 %25
  store i8 %20, ptr %26, align 1, !tbaa !9
  br label %27

27:                                               ; preds = %13
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %2, align 4, !tbaa !5
  br label %9, !llvm.loop !10

30:                                               ; preds = %9
  br label %31

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

34:                                               ; preds = %4
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %65, %34
  %36 = load i32, ptr %1, align 4, !tbaa !5
  %37 = load i32, ptr @n, align 4, !tbaa !5
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %39, label %68

39:                                               ; preds = %35
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %61, %39
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = load i32, ptr @n, align 4, !tbaa !5
  %43 = icmp slt i32 %41, %42
  br i1 %43, label %44, label %64

44:                                               ; preds = %40
  %45 = load i32, ptr %1, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [11 x [11 x i8]], ptr %3, i64 0, i64 %46
  %48 = load i32, ptr @n, align 4, !tbaa !5
  %49 = load i32, ptr %2, align 4, !tbaa !5
  %50 = sub nsw i32 %48, %49
  %51 = sub nsw i32 %50, 1
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [11 x i8], ptr %47, i64 0, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !9
  %55 = load i32, ptr %1, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [11 x [11 x i8]], ptr @init, i64 0, i64 %56
  %58 = load i32, ptr %2, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [11 x i8], ptr %57, i64 0, i64 %59
  store i8 %54, ptr %60, align 1, !tbaa !9
  br label %61

61:                                               ; preds = %44
  %62 = load i32, ptr %2, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %2, align 4, !tbaa !5
  br label %40, !llvm.loop !13

64:                                               ; preds = %40
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %1, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %1, align 4, !tbaa !5
  br label %35, !llvm.loop !14

68:                                               ; preds = %35
  %69 = call noundef zeroext i1 @_Z5matchv()
  call void @llvm.lifetime.end.p0(i64 121, 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 i1 %69
}

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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !11}


; 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 local_unnamed_addr global i32, align 4
@init = external dso_local local_unnamed_addr global [11 x [11 x i8]], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z5matchv() local_unnamed_addr #0

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z10transform2v() local_unnamed_addr #0 {
  %1 = alloca [11 x [11 x i8]], align 16
  call void @llvm.lifetime.start.p0(i64 121, ptr nonnull %1) #3
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.preheader20.us.preheader, label %._crit_edge25

.preheader20.us.preheader:                        ; preds = %0
  %4 = zext nneg i32 %2 to i64
  %xtraiter = and i64 %4, 3
  %5 = icmp ult i32 %2, 4
  br i1 %5, label %.preheader19.unr-lcssa, label %.preheader20.us.preheader.new

.preheader20.us.preheader.new:                    ; preds = %.preheader20.us.preheader
  %unroll_iter = and i64 %4, 2147483644
  br label %.preheader20.us

.preheader20.us:                                  ; preds = %.preheader20.us, %.preheader20.us.preheader.new
  %indvar = phi i64 [ 0, %.preheader20.us.preheader.new ], [ %indvar.next.3, %.preheader20.us ]
  %niter = phi i64 [ 0, %.preheader20.us.preheader.new ], [ %niter.next.3, %.preheader20.us ]
  %6 = mul nuw nsw i64 %indvar, 11
  %scevgep = getelementptr i8, ptr %1, i64 %6
  %scevgep27 = getelementptr i8, ptr @init, i64 %6
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep, ptr align 4 %scevgep27, i64 %4, i1 false), !tbaa !9
  %7 = mul nuw i64 %indvar, 11
  %8 = add nuw i64 %7, 11
  %scevgep.1 = getelementptr i8, ptr %1, i64 %8
  %scevgep27.1 = getelementptr i8, ptr @init, i64 %8
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %scevgep.1, ptr align 1 %scevgep27.1, i64 %4, i1 false), !tbaa !9
  %9 = mul nuw i64 %indvar, 11
  %10 = add nuw i64 %9, 22
  %scevgep.2 = getelementptr i8, ptr %1, i64 %10
  %scevgep27.2 = getelementptr i8, ptr @init, i64 %10
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %scevgep.2, ptr align 2 %scevgep27.2, i64 %4, i1 false), !tbaa !9
  %11 = mul nuw i64 %indvar, 11
  %12 = add nuw i64 %11, 33
  %scevgep.3 = getelementptr i8, ptr %1, i64 %12
  %scevgep27.3 = getelementptr i8, ptr @init, i64 %12
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %scevgep.3, ptr align 1 %scevgep27.3, i64 %4, i1 false), !tbaa !9
  %indvar.next.3 = add nuw nsw i64 %indvar, 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 %.preheader19.unr-lcssa, label %.preheader20.us, !llvm.loop !10

.preheader19.unr-lcssa:                           ; preds = %.preheader20.us, %.preheader20.us.preheader
  %indvar.unr = phi i64 [ 0, %.preheader20.us.preheader ], [ %indvar.next.3, %.preheader20.us ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader19, label %.preheader20.us.epil

.preheader20.us.epil:                             ; preds = %.preheader19.unr-lcssa, %.preheader20.us.epil
  %indvar.epil = phi i64 [ %indvar.next.epil, %.preheader20.us.epil ], [ %indvar.unr, %.preheader19.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader20.us.epil ], [ 0, %.preheader19.unr-lcssa ]
  %13 = mul nuw nsw i64 %indvar.epil, 11
  %scevgep.epil = getelementptr i8, ptr %1, i64 %13
  %scevgep27.epil = getelementptr i8, ptr @init, i64 %13
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %scevgep.epil, ptr align 1 %scevgep27.epil, i64 %4, i1 false), !tbaa !9
  %indvar.next.epil = add nuw nsw i64 %indvar.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 %.preheader19, label %.preheader20.us.epil, !llvm.loop !12

.preheader19:                                     ; preds = %.preheader20.us.epil, %.preheader19.unr-lcssa
  br i1 %3, label %.preheader.us.preheader, label %._crit_edge25

.preheader.us.preheader:                          ; preds = %.preheader19
  %14 = zext nneg i32 %2 to i64
  %wide.trip.count36 = zext nneg i32 %2 to i64
  %min.iters.check = icmp ult i32 %2, 8
  %min.iters.check38 = icmp ult i32 %2, 32
  %n.vec = and i64 %4, 2147483616
  %cmp.n = icmp eq i64 %n.vec, %4
  %n.vec.remaining = and i64 %4, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  %n.vec42 = and i64 %4, 2147483640
  %invariant.gep = getelementptr i8, ptr %1, i64 -7
  %cmp.n47 = icmp eq i64 %n.vec42, %4
  br label %iter.check

iter.check:                                       ; preds = %.preheader.us.preheader, %._crit_edge.us26
  %indvars.iv33 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next34, %._crit_edge.us26 ]
  br i1 %min.iters.check, label %vec.epilog.scalar.ph.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %iter.check
  br i1 %min.iters.check38, label %vec.epilog.ph, label %vector.body

vector.body:                                      ; preds = %vector.main.loop.iter.check, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %vector.main.loop.iter.check ]
  %15 = xor i64 %index, -1
  %16 = add nsw i64 %14, %15
  %17 = getelementptr inbounds [11 x [11 x i8]], ptr %1, i64 0, i64 %indvars.iv33, i64 %16
  %18 = getelementptr inbounds i8, ptr %17, i64 -15
  %19 = getelementptr inbounds i8, ptr %17, i64 -31
  %wide.load = load <16 x i8>, ptr %18, align 1, !tbaa !9
  %reverse = shufflevector <16 x i8> %wide.load, <16 x i8> poison, <16 x i32> <i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
  %wide.load39 = load <16 x i8>, ptr %19, align 1, !tbaa !9
  %reverse40 = shufflevector <16 x i8> %wide.load39, <16 x i8> poison, <16 x i32> <i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
  %20 = getelementptr inbounds [11 x [11 x i8]], ptr @init, i64 0, i64 %indvars.iv33, i64 %index
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  store <16 x i8> %reverse, ptr %20, align 1, !tbaa !9
  store <16 x i8> %reverse40, ptr %21, align 1, !tbaa !9
  %index.next = add nuw i64 %index, 32
  %22 = icmp eq i64 %index.next, %n.vec
  br i1 %22, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us26, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block
  br i1 %min.epilog.iters.check, label %vec.epilog.scalar.ph.preheader, label %vec.epilog.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index43 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next46, %vec.epilog.vector.body ]
  %23 = xor i64 %index43, -1
  %24 = add nsw i64 %14, %23
  %gep = getelementptr [11 x [11 x i8]], ptr %invariant.gep, i64 0, i64 %indvars.iv33, i64 %24
  %wide.load44 = load <8 x i8>, ptr %gep, align 1, !tbaa !9
  %reverse45 = shufflevector <8 x i8> %wide.load44, <8 x i8> poison, <8 x i32> <i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
  %25 = getelementptr inbounds [11 x [11 x i8]], ptr @init, i64 0, i64 %indvars.iv33, i64 %index43
  store <8 x i8> %reverse45, ptr %25, align 1, !tbaa !9
  %index.next46 = add nuw i64 %index43, 8
  %26 = icmp eq i64 %index.next46, %n.vec42
  br i1 %26, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !17

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  br i1 %cmp.n47, label %._crit_edge.us26, label %vec.epilog.scalar.ph.preheader

vec.epilog.scalar.ph.preheader:                   ; preds = %vec.epilog.middle.block, %iter.check, %vec.epilog.iter.check
  %indvars.iv.ph = phi i64 [ 0, %iter.check ], [ %n.vec, %vec.epilog.iter.check ], [ %n.vec42, %vec.epilog.middle.block ]
  br label %vec.epilog.scalar.ph

vec.epilog.scalar.ph:                             ; preds = %vec.epilog.scalar.ph.preheader, %vec.epilog.scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %vec.epilog.scalar.ph ], [ %indvars.iv.ph, %vec.epilog.scalar.ph.preheader ]
  %27 = xor i64 %indvars.iv, -1
  %28 = add nsw i64 %14, %27
  %29 = getelementptr inbounds [11 x [11 x i8]], ptr %1, i64 0, i64 %indvars.iv33, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !9
  %31 = getelementptr inbounds [11 x [11 x i8]], ptr @init, i64 0, i64 %indvars.iv33, i64 %indvars.iv
  store i8 %30, ptr %31, align 1, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond32.not = icmp eq i64 %indvars.iv.next, %wide.trip.count36
  br i1 %exitcond32.not, label %._crit_edge.us26, label %vec.epilog.scalar.ph, !llvm.loop !18

._crit_edge.us26:                                 ; preds = %vec.epilog.scalar.ph, %vec.epilog.middle.block, %middle.block
  %indvars.iv.next34 = add nuw nsw i64 %indvars.iv33, 1
  %exitcond37.not = icmp eq i64 %indvars.iv.next34, %wide.trip.count36
  br i1 %exitcond37.not, label %._crit_edge25, label %iter.check, !llvm.loop !19

._crit_edge25:                                    ; preds = %._crit_edge.us26, %0, %.preheader19
  %32 = tail call noundef zeroext i1 @_Z5matchv()
  call void @llvm.lifetime.end.p0(i64 121, ptr nonnull %1) #3
  ret i1 %32
}

; 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) #2

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}
!14 = distinct !{!14, !11, !15, !16}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !11, !15, !16}
!18 = distinct !{!18, !11, !16, !15}
!19 = distinct !{!19, !11}


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