; LLVM IR from bqi343/cp-notebook :: Contests/USACO Training/Ch 6/6.1 cowxor.ll (Z2seii)

; 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
@cow = external dso_local global [100000 x i32], align 16
@z = external dso_local global [2097152 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z2seii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = icmp eq i32 %9, -1
  br i1 %10, label %11, label %13

11:                                               ; preds = %2
  %12 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %12, ptr %3, align 4
  br label %62

13:                                               ; preds = %2
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = mul nsw i32 2, %14
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %4, align 4, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 16 @z, i8 -1, i64 8388608, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  store i32 0, ptr @z, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %50, %13
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = load i32, ptr @N, align 4, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  store i32 2, ptr %8, align 4
  br label %53

22:                                               ; preds = %17
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [100000 x i32], ptr @cow, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = ashr i32 %26, %27
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = xor i32 %29, %28
  store i32 %30, ptr %6, align 4, !tbaa !5
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = xor i32 %31, %32
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [2097152 x i32], ptr @z, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = icmp eq i32 %36, -1
  br i1 %37, label %38, label %44

38:                                               ; preds = %22
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [2097152 x i32], ptr @z, i64 0, i64 %42
  store i32 %40, ptr %43, align 4, !tbaa !5
  br label %49

44:                                               ; preds = %22
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = sub nsw i32 %46, 1
  %48 = call noundef i32 @_Z2seii(i32 noundef %45, i32 noundef %47)
  store i32 %48, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %53

49:                                               ; preds = %38
  br label %50

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

53:                                               ; preds = %44, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %54 = load i32, ptr %8, align 4
  switch i32 %54, label %61 [
    i32 2, label %55
  ]

55:                                               ; preds = %53
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = sub nsw i32 %56, 1
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = sub nsw i32 %58, 1
  %60 = call noundef i32 @_Z2seii(i32 noundef %57, i32 noundef %59)
  store i32 %60, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %61

61:                                               ; preds = %55, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %62

62:                                               ; preds = %61, %11
  %63 = load i32, ptr %3, align 4
  ret i32 %63
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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

attributes #0 = { mustprogress 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) }
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 = !{!"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"

@N = external dso_local local_unnamed_addr global i32, align 4
@cow = external dso_local local_unnamed_addr global [100000 x i32], align 16
@z = external dso_local local_unnamed_addr global [2097152 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z2seii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %1, -1
  br i1 %3, label %tailrecurse._crit_edge, label %.lr.ph30

.lr.ph30:                                         ; preds = %2
  %4 = load i32, ptr @N, align 4, !tbaa !5
  %.not24 = icmp sgt i32 %4, 0
  br i1 %.not24, label %.lr.ph.us.preheader, label %tailrecurse.backedge.preheader

tailrecurse.backedge.preheader:                   ; preds = %.lr.ph30
  %5 = add i32 %1, 1
  %xtraiter = and i32 %5, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %tailrecurse.backedge.prol.loopexit, label %tailrecurse.backedge.prol

tailrecurse.backedge.prol:                        ; preds = %tailrecurse.backedge.preheader, %tailrecurse.backedge.prol
  %.tr2328.prol = phi i32 [ %7, %tailrecurse.backedge.prol ], [ %1, %tailrecurse.backedge.preheader ]
  %.tr27.prol = phi i32 [ %6, %tailrecurse.backedge.prol ], [ %0, %tailrecurse.backedge.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %tailrecurse.backedge.prol ], [ 0, %tailrecurse.backedge.preheader ]
  %6 = shl nsw i32 %.tr27.prol, 1
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8388608) @z, i8 -1, i64 8388608, i1 false)
  store i32 0, ptr @z, align 16, !tbaa !5
  %7 = add nsw i32 %.tr2328.prol, -1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %tailrecurse.backedge.prol.loopexit, label %tailrecurse.backedge.prol, !llvm.loop !9

tailrecurse.backedge.prol.loopexit:               ; preds = %tailrecurse.backedge.prol, %tailrecurse.backedge.preheader
  %.lcssa.unr = phi i32 [ poison, %tailrecurse.backedge.preheader ], [ %6, %tailrecurse.backedge.prol ]
  %.tr2328.unr = phi i32 [ %1, %tailrecurse.backedge.preheader ], [ %7, %tailrecurse.backedge.prol ]
  %.tr27.unr = phi i32 [ %0, %tailrecurse.backedge.preheader ], [ %6, %tailrecurse.backedge.prol ]
  %8 = icmp ult i32 %1, 3
  br i1 %8, label %tailrecurse._crit_edge, label %tailrecurse.backedge

.lr.ph.us.preheader:                              ; preds = %.lr.ph30
  %wide.trip.count = zext nneg i32 %4 to i64
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %tailrecurse.backedge.us
  %.tr2328.us = phi i32 [ %.tr23.be.us, %tailrecurse.backedge.us ], [ %1, %.lr.ph.us.preheader ]
  %.tr27.us = phi i32 [ %.tr.be.us, %tailrecurse.backedge.us ], [ %0, %.lr.ph.us.preheader ]
  %9 = shl nsw i32 %.tr27.us, 1
  %10 = or disjoint i32 %9, 1
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8388608) @z, i8 -1, i64 8388608, i1 false)
  store i32 0, ptr @z, align 16, !tbaa !5
  br label %11

11:                                               ; preds = %.lr.ph.us, %21
  %indvars.iv = phi i64 [ 0, %.lr.ph.us ], [ %indvars.iv.next, %21 ]
  %.01925.us = phi i32 [ 0, %.lr.ph.us ], [ %15, %21 ]
  %12 = getelementptr inbounds [100000 x i32], ptr @cow, i64 0, i64 %indvars.iv
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = ashr i32 %13, %.tr2328.us
  %15 = xor i32 %14, %.01925.us
  %16 = xor i32 %15, %10
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [2097152 x i32], ptr @z, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp eq i32 %19, -1
  br i1 %20, label %21, label %tailrecurse.backedge.us

21:                                               ; preds = %11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %22 = sext i32 %15 to i64
  %23 = getelementptr inbounds [2097152 x i32], ptr @z, i64 0, i64 %22
  %24 = trunc nuw nsw i64 %indvars.iv.next to i32
  store i32 %24, ptr %23, align 4, !tbaa !5
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %tailrecurse.backedge.us, label %11, !llvm.loop !11

tailrecurse.backedge.us:                          ; preds = %21, %11
  %.tr.be.us = phi i32 [ %10, %11 ], [ %9, %21 ]
  %.tr23.be.us = add nsw i32 %.tr2328.us, -1
  %25 = icmp eq i32 %.tr2328.us, 0
  br i1 %25, label %tailrecurse._crit_edge, label %.lr.ph.us

tailrecurse.backedge:                             ; preds = %tailrecurse.backedge.prol.loopexit, %tailrecurse.backedge
  %.tr2328 = phi i32 [ %27, %tailrecurse.backedge ], [ %.tr2328.unr, %tailrecurse.backedge.prol.loopexit ]
  %.tr27 = phi i32 [ %26, %tailrecurse.backedge ], [ %.tr27.unr, %tailrecurse.backedge.prol.loopexit ]
  %26 = shl i32 %.tr27, 4
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8388608) @z, i8 -1, i64 8388608, i1 false)
  store i32 0, ptr @z, align 16, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8388608) @z, i8 -1, i64 8388608, i1 false)
  store i32 0, ptr @z, align 16, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8388608) @z, i8 -1, i64 8388608, i1 false)
  store i32 0, ptr @z, align 16, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8388608) @z, i8 -1, i64 8388608, i1 false)
  store i32 0, ptr @z, align 16, !tbaa !5
  %27 = add nsw i32 %.tr2328, -4
  %28 = icmp eq i32 %.tr2328, 3
  br i1 %28, label %tailrecurse._crit_edge, label %tailrecurse.backedge

tailrecurse._crit_edge:                           ; preds = %tailrecurse.backedge.prol.loopexit, %tailrecurse.backedge, %tailrecurse.backedge.us, %2
  %.tr.lcssa = phi i32 [ %0, %2 ], [ %.tr.be.us, %tailrecurse.backedge.us ], [ %.lcssa.unr, %tailrecurse.backedge.prol.loopexit ], [ %26, %tailrecurse.backedge ]
  ret i32 %.tr.lcssa
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, 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 = { mustprogress 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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