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

; 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.ceiling_t = type { %struct.thinker_s, i32, ptr, i32, i32, i32, i32, 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 }

@activeceilings = external dso_local global [30 x ptr], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @T_MoveCeiling(ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local void @P_ActivateInStasisCeiling(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %4

4:                                                ; preds = %52, %1
  %5 = load i32, ptr %3, align 4, !tbaa !9
  %6 = icmp slt i32 %5, 30
  br i1 %6, label %7, label %55

7:                                                ; preds = %4
  %8 = load i32, ptr %3, align 4, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [30 x ptr], ptr @activeceilings, i64 0, i64 %9
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %51

13:                                               ; preds = %7
  %14 = load i32, ptr %3, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [30 x ptr], ptr @activeceilings, i64 0, i64 %15
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.ceiling_t, ptr %17, i32 0, i32 8
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.line_s, ptr %20, i32 0, i32 6
  %22 = load i16, ptr %21, align 4, !tbaa !14
  %23 = sext i16 %22 to i32
  %24 = icmp eq i32 %19, %23
  br i1 %24, label %25, label %51

25:                                               ; preds = %13
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [30 x ptr], ptr @activeceilings, i64 0, i64 %27
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.ceiling_t, ptr %29, i32 0, i32 7
  %31 = load i32, ptr %30, align 8, !tbaa !17
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %33, label %51

33:                                               ; preds = %25
  %34 = load i32, ptr %3, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [30 x ptr], ptr @activeceilings, i64 0, i64 %35
  %37 = load ptr, ptr %36, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.ceiling_t, ptr %37, i32 0, i32 9
  %39 = load i32, ptr %38, align 8, !tbaa !18
  %40 = load i32, ptr %3, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [30 x ptr], ptr @activeceilings, i64 0, i64 %41
  %43 = load ptr, ptr %42, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.ceiling_t, ptr %43, i32 0, i32 7
  store i32 %39, ptr %44, align 8, !tbaa !17
  %45 = load i32, ptr %3, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [30 x ptr], ptr @activeceilings, i64 0, i64 %46
  %48 = load ptr, ptr %47, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.ceiling_t, ptr %48, i32 0, i32 0
  %50 = getelementptr inbounds %struct.thinker_s, ptr %49, i32 0, i32 2
  store ptr @T_MoveCeiling, ptr %50, align 8, !tbaa !19
  br label %51

51:                                               ; preds = %33, %25, %13, %7
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %3, align 4, !tbaa !9
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %3, align 4, !tbaa !9
  br label %4, !llvm.loop !20

55:                                               ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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, !10, i64 60}
!12 = !{!"", !13, i64 0, !10, i64 24, !6, i64 32, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64}
!13 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!14 = !{!15, !16, i64 28}
!15 = !{!"line_s", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !16, i64 24, !16, i64 26, !16, i64 28, !7, i64 30, !7, i64 36, !10, i64 52, !6, i64 56, !6, i64 64, !10, i64 72, !6, i64 80}
!16 = !{!"short", !7, i64 0}
!17 = !{!12, !10, i64 56}
!18 = !{!12, !10, i64 64}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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

@activeceilings = external dso_local local_unnamed_addr global [30 x ptr], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @T_MoveCeiling(ptr noundef) #0

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @P_ActivateInStasisCeiling(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 28
  br label %3

3:                                                ; preds = %1, %20
  %indvars.iv = phi i64 [ 0, %1 ], [ %indvars.iv.next, %20 ]
  %4 = getelementptr inbounds [30 x ptr], ptr @activeceilings, i64 0, i64 %indvars.iv
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %20, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %5, i64 60
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = load i16, ptr %2, align 4, !tbaa !13
  %10 = sext i16 %9 to i32
  %11 = icmp eq i32 %8, %10
  br i1 %11, label %12, label %20

12:                                               ; preds = %6
  %13 = getelementptr inbounds i8, ptr %5, i64 56
  %14 = load i32, ptr %13, align 8, !tbaa !16
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %20

16:                                               ; preds = %12
  %17 = getelementptr inbounds i8, ptr %5, i64 64
  %18 = load i32, ptr %17, align 8, !tbaa !17
  store i32 %18, ptr %13, align 8, !tbaa !16
  %19 = getelementptr inbounds i8, ptr %5, i64 16
  store ptr @T_MoveCeiling, ptr %19, align 8, !tbaa !18
  br label %20

20:                                               ; preds = %3, %6, %12, %16
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 30
  br i1 %exitcond.not, label %21, label %3, !llvm.loop !19

21:                                               ; preds = %20
  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" }
attributes #1 = { nofree norecurse nosync nounwind 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 60}
!10 = !{!"", !11, i64 0, !12, i64 24, !6, i64 32, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52, !12, i64 56, !12, i64 60, !12, i64 64}
!11 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !15, i64 28}
!14 = !{!"line_s", !6, i64 0, !6, i64 8, !12, i64 16, !12, i64 20, !15, i64 24, !15, i64 26, !15, i64 28, !7, i64 30, !7, i64 36, !12, i64 52, !6, i64 56, !6, i64 64, !12, i64 72, !6, i64 80}
!15 = !{!"short", !7, i64 0}
!16 = !{!10, !12, i64 56}
!17 = !{!10, !12, i64 64}
!18 = !{!7, !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass