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

; 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.glow_t = type { %struct.thinker_s, ptr, i32, i32, i32 }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }
%struct.sector_t = type { i32, i32, i16, i16, i16, i16, i16, i32, ptr, [4 x i32], %struct.degenmobj_t, i32, ptr, ptr, i32, ptr }
%struct.degenmobj_t = type { %struct.thinker_s, i32, i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local void @T_Glow(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds %struct.glow_t, ptr %3, i32 0, i32 4
  %5 = load i32, ptr %4, align 8, !tbaa !9
  switch i32 %5, label %68 [
    i32 -1, label %6
    i32 1, label %37
  ]

6:                                                ; preds = %1
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.glow_t, ptr %7, i32 0, i32 1
  %9 = load ptr, ptr %8, align 8, !tbaa !13
  %10 = getelementptr inbounds %struct.sector_t, ptr %9, i32 0, i32 4
  %11 = load i16, ptr %10, align 4, !tbaa !14
  %12 = sext i16 %11 to i32
  %13 = sub nsw i32 %12, 8
  %14 = trunc i32 %13 to i16
  store i16 %14, ptr %10, align 4, !tbaa !14
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.glow_t, ptr %15, i32 0, i32 1
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = getelementptr inbounds %struct.sector_t, ptr %17, i32 0, i32 4
  %19 = load i16, ptr %18, align 4, !tbaa !14
  %20 = sext i16 %19 to i32
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.glow_t, ptr %21, i32 0, i32 2
  %23 = load i32, ptr %22, align 8, !tbaa !18
  %24 = icmp sle i32 %20, %23
  br i1 %24, label %25, label %36

25:                                               ; preds = %6
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.glow_t, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  %29 = getelementptr inbounds %struct.sector_t, ptr %28, i32 0, i32 4
  %30 = load i16, ptr %29, align 4, !tbaa !14
  %31 = sext i16 %30 to i32
  %32 = add nsw i32 %31, 8
  %33 = trunc i32 %32 to i16
  store i16 %33, ptr %29, align 4, !tbaa !14
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.glow_t, ptr %34, i32 0, i32 4
  store i32 1, ptr %35, align 8, !tbaa !9
  br label %36

36:                                               ; preds = %25, %6
  br label %68

37:                                               ; preds = %1
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.glow_t, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  %41 = getelementptr inbounds %struct.sector_t, ptr %40, i32 0, i32 4
  %42 = load i16, ptr %41, align 4, !tbaa !14
  %43 = sext i16 %42 to i32
  %44 = add nsw i32 %43, 8
  %45 = trunc i32 %44 to i16
  store i16 %45, ptr %41, align 4, !tbaa !14
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.glow_t, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !13
  %49 = getelementptr inbounds %struct.sector_t, ptr %48, i32 0, i32 4
  %50 = load i16, ptr %49, align 4, !tbaa !14
  %51 = sext i16 %50 to i32
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.glow_t, ptr %52, i32 0, i32 3
  %54 = load i32, ptr %53, align 4, !tbaa !19
  %55 = icmp sge i32 %51, %54
  br i1 %55, label %56, label %67

56:                                               ; preds = %37
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.glow_t, ptr %57, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8, !tbaa !13
  %60 = getelementptr inbounds %struct.sector_t, ptr %59, i32 0, i32 4
  %61 = load i16, ptr %60, align 4, !tbaa !14
  %62 = sext i16 %61 to i32
  %63 = sub nsw i32 %62, 8
  %64 = trunc i32 %63 to i16
  store i16 %64, ptr %60, align 4, !tbaa !14
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.glow_t, ptr %65, i32 0, i32 4
  store i32 -1, ptr %66, align 8, !tbaa !9
  br label %67

67:                                               ; preds = %56, %37
  br label %68

68:                                               ; preds = %67, %36, %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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 40}
!10 = !{!"", !11, i64 0, !6, i64 24, !12, i64 32, !12, i64 36, !12, i64 40}
!11 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 24}
!14 = !{!15, !16, i64 12}
!15 = !{!"", !12, i64 0, !12, i64 4, !16, i64 8, !16, i64 10, !16, i64 12, !16, i64 14, !16, i64 16, !12, i64 20, !6, i64 24, !7, i64 32, !17, i64 48, !12, i64 88, !6, i64 96, !6, i64 104, !12, i64 112, !6, i64 120}
!16 = !{!"short", !7, i64 0}
!17 = !{!"", !11, i64 0, !12, i64 24, !12, i64 28, !12, i64 32}
!18 = !{!10, !12, i64 32}
!19 = !{!10, !12, i64 36}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @T_Glow(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 40
  %3 = load i32, ptr %2, align 8, !tbaa !5
  switch i32 %3, label %24 [
    i32 -1, label %4
    i32 1, label %14
  ]

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load ptr, ptr %5, align 8, !tbaa !12
  %7 = getelementptr inbounds i8, ptr %6, i64 12
  %8 = load i16, ptr %7, align 4, !tbaa !13
  %9 = add i16 %8, -8
  store i16 %9, ptr %7, align 4, !tbaa !13
  %10 = sext i16 %9 to i32
  %11 = getelementptr inbounds i8, ptr %0, i64 32
  %12 = load i32, ptr %11, align 8, !tbaa !17
  %.not12 = icmp slt i32 %12, %10
  br i1 %.not12, label %24, label %13

13:                                               ; preds = %4
  store i16 %8, ptr %7, align 4, !tbaa !13
  br label %.sink.split

14:                                               ; preds = %1
  %15 = getelementptr inbounds i8, ptr %0, i64 24
  %16 = load ptr, ptr %15, align 8, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %16, i64 12
  %18 = load i16, ptr %17, align 4, !tbaa !13
  %19 = add i16 %18, 8
  store i16 %19, ptr %17, align 4, !tbaa !13
  %20 = sext i16 %19 to i32
  %21 = getelementptr inbounds i8, ptr %0, i64 36
  %22 = load i32, ptr %21, align 4, !tbaa !18
  %.not = icmp sgt i32 %22, %20
  br i1 %.not, label %24, label %23

23:                                               ; preds = %14
  store i16 %18, ptr %17, align 4, !tbaa !13
  br label %.sink.split

.sink.split:                                      ; preds = %13, %23
  %.sink = phi i32 [ -1, %23 ], [ 1, %13 ]
  store i32 %.sink, ptr %2, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %.sink.split, %14, %4, %1
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) 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, !11, i64 40}
!6 = !{!"", !7, i64 0, !8, i64 24, !11, i64 32, !11, i64 36, !11, i64 40}
!7 = !{!"thinker_s", !8, i64 0, !8, i64 8, !9, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!6, !8, i64 24}
!13 = !{!14, !15, i64 12}
!14 = !{!"", !11, i64 0, !11, i64 4, !15, i64 8, !15, i64 10, !15, i64 12, !15, i64 14, !15, i64 16, !11, i64 20, !8, i64 24, !9, i64 32, !16, i64 48, !11, i64 88, !8, i64 96, !8, i64 104, !11, i64 112, !8, i64 120}
!15 = !{!"short", !9, i64 0}
!16 = !{!"", !7, i64 0, !11, i64 24, !11, i64 28, !11, i64 32}
!17 = !{!6, !11, i64 32}
!18 = !{!6, !11, i64 36}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass