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

; 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.player_s = type { ptr, i32, %struct.ticcmd_t, i32, i32, i32, i32, i32, i32, i32, [6 x i32], [6 x i32], i32, [4 x i32], i32, i32, [9 x i32], [4 x i32], [4 x i32], i32, i32, i32, i32, i32, i32, i32, ptr, i32, i32, ptr, i32, i32, i32, [2 x %struct.pspdef_t], i32 }
%struct.ticcmd_t = type { i8, i8, i16, i16, i8, i8 }
%struct.pspdef_t = type { ptr, i32, i32, i32 }
%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 }
%struct.hu_itext_t = type { %struct.hu_textline_t, i32, ptr, i32 }
%struct.patch_t = type { i16, i16, i16, i16, [8 x i32] }

@mapnames = external dso_local global [45 x ptr], align 16
@mapnames2 = external dso_local global [32 x ptr], align 16
@hu_font = external dso_local global [63 x ptr], align 16
@headsupactive = external hidden global i32, align 4
@players = external global [4 x %struct.player_s], align 16
@consoleplayer = external global i32, align 4
@plr = external hidden global ptr, align 8
@message_on = external hidden global i32, align 4
@message_dontfuckwithme = external dso_local global i32, align 4
@message_nottobefuckedwith = external hidden global i32, align 4
@chat_on = external dso_local global i32, align 4
@w_message = external hidden global %struct.hu_stext_t, align 8
@w_title = external hidden global %struct.hu_textline_t, align 8
@gamemode = external global i32, align 4
@gameepisode = external global i32, align 4
@gamemap = external global i32, align 4
@w_chat = external hidden global %struct.hu_itext_t, align 8
@w_inputbuffer = external hidden global [4 x %struct.hu_itext_t], align 16
@always_off = external hidden 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 @HU_Stop() #1

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

5:                                                ; preds = %0
  call void @HU_Stop()
  br label %6

6:                                                ; preds = %5, %0
  %7 = load i32, ptr @consoleplayer, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %8
  store ptr %9, ptr @plr, align 8, !tbaa !9
  store i32 0, ptr @message_on, align 4, !tbaa !5
  store i32 0, ptr @message_dontfuckwithme, align 4, !tbaa !5
  store i32 0, ptr @message_nottobefuckedwith, align 4, !tbaa !5
  store i32 0, ptr @chat_on, align 4, !tbaa !5
  call void @HUlib_initSText(ptr noundef @w_message, i32 noundef 0, i32 noundef 0, i32 noundef 1, ptr noundef @hu_font, i32 noundef 33, ptr noundef @message_on)
  %10 = load ptr, ptr @hu_font, align 16, !tbaa !9
  %11 = getelementptr inbounds %struct.patch_t, ptr %10, i32 0, i32 1
  %12 = load i16, ptr %11, align 2, !tbaa !11
  %13 = sext i16 %12 to i32
  %14 = sub nsw i32 167, %13
  call void @HUlib_initTextLine(ptr noundef @w_title, i32 noundef 0, i32 noundef %14, ptr noundef @hu_font, i32 noundef 33)
  %15 = load i32, ptr @gamemode, align 4, !tbaa !5
  switch i32 %15, label %27 [
    i32 0, label %16
    i32 1, label %16
    i32 3, label %16
    i32 2, label %26
  ]

16:                                               ; preds = %6, %6, %6
  %17 = load i32, ptr @gameepisode, align 4, !tbaa !5
  %18 = sub nsw i32 %17, 1
  %19 = mul nsw i32 %18, 9
  %20 = load i32, ptr @gamemap, align 4, !tbaa !5
  %21 = add nsw i32 %19, %20
  %22 = sub nsw i32 %21, 1
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [45 x ptr], ptr @mapnames, i64 0, i64 %23
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  store ptr %25, ptr %2, align 8, !tbaa !9
  br label %33

26:                                               ; preds = %6
  br label %27

27:                                               ; preds = %26, %6
  %28 = load i32, ptr @gamemap, align 4, !tbaa !5
  %29 = sub nsw i32 %28, 1
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [32 x ptr], ptr @mapnames2, i64 0, i64 %30
  %32 = load ptr, ptr %31, align 8, !tbaa !9
  store ptr %32, ptr %2, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %27, %16
  br label %34

34:                                               ; preds = %38, %33
  %35 = load ptr, ptr %2, align 8, !tbaa !9
  %36 = load i8, ptr %35, align 1, !tbaa !14
  %37 = icmp ne i8 %36, 0
  br i1 %37, label %38, label %43

38:                                               ; preds = %34
  %39 = load ptr, ptr %2, align 8, !tbaa !9
  %40 = getelementptr inbounds i8, ptr %39, i32 1
  store ptr %40, ptr %2, align 8, !tbaa !9
  %41 = load i8, ptr %39, align 1, !tbaa !14
  %42 = call i32 @HUlib_addCharToTextLine(ptr noundef @w_title, i8 noundef signext %41)
  br label %34, !llvm.loop !15

43:                                               ; preds = %34
  %44 = load ptr, ptr @hu_font, align 16, !tbaa !9
  %45 = getelementptr inbounds %struct.patch_t, ptr %44, i32 0, i32 1
  %46 = load i16, ptr %45, align 2, !tbaa !11
  %47 = sext i16 %46 to i32
  %48 = add nsw i32 %47, 1
  %49 = mul nsw i32 1, %48
  %50 = add nsw i32 0, %49
  call void @HUlib_initIText(ptr noundef @w_chat, i32 noundef 0, i32 noundef %50, ptr noundef @hu_font, i32 noundef 33, ptr noundef @chat_on)
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %58, %43
  %52 = load i32, ptr %1, align 4, !tbaa !5
  %53 = icmp slt i32 %52, 4
  br i1 %53, label %54, label %61

54:                                               ; preds = %51
  %55 = load i32, ptr %1, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [4 x %struct.hu_itext_t], ptr @w_inputbuffer, i64 0, i64 %56
  call void @HUlib_initIText(ptr noundef %57, i32 noundef 0, i32 noundef 0, ptr noundef null, i32 noundef 0, ptr noundef @always_off)
  br label %58

58:                                               ; preds = %54
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %1, align 4, !tbaa !5
  br label %51, !llvm.loop !17

61:                                               ; preds = %51
  store i32 1, ptr @headsupactive, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

declare void @HUlib_initSText(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef) #2

declare void @HUlib_initTextLine(ptr noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef) #2

declare i32 @HUlib_addCharToTextLine(ptr noundef, i8 noundef signext) #2

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


; 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.player_s = type { ptr, i32, %struct.ticcmd_t, i32, i32, i32, i32, i32, i32, i32, [6 x i32], [6 x i32], i32, [4 x i32], i32, i32, [9 x i32], [4 x i32], [4 x i32], i32, i32, i32, i32, i32, i32, i32, ptr, i32, i32, ptr, i32, i32, i32, [2 x %struct.pspdef_t], i32 }
%struct.ticcmd_t = type { i8, i8, i16, i16, i8, i8 }
%struct.pspdef_t = type { ptr, i32, i32, i32 }
%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 }
%struct.hu_itext_t = type { %struct.hu_textline_t, i32, ptr, i32 }

@mapnames = external dso_local local_unnamed_addr global [45 x ptr], align 16
@mapnames2 = external dso_local local_unnamed_addr global [32 x ptr], align 16
@hu_font = external dso_local global [63 x ptr], align 16
@headsupactive = external hidden local_unnamed_addr global i32, align 4
@players = external global [4 x %struct.player_s], align 16
@consoleplayer = external local_unnamed_addr global i32, align 4
@plr = external hidden local_unnamed_addr global ptr, align 8
@message_on = external hidden global i32, align 4
@message_dontfuckwithme = external dso_local local_unnamed_addr global i32, align 4
@message_nottobefuckedwith = external hidden local_unnamed_addr global i32, align 4
@chat_on = external dso_local global i32, align 4
@w_message = external hidden global %struct.hu_stext_t, align 8
@w_title = external hidden global %struct.hu_textline_t, align 8
@gamemode = external local_unnamed_addr global i32, align 4
@gameepisode = external local_unnamed_addr global i32, align 4
@gamemap = external local_unnamed_addr global i32, align 4
@w_chat = external hidden global %struct.hu_itext_t, align 8
@w_inputbuffer = external hidden global [4 x %struct.hu_itext_t], align 16
@always_off = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local void @HU_Stop() local_unnamed_addr #0

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

2:                                                ; preds = %0
  tail call void @HU_Stop()
  br label %3

3:                                                ; preds = %2, %0
  %4 = load i32, ptr @consoleplayer, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [4 x %struct.player_s], ptr @players, i64 0, i64 %5
  store ptr %6, ptr @plr, align 8, !tbaa !9
  store i32 0, ptr @message_on, align 4, !tbaa !5
  store i32 0, ptr @message_dontfuckwithme, align 4, !tbaa !5
  store i32 0, ptr @message_nottobefuckedwith, align 4, !tbaa !5
  store i32 0, ptr @chat_on, align 4, !tbaa !5
  tail call void @HUlib_initSText(ptr noundef nonnull @w_message, i32 noundef 0, i32 noundef 0, i32 noundef 1, ptr noundef nonnull @hu_font, i32 noundef 33, ptr noundef nonnull @message_on) #2
  %7 = load ptr, ptr @hu_font, align 16, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %7, i64 2
  %9 = load i16, ptr %8, align 2, !tbaa !11
  %10 = sext i16 %9 to i32
  %11 = sub nsw i32 167, %10
  tail call void @HUlib_initTextLine(ptr noundef nonnull @w_title, i32 noundef 0, i32 noundef %11, ptr noundef nonnull @hu_font, i32 noundef 33) #2
  %12 = load i32, ptr @gamemode, align 4, !tbaa !5
  switch i32 %12, label %21 [
    i32 0, label %13
    i32 1, label %13
    i32 3, label %13
  ]

13:                                               ; preds = %3, %3, %3
  %14 = load i32, ptr @gameepisode, align 4, !tbaa !5
  %15 = mul i32 %14, 9
  %16 = load i32, ptr @gamemap, align 4, !tbaa !5
  %17 = add i32 %15, -10
  %18 = add i32 %17, %16
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [45 x ptr], ptr @mapnames, i64 0, i64 %19
  br label %26

21:                                               ; preds = %3
  %22 = load i32, ptr @gamemap, align 4, !tbaa !5
  %23 = add nsw i32 %22, -1
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [32 x ptr], ptr @mapnames2, i64 0, i64 %24
  br label %26

26:                                               ; preds = %21, %13
  %.0.in = phi ptr [ %25, %21 ], [ %20, %13 ]
  %.0 = load ptr, ptr %.0.in, align 8, !tbaa !9
  %27 = load i8, ptr %.0, align 1, !tbaa !14
  %.not67 = icmp eq i8 %27, 0
  br i1 %.not67, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %26, %.lr.ph
  %28 = phi i8 [ %31, %.lr.ph ], [ %27, %26 ]
  %.18 = phi ptr [ %29, %.lr.ph ], [ %.0, %26 ]
  %29 = getelementptr inbounds i8, ptr %.18, i64 1
  %30 = tail call i32 @HUlib_addCharToTextLine(ptr noundef nonnull @w_title, i8 noundef signext %28) #2
  %31 = load i8, ptr %29, align 1, !tbaa !14
  %.not6 = icmp eq i8 %31, 0
  br i1 %.not6, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %26
  %32 = load ptr, ptr @hu_font, align 16, !tbaa !9
  %33 = getelementptr inbounds i8, ptr %32, i64 2
  %34 = load i16, ptr %33, align 2, !tbaa !11
  %35 = sext i16 %34 to i32
  %36 = add nsw i32 %35, 1
  tail call void @HUlib_initIText(ptr noundef nonnull @w_chat, i32 noundef 0, i32 noundef %36, ptr noundef nonnull @hu_font, i32 noundef 33, ptr noundef nonnull @chat_on) #2
  tail call void @HUlib_initIText(ptr noundef nonnull @w_inputbuffer, i32 noundef 0, i32 noundef 0, ptr noundef null, i32 noundef 0, ptr noundef nonnull @always_off) #2
  tail call void @HUlib_initIText(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_inputbuffer, i64 136), i32 noundef 0, i32 noundef 0, ptr noundef null, i32 noundef 0, ptr noundef nonnull @always_off) #2
  tail call void @HUlib_initIText(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_inputbuffer, i64 272), i32 noundef 0, i32 noundef 0, ptr noundef null, i32 noundef 0, ptr noundef nonnull @always_off) #2
  tail call void @HUlib_initIText(ptr noundef nonnull getelementptr inbounds (i8, ptr @w_inputbuffer, i64 408), i32 noundef 0, i32 noundef 0, ptr noundef null, i32 noundef 0, ptr noundef nonnull @always_off) #2
  store i32 1, ptr @headsupactive, align 4, !tbaa !5
  ret void
}

declare void @HUlib_initSText(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare void @HUlib_initTextLine(ptr noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

declare i32 @HUlib_addCharToTextLine(ptr noundef, i8 noundef signext) local_unnamed_addr #1

declare void @HUlib_initIText(ptr noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !13, i64 2}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !7, i64 8}
!13 = !{!"short", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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