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

@viewheight = external dso_local global i32, align 4
@scaledviewwidth = external dso_local global i32, align 4

; 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
declare dso_local void @R_VideoErase(i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @R_DrawViewBorder() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %6 = load i32, ptr @scaledviewwidth, align 4, !tbaa !5
  %7 = icmp eq i32 %6, 320
  br i1 %7, label %8, label %9

8:                                                ; preds = %0
  store i32 1, ptr %5, align 4
  br label %51

9:                                                ; preds = %0
  %10 = load i32, ptr @viewheight, align 4, !tbaa !5
  %11 = sub nsw i32 168, %10
  %12 = sdiv i32 %11, 2
  store i32 %12, ptr %1, align 4, !tbaa !5
  %13 = load i32, ptr @scaledviewwidth, align 4, !tbaa !5
  %14 = sub nsw i32 320, %13
  %15 = sdiv i32 %14, 2
  store i32 %15, ptr %2, align 4, !tbaa !5
  %16 = load i32, ptr %1, align 4, !tbaa !5
  %17 = mul nsw i32 %16, 320
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = add nsw i32 %17, %18
  call void @R_VideoErase(i32 noundef 0, i32 noundef %19)
  %20 = load i32, ptr @viewheight, align 4, !tbaa !5
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = add nsw i32 %20, %21
  %23 = mul nsw i32 %22, 320
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sub nsw i32 %23, %24
  store i32 %25, ptr %3, align 4, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = load i32, ptr %1, align 4, !tbaa !5
  %28 = mul nsw i32 %27, 320
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = add nsw i32 %28, %29
  call void @R_VideoErase(i32 noundef %26, i32 noundef %30)
  %31 = load i32, ptr %1, align 4, !tbaa !5
  %32 = mul nsw i32 %31, 320
  %33 = add nsw i32 %32, 320
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = sub nsw i32 %33, %34
  store i32 %35, ptr %3, align 4, !tbaa !5
  %36 = load i32, ptr %2, align 4, !tbaa !5
  %37 = shl i32 %36, 1
  store i32 %37, ptr %2, align 4, !tbaa !5
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %47, %9
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = load i32, ptr @viewheight, align 4, !tbaa !5
  %41 = icmp slt i32 %39, %40
  br i1 %41, label %42, label %50

42:                                               ; preds = %38
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = load i32, ptr %2, align 4, !tbaa !5
  call void @R_VideoErase(i32 noundef %43, i32 noundef %44)
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = add nsw i32 %45, 320
  store i32 %46, ptr %3, align 4, !tbaa !5
  br label %47

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

50:                                               ; preds = %38
  call void @V_MarkRect(i32 noundef 0, i32 noundef 0, i32 noundef 320, i32 noundef 168)
  store i32 0, ptr %5, align 4
  br label %51

51:                                               ; preds = %50, %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  %52 = load i32, ptr %5, align 4
  switch i32 %52, label %54 [
    i32 0, label %53
    i32 1, label %53
  ]

53:                                               ; preds = %51, %51
  ret void

54:                                               ; preds = %51
  unreachable
}

declare void @V_MarkRect(i32 noundef, i32 noundef, i32 noundef, i32 noundef) #2

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 = { "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 = { 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"}


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

@viewheight = external dso_local local_unnamed_addr global i32, align 4
@scaledviewwidth = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local void @R_DrawViewBorder() local_unnamed_addr #0 {
  %1 = load i32, ptr @scaledviewwidth, align 4, !tbaa !5
  %2 = icmp eq i32 %1, 320
  br i1 %2, label %21, label %3

3:                                                ; preds = %0
  %4 = load i32, ptr @viewheight, align 4, !tbaa !5
  %5 = sub nsw i32 168, %4
  %6 = sdiv i32 %5, 2
  %7 = sub nsw i32 320, %1
  %8 = sdiv i32 %7, 2
  %9 = mul nsw i32 %6, 320
  %10 = add nsw i32 %9, %8
  tail call void @R_VideoErase(i32 noundef 0, i32 noundef %10)
  %11 = load i32, ptr @viewheight, align 4, !tbaa !5
  %12 = add nsw i32 %11, %6
  %13 = mul nsw i32 %12, 320
  %14 = sub nsw i32 %13, %8
  tail call void @R_VideoErase(i32 noundef %14, i32 noundef %10)
  %15 = shl nsw i32 %8, 1
  %16 = load i32, ptr @viewheight, align 4, !tbaa !5
  %17 = icmp sgt i32 %16, 1
  br i1 %17, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %reass.sub = sub i32 %9, %8
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.018 = phi i32 [ %18, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %.01517.in = phi i32 [ %.01517, %.lr.ph ], [ %reass.sub, %.lr.ph.preheader ]
  %.01517 = add i32 %.01517.in, 320
  tail call void @R_VideoErase(i32 noundef %.01517, i32 noundef %15)
  %18 = add nuw nsw i32 %.018, 1
  %19 = load i32, ptr @viewheight, align 4, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %3
  tail call void @V_MarkRect(i32 noundef 0, i32 noundef 0, i32 noundef 320, i32 noundef 168) #2
  br label %21

21:                                               ; preds = %0, %._crit_edge
  ret void
}

declare void @V_MarkRect(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

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 = { "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"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass