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

; 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.hu_textline_t = type { i32, i32, ptr, i32, [81 x i8], i32, i32 }
%struct.patch_t = type { i16, i16, i16, i16, [8 x i32] }

@automapactive = external global i32, align 4
@viewwindowx = external global i32, align 4
@viewwindowy = external global i32, align 4
@viewheight = external global i32, align 4
@viewwidth = external 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
define dso_local void @HUlib_eraseTextLine(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %6 = load i32, ptr @automapactive, align 4, !tbaa !9
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %67, label %8

8:                                                ; preds = %1
  %9 = load i32, ptr @viewwindowx, align 4, !tbaa !9
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %67

11:                                               ; preds = %8
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.hu_textline_t, ptr %12, i32 0, i32 6
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %67

16:                                               ; preds = %11
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.hu_textline_t, ptr %17, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = getelementptr inbounds ptr, ptr %19, i64 0
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.patch_t, ptr %21, i32 0, i32 1
  %23 = load i16, ptr %22, align 2, !tbaa !14
  %24 = sext i16 %23 to i32
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %3, align 4, !tbaa !9
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.hu_textline_t, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !17
  store i32 %28, ptr %4, align 4, !tbaa !9
  %29 = load i32, ptr %4, align 4, !tbaa !9
  %30 = mul nsw i32 %29, 320
  store i32 %30, ptr %5, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %61, %16
  %32 = load i32, ptr %4, align 4, !tbaa !9
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.hu_textline_t, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 4, !tbaa !17
  %36 = load i32, ptr %3, align 4, !tbaa !9
  %37 = add nsw i32 %35, %36
  %38 = icmp slt i32 %32, %37
  br i1 %38, label %39, label %66

39:                                               ; preds = %31
  %40 = load i32, ptr %4, align 4, !tbaa !9
  %41 = load i32, ptr @viewwindowy, align 4, !tbaa !9
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %49, label %43

43:                                               ; preds = %39
  %44 = load i32, ptr %4, align 4, !tbaa !9
  %45 = load i32, ptr @viewwindowy, align 4, !tbaa !9
  %46 = load i32, ptr @viewheight, align 4, !tbaa !9
  %47 = add nsw i32 %45, %46
  %48 = icmp sge i32 %44, %47
  br i1 %48, label %49, label %51

49:                                               ; preds = %43, %39
  %50 = load i32, ptr %5, align 4, !tbaa !9
  call void @R_VideoErase(i32 noundef %50, i32 noundef 320)
  br label %60

51:                                               ; preds = %43
  %52 = load i32, ptr %5, align 4, !tbaa !9
  %53 = load i32, ptr @viewwindowx, align 4, !tbaa !9
  call void @R_VideoErase(i32 noundef %52, i32 noundef %53)
  %54 = load i32, ptr %5, align 4, !tbaa !9
  %55 = load i32, ptr @viewwindowx, align 4, !tbaa !9
  %56 = add nsw i32 %54, %55
  %57 = load i32, ptr @viewwidth, align 4, !tbaa !9
  %58 = add nsw i32 %56, %57
  %59 = load i32, ptr @viewwindowx, align 4, !tbaa !9
  call void @R_VideoErase(i32 noundef %58, i32 noundef %59)
  br label %60

60:                                               ; preds = %51, %49
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %4, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %4, align 4, !tbaa !9
  %64 = load i32, ptr %5, align 4, !tbaa !9
  %65 = add nsw i32 %64, 320
  store i32 %65, ptr %5, align 4, !tbaa !9
  br label %31, !llvm.loop !18

66:                                               ; preds = %31
  br label %67

67:                                               ; preds = %66, %11, %8, %1
  %68 = load ptr, ptr %2, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.hu_textline_t, ptr %68, i32 0, i32 6
  %70 = load i32, ptr %69, align 4, !tbaa !11
  %71 = icmp ne i32 %70, 0
  br i1 %71, label %72, label %77

72:                                               ; preds = %67
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.hu_textline_t, ptr %73, i32 0, i32 6
  %75 = load i32, ptr %74, align 4, !tbaa !11
  %76 = add nsw i32 %75, -1
  store i32 %76, ptr %74, align 4, !tbaa !11
  br label %77

77:                                               ; preds = %72, %67
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

declare void @R_VideoErase(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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 108}
!12 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8, !10, i64 16, !7, i64 20, !10, i64 104, !10, i64 108}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !16, i64 2}
!15 = !{!"", !16, i64 0, !16, i64 2, !16, i64 4, !16, i64 6, !7, i64 8}
!16 = !{!"short", !7, i64 0}
!17 = !{!12, !10, i64 4}
!18 = distinct !{!18, !19}
!19 = !{!"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"

@automapactive = external local_unnamed_addr global i32, align 4
@viewwindowx = external local_unnamed_addr global i32, align 4
@viewwindowy = external local_unnamed_addr global i32, align 4
@viewheight = external local_unnamed_addr global i32, align 4
@viewwidth = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @HUlib_eraseTextLine(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @automapactive, align 4, !tbaa !5
  %3 = icmp eq i32 %2, 0
  %4 = load i32, ptr @viewwindowx, align 4
  %5 = icmp ne i32 %4, 0
  %or.cond = select i1 %3, i1 %5, i1 false
  br i1 %or.cond, label %6, label %.loopexit

6:                                                ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 108
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %.loopexit, label %9

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %12, i64 2
  %14 = load i16, ptr %13, align 2, !tbaa !14
  %15 = sext i16 %14 to i32
  %16 = add nsw i32 %15, 1
  %17 = getelementptr inbounds i8, ptr %0, i64 4
  %18 = icmp sgt i16 %14, -1
  br i1 %18, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %9
  %19 = load i32, ptr %17, align 4, !tbaa !17
  %20 = mul nsw i32 %19, 320
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %33
  %.023 = phi i32 [ %35, %33 ], [ %20, %.lr.ph.preheader ]
  %.01621 = phi i32 [ %34, %33 ], [ %19, %.lr.ph.preheader ]
  %21 = load i32, ptr @viewwindowy, align 4, !tbaa !5
  %22 = icmp slt i32 %.01621, %21
  br i1 %22, label %26, label %23

23:                                               ; preds = %.lr.ph
  %24 = load i32, ptr @viewheight, align 4, !tbaa !5
  %25 = add nsw i32 %24, %21
  %.not20 = icmp slt i32 %.01621, %25
  br i1 %.not20, label %27, label %26

26:                                               ; preds = %23, %.lr.ph
  tail call void @R_VideoErase(i32 noundef %.023, i32 noundef 320) #2
  br label %33

27:                                               ; preds = %23
  %28 = load i32, ptr @viewwindowx, align 4, !tbaa !5
  tail call void @R_VideoErase(i32 noundef %.023, i32 noundef %28) #2
  %29 = load i32, ptr @viewwindowx, align 4, !tbaa !5
  %30 = add nsw i32 %29, %.023
  %31 = load i32, ptr @viewwidth, align 4, !tbaa !5
  %32 = add nsw i32 %30, %31
  tail call void @R_VideoErase(i32 noundef %32, i32 noundef %29) #2
  br label %33

33:                                               ; preds = %26, %27
  %34 = add nsw i32 %.01621, 1
  %35 = add nsw i32 %.023, 320
  %36 = load i32, ptr %17, align 4, !tbaa !17
  %37 = add nsw i32 %16, %36
  %38 = icmp slt i32 %34, %37
  br i1 %38, label %.lr.ph, label %.loopexit, !llvm.loop !18

.loopexit:                                        ; preds = %33, %9, %6, %1
  %39 = getelementptr inbounds i8, ptr %0, i64 108
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %.not19 = icmp eq i32 %40, 0
  br i1 %.not19, label %43, label %41

41:                                               ; preds = %.loopexit
  %42 = add nsw i32 %40, -1
  store i32 %42, ptr %39, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %41, %.loopexit
  ret void
}

declare void @R_VideoErase(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 = !{!10, !6, i64 108}
!10 = !{!"", !6, i64 0, !6, i64 4, !11, i64 8, !6, i64 16, !7, i64 20, !6, i64 104, !6, i64 108}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !16, i64 2}
!15 = !{!"", !16, i64 0, !16, i64 2, !16, i64 4, !16, i64 6, !7, i64 8}
!16 = !{!"short", !7, i64 0}
!17 = !{!10, !6, i64 4}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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