; 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 i32 @HUlib_addCharToTextLine(ptr noundef, i8 noundef signext) #0

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

; Function Attrs: nounwind uwtable
define dso_local void @HUlib_addMessageToSText(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  call void @HUlib_addLineToSText(ptr noundef %7)
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %28

10:                                               ; preds = %3
  br label %11

11:                                               ; preds = %15, %10
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = icmp ne i8 %13, 0
  br i1 %14, label %15, label %27

15:                                               ; preds = %11
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.hu_stext_t, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %4, 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 !10
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %17, i64 0, i64 %21
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %23, i32 1
  store ptr %24, ptr %5, align 8, !tbaa !5
  %25 = load i8, ptr %23, align 1, !tbaa !9
  %26 = call i32 @HUlib_addCharToTextLine(ptr noundef %22, i8 noundef signext %25)
  br label %11, !llvm.loop !13

27:                                               ; preds = %11
  br label %28

28:                                               ; preds = %27, %3
  br label %29

29:                                               ; preds = %33, %28
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = icmp ne i8 %31, 0
  br i1 %32, label %33, label %45

33:                                               ; preds = %29
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.hu_stext_t, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.hu_stext_t, ptr %36, i32 0, i32 2
  %38 = load i32, ptr %37, align 4, !tbaa !10
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %35, i64 0, i64 %39
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i32 1
  store ptr %42, ptr %6, align 8, !tbaa !5
  %43 = load i8, ptr %41, align 1, !tbaa !9
  %44 = call i32 @HUlib_addCharToTextLine(ptr noundef %40, i8 noundef signext %43)
  br label %29, !llvm.loop !15

45:                                               ; preds = %29
  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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 452}
!11 = !{!"", !7, i64 0, !12, i64 448, !12, i64 452, !6, i64 456, !12, i64 464}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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 i32 @HUlib_addCharToTextLine(ptr noundef, i8 noundef signext) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @HUlib_addMessageToSText(ptr noundef %0, ptr noundef readonly %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  tail call void @HUlib_addLineToSText(ptr noundef %0)
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3
  %4 = load i8, ptr %1, align 1, !tbaa !5
  %.not1416 = icmp eq i8 %4, 0
  br i1 %.not1416, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %5 = getelementptr inbounds i8, ptr %0, i64 452
  br label %6

6:                                                ; preds = %.lr.ph, %6
  %7 = phi i8 [ %4, %.lr.ph ], [ %13, %6 ]
  %.01017 = phi ptr [ %1, %.lr.ph ], [ %11, %6 ]
  %8 = load i32, ptr %5, align 4, !tbaa !8
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %0, i64 0, i64 %9
  %11 = getelementptr inbounds i8, ptr %.01017, i64 1
  %12 = tail call i32 @HUlib_addCharToTextLine(ptr noundef %10, i8 noundef signext %7)
  %13 = load i8, ptr %11, align 1, !tbaa !5
  %.not14 = icmp eq i8 %13, 0
  br i1 %.not14, label %.loopexit, label %6, !llvm.loop !12

.loopexit:                                        ; preds = %6, %.preheader, %3
  %14 = load i8, ptr %2, align 1, !tbaa !5
  %.not1518 = icmp eq i8 %14, 0
  br i1 %.not1518, label %._crit_edge, label %.lr.ph20

.lr.ph20:                                         ; preds = %.loopexit
  %15 = getelementptr inbounds i8, ptr %0, i64 452
  br label %16

16:                                               ; preds = %.lr.ph20, %16
  %17 = phi i8 [ %14, %.lr.ph20 ], [ %23, %16 ]
  %.019 = phi ptr [ %2, %.lr.ph20 ], [ %21, %16 ]
  %18 = load i32, ptr %15, align 4, !tbaa !8
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [4 x %struct.hu_textline_t], ptr %0, i64 0, i64 %19
  %21 = getelementptr inbounds i8, ptr %.019, i64 1
  %22 = tail call i32 @HUlib_addCharToTextLine(ptr noundef %20, i8 noundef signext %17)
  %23 = load i8, ptr %21, align 1, !tbaa !5
  %.not15 = icmp eq i8 %23, 0
  br i1 %.not15, label %._crit_edge, label %16, !llvm.loop !14

._crit_edge:                                      ; preds = %16, %.loopexit
  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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 452}
!9 = !{!"", !6, i64 0, !10, i64 448, !10, i64 452, !11, i64 456, !10, i64 464}
!10 = !{!"int", !6, i64 0}
!11 = !{!"any pointer", !6, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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