; LLVM IR from tangjz/acm-icpc :: usaco/Training/Chapter 2 Bigger challenges/lamps.ll (Z8make_bitPcii)

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z8make_bitPcii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = load i32, ptr %6, align 4, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i8, ptr %7, i64 %9
  store i8 0, ptr %10, align 1, !tbaa !11
  br label %11

11:                                               ; preds = %15, %3
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = add nsw i32 %12, -1
  store i32 %13, ptr %6, align 4, !tbaa !9
  %14 = icmp ne i32 %12, 0
  br i1 %14, label %15, label %26

15:                                               ; preds = %11
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = and i32 %16, 1
  %18 = add nsw i32 %17, 48
  %19 = trunc i32 %18 to i8
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  store i8 %19, ptr %23, align 1, !tbaa !11
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = ashr i32 %24, 1
  store i32 %25, ptr %5, align 4, !tbaa !9
  br label %11, !llvm.loop !12

26:                                               ; preds = %11
  ret void
}

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

!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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(argmem: write) uwtable
define dso_local void @_Z8make_bitPcii(ptr nocapture noundef writeonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %2 to i64
  %5 = getelementptr inbounds i8, ptr %0, i64 %4
  store i8 0, ptr %5, align 1, !tbaa !5
  %.not7 = icmp eq i32 %2, 0
  br i1 %.not7, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %xtraiter = and i64 %4, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.lr.ph.prol ], [ %4, %.lr.ph.preheader ]
  %.068.prol = phi i32 [ %10, %.lr.ph.prol ], [ %1, %.lr.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.prol, -1
  %6 = trunc i32 %.068.prol to i8
  %7 = and i8 %6, 1
  %8 = or disjoint i8 %7, 48
  %9 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next.prol
  store i8 %8, ptr %9, align 1, !tbaa !5
  %10 = ashr i32 %.068.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !8

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ %4, %.lr.ph.preheader ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %.068.unr = phi i32 [ %1, %.lr.ph.preheader ], [ %10, %.lr.ph.prol ]
  %11 = icmp ult i32 %2, 4
  br i1 %11, label %._crit_edge, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %0, i64 -1
  %invariant.gep11 = getelementptr i8, ptr %0, i64 -2
  %invariant.gep13 = getelementptr i8, ptr %0, i64 -3
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %.068 = phi i32 [ %.068.unr, %.lr.ph.preheader.new ], [ %28, %.lr.ph ]
  %12 = trunc i32 %.068 to i8
  %13 = and i8 %12, 1
  %14 = or disjoint i8 %13, 48
  %gep = getelementptr i8, ptr %invariant.gep, i64 %indvars.iv
  store i8 %14, ptr %gep, align 1, !tbaa !5
  %15 = trunc i32 %.068 to i8
  %16 = lshr i8 %15, 1
  %17 = and i8 %16, 1
  %18 = or disjoint i8 %17, 48
  %gep12 = getelementptr i8, ptr %invariant.gep11, i64 %indvars.iv
  store i8 %18, ptr %gep12, align 1, !tbaa !5
  %19 = trunc i32 %.068 to i8
  %20 = lshr i8 %19, 2
  %21 = and i8 %20, 1
  %22 = or disjoint i8 %21, 48
  %gep14 = getelementptr i8, ptr %invariant.gep13, i64 %indvars.iv
  store i8 %22, ptr %gep14, align 1, !tbaa !5
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %23 = trunc i32 %.068 to i8
  %24 = lshr i8 %23, 3
  %25 = and i8 %24, 1
  %26 = or disjoint i8 %25, 48
  %27 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next.3
  store i8 %26, ptr %27, align 1, !tbaa !5
  %28 = ashr i32 %.068, 4
  %.not.3 = icmp eq i64 %indvars.iv.next.3, 0
  br i1 %.not.3, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %3
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: write) 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.unroll.disable"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass