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

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

%struct.patch_t = type { i16, i16, i16, i16, [8 x i32] }
%struct.post_t = type { i8, i8 }

@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 @F_DrawPatchCol(i32 noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  %13 = load ptr, ptr %5, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.patch_t, ptr %13, i32 0, i32 4
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [8 x i32], ptr %14, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i8, ptr %12, i64 %19
  store ptr %20, ptr %7, align 8, !tbaa !9
  %21 = load ptr, ptr @screens, align 16, !tbaa !9
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i8, ptr %21, i64 %23
  store ptr %24, ptr %10, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %57, %3
  %26 = load ptr, ptr %7, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.post_t, ptr %26, i32 0, i32 0
  %28 = load i8, ptr %27, align 1, !tbaa !11
  %29 = zext i8 %28 to i32
  %30 = icmp ne i32 %29, 255
  br i1 %30, label %31, label %66

31:                                               ; preds = %25
  %32 = load ptr, ptr %7, align 8, !tbaa !9
  %33 = getelementptr inbounds i8, ptr %32, i64 3
  store ptr %33, ptr %8, align 8, !tbaa !9
  %34 = load ptr, ptr %10, align 8, !tbaa !9
  %35 = load ptr, ptr %7, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.post_t, ptr %35, i32 0, i32 0
  %37 = load i8, ptr %36, align 1, !tbaa !11
  %38 = zext i8 %37 to i32
  %39 = mul nsw i32 %38, 320
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %34, i64 %40
  store ptr %41, ptr %9, align 8, !tbaa !9
  %42 = load ptr, ptr %7, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.post_t, ptr %42, i32 0, i32 1
  %44 = load i8, ptr %43, align 1, !tbaa !13
  %45 = zext i8 %44 to i32
  store i32 %45, ptr %11, align 4, !tbaa !5
  br label %46

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

50:                                               ; preds = %46
  %51 = load ptr, ptr %8, align 8, !tbaa !9
  %52 = getelementptr inbounds i8, ptr %51, i32 1
  store ptr %52, ptr %8, align 8, !tbaa !9
  %53 = load i8, ptr %51, align 1, !tbaa !14
  %54 = load ptr, ptr %9, align 8, !tbaa !9
  store i8 %53, ptr %54, align 1, !tbaa !14
  %55 = load ptr, ptr %9, align 8, !tbaa !9
  %56 = getelementptr inbounds i8, ptr %55, i64 320
  store ptr %56, ptr %9, align 8, !tbaa !9
  br label %46, !llvm.loop !15

57:                                               ; preds = %46
  %58 = load ptr, ptr %7, align 8, !tbaa !9
  %59 = load ptr, ptr %7, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.post_t, ptr %59, i32 0, i32 1
  %61 = load i8, ptr %60, align 1, !tbaa !13
  %62 = zext i8 %61 to i32
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds i8, ptr %58, i64 %63
  %65 = getelementptr inbounds i8, ptr %64, i64 4
  store ptr %65, ptr %7, align 8, !tbaa !9
  br label %25, !llvm.loop !17

66:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1}
!13 = !{!12, !7, i64 1}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; 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 local_unnamed_addr global [5 x ptr], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @F_DrawPatchCol(i32 noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %1, i64 8
  %5 = sext i32 %2 to i64
  %6 = getelementptr inbounds [8 x i32], ptr %4, i64 0, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds i8, ptr %1, i64 %8
  %10 = load ptr, ptr @screens, align 16, !tbaa !9
  %11 = sext i32 %0 to i64
  %12 = getelementptr inbounds i8, ptr %10, i64 %11
  %13 = load i8, ptr %9, align 1, !tbaa !11
  %.not23 = icmp eq i8 %13, -1
  br i1 %.not23, label %._crit_edge27, label %.lr.ph26

.lr.ph26:                                         ; preds = %3, %._crit_edge
  %14 = phi i8 [ %56, %._crit_edge ], [ %13, %3 ]
  %.01424 = phi ptr [ %55, %._crit_edge ], [ %9, %3 ]
  %15 = getelementptr inbounds i8, ptr %.01424, i64 1
  %16 = load i8, ptr %15, align 1, !tbaa !13
  %.not1819 = icmp eq i8 %16, 0
  br i1 %.not1819, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph26
  %17 = zext i8 %16 to i32
  %18 = zext i8 %14 to i64
  %19 = mul nuw nsw i64 %18, 320
  %20 = getelementptr inbounds i8, ptr %12, i64 %19
  %21 = getelementptr inbounds i8, ptr %.01424, i64 3
  %xtraiter = and i32 %17, 7
  %lcmp.mod.not = icmp eq i32 %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
  %.022.prol = phi i32 [ %22, %.lr.ph.prol ], [ %17, %.lr.ph.preheader ]
  %.01521.prol = phi ptr [ %25, %.lr.ph.prol ], [ %20, %.lr.ph.preheader ]
  %.01620.prol = phi ptr [ %23, %.lr.ph.prol ], [ %21, %.lr.ph.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %22 = add nsw i32 %.022.prol, -1
  %23 = getelementptr inbounds i8, ptr %.01620.prol, i64 1
  %24 = load i8, ptr %.01620.prol, align 1, !tbaa !14
  store i8 %24, ptr %.01521.prol, align 1, !tbaa !14
  %25 = getelementptr inbounds i8, ptr %.01521.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 %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !15

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.022.unr = phi i32 [ %17, %.lr.ph.preheader ], [ %22, %.lr.ph.prol ]
  %.01521.unr = phi ptr [ %20, %.lr.ph.preheader ], [ %25, %.lr.ph.prol ]
  %.01620.unr = phi ptr [ %21, %.lr.ph.preheader ], [ %23, %.lr.ph.prol ]
  %26 = icmp ult i8 %16, 8
  br i1 %26, label %._crit_edge.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.022 = phi i32 [ %48, %.lr.ph ], [ %.022.unr, %.lr.ph.prol.loopexit ]
  %.01521 = phi ptr [ %51, %.lr.ph ], [ %.01521.unr, %.lr.ph.prol.loopexit ]
  %.01620 = phi ptr [ %49, %.lr.ph ], [ %.01620.unr, %.lr.ph.prol.loopexit ]
  %27 = getelementptr inbounds i8, ptr %.01620, i64 1
  %28 = load i8, ptr %.01620, align 1, !tbaa !14
  store i8 %28, ptr %.01521, align 1, !tbaa !14
  %29 = getelementptr inbounds i8, ptr %.01521, i64 320
  %30 = getelementptr inbounds i8, ptr %.01620, i64 2
  %31 = load i8, ptr %27, align 1, !tbaa !14
  store i8 %31, ptr %29, align 1, !tbaa !14
  %32 = getelementptr inbounds i8, ptr %.01521, i64 640
  %33 = getelementptr inbounds i8, ptr %.01620, i64 3
  %34 = load i8, ptr %30, align 1, !tbaa !14
  store i8 %34, ptr %32, align 1, !tbaa !14
  %35 = getelementptr inbounds i8, ptr %.01521, i64 960
  %36 = getelementptr inbounds i8, ptr %.01620, i64 4
  %37 = load i8, ptr %33, align 1, !tbaa !14
  store i8 %37, ptr %35, align 1, !tbaa !14
  %38 = getelementptr inbounds i8, ptr %.01521, i64 1280
  %39 = getelementptr inbounds i8, ptr %.01620, i64 5
  %40 = load i8, ptr %36, align 1, !tbaa !14
  store i8 %40, ptr %38, align 1, !tbaa !14
  %41 = getelementptr inbounds i8, ptr %.01521, i64 1600
  %42 = getelementptr inbounds i8, ptr %.01620, i64 6
  %43 = load i8, ptr %39, align 1, !tbaa !14
  store i8 %43, ptr %41, align 1, !tbaa !14
  %44 = getelementptr inbounds i8, ptr %.01521, i64 1920
  %45 = getelementptr inbounds i8, ptr %.01620, i64 7
  %46 = load i8, ptr %42, align 1, !tbaa !14
  store i8 %46, ptr %44, align 1, !tbaa !14
  %47 = getelementptr inbounds i8, ptr %.01521, i64 2240
  %48 = add nsw i32 %.022, -8
  %49 = getelementptr inbounds i8, ptr %.01620, i64 8
  %50 = load i8, ptr %45, align 1, !tbaa !14
  store i8 %50, ptr %47, align 1, !tbaa !14
  %51 = getelementptr inbounds i8, ptr %.01521, i64 2560
  %.not18.7 = icmp eq i32 %48, 0
  br i1 %.not18.7, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !17

._crit_edge.loopexit:                             ; preds = %.lr.ph, %.lr.ph.prol.loopexit
  %.pre = load i8, ptr %15, align 1, !tbaa !13
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.lr.ph26
  %52 = phi i8 [ %.pre, %._crit_edge.loopexit ], [ 0, %.lr.ph26 ]
  %53 = zext i8 %52 to i64
  %54 = getelementptr inbounds i8, ptr %.01424, i64 %53
  %55 = getelementptr inbounds i8, ptr %54, i64 4
  %56 = load i8, ptr %55, align 1, !tbaa !11
  %.not = icmp eq i8 %56, -1
  br i1 %.not, label %._crit_edge27, label %.lr.ph26, !llvm.loop !19

._crit_edge27:                                    ; preds = %._crit_edge, %3
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1}
!13 = !{!12, !7, i64 1}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.unroll.disable"}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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