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

; 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] }
%struct.subsector_s = type { ptr, i16, i16 }

@numnodes = external global i32, align 4
@subsectors = external global ptr, align 8
@nodes = external global ptr, align 8

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

12:                                               ; preds = %2
  %13 = load ptr, ptr @subsectors, align 8, !tbaa !9
  store ptr %13, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %44

14:                                               ; preds = %2
  %15 = load i32, ptr @numnodes, align 4, !tbaa !5
  %16 = sub nsw i32 %15, 1
  store i32 %16, ptr %8, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %22, %14
  %18 = load i32, ptr %8, align 4, !tbaa !5
  %19 = and i32 %18, 32768
  %20 = icmp ne i32 %19, 0
  %21 = xor i1 %20, true
  br i1 %21, label %22, label %38

22:                                               ; preds = %17
  %23 = load ptr, ptr @nodes, align 8, !tbaa !9
  %24 = load i32, ptr %8, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds %struct.node_t, ptr %23, i64 %25
  store ptr %26, ptr %6, align 8, !tbaa !9
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = load ptr, ptr %6, align 8, !tbaa !9
  %30 = call i32 @R_PointOnSide(i32 noundef %27, i32 noundef %28, ptr noundef %29)
  store i32 %30, ptr %7, align 4, !tbaa !5
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.node_t, ptr %31, i32 0, i32 5
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [2 x i16], ptr %32, i64 0, i64 %34
  %36 = load i16, ptr %35, align 2, !tbaa !11
  %37 = zext i16 %36 to i32
  store i32 %37, ptr %8, align 4, !tbaa !5
  br label %17, !llvm.loop !13

38:                                               ; preds = %17
  %39 = load ptr, ptr @subsectors, align 8, !tbaa !9
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = and i32 %40, -32769
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds %struct.subsector_s, ptr %39, i64 %42
  store ptr %43, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %44

44:                                               ; preds = %38, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %45 = load ptr, ptr %3, align 8
  ret ptr %45
}

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 = !{!"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}
!13 = distinct !{!13, !14}
!14 = !{!"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"

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

@numnodes = external local_unnamed_addr global i32, align 4
@subsectors = external local_unnamed_addr global ptr, align 8
@nodes = external local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
define dso_local ptr @R_PointInSubsector(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @numnodes, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %6

4:                                                ; preds = %2
  %5 = load ptr, ptr @subsectors, align 8, !tbaa !9
  br label %22

6:                                                ; preds = %2
  %7 = add nsw i32 %3, -1
  %8 = and i32 %7, 32768
  %.not1011 = icmp eq i32 %8, 0
  br i1 %.not1011, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %6, %.lr.ph
  %.012 = phi i32 [ %17, %.lr.ph ], [ %7, %6 ]
  %9 = load ptr, ptr @nodes, align 8, !tbaa !9
  %10 = sext i32 %.012 to i64
  %11 = getelementptr inbounds %struct.node_t, ptr %9, i64 %10
  %12 = tail call i32 @R_PointOnSide(i32 noundef %0, i32 noundef %1, ptr noundef %11)
  %13 = getelementptr inbounds i8, ptr %11, i64 48
  %14 = sext i32 %12 to i64
  %15 = getelementptr inbounds [2 x i16], ptr %13, i64 0, i64 %14
  %16 = load i16, ptr %15, align 2, !tbaa !11
  %17 = zext i16 %16 to i32
  %.not10 = icmp sgt i16 %16, -1
  br i1 %.not10, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %6
  %.0.lcssa = phi i32 [ %7, %6 ], [ %17, %.lr.ph ]
  %18 = load ptr, ptr @subsectors, align 8, !tbaa !9
  %19 = and i32 %.0.lcssa, -32769
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds %struct.subsector_s, ptr %18, i64 %20
  br label %22

22:                                               ; preds = %._crit_edge, %4
  %.08 = phi ptr [ %21, %._crit_edge ], [ %5, %4 ]
  ret ptr %.08
}

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

!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}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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