; 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.2 = external hidden unnamed_addr constant [16 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; 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_DrawBlock(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr 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 ptr, align 8
  %13 = alloca ptr, align 8
  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 ptr %5, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %14 = load i32, ptr %7, align 4, !tbaa !5
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %32, label %16

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

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

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

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

32:                                               ; preds = %29, %24, %21, %16, %6
  call void (ptr, ...) @I_Error(ptr noundef @.str.2) #5
  unreachable

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

49:                                               ; preds = %53, %33
  %50 = load i32, ptr %11, align 4, !tbaa !5
  %51 = add nsw i32 %50, -1
  store i32 %51, ptr %11, align 4, !tbaa !5
  %52 = icmp ne i32 %50, 0
  br i1 %52, label %53, label %64

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

64:                                               ; preds = %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  ret void
}

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: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { noreturn }

!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.2 = external hidden unnamed_addr constant [16 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: mustprogress 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

; Function Attrs: nounwind uwtable
define dso_local void @V_DrawBlock(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr nocapture noundef readonly %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.2) #3
  unreachable

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

.lr.ph:                                           ; preds = %17
  %18 = zext nneg i32 %2 to i64
  %19 = getelementptr inbounds [5 x ptr], ptr @screens, i64 0, i64 %18
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %21 = mul nuw nsw i32 %1, 320
  %22 = zext nneg i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  %24 = zext nneg i32 %0 to i64
  %25 = getelementptr inbounds i8, ptr %23, i64 %24
  %26 = sext i32 %3 to i64
  %xtraiter = and i32 %4, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.preheader

.prol.preheader:                                  ; preds = %.lr.ph, %.prol.preheader
  %.031.prol = phi ptr [ %29, %.prol.preheader ], [ %25, %.lr.ph ]
  %.02330.prol = phi ptr [ %28, %.prol.preheader ], [ %5, %.lr.ph ]
  %.02429.prol = phi i32 [ %27, %.prol.preheader ], [ %4, %.lr.ph ]
  %prol.iter = phi i32 [ %prol.iter.next, %.prol.preheader ], [ 0, %.lr.ph ]
  %27 = add nsw i32 %.02429.prol, -1
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.031.prol, ptr align 1 %.02330.prol, i64 %26, i1 false)
  %28 = getelementptr inbounds i8, ptr %.02330.prol, i64 %26
  %29 = getelementptr inbounds i8, ptr %.031.prol, i64 320
  %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 %.prol.loopexit, label %.prol.preheader, !llvm.loop !9

.prol.loopexit:                                   ; preds = %.prol.preheader, %.lr.ph
  %.031.unr = phi ptr [ %25, %.lr.ph ], [ %29, %.prol.preheader ]
  %.02330.unr = phi ptr [ %5, %.lr.ph ], [ %28, %.prol.preheader ]
  %.02429.unr = phi i32 [ %4, %.lr.ph ], [ %27, %.prol.preheader ]
  %30 = icmp ult i32 %4, 4
  br i1 %30, label %._crit_edge, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.prol.loopexit, %.lr.ph.new
  %.031 = phi ptr [ %39, %.lr.ph.new ], [ %.031.unr, %.prol.loopexit ]
  %.02330 = phi ptr [ %38, %.lr.ph.new ], [ %.02330.unr, %.prol.loopexit ]
  %.02429 = phi i32 [ %37, %.lr.ph.new ], [ %.02429.unr, %.prol.loopexit ]
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.031, ptr align 1 %.02330, i64 %26, i1 false)
  %31 = getelementptr inbounds i8, ptr %.02330, i64 %26
  %32 = getelementptr inbounds i8, ptr %.031, i64 320
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %32, ptr align 1 %31, i64 %26, i1 false)
  %33 = getelementptr inbounds i8, ptr %31, i64 %26
  %34 = getelementptr inbounds i8, ptr %.031, i64 640
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %34, ptr align 1 %33, i64 %26, i1 false)
  %35 = getelementptr inbounds i8, ptr %33, i64 %26
  %36 = getelementptr inbounds i8, ptr %.031, i64 960
  %37 = add nsw i32 %.02429, -4
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %36, ptr align 1 %35, i64 %26, i1 false)
  %38 = getelementptr inbounds i8, ptr %35, i64 %26
  %39 = getelementptr inbounds i8, ptr %.031, i64 1280
  %.not.3 = icmp eq i32 %37, 0
  br i1 %.not.3, label %._crit_edge, label %.lr.ph.new, !llvm.loop !11

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

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: readwrite) }
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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass