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

; 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.visplane_t = type { i32, i32, i32, i32, i32, i16, [320 x i16], i16, i16, [320 x i16], i16 }

@visplanes = external dso_local global [128 x %struct.visplane_t], align 16
@lastvisplane = external dso_local global ptr, align 8
@skyflatnum = external global i32, align 4
@.str.1 = external hidden unnamed_addr constant [31 x i8], align 1

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

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) #1

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @R_FindPlane(i32 noundef %0, i32 noundef %1, i32 noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = load i32, ptr @skyflatnum, align 4, !tbaa !5
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %3
  store i32 0, ptr %5, align 4, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %13, %3
  store ptr @visplanes, ptr %8, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %39, %14
  %16 = load ptr, ptr %8, align 8, !tbaa !9
  %17 = load ptr, ptr @lastvisplane, align 8, !tbaa !9
  %18 = icmp ult ptr %16, %17
  br i1 %18, label %19, label %42

19:                                               ; preds = %15
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = load ptr, ptr %8, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.visplane_t, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = icmp eq i32 %20, %23
  br i1 %24, label %25, label %38

25:                                               ; preds = %19
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = load ptr, ptr %8, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.visplane_t, ptr %27, i32 0, i32 1
  %29 = load i32, ptr %28, align 4, !tbaa !14
  %30 = icmp eq i32 %26, %29
  br i1 %30, label %31, label %38

31:                                               ; preds = %25
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = load ptr, ptr %8, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.visplane_t, ptr %33, i32 0, i32 2
  %35 = load i32, ptr %34, align 4, !tbaa !15
  %36 = icmp eq i32 %32, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %31
  br label %42

38:                                               ; preds = %31, %25, %19
  br label %39

39:                                               ; preds = %38
  %40 = load ptr, ptr %8, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.visplane_t, ptr %40, i32 1
  store ptr %41, ptr %8, align 8, !tbaa !9
  br label %15, !llvm.loop !16

42:                                               ; preds = %37, %15
  %43 = load ptr, ptr %8, align 8, !tbaa !9
  %44 = load ptr, ptr @lastvisplane, align 8, !tbaa !9
  %45 = icmp ult ptr %43, %44
  br i1 %45, label %46, label %48

46:                                               ; preds = %42
  %47 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %47, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %75

48:                                               ; preds = %42
  %49 = load ptr, ptr @lastvisplane, align 8, !tbaa !9
  %50 = ptrtoint ptr %49 to i64
  %51 = sub i64 %50, ptrtoint (ptr @visplanes to i64)
  %52 = sdiv exact i64 %51, 1308
  %53 = icmp eq i64 %52, 128
  br i1 %53, label %54, label %55

54:                                               ; preds = %48
  call void (ptr, ...) @I_Error(ptr noundef @.str.1) #5
  unreachable

55:                                               ; preds = %48
  %56 = load ptr, ptr @lastvisplane, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.visplane_t, ptr %56, i32 1
  store ptr %57, ptr @lastvisplane, align 8, !tbaa !9
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = load ptr, ptr %8, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.visplane_t, ptr %59, i32 0, i32 0
  store i32 %58, ptr %60, align 4, !tbaa !11
  %61 = load i32, ptr %6, align 4, !tbaa !5
  %62 = load ptr, ptr %8, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.visplane_t, ptr %62, i32 0, i32 1
  store i32 %61, ptr %63, align 4, !tbaa !14
  %64 = load i32, ptr %7, align 4, !tbaa !5
  %65 = load ptr, ptr %8, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.visplane_t, ptr %65, i32 0, i32 2
  store i32 %64, ptr %66, align 4, !tbaa !15
  %67 = load ptr, ptr %8, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.visplane_t, ptr %67, i32 0, i32 3
  store i32 320, ptr %68, align 4, !tbaa !18
  %69 = load ptr, ptr %8, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.visplane_t, ptr %69, i32 0, i32 4
  store i32 -1, ptr %70, align 4, !tbaa !19
  %71 = load ptr, ptr %8, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.visplane_t, ptr %71, i32 0, i32 6
  %73 = getelementptr inbounds [320 x i16], ptr %72, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 2 %73, i8 -1, i64 640, i1 false)
  %74 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %74, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %75

75:                                               ; preds = %55, %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %76 = load ptr, ptr %4, align 8
  ret ptr %76
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noreturn "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 nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #4 = { nounwind }
attributes #5 = { noreturn }

!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, !6, i64 0}
!12 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !13, i64 20, !7, i64 22, !13, i64 662, !13, i64 664, !7, i64 666, !13, i64 1306}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !6, i64 4}
!15 = !{!12, !6, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !6, i64 12}
!19 = !{!12, !6, i64 16}


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

%struct.visplane_t = type { i32, i32, i32, i32, i32, i16, [320 x i16], i16, i16, [320 x i16], i16 }

@visplanes = external dso_local global [128 x %struct.visplane_t], align 16
@lastvisplane = external dso_local local_unnamed_addr global ptr, align 8
@skyflatnum = external local_unnamed_addr global i32, align 4
@.str.1 = external hidden unnamed_addr constant [31 x i8], align 1

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local nonnull ptr @R_FindPlane(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = load i32, ptr @skyflatnum, align 4, !tbaa !5
  %5 = icmp eq i32 %4, %1
  %spec.select = select i1 %5, i32 0, i32 %0
  %spec.select25 = select i1 %5, i32 0, i32 %2
  %6 = load ptr, ptr @lastvisplane, align 8, !tbaa !9
  %7 = icmp ugt ptr %6, @visplanes
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %18
  %.029 = phi ptr [ %19, %18 ], [ @visplanes, %3 ]
  %8 = load i32, ptr %.029, align 4, !tbaa !11
  %9 = icmp eq i32 %spec.select, %8
  br i1 %9, label %10, label %18

10:                                               ; preds = %.lr.ph
  %11 = getelementptr inbounds i8, ptr %.029, i64 4
  %12 = load i32, ptr %11, align 4, !tbaa !14
  %13 = icmp eq i32 %12, %1
  br i1 %13, label %14, label %18

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %.029, i64 8
  %16 = load i32, ptr %15, align 4, !tbaa !15
  %17 = icmp eq i32 %spec.select25, %16
  br i1 %17, label %.thread, label %18

18:                                               ; preds = %.lr.ph, %10, %14
  %19 = getelementptr inbounds i8, ptr %.029, i64 1308
  %20 = icmp ult ptr %19, %6
  br i1 %20, label %.lr.ph, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %18, %3
  %.0.lcssa = phi ptr [ @visplanes, %3 ], [ %19, %18 ]
  %21 = ptrtoint ptr %6 to i64
  %22 = sub i64 %21, ptrtoint (ptr @visplanes to i64)
  %23 = icmp eq i64 %22, 167424
  br i1 %23, label %24, label %25

24:                                               ; preds = %._crit_edge
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.1) #3
  unreachable

25:                                               ; preds = %._crit_edge
  %26 = getelementptr inbounds i8, ptr %6, i64 1308
  store ptr %26, ptr @lastvisplane, align 8, !tbaa !9
  store i32 %spec.select, ptr %.0.lcssa, align 4, !tbaa !11
  %27 = getelementptr inbounds i8, ptr %.0.lcssa, i64 4
  store i32 %1, ptr %27, align 4, !tbaa !14
  %28 = getelementptr inbounds i8, ptr %.0.lcssa, i64 8
  store i32 %spec.select25, ptr %28, align 4, !tbaa !15
  %29 = getelementptr inbounds i8, ptr %.0.lcssa, i64 12
  store i32 320, ptr %29, align 4, !tbaa !18
  %30 = getelementptr inbounds i8, ptr %.0.lcssa, i64 16
  store i32 -1, ptr %30, align 4, !tbaa !19
  %31 = getelementptr inbounds i8, ptr %.0.lcssa, i64 22
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(640) %31, i8 -1, i64 640, i1 false)
  br label %.thread

.thread:                                          ; preds = %14, %25
  %.028 = phi ptr [ %.0.lcssa, %25 ], [ %.029, %14 ]
  ret ptr %.028
}

attributes #0 = { noreturn "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { noreturn 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, !6, i64 0}
!12 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !13, i64 20, !7, i64 22, !13, i64 662, !13, i64 664, !7, i64 666, !13, i64 1306}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !6, i64 4}
!15 = !{!12, !6, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !6, i64 12}
!19 = !{!12, !6, i64 16}


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