; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (st_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 }

@plyr = external hidden global ptr, align 8
@st_palette = external hidden global i32, align 4
@lu_palette = 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
define dso_local void @ST_doPaletteStuff() #1 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %5 = load ptr, ptr @plyr, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.player_s, ptr %5, i32 0, i32 27
  %7 = load i32, ptr %6, align 8, !tbaa !9
  store i32 %7, ptr %3, align 4, !tbaa !14
  %8 = load ptr, ptr @plyr, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.player_s, ptr %8, i32 0, i32 10
  %10 = getelementptr inbounds [6 x i32], ptr %9, i64 0, i64 1
  %11 = load i32, ptr %10, align 4, !tbaa !14
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %26

13:                                               ; preds = %0
  %14 = load ptr, ptr @plyr, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.player_s, ptr %14, i32 0, i32 10
  %16 = getelementptr inbounds [6 x i32], ptr %15, i64 0, i64 1
  %17 = load i32, ptr %16, align 4, !tbaa !14
  %18 = ashr i32 %17, 6
  %19 = sub nsw i32 12, %18
  store i32 %19, ptr %4, align 4, !tbaa !14
  %20 = load i32, ptr %4, align 4, !tbaa !14
  %21 = load i32, ptr %3, align 4, !tbaa !14
  %22 = icmp sgt i32 %20, %21
  br i1 %22, label %23, label %25

23:                                               ; preds = %13
  %24 = load i32, ptr %4, align 4, !tbaa !14
  store i32 %24, ptr %3, align 4, !tbaa !14
  br label %25

25:                                               ; preds = %23, %13
  br label %26

26:                                               ; preds = %25, %0
  %27 = load i32, ptr %3, align 4, !tbaa !14
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %39

29:                                               ; preds = %26
  %30 = load i32, ptr %3, align 4, !tbaa !14
  %31 = add nsw i32 %30, 7
  %32 = ashr i32 %31, 3
  store i32 %32, ptr %1, align 4, !tbaa !14
  %33 = load i32, ptr %1, align 4, !tbaa !14
  %34 = icmp sge i32 %33, 8
  br i1 %34, label %35, label %36

35:                                               ; preds = %29
  store i32 7, ptr %1, align 4, !tbaa !14
  br label %36

36:                                               ; preds = %35, %29
  %37 = load i32, ptr %1, align 4, !tbaa !14
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %1, align 4, !tbaa !14
  br label %73

39:                                               ; preds = %26
  %40 = load ptr, ptr @plyr, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.player_s, ptr %40, i32 0, i32 28
  %42 = load i32, ptr %41, align 4, !tbaa !15
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %56

44:                                               ; preds = %39
  %45 = load ptr, ptr @plyr, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.player_s, ptr %45, i32 0, i32 28
  %47 = load i32, ptr %46, align 4, !tbaa !15
  %48 = add nsw i32 %47, 7
  %49 = ashr i32 %48, 3
  store i32 %49, ptr %1, align 4, !tbaa !14
  %50 = load i32, ptr %1, align 4, !tbaa !14
  %51 = icmp sge i32 %50, 4
  br i1 %51, label %52, label %53

52:                                               ; preds = %44
  store i32 3, ptr %1, align 4, !tbaa !14
  br label %53

53:                                               ; preds = %52, %44
  %54 = load i32, ptr %1, align 4, !tbaa !14
  %55 = add nsw i32 %54, 9
  store i32 %55, ptr %1, align 4, !tbaa !14
  br label %72

56:                                               ; preds = %39
  %57 = load ptr, ptr @plyr, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.player_s, ptr %57, i32 0, i32 10
  %59 = getelementptr inbounds [6 x i32], ptr %58, i64 0, i64 3
  %60 = load i32, ptr %59, align 4, !tbaa !14
  %61 = icmp sgt i32 %60, 128
  br i1 %61, label %69, label %62

62:                                               ; preds = %56
  %63 = load ptr, ptr @plyr, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.player_s, ptr %63, i32 0, i32 10
  %65 = getelementptr inbounds [6 x i32], ptr %64, i64 0, i64 3
  %66 = load i32, ptr %65, align 4, !tbaa !14
  %67 = and i32 %66, 8
  %68 = icmp ne i32 %67, 0
  br i1 %68, label %69, label %70

69:                                               ; preds = %62, %56
  store i32 13, ptr %1, align 4, !tbaa !14
  br label %71

70:                                               ; preds = %62
  store i32 0, ptr %1, align 4, !tbaa !14
  br label %71

71:                                               ; preds = %70, %69
  br label %72

72:                                               ; preds = %71, %53
  br label %73

73:                                               ; preds = %72, %36
  %74 = load i32, ptr %1, align 4, !tbaa !14
  %75 = load i32, ptr @st_palette, align 4, !tbaa !14
  %76 = icmp ne i32 %74, %75
  br i1 %76, label %77, label %86

77:                                               ; preds = %73
  %78 = load i32, ptr %1, align 4, !tbaa !14
  store i32 %78, ptr @st_palette, align 4, !tbaa !14
  %79 = load i32, ptr @lu_palette, align 4, !tbaa !14
  %80 = call ptr @W_CacheLumpNum(i32 noundef %79, i32 noundef 101)
  %81 = load i32, ptr %1, align 4, !tbaa !14
  %82 = mul nsw i32 %81, 768
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds i8, ptr %80, i64 %83
  store ptr %84, ptr %2, align 8, !tbaa !5
  %85 = load ptr, ptr %2, align 8, !tbaa !5
  call void @I_SetPalette(ptr noundef %85)
  br label %86

86:                                               ; preds = %77, %73
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  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 ptr @W_CacheLumpNum(i32 noundef, i32 noundef) #2

declare void @I_SetPalette(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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 232}
!10 = !{!"player_s", !6, i64 0, !11, i64 8, !12, i64 12, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !7, i64 48, !7, i64 72, !11, i64 96, !7, i64 100, !11, i64 116, !11, i64 120, !7, i64 124, !7, i64 160, !7, i64 176, !11, i64 192, !11, i64 196, !11, i64 200, !11, i64 204, !11, i64 208, !11, i64 212, !11, i64 216, !6, i64 224, !11, i64 232, !11, i64 236, !6, i64 240, !11, i64 248, !11, i64 252, !11, i64 256, !7, i64 264, !11, i64 312}
!11 = !{!"int", !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1, !13, i64 2, !13, i64 4, !7, i64 6, !7, i64 7}
!13 = !{!"short", !7, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !11, i64 236}


; 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"

@plyr = external hidden local_unnamed_addr global ptr, align 8
@st_palette = external hidden local_unnamed_addr global i32, align 4
@lu_palette = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @ST_doPaletteStuff() local_unnamed_addr #0 {
  %1 = load ptr, ptr @plyr, align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 232
  %3 = load i32, ptr %2, align 8, !tbaa !9
  %4 = getelementptr inbounds i8, ptr %1, i64 52
  %5 = load i32, ptr %4, align 4, !tbaa !14
  %.not = icmp eq i32 %5, 0
  %6 = ashr i32 %5, 6
  %7 = sub nsw i32 12, %6
  %spec.select = tail call i32 @llvm.smax.i32(i32 %7, i32 %3)
  %.013 = select i1 %.not, i32 %3, i32 %spec.select
  %.not19 = icmp eq i32 %.013, 0
  br i1 %.not19, label %12, label %8

8:                                                ; preds = %0
  %9 = add nsw i32 %.013, 7
  %10 = ashr i32 %9, 3
  %spec.store.select = tail call i32 @llvm.smin.i32(i32 %10, i32 7)
  %11 = add nsw i32 %spec.store.select, 1
  br label %24

12:                                               ; preds = %0
  %13 = getelementptr inbounds i8, ptr %1, i64 236
  %14 = load i32, ptr %13, align 4, !tbaa !15
  %.not20 = icmp eq i32 %14, 0
  br i1 %.not20, label %19, label %15

15:                                               ; preds = %12
  %16 = add nsw i32 %14, 7
  %17 = ashr i32 %16, 3
  %spec.store.select1 = tail call i32 @llvm.smin.i32(i32 %17, i32 3)
  %18 = add nsw i32 %spec.store.select1, 9
  br label %24

19:                                               ; preds = %12
  %20 = getelementptr inbounds i8, ptr %1, i64 60
  %21 = load i32, ptr %20, align 4, !tbaa !14
  %22 = icmp slt i32 %21, 129
  %23 = and i32 %21, 8
  %.not21 = icmp eq i32 %23, 0
  %or.cond = and i1 %22, %.not21
  %spec.select23 = select i1 %or.cond, i32 0, i32 13
  br label %24

24:                                               ; preds = %19, %15, %8
  %.0 = phi i32 [ %11, %8 ], [ %18, %15 ], [ %spec.select23, %19 ]
  %25 = load i32, ptr @st_palette, align 4, !tbaa !14
  %.not22 = icmp eq i32 %.0, %25
  br i1 %.not22, label %32, label %26

26:                                               ; preds = %24
  store i32 %.0, ptr @st_palette, align 4, !tbaa !14
  %27 = load i32, ptr @lu_palette, align 4, !tbaa !14
  %28 = tail call ptr @W_CacheLumpNum(i32 noundef %27, i32 noundef 101) #3
  %29 = mul nsw i32 %.0, 768
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i8, ptr %28, i64 %30
  tail call void @I_SetPalette(ptr noundef %31) #3
  br label %32

32:                                               ; preds = %26, %24
  ret void
}

declare ptr @W_CacheLumpNum(i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @I_SetPalette(ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !11, i64 232}
!10 = !{!"player_s", !6, i64 0, !11, i64 8, !12, i64 12, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !7, i64 48, !7, i64 72, !11, i64 96, !7, i64 100, !11, i64 116, !11, i64 120, !7, i64 124, !7, i64 160, !7, i64 176, !11, i64 192, !11, i64 196, !11, i64 200, !11, i64 204, !11, i64 208, !11, i64 212, !11, i64 216, !6, i64 224, !11, i64 232, !11, i64 236, !6, i64 240, !11, i64 248, !11, i64 252, !11, i64 256, !7, i64 264, !11, i64 312}
!11 = !{!"int", !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1, !13, i64 2, !13, i64 4, !7, i64 6, !7, i64 7}
!13 = !{!"short", !7, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !11, i64 236}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass