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

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

@screens = external dso_local global [5 x ptr], align 16
@.str.1 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @V_MarkRect(i32 noundef, i32 noundef, i32 noundef, i32 noundef) #0

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

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) #2

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

; Function Attrs: nounwind uwtable
define dso_local void @V_FillRect(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) #0 {
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store i32 %0, ptr %7, align 4, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !5
  store i32 %5, ptr %12, align 4, !tbaa !5
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %33, label %17

17:                                               ; preds = %6
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = load i32, ptr %10, align 4, !tbaa !5
  %20 = add nsw i32 %18, %19
  %21 = icmp sgt i32 %20, 320
  br i1 %21, label %33, label %22

22:                                               ; preds = %17
  %23 = load i32, ptr %8, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %33, label %25

25:                                               ; preds = %22
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = load i32, ptr %11, align 4, !tbaa !5
  %28 = add nsw i32 %26, %27
  %29 = icmp sgt i32 %28, 200
  br i1 %29, label %33, label %30

30:                                               ; preds = %25
  %31 = load i32, ptr %9, align 4, !tbaa !5
  %32 = icmp ugt i32 %31, 4
  br i1 %32, label %33, label %34

33:                                               ; preds = %30, %25, %22, %17, %6
  call void (ptr, ...) @I_Error(ptr noundef @.str.1) #4
  unreachable

34:                                               ; preds = %30
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !5
  %37 = load i32, ptr %10, align 4, !tbaa !5
  %38 = load i32, ptr %11, align 4, !tbaa !5
  call void @V_MarkRect(i32 noundef %35, i32 noundef %36, i32 noundef %37, i32 noundef %38)
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %39 = load i32, ptr %9, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [5 x ptr], ptr @screens, i64 0, i64 %40
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = load i32, ptr %8, align 4, !tbaa !5
  %44 = mul nsw i32 320, %43
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i8, ptr %42, i64 %45
  %47 = load i32, ptr %7, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i8, ptr %46, i64 %48
  store ptr %49, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  store i32 0, ptr %14, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %63, %34
  %51 = load i32, ptr %14, align 4, !tbaa !5
  %52 = load i32, ptr %11, align 4, !tbaa !5
  %53 = icmp slt i32 %51, %52
  br i1 %53, label %55, label %54

54:                                               ; preds = %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  br label %66

55:                                               ; preds = %50
  %56 = load ptr, ptr %13, align 8, !tbaa !9
  %57 = load i32, ptr %12, align 4, !tbaa !5
  %58 = trunc i32 %57 to i8
  %59 = load i32, ptr %10, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %56, i8 %58, i64 %60, i1 false)
  %61 = load ptr, ptr %13, align 8, !tbaa !9
  %62 = getelementptr inbounds i8, ptr %61, i64 320
  store ptr %62, ptr %13, align 8, !tbaa !9
  br label %63

63:                                               ; preds = %55
  %64 = load i32, ptr %14, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %14, align 4, !tbaa !5
  br label %50, !llvm.loop !11

66:                                               ; preds = %54
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { 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 = { noreturn "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 #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { noreturn }
attributes #5 = { 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@screens = external dso_local local_unnamed_addr global [5 x ptr], align 16
@.str.1 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @V_MarkRect(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @V_FillRect(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #0 {
  %7 = icmp slt i32 %0, 0
  br i1 %7, label %16, label %8

8:                                                ; preds = %6
  %9 = add nsw i32 %3, %0
  %10 = icmp sgt i32 %9, 320
  %11 = icmp slt i32 %1, 0
  %or.cond = or i1 %11, %10
  br i1 %or.cond, label %16, label %12

12:                                               ; preds = %8
  %13 = add nsw i32 %4, %1
  %14 = icmp sgt i32 %13, 200
  %15 = icmp ugt i32 %2, 4
  %or.cond3 = or i1 %15, %14
  br i1 %or.cond3, label %16, label %17

16:                                               ; preds = %12, %8, %6
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.1) #3
  unreachable

17:                                               ; preds = %12
  tail call void @V_MarkRect(i32 noundef %0, i32 noundef %1, i32 noundef %3, i32 noundef %4)
  %18 = icmp sgt i32 %4, 0
  br i1 %18, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %17
  %19 = zext nneg i32 %2 to i64
  %20 = getelementptr inbounds [5 x ptr], ptr @screens, i64 0, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = mul nuw nsw i32 %1, 320
  %23 = zext nneg i32 %22 to i64
  %24 = getelementptr inbounds i8, ptr %21, i64 %23
  %25 = zext nneg i32 %0 to i64
  %26 = getelementptr inbounds i8, ptr %24, i64 %25
  %27 = trunc i32 %5 to i8
  %28 = sext i32 %3 to i64
  %xtraiter = and i32 %4, 7
  %29 = icmp ult i32 %4, 8
  br i1 %29, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i32 %4, 2147483640
  br label %31

._crit_edge.loopexit.unr-lcssa:                   ; preds = %31, %.lr.ph
  %.02327.unr = phi ptr [ %26, %.lr.ph ], [ %39, %31 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader
  %.02327.epil = phi ptr [ %30, %.epil.preheader ], [ %.02327.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  tail call void @llvm.memset.p0.i64(ptr align 1 %.02327.epil, i8 %27, i64 %28, i1 false)
  %30 = getelementptr inbounds i8, ptr %.02327.epil, i64 320
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.epil.preheader, !llvm.loop !9

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader, %17
  ret void

31:                                               ; preds = %31, %.lr.ph.new
  %.02327 = phi ptr [ %26, %.lr.ph.new ], [ %39, %31 ]
  %niter = phi i32 [ 0, %.lr.ph.new ], [ %niter.next.7, %31 ]
  tail call void @llvm.memset.p0.i64(ptr align 1 %.02327, i8 %27, i64 %28, i1 false)
  %32 = getelementptr inbounds i8, ptr %.02327, i64 320
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %32, i8 %27, i64 %28, i1 false)
  %33 = getelementptr inbounds i8, ptr %.02327, i64 640
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %33, i8 %27, i64 %28, i1 false)
  %34 = getelementptr inbounds i8, ptr %.02327, i64 960
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %34, i8 %27, i64 %28, i1 false)
  %35 = getelementptr inbounds i8, ptr %.02327, i64 1280
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %35, i8 %27, i64 %28, i1 false)
  %36 = getelementptr inbounds i8, ptr %.02327, i64 1600
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %36, i8 %27, i64 %28, i1 false)
  %37 = getelementptr inbounds i8, ptr %.02327, i64 1920
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %37, i8 %27, i64 %28, i1 false)
  %38 = getelementptr inbounds i8, ptr %.02327, i64 2240
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %38, i8 %27, i64 %28, i1 false)
  %39 = getelementptr inbounds i8, ptr %.02327, i64 2560
  %niter.next.7 = add i32 %niter, 8
  %niter.ncmp.7 = icmp eq i32 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.loopexit.unr-lcssa, label %31, !llvm.loop !11
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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


; Effective O3 passes: GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass