; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; 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) #2

; 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 @V_GetBlock(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) #3 {
  %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 %9, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [5 x ptr], ptr @screens, i64 0, i64 %35
  %37 = load ptr, ptr %36, align 8, !tbaa !9
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = mul nsw i32 %38, 320
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %37, i64 %40
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i8, ptr %41, i64 %43
  store ptr %44, ptr %13, align 8, !tbaa !9
  br label %45

45:                                               ; preds = %49, %33
  %46 = load i32, ptr %11, align 4, !tbaa !5
  %47 = add nsw i32 %46, -1
  store i32 %47, ptr %11, align 4, !tbaa !5
  %48 = icmp ne i32 %46, 0
  br i1 %48, label %49, label %60

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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: noreturn
declare void @I_Error(ptr noundef, ...) local_unnamed_addr #0

; 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) #1

; Function Attrs: nounwind uwtable
define dso_local void @V_GetBlock(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr nocapture noundef writeonly %5) local_unnamed_addr #2 {
  %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
  %.not24 = icmp eq i32 %4, 0
  br i1 %.not24, 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
  %.027.prol = phi ptr [ %28, %.prol.preheader ], [ %25, %.lr.ph ]
  %.01926.prol = phi ptr [ %29, %.prol.preheader ], [ %5, %.lr.ph ]
  %.02025.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 %.02025.prol, -1
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.01926.prol, ptr align 1 %.027.prol, i64 %26, i1 false)
  %28 = getelementptr inbounds i8, ptr %.027.prol, i64 320
  %29 = getelementptr inbounds i8, ptr %.01926.prol, i64 %26
  %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
  %.027.unr = phi ptr [ %25, %.lr.ph ], [ %28, %.prol.preheader ]
  %.01926.unr = phi ptr [ %5, %.lr.ph ], [ %29, %.prol.preheader ]
  %.02025.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
  %.027 = phi ptr [ %38, %.lr.ph.new ], [ %.027.unr, %.prol.loopexit ]
  %.01926 = phi ptr [ %39, %.lr.ph.new ], [ %.01926.unr, %.prol.loopexit ]
  %.02025 = phi i32 [ %37, %.lr.ph.new ], [ %.02025.unr, %.prol.loopexit ]
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.01926, ptr align 1 %.027, i64 %26, i1 false)
  %31 = getelementptr inbounds i8, ptr %.027, i64 320
  %32 = getelementptr inbounds i8, ptr %.01926, i64 %26
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %32, ptr nonnull align 1 %31, i64 %26, i1 false)
  %33 = getelementptr inbounds i8, ptr %.027, i64 640
  %34 = getelementptr inbounds i8, ptr %32, i64 %26
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %34, ptr nonnull align 1 %33, i64 %26, i1 false)
  %35 = getelementptr inbounds i8, ptr %.027, i64 960
  %36 = getelementptr inbounds i8, ptr %34, i64 %26
  %37 = add nsw i32 %.02025, -4
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %36, ptr nonnull align 1 %35, i64 %26, i1 false)
  %38 = getelementptr inbounds i8, ptr %.027, i64 1280
  %39 = getelementptr inbounds i8, ptr %36, i64 %26
  %.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 = { 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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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