; 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: 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 @HUlib_eraseTextLine(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @HUlib_eraseSText(ptr noundef %0) #1 {
  %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
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %4

4:                                                ; preds = %34, %1
  %5 = load i32, ptr %3, align 4, !tbaa !9
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.hu_stext_t, ptr %6, i32 0, i32 1
  %8 = load i32, ptr %7, align 8, !tbaa !11
  %9 = icmp slt i32 %5, %8
  br i1 %9, label %10, label %37

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

15:                                               ; preds = %10
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.hu_stext_t, ptr %16, i32 0, i32 3
  %18 = load ptr, ptr %17, align 8, !tbaa !14
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %28, label %21

21:                                               ; preds = %15
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.hu_stext_t, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %23, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.hu_textline_t, ptr %26, i32 0, i32 6
  store i32 4, ptr %27, align 4, !tbaa !15
  br label %28

28:                                               ; preds = %21, %15, %10
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.hu_stext_t, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %3, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %30, i64 0, i64 %32
  call void @HUlib_eraseTextLine(ptr noundef %33)
  br label %34

34:                                               ; preds = %28
  %35 = load i32, ptr %3, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %3, align 4, !tbaa !9
  br label %4, !llvm.loop !17

37:                                               ; preds = %4
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.hu_stext_t, ptr %38, i32 0, i32 3
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.hu_stext_t, ptr %42, i32 0, i32 4
  store i32 %41, ptr %43, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #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 = !{!"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 448}
!12 = !{!"", !7, i64 0, !10, i64 448, !10, i64 452, !6, i64 456, !10, i64 464}
!13 = !{!12, !10, i64 464}
!14 = !{!12, !6, i64 456}
!15 = !{!16, !10, i64 108}
!16 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8, !10, i64 16, !7, i64 20, !10, i64 104, !10, i64 108}
!17 = distinct !{!17, !18}
!18 = !{!"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_eraseTextLine(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @HUlib_eraseSText(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 448
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 464
  %6 = getelementptr inbounds i8, ptr %0, i64 456
  br label %7

7:                                                ; preds = %.lr.ph, %14
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %14 ]
  %8 = load i32, ptr %5, align 8, !tbaa !11
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %14, label %9

9:                                                ; preds = %7
  %10 = load ptr, ptr %6, align 8, !tbaa !12
  %11 = load i32, ptr %10, align 4, !tbaa !13
  %.not10 = icmp eq i32 %11, 0
  br i1 %.not10, label %12, label %14

12:                                               ; preds = %9
  %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 !14
  br label %14

14:                                               ; preds = %12, %9, %7
  %15 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %0, i64 0, i64 %indvars.iv
  tail call void @HUlib_eraseTextLine(ptr noundef %15)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = load i32, ptr %2, align 8, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = icmp slt i64 %indvars.iv.next, %17
  br i1 %18, label %7, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %14, %1
  %19 = getelementptr inbounds i8, ptr %0, i64 456
  %20 = load ptr, ptr %19, align 8, !tbaa !12
  %21 = load i32, ptr %20, align 4, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %0, i64 464
  store i32 %21, ptr %22, align 8, !tbaa !11
  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 448}
!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 464}
!12 = !{!6, !10, i64 456}
!13 = !{!9, !9, i64 0}
!14 = !{!15, !9, i64 108}
!15 = !{!"", !9, i64 0, !9, i64 4, !10, i64 8, !9, i64 16, !7, i64 20, !9, i64 104, !9, i64 108}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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