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

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

@strace = external dso_local global %struct.divline_t, align 4
@t2x = external dso_local global i32, align 4
@t2y = external dso_local global i32, align 4
@nodes = external global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @P_DivlineSide(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
declare dso_local i32 @P_CrossSubsector(i32 noundef) #0

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

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

13:                                               ; preds = %10
  %14 = call i32 @P_CrossSubsector(i32 noundef 0)
  store i32 %14, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %60

15:                                               ; preds = %10
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = and i32 %16, -32769
  %18 = call i32 @P_CrossSubsector(i32 noundef %17)
  store i32 %18, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %60

19:                                               ; preds = %1
  %20 = load ptr, ptr @nodes, align 8, !tbaa !9
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds %struct.node_t, ptr %20, i64 %22
  store ptr %23, ptr %4, align 8, !tbaa !9
  %24 = load i32, ptr @strace, align 4, !tbaa !11
  %25 = load i32, ptr getelementptr inbounds (%struct.divline_t, ptr @strace, i32 0, i32 1), align 4, !tbaa !13
  %26 = load ptr, ptr %4, align 8, !tbaa !9
  %27 = call i32 @P_DivlineSide(i32 noundef %24, i32 noundef %25, ptr noundef %26)
  store i32 %27, ptr %5, align 4, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = icmp eq i32 %28, 2
  br i1 %29, label %30, label %31

30:                                               ; preds = %19
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %30, %19
  %32 = load ptr, ptr %4, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.node_t, ptr %32, i32 0, i32 5
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [2 x i16], ptr %33, i64 0, i64 %35
  %37 = load i16, ptr %36, align 2, !tbaa !14
  %38 = zext i16 %37 to i32
  %39 = call i32 @P_CrossBSPNode(i32 noundef %38)
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %42, label %41

41:                                               ; preds = %31
  store i32 0, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %60

42:                                               ; preds = %31
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = load i32, ptr @t2x, align 4, !tbaa !5
  %45 = load i32, ptr @t2y, align 4, !tbaa !5
  %46 = load ptr, ptr %4, align 8, !tbaa !9
  %47 = call i32 @P_DivlineSide(i32 noundef %44, i32 noundef %45, ptr noundef %46)
  %48 = icmp eq i32 %43, %47
  br i1 %48, label %49, label %50

49:                                               ; preds = %42
  store i32 1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %60

50:                                               ; preds = %42
  %51 = load ptr, ptr %4, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.node_t, ptr %51, i32 0, i32 5
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = xor i32 %53, 1
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [2 x i16], ptr %52, i64 0, i64 %55
  %57 = load i16, ptr %56, align 2, !tbaa !14
  %58 = zext i16 %57 to i32
  %59 = call i32 @P_CrossBSPNode(i32 noundef %58)
  store i32 %59, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %60

60:                                               ; preds = %50, %49, %41, %15, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %61 = load i32, ptr %2, align 4
  ret i32 %61
}

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, !6, i64 0}
!12 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!13 = !{!12, !6, i64 4}
!14 = !{!15, !15, i64 0}
!15 = !{!"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.divline_t = type { i32, i32, i32, i32 }
%struct.node_t = type { i32, i32, i32, i32, [2 x [4 x i32]], [2 x i16] }

@strace = external dso_local local_unnamed_addr global %struct.divline_t, align 4
@t2x = external dso_local local_unnamed_addr global i32, align 4
@t2y = external dso_local local_unnamed_addr global i32, align 4
@nodes = external local_unnamed_addr global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @P_CrossBSPNode(i32 noundef %0) local_unnamed_addr #0 {
  %2 = and i32 %0, 32768
  %.not15 = icmp eq i32 %2, 0
  br i1 %.not15, 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.lcssa18 = phi i32 [ %0, %tailrecurse._crit_edge ], [ %27, %tailrecurse ]
  %4 = and i32 %.tr.lcssa18, -32769
  br label %.loopexit.sink.split

.lr.ph:                                           ; preds = %1, %tailrecurse
  %.tr16 = phi i32 [ %27, %tailrecurse ], [ %0, %1 ]
  %5 = load ptr, ptr @nodes, align 8, !tbaa !5
  %6 = sext i32 %.tr16 to i64
  %7 = getelementptr inbounds %struct.node_t, ptr %5, i64 %6
  %8 = load i32, ptr @strace, align 4, !tbaa !9
  %9 = load i32, ptr getelementptr inbounds (i8, ptr @strace, i64 4), align 4, !tbaa !12
  %10 = tail call i32 @P_DivlineSide(i32 noundef %8, i32 noundef %9, ptr noundef %7)
  %11 = icmp eq i32 %10, 2
  %spec.store.select = select i1 %11, i32 0, i32 %10
  %12 = getelementptr inbounds i8, ptr %7, i64 48
  %13 = sext i32 %spec.store.select to i64
  %14 = getelementptr inbounds [2 x i16], ptr %12, i64 0, i64 %13
  %15 = load i16, ptr %14, align 2, !tbaa !13
  %16 = zext i16 %15 to i32
  %17 = tail call i32 @P_CrossBSPNode(i32 noundef %16)
  %.not13 = icmp eq i32 %17, 0
  br i1 %.not13, label %.loopexit, label %18

18:                                               ; preds = %.lr.ph
  %19 = load i32, ptr @t2x, align 4, !tbaa !15
  %20 = load i32, ptr @t2y, align 4, !tbaa !15
  %21 = tail call i32 @P_DivlineSide(i32 noundef %19, i32 noundef %20, ptr noundef nonnull %7)
  %22 = icmp eq i32 %spec.store.select, %21
  br i1 %22, label %.loopexit, label %tailrecurse

tailrecurse:                                      ; preds = %18
  %23 = xor i32 %spec.store.select, 1
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [2 x i16], ptr %12, i64 0, i64 %24
  %26 = load i16, ptr %25, align 2, !tbaa !13
  %27 = zext i16 %26 to i32
  %.not = icmp sgt i16 %26, -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 ]
  %28 = tail call i32 @P_CrossSubsector(i32 noundef %.sink)
  br label %.loopexit

.loopexit:                                        ; preds = %18, %.lr.ph, %.loopexit.sink.split
  %.0 = phi i32 [ %28, %.loopexit.sink.split ], [ 1, %18 ], [ 0, %.lr.ph ]
  ret i32 %.0
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 4}
!13 = !{!14, !14, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!11, !11, i64 0}


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