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

@sectors = external global ptr, align 8
@numsectors = external 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 @EV_TurnTagLightsOff(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %9 = load ptr, ptr @sectors, align 8, !tbaa !5
  store ptr %9, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %71, %1
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = load i32, ptr @numsectors, align 4, !tbaa !9
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %14, label %76

14:                                               ; preds = %10
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.sector_t, ptr %15, i32 0, i32 6
  %17 = load i16, ptr %16, align 8, !tbaa !11
  %18 = sext i16 %17 to i32
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.line_s, ptr %19, i32 0, i32 6
  %21 = load i16, ptr %20, align 4, !tbaa !16
  %22 = sext i16 %21 to i32
  %23 = icmp eq i32 %18, %22
  br i1 %23, label %24, label %70

24:                                               ; preds = %14
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.sector_t, ptr %25, i32 0, i32 4
  %27 = load i16, ptr %26, align 4, !tbaa !18
  %28 = sext i16 %27 to i32
  store i32 %28, ptr %5, align 4, !tbaa !9
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %62, %24
  %30 = load i32, ptr %3, align 4, !tbaa !9
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.sector_t, ptr %31, i32 0, i32 14
  %33 = load i32, ptr %32, align 8, !tbaa !19
  %34 = icmp slt i32 %30, %33
  br i1 %34, label %35, label %65

35:                                               ; preds = %29
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.sector_t, ptr %36, i32 0, i32 15
  %38 = load ptr, ptr %37, align 8, !tbaa !20
  %39 = load i32, ptr %3, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds ptr, ptr %38, i64 %40
  %42 = load ptr, ptr %41, align 8, !tbaa !5
  store ptr %42, ptr %8, align 8, !tbaa !5
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = call ptr @getNextSector(ptr noundef %43, ptr noundef %44)
  store ptr %45, ptr %7, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = icmp ne ptr %46, null
  br i1 %47, label %49, label %48

48:                                               ; preds = %35
  br label %62

49:                                               ; preds = %35
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.sector_t, ptr %50, i32 0, i32 4
  %52 = load i16, ptr %51, align 4, !tbaa !18
  %53 = sext i16 %52 to i32
  %54 = load i32, ptr %5, align 4, !tbaa !9
  %55 = icmp slt i32 %53, %54
  br i1 %55, label %56, label %61

56:                                               ; preds = %49
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.sector_t, ptr %57, i32 0, i32 4
  %59 = load i16, ptr %58, align 4, !tbaa !18
  %60 = sext i16 %59 to i32
  store i32 %60, ptr %5, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %56, %49
  br label %62

62:                                               ; preds = %61, %48
  %63 = load i32, ptr %3, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %3, align 4, !tbaa !9
  br label %29, !llvm.loop !21

65:                                               ; preds = %29
  %66 = load i32, ptr %5, align 4, !tbaa !9
  %67 = trunc i32 %66 to i16
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.sector_t, ptr %68, i32 0, i32 4
  store i16 %67, ptr %69, align 4, !tbaa !18
  br label %70

70:                                               ; preds = %65, %14
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %4, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %4, align 4, !tbaa !9
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.sector_t, ptr %74, i32 1
  store ptr %75, ptr %6, align 8, !tbaa !5
  br label %10, !llvm.loop !23

76:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

declare ptr @getNextSector(ptr 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 = !{!"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, !13, i64 16}
!12 = !{!"", !10, i64 0, !10, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !10, i64 20, !6, i64 24, !7, i64 32, !14, i64 48, !10, i64 88, !6, i64 96, !6, i64 104, !10, i64 112, !6, i64 120}
!13 = !{!"short", !7, i64 0}
!14 = !{!"", !15, i64 0, !10, i64 24, !10, i64 28, !10, i64 32}
!15 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!16 = !{!17, !13, i64 28}
!17 = !{!"line_s", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !13, i64 24, !13, i64 26, !13, i64 28, !7, i64 30, !7, i64 36, !10, i64 52, !6, i64 56, !6, i64 64, !10, i64 72, !6, i64 80}
!18 = !{!12, !13, i64 12}
!19 = !{!12, !10, i64 112}
!20 = !{!12, !6, i64 120}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


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

@sectors = external local_unnamed_addr global ptr, align 8
@numsectors = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @EV_TurnTagLightsOff(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @numsectors, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph29, label %._crit_edge30

.lr.ph29:                                         ; preds = %1
  %4 = load ptr, ptr @sectors, align 8, !tbaa !9
  %5 = getelementptr inbounds i8, ptr %0, i64 28
  br label %6

6:                                                ; preds = %.lr.ph29, %34
  %.pre32 = phi i32 [ %2, %.lr.ph29 ], [ %.pre33, %34 ]
  %7 = phi i32 [ %2, %.lr.ph29 ], [ %35, %34 ]
  %.01827 = phi i32 [ 0, %.lr.ph29 ], [ %36, %34 ]
  %.01925 = phi ptr [ %4, %.lr.ph29 ], [ %37, %34 ]
  %8 = getelementptr inbounds i8, ptr %.01925, i64 16
  %9 = load i16, ptr %8, align 8, !tbaa !11
  %10 = load i16, ptr %5, align 4, !tbaa !16
  %11 = icmp eq i16 %9, %10
  br i1 %11, label %12, label %34

12:                                               ; preds = %6
  %13 = getelementptr inbounds i8, ptr %.01925, i64 12
  %14 = load i16, ptr %13, align 4, !tbaa !18
  %15 = sext i16 %14 to i32
  %16 = getelementptr inbounds i8, ptr %.01925, i64 112
  %17 = load i32, ptr %16, align 8, !tbaa !19
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %12
  %19 = getelementptr inbounds i8, ptr %.01925, i64 120
  br label %20

20:                                               ; preds = %.lr.ph, %29
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %29 ]
  %.02023 = phi i32 [ %15, %.lr.ph ], [ %.1, %29 ]
  %21 = load ptr, ptr %19, align 8, !tbaa !20
  %22 = getelementptr inbounds ptr, ptr %21, i64 %indvars.iv
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = tail call ptr @getNextSector(ptr noundef %23, ptr noundef nonnull %.01925) #3
  %.not = icmp eq ptr %24, null
  br i1 %.not, label %29, label %25

25:                                               ; preds = %20
  %26 = getelementptr inbounds i8, ptr %24, i64 12
  %27 = load i16, ptr %26, align 4, !tbaa !18
  %28 = sext i16 %27 to i32
  %spec.select = tail call i32 @llvm.smin.i32(i32 %.02023, i32 %28)
  br label %29

29:                                               ; preds = %25, %20
  %.1 = phi i32 [ %.02023, %20 ], [ %spec.select, %25 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %30 = load i32, ptr %16, align 8, !tbaa !19
  %31 = sext i32 %30 to i64
  %32 = icmp slt i64 %indvars.iv.next, %31
  br i1 %32, label %20, label %._crit_edge.loopexit, !llvm.loop !21

._crit_edge.loopexit:                             ; preds = %29
  %.pre.pre = load i32, ptr @numsectors, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %12
  %.pre = phi i32 [ %.pre32, %12 ], [ %.pre.pre, %._crit_edge.loopexit ]
  %.020.lcssa = phi i32 [ %15, %12 ], [ %.1, %._crit_edge.loopexit ]
  %33 = trunc nsw i32 %.020.lcssa to i16
  store i16 %33, ptr %13, align 4, !tbaa !18
  br label %34

34:                                               ; preds = %6, %._crit_edge
  %.pre33 = phi i32 [ %.pre32, %6 ], [ %.pre, %._crit_edge ]
  %35 = phi i32 [ %7, %6 ], [ %.pre, %._crit_edge ]
  %36 = add nuw nsw i32 %.01827, 1
  %37 = getelementptr inbounds i8, ptr %.01925, i64 128
  %38 = icmp slt i32 %36, %35
  br i1 %38, label %6, label %._crit_edge30, !llvm.loop !23

._crit_edge30:                                    ; preds = %34, %1
  ret void
}

declare ptr @getNextSector(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.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 = !{!"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 16}
!12 = !{!"", !6, i64 0, !6, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !6, i64 20, !10, i64 24, !7, i64 32, !14, i64 48, !6, i64 88, !10, i64 96, !10, i64 104, !6, i64 112, !10, i64 120}
!13 = !{!"short", !7, i64 0}
!14 = !{!"", !15, i64 0, !6, i64 24, !6, i64 28, !6, i64 32}
!15 = !{!"thinker_s", !10, i64 0, !10, i64 8, !7, i64 16}
!16 = !{!17, !13, i64 28}
!17 = !{!"line_s", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 20, !13, i64 24, !13, i64 26, !13, i64 28, !7, i64 30, !7, i64 36, !6, i64 52, !10, i64 56, !10, i64 64, !6, i64 72, !10, i64 80}
!18 = !{!12, !13, i64 12}
!19 = !{!12, !6, i64 112}
!20 = !{!12, !10, i64 120}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = distinct !{!23, !22}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass