; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (r_draw)

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

@ylookup = external dso_local global [200 x ptr], align 16
@columnofs = external dso_local global [320 x i32], align 16
@viewwindowx = external dso_local global i32, align 4
@viewwindowy = external dso_local global i32, align 4
@screens = external global [5 x ptr], align 16

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

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

; Function Attrs: nounwind uwtable
define dso_local void @R_InitBuffer(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %6 = load i32, ptr %3, align 4, !tbaa !5
  %7 = sub nsw i32 320, %6
  %8 = ashr i32 %7, 1
  store i32 %8, ptr @viewwindowx, align 4, !tbaa !5
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %20, %2
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %23

13:                                               ; preds = %9
  %14 = load i32, ptr @viewwindowx, align 4, !tbaa !5
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = add nsw i32 %14, %15
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [320 x i32], ptr @columnofs, i64 0, i64 %18
  store i32 %16, ptr %19, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %13
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %5, align 4, !tbaa !5
  br label %9, !llvm.loop !9

23:                                               ; preds = %9
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = icmp eq i32 %24, 320
  br i1 %25, label %26, label %27

26:                                               ; preds = %23
  store i32 0, ptr @viewwindowy, align 4, !tbaa !5
  br label %31

27:                                               ; preds = %23
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = sub nsw i32 168, %28
  %30 = ashr i32 %29, 1
  store i32 %30, ptr @viewwindowy, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %27, %26
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %47, %31
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %36, label %50

36:                                               ; preds = %32
  %37 = load ptr, ptr @screens, align 16, !tbaa !11
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = load i32, ptr @viewwindowy, align 4, !tbaa !5
  %40 = add nsw i32 %38, %39
  %41 = mul nsw i32 %40, 320
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i8, ptr %37, i64 %42
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [200 x ptr], ptr @ylookup, i64 0, i64 %45
  store ptr %43, ptr %46, align 8, !tbaa !11
  br label %47

47:                                               ; preds = %36
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %5, align 4, !tbaa !5
  br label %32, !llvm.loop !13

50:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !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"

@ylookup = external dso_local local_unnamed_addr global [200 x ptr], align 16
@columnofs = external dso_local local_unnamed_addr global [320 x i32], align 16
@viewwindowx = external dso_local local_unnamed_addr global i32, align 4
@viewwindowy = external dso_local local_unnamed_addr global i32, align 4
@screens = external local_unnamed_addr global [5 x ptr], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @R_InitBuffer(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sub nsw i32 320, %0
  %4 = ashr i32 %3, 1
  store i32 %4, ptr @viewwindowx, align 4, !tbaa !5
  %5 = icmp sgt i32 %0, 0
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %0 to i64
  %min.iters.check = icmp ult i32 %0, 8
  br i1 %min.iters.check, label %.lr.ph.preheader43, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %wide.trip.count, 2147483640
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %4, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %step.add = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %6 = getelementptr inbounds [320 x i32], ptr @columnofs, i64 0, i64 %index
  %7 = add <4 x i32> %broadcast.splat, %vec.ind
  %8 = add <4 x i32> %broadcast.splat, %step.add
  %9 = getelementptr inbounds i8, ptr %6, i64 16
  store <4 x i32> %7, ptr %6, align 16, !tbaa !5
  store <4 x i32> %8, ptr %9, align 16, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %10 = icmp eq i64 %index.next, %n.vec
  br i1 %10, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader43

.lr.ph.preheader43:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader43, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader43 ]
  %11 = getelementptr inbounds [320 x i32], ptr @columnofs, i64 0, i64 %indvars.iv
  %12 = trunc i64 %indvars.iv to i32
  %13 = add i32 %4, %12
  store i32 %13, ptr %11, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %2
  %14 = icmp eq i32 %0, 320
  %15 = sub nsw i32 168, %1
  %16 = ashr i32 %15, 1
  %storemerge = select i1 %14, i32 0, i32 %16
  store i32 %storemerge, ptr @viewwindowy, align 4, !tbaa !5
  %17 = icmp sgt i32 %1, 0
  br i1 %17, label %.lr.ph17, label %._crit_edge18

.lr.ph17:                                         ; preds = %._crit_edge
  %18 = load ptr, ptr @screens, align 16, !tbaa !14
  %19 = sext i32 %storemerge to i64
  %wide.trip.count23 = zext nneg i32 %1 to i64
  %min.iters.check28 = icmp ult i32 %1, 4
  br i1 %min.iters.check28, label %scalar.ph27.preheader, label %vector.ph29

vector.ph29:                                      ; preds = %.lr.ph17
  %n.vec31 = and i64 %wide.trip.count23, 2147483644
  %broadcast.splatinsert39 = insertelement <2 x i64> poison, i64 %19, i64 0
  %broadcast.splat40 = shufflevector <2 x i64> %broadcast.splatinsert39, <2 x i64> poison, <2 x i32> zeroinitializer
  br label %vector.body33

vector.body33:                                    ; preds = %vector.body33, %vector.ph29
  %index34 = phi i64 [ 0, %vector.ph29 ], [ %index.next41, %vector.body33 ]
  %vec.ind35 = phi <2 x i64> [ <i64 0, i64 1>, %vector.ph29 ], [ %vec.ind.next38, %vector.body33 ]
  %step.add36 = add <2 x i64> %vec.ind35, <i64 2, i64 2>
  %20 = add nsw <2 x i64> %vec.ind35, %broadcast.splat40
  %21 = add nsw <2 x i64> %step.add36, %broadcast.splat40
  %22 = mul nsw <2 x i64> %20, <i64 320, i64 320>
  %23 = mul nsw <2 x i64> %21, <i64 320, i64 320>
  %24 = getelementptr inbounds i8, ptr %18, <2 x i64> %22
  %25 = getelementptr inbounds i8, ptr %18, <2 x i64> %23
  %26 = getelementptr inbounds [200 x ptr], ptr @ylookup, i64 0, i64 %index34
  %27 = getelementptr inbounds i8, ptr %26, i64 16
  store <2 x ptr> %24, ptr %26, align 16, !tbaa !14
  store <2 x ptr> %25, ptr %27, align 16, !tbaa !14
  %index.next41 = add nuw i64 %index34, 4
  %vec.ind.next38 = add <2 x i64> %vec.ind35, <i64 4, i64 4>
  %28 = icmp eq i64 %index.next41, %n.vec31
  br i1 %28, label %middle.block26, label %vector.body33, !llvm.loop !16

middle.block26:                                   ; preds = %vector.body33
  %cmp.n42 = icmp eq i64 %n.vec31, %wide.trip.count23
  br i1 %cmp.n42, label %._crit_edge18, label %scalar.ph27.preheader

scalar.ph27.preheader:                            ; preds = %middle.block26, %.lr.ph17
  %indvars.iv20.ph = phi i64 [ 0, %.lr.ph17 ], [ %n.vec31, %middle.block26 ]
  br label %scalar.ph27

scalar.ph27:                                      ; preds = %scalar.ph27.preheader, %scalar.ph27
  %indvars.iv20 = phi i64 [ %indvars.iv.next21, %scalar.ph27 ], [ %indvars.iv20.ph, %scalar.ph27.preheader ]
  %29 = add nsw i64 %indvars.iv20, %19
  %30 = mul nsw i64 %29, 320
  %31 = getelementptr inbounds i8, ptr %18, i64 %30
  %32 = getelementptr inbounds [200 x ptr], ptr @ylookup, i64 0, i64 %indvars.iv20
  store ptr %31, ptr %32, align 8, !tbaa !14
  %indvars.iv.next21 = add nuw nsw i64 %indvars.iv20, 1
  %exitcond24.not = icmp eq i64 %indvars.iv.next21, %wide.trip.count23
  br i1 %exitcond24.not, label %._crit_edge18, label %scalar.ph27, !llvm.loop !17

._crit_edge18:                                    ; preds = %scalar.ph27, %middle.block26, %._crit_edge
  ret void
}

attributes #0 = { 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/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = !{!15, !15, i64 0}
!15 = !{!"any pointer", !7, i64 0}
!16 = distinct !{!16, !10, !11, !12}
!17 = distinct !{!17, !10, !12, !11}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass