; 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

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

; Function Attrs: nounwind uwtable
define dso_local void @EV_LightTurnOn(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %10 = load ptr, ptr @sectors, align 8, !tbaa !5
  store ptr %10, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %72, %2
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = load i32, ptr @numsectors, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %15, label %77

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

25:                                               ; preds = %15
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %66, label %28

28:                                               ; preds = %25
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %29

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

35:                                               ; preds = %29
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.sector_t, ptr %36, i32 0, i32 15
  %38 = load ptr, ptr %37, align 8, !tbaa !19
  %39 = load i32, ptr %6, 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 %9, align 8, !tbaa !5
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = call ptr @getNextSector(ptr noundef %43, ptr noundef %44)
  store ptr %45, ptr %8, align 8, !tbaa !5
  %46 = load ptr, ptr %8, 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 %8, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.sector_t, ptr %50, i32 0, i32 4
  %52 = load i16, ptr %51, align 4, !tbaa !20
  %53 = sext i16 %52 to i32
  %54 = load i32, ptr %4, align 4, !tbaa !9
  %55 = icmp sgt i32 %53, %54
  br i1 %55, label %56, label %61

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

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

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

65:                                               ; preds = %29
  br label %66

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

71:                                               ; preds = %66, %15
  br label %72

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

77:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  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 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !10, i64 112}
!19 = !{!12, !6, i64 120}
!20 = !{!12, !13, i64 12}
!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

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

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

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

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

13:                                               ; preds = %7
  %.not = icmp eq i32 %.028, 0
  br i1 %.not, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %13
  %14 = getelementptr inbounds i8, ptr %.01926, i64 112
  %15 = load i32, ptr %14, align 8, !tbaa !18
  %16 = icmp sgt i32 %15, 0
  br i1 %16, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader
  %17 = getelementptr inbounds i8, ptr %.01926, i64 120
  br label %18

18:                                               ; preds = %.lr.ph, %27
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %27 ]
  %.125 = phi i32 [ 0, %.lr.ph ], [ %.2, %27 ]
  %19 = load ptr, ptr %17, align 8, !tbaa !19
  %20 = getelementptr inbounds ptr, ptr %19, i64 %indvars.iv
  %21 = load ptr, ptr %20, align 8, !tbaa !9
  %22 = tail call ptr @getNextSector(ptr noundef %21, ptr noundef nonnull %.01926) #3
  %.not23 = icmp eq ptr %22, null
  br i1 %.not23, label %27, label %23

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

27:                                               ; preds = %23, %18
  %.2 = phi i32 [ %.125, %18 ], [ %spec.select, %23 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %28 = load i32, ptr %14, align 8, !tbaa !18
  %29 = sext i32 %28 to i64
  %30 = icmp slt i64 %indvars.iv.next, %29
  br i1 %30, label %18, label %.loopexit.loopexit, !llvm.loop !21

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

.loopexit:                                        ; preds = %.loopexit.loopexit, %.preheader, %13
  %.pre = phi i32 [ %.pre31, %13 ], [ %.pre31, %.preheader ], [ %.pre.pre, %.loopexit.loopexit ]
  %.3 = phi i32 [ %.028, %13 ], [ 0, %.preheader ], [ %.2, %.loopexit.loopexit ]
  %31 = trunc i32 %.3 to i16
  %32 = getelementptr inbounds i8, ptr %.01926, i64 12
  store i16 %31, ptr %32, align 4, !tbaa !20
  br label %33

33:                                               ; preds = %7, %.loopexit
  %.pre32 = phi i32 [ %.pre, %.loopexit ], [ %.pre31, %7 ]
  %34 = phi i32 [ %.pre, %.loopexit ], [ %8, %7 ]
  %.4 = phi i32 [ %.3, %.loopexit ], [ %.028, %7 ]
  %35 = add nuw nsw i32 %.01827, 1
  %36 = getelementptr inbounds i8, ptr %.01926, i64 128
  %37 = icmp slt i32 %35, %34
  br i1 %37, label %7, label %._crit_edge, !llvm.loop !23

._crit_edge:                                      ; preds = %33, %2
  ret void
}

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

attributes #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 = { 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 = { 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, !6, i64 112}
!19 = !{!12, !10, i64 120}
!20 = !{!12, !13, i64 12}
!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