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

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

@openrange = external dso_local global i32, align 4
@opentop = external dso_local global i32, align 4
@openbottom = external dso_local global i32, align 4
@lowfloor = external dso_local 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 @P_LineOpening(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.line_s, ptr %6, i32 0, i32 7
  %8 = getelementptr inbounds [2 x i16], ptr %7, i64 0, i64 1
  %9 = load i16, ptr %8, align 2, !tbaa !9
  %10 = sext i16 %9 to i32
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  store i32 0, ptr @openrange, align 4, !tbaa !11
  store i32 1, ptr %5, align 4
  br label %61

13:                                               ; preds = %1
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.line_s, ptr %14, i32 0, i32 10
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  store ptr %16, ptr %3, align 8, !tbaa !5
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.line_s, ptr %17, i32 0, i32 11
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  store ptr %19, ptr %4, align 8, !tbaa !5
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.sector_t, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 4, !tbaa !16
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.sector_t, ptr %23, i32 0, i32 1
  %25 = load i32, ptr %24, align 4, !tbaa !16
  %26 = icmp slt i32 %22, %25
  br i1 %26, label %27, label %31

27:                                               ; preds = %13
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.sector_t, ptr %28, i32 0, i32 1
  %30 = load i32, ptr %29, align 4, !tbaa !16
  store i32 %30, ptr @opentop, align 4, !tbaa !11
  br label %35

31:                                               ; preds = %13
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.sector_t, ptr %32, i32 0, i32 1
  %34 = load i32, ptr %33, align 4, !tbaa !16
  store i32 %34, ptr @opentop, align 4, !tbaa !11
  br label %35

35:                                               ; preds = %31, %27
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.sector_t, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !20
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.sector_t, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %40, align 8, !tbaa !20
  %42 = icmp sgt i32 %38, %41
  br i1 %42, label %43, label %50

43:                                               ; preds = %35
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.sector_t, ptr %44, i32 0, i32 0
  %46 = load i32, ptr %45, align 8, !tbaa !20
  store i32 %46, ptr @openbottom, align 4, !tbaa !11
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.sector_t, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !20
  store i32 %49, ptr @lowfloor, align 4, !tbaa !11
  br label %57

50:                                               ; preds = %35
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.sector_t, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !20
  store i32 %53, ptr @openbottom, align 4, !tbaa !11
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.sector_t, ptr %54, i32 0, i32 0
  %56 = load i32, ptr %55, align 8, !tbaa !20
  store i32 %56, ptr @lowfloor, align 4, !tbaa !11
  br label %57

57:                                               ; preds = %50, %43
  %58 = load i32, ptr @opentop, align 4, !tbaa !11
  %59 = load i32, ptr @openbottom, align 4, !tbaa !11
  %60 = sub nsw i32 %58, %59
  store i32 %60, ptr @openrange, align 4, !tbaa !11
  store i32 0, ptr %5, align 4
  br label %61

61:                                               ; preds = %57, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  %62 = load i32, ptr %5, align 4
  switch i32 %62, label %64 [
    i32 0, label %63
    i32 1, label %63
  ]

63:                                               ; preds = %61, %61
  ret void

64:                                               ; preds = %61
  unreachable
}

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 = { 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 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 56}
!14 = !{!"line_s", !6, i64 0, !6, i64 8, !12, i64 16, !12, i64 20, !10, i64 24, !10, i64 26, !10, i64 28, !7, i64 30, !7, i64 36, !12, i64 52, !6, i64 56, !6, i64 64, !12, i64 72, !6, i64 80}
!15 = !{!14, !6, i64 64}
!16 = !{!17, !12, i64 4}
!17 = !{!"", !12, i64 0, !12, i64 4, !10, i64 8, !10, i64 10, !10, i64 12, !10, i64 14, !10, i64 16, !12, i64 20, !6, i64 24, !7, i64 32, !18, i64 48, !12, i64 88, !6, i64 96, !6, i64 104, !12, i64 112, !6, i64 120}
!18 = !{!"", !19, i64 0, !12, i64 24, !12, i64 28, !12, i64 32}
!19 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!20 = !{!17, !12, i64 0}


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

@openrange = external dso_local local_unnamed_addr global i32, align 4
@opentop = external dso_local local_unnamed_addr global i32, align 4
@openbottom = external dso_local local_unnamed_addr global i32, align 4
@lowfloor = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local void @P_LineOpening(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load i16, ptr %2, align 2, !tbaa !5
  %4 = icmp eq i16 %3, -1
  br i1 %4, label %18, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 56
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %0, i64 64
  %9 = load ptr, ptr %8, align 8, !tbaa !13
  %10 = getelementptr inbounds i8, ptr %7, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !14
  %12 = getelementptr inbounds i8, ptr %9, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !14
  %. = tail call i32 @llvm.smin.i32(i32 %11, i32 %13)
  store i32 %., ptr @opentop, align 4, !tbaa !18
  %14 = load i32, ptr %7, align 8, !tbaa !19
  %15 = load i32, ptr %9, align 8, !tbaa !19
  %16 = icmp sgt i32 %14, %15
  %storemerge17 = tail call i32 @llvm.smax.i32(i32 %14, i32 %15)
  store i32 %storemerge17, ptr @openbottom, align 4, !tbaa !18
  %.val = load i32, ptr %9, align 8
  %.val19 = load i32, ptr %7, align 8
  %storemerge16 = select i1 %16, i32 %.val, i32 %.val19
  store i32 %storemerge16, ptr @lowfloor, align 4, !tbaa !18
  %17 = sub nsw i32 %., %storemerge17
  br label %18

18:                                               ; preds = %1, %5
  %storemerge18 = phi i32 [ %17, %5 ], [ 0, %1 ]
  store i32 %storemerge18, ptr @openrange, align 4, !tbaa !18
  ret void
}

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

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: read, 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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 56}
!10 = !{!"line_s", !11, i64 0, !11, i64 8, !12, i64 16, !12, i64 20, !6, i64 24, !6, i64 26, !6, i64 28, !7, i64 30, !7, i64 36, !12, i64 52, !11, i64 56, !11, i64 64, !12, i64 72, !11, i64 80}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !11, i64 64}
!14 = !{!15, !12, i64 4}
!15 = !{!"", !12, i64 0, !12, i64 4, !6, i64 8, !6, i64 10, !6, i64 12, !6, i64 14, !6, i64 16, !12, i64 20, !11, i64 24, !7, i64 32, !16, i64 48, !12, i64 88, !11, i64 96, !11, i64 104, !12, i64 112, !11, i64 120}
!16 = !{!"", !17, i64 0, !12, i64 24, !12, i64 28, !12, i64 32}
!17 = !{!"thinker_s", !11, i64 0, !11, i64 8, !7, i64 16}
!18 = !{!12, !12, i64 0}
!19 = !{!15, !12, i64 0}


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