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

; Function Attrs: nounwind uwtable
declare dso_local void @HUlib_clearTextLine(ptr 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: 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 @HUlib_addLineToSText(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.hu_stext_t, ptr %4, i32 0, i32 2
  %6 = load i32, ptr %5, align 4, !tbaa !9
  %7 = add nsw i32 %6, 1
  store i32 %7, ptr %5, align 4, !tbaa !9
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.hu_stext_t, ptr %8, i32 0, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !12
  %11 = icmp eq i32 %7, %10
  br i1 %11, label %12, label %15

12:                                               ; preds = %1
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.hu_stext_t, ptr %13, i32 0, i32 2
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %12, %1
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.hu_stext_t, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.hu_stext_t, ptr %18, i32 0, i32 2
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %17, i64 0, i64 %21
  call void @HUlib_clearTextLine(ptr noundef %22)
  store i32 0, ptr %3, align 4, !tbaa !13
  br label %23

23:                                               ; preds = %36, %15
  %24 = load i32, ptr %3, align 4, !tbaa !13
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.hu_stext_t, ptr %25, i32 0, i32 1
  %27 = load i32, ptr %26, align 8, !tbaa !12
  %28 = icmp slt i32 %24, %27
  br i1 %28, label %29, label %39

29:                                               ; preds = %23
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.hu_stext_t, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %3, align 4, !tbaa !13
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %31, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.hu_textline_t, ptr %34, i32 0, i32 6
  store i32 4, ptr %35, align 4, !tbaa !14
  br label %36

36:                                               ; preds = %29
  %37 = load i32, ptr %3, align 4, !tbaa !13
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %3, align 4, !tbaa !13
  br label %23, !llvm.loop !16

39:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  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 = { 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, !11, i64 452}
!10 = !{!"", !7, i64 0, !11, i64 448, !11, i64 452, !6, i64 456, !11, i64 464}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 448}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !11, i64 108}
!15 = !{!"", !11, i64 0, !11, i64 4, !6, i64 8, !11, i64 16, !7, i64 20, !11, i64 104, !11, i64 108}
!16 = distinct !{!16, !17}
!17 = !{!"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"

%struct.hu_textline_t = type { i32, i32, ptr, i32, [81 x i8], i32, i32 }

; Function Attrs: nounwind uwtable
declare dso_local void @HUlib_clearTextLine(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @HUlib_addLineToSText(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 452
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = add nsw i32 %3, 1
  %5 = getelementptr inbounds i8, ptr %0, i64 448
  %6 = load i32, ptr %5, align 8, !tbaa !11
  %7 = icmp eq i32 %4, %6
  %spec.store.select = select i1 %7, i32 0, i32 %4
  store i32 %spec.store.select, ptr %2, align 4
  %8 = sext i32 %spec.store.select to i64
  %9 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %0, i64 0, i64 %8
  tail call void @HUlib_clearTextLine(ptr noundef %9)
  %10 = load i32, ptr %5, align 8, !tbaa !11
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %wide.trip.count = zext nneg i32 %10 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %12 = icmp ult i32 %10, 4
  br i1 %12, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %13 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %0, i64 0, i64 %indvars.iv, i32 6
  store i32 4, ptr %13, align 4, !tbaa !12
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %14 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %0, i64 0, i64 %indvars.iv.next, i32 6
  store i32 4, ptr %14, align 4, !tbaa !12
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %15 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %0, i64 0, i64 %indvars.iv.next.1, i32 6
  store i32 4, ptr %15, align 4, !tbaa !12
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %16 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %0, i64 0, i64 %indvars.iv.next.2, i32 6
  store i32 4, ptr %16, align 4, !tbaa !12
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !14

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %17 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %0, i64 0, i64 %indvars.iv.epil, i32 6
  store i32 4, ptr %17, align 4, !tbaa !12
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !16

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %1
  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" }

!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, !9, i64 452}
!6 = !{!"", !7, i64 0, !9, i64 448, !9, i64 452, !10, i64 456, !9, i64 464}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!6, !9, i64 448}
!12 = !{!13, !9, i64 108}
!13 = !{!"", !9, i64 0, !9, i64 4, !10, i64 8, !9, i64 16, !7, i64 20, !9, i64 104, !9, i64 108}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass