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

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

@viewx = external global i32, align 4
@viewy = external global i32, align 4
@nodes = external global ptr, align 8

; 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
declare dso_local i32 @R_CheckBBox(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @R_Subsector(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @R_RenderBSPNode(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = and i32 %6, 32768
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %17

9:                                                ; preds = %1
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  call void @R_Subsector(i32 noundef 0)
  br label %16

13:                                               ; preds = %9
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = and i32 %14, -32769
  call void @R_Subsector(i32 noundef %15)
  br label %16

16:                                               ; preds = %13, %12
  store i32 1, ptr %5, align 4
  br label %52

17:                                               ; preds = %1
  %18 = load ptr, ptr @nodes, align 8, !tbaa !9
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds %struct.node_t, ptr %18, i64 %20
  store ptr %21, ptr %3, align 8, !tbaa !9
  %22 = load i32, ptr @viewx, align 4, !tbaa !5
  %23 = load i32, ptr @viewy, align 4, !tbaa !5
  %24 = load ptr, ptr %3, align 8, !tbaa !9
  %25 = call i32 @R_PointOnSide(i32 noundef %22, i32 noundef %23, ptr noundef %24)
  store i32 %25, ptr %4, align 4, !tbaa !5
  %26 = load ptr, ptr %3, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.node_t, ptr %26, i32 0, i32 5
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [2 x i16], ptr %27, i64 0, i64 %29
  %31 = load i16, ptr %30, align 2, !tbaa !11
  %32 = zext i16 %31 to i32
  call void @R_RenderBSPNode(i32 noundef %32)
  %33 = load ptr, ptr %3, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.node_t, ptr %33, i32 0, i32 4
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = xor i32 %35, 1
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [2 x [4 x i32]], ptr %34, i64 0, i64 %37
  %39 = getelementptr inbounds [4 x i32], ptr %38, i64 0, i64 0
  %40 = call i32 @R_CheckBBox(ptr noundef %39)
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %51

42:                                               ; preds = %17
  %43 = load ptr, ptr %3, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.node_t, ptr %43, i32 0, i32 5
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = xor i32 %45, 1
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [2 x i16], ptr %44, i64 0, i64 %47
  %49 = load i16, ptr %48, align 2, !tbaa !11
  %50 = zext i16 %49 to i32
  call void @R_RenderBSPNode(i32 noundef %50)
  br label %51

51:                                               ; preds = %42, %17
  store i32 0, ptr %5, align 4
  br label %52

52:                                               ; preds = %51, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %53 = load i32, ptr %5, align 4
  switch i32 %53, label %55 [
    i32 0, label %54
    i32 1, label %54
  ]

54:                                               ; preds = %52, %52
  ret void

55:                                               ; preds = %52
  unreachable
}

declare i32 @R_PointOnSide(i32 noundef, i32 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 = !{!"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, !12, i64 0}
!12 = !{!"short", !7, 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"

%struct.node_t = type { i32, i32, i32, i32, [2 x [4 x i32]], [2 x i16] }

@viewx = external local_unnamed_addr global i32, align 4
@viewy = external local_unnamed_addr global i32, align 4
@nodes = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @R_CheckBBox(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @R_Subsector(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @R_RenderBSPNode(i32 noundef %0) local_unnamed_addr #0 {
  %2 = and i32 %0, 32768
  %.not12 = icmp eq i32 %2, 0
  br i1 %.not12, label %.lr.ph, label %tailrecurse._crit_edge

tailrecurse._crit_edge:                           ; preds = %1
  %3 = icmp eq i32 %0, -1
  br i1 %3, label %.loopexit.sink.split, label %tailrecurse._crit_edge.thread

tailrecurse._crit_edge.thread:                    ; preds = %tailrecurse, %tailrecurse._crit_edge
  %.tr.lcssa15 = phi i32 [ %0, %tailrecurse._crit_edge ], [ %23, %tailrecurse ]
  %4 = and i32 %.tr.lcssa15, -32769
  br label %.loopexit.sink.split

.lr.ph:                                           ; preds = %1, %tailrecurse
  %.tr13 = phi i32 [ %23, %tailrecurse ], [ %0, %1 ]
  %5 = load ptr, ptr @nodes, align 8, !tbaa !5
  %6 = sext i32 %.tr13 to i64
  %7 = getelementptr inbounds %struct.node_t, ptr %5, i64 %6
  %8 = load i32, ptr @viewx, align 4, !tbaa !9
  %9 = load i32, ptr @viewy, align 4, !tbaa !9
  %10 = tail call i32 @R_PointOnSide(i32 noundef %8, i32 noundef %9, ptr noundef %7) #2
  %11 = getelementptr inbounds i8, ptr %7, i64 48
  %12 = sext i32 %10 to i64
  %13 = getelementptr inbounds [2 x i16], ptr %11, i64 0, i64 %12
  %14 = load i16, ptr %13, align 2, !tbaa !11
  %15 = zext i16 %14 to i32
  tail call void @R_RenderBSPNode(i32 noundef %15)
  %16 = getelementptr inbounds i8, ptr %7, i64 16
  %17 = xor i32 %10, 1
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [2 x [4 x i32]], ptr %16, i64 0, i64 %18
  %20 = tail call i32 @R_CheckBBox(ptr noundef nonnull %19)
  %.not10 = icmp eq i32 %20, 0
  br i1 %.not10, label %.loopexit, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph
  %21 = getelementptr inbounds [2 x i16], ptr %11, i64 0, i64 %18
  %22 = load i16, ptr %21, align 2, !tbaa !11
  %23 = zext i16 %22 to i32
  %.not = icmp sgt i16 %22, -1
  br i1 %.not, label %.lr.ph, label %tailrecurse._crit_edge.thread

.loopexit.sink.split:                             ; preds = %tailrecurse._crit_edge, %tailrecurse._crit_edge.thread
  %.sink = phi i32 [ %4, %tailrecurse._crit_edge.thread ], [ 0, %tailrecurse._crit_edge ]
  tail call void @R_Subsector(i32 noundef %.sink)
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %.loopexit.sink.split
  ret void
}

declare i32 @R_PointOnSide(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

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 = { 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, !12, i64 0}
!12 = !{!"short", !7, i64 0}


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