; 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.mobj_s = type { %struct.thinker_s, i32, i32, i32, ptr, ptr, i32, i32, i32, ptr, ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32, ptr, i32, ptr, i32, i32, i32, i32, ptr, i32, i32, ptr, i32, %struct.mapthing_t, ptr }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }
%struct.mapthing_t = type { i16, i16, i16, i16, i16 }
%struct.subsector_s = type { ptr, i16, i16 }

@validcount = external global i32, align 4
@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
@sightzstart = external dso_local global i32, align 4
@bottomslope = external dso_local global i32, align 4
@topslope = external dso_local global i32, align 4
@sectors = external global ptr, align 8
@numsectors = external global i32, align 4
@rejectmatrix = external global ptr, align 8
@sightcounts = external dso_local global [2 x i32], align 4
@numnodes = external 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
declare dso_local i32 @P_CrossBSPNode(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @P_CheckSight(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.mobj_s, ptr %12, i32 0, i32 11
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.subsector_s, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  %17 = load ptr, ptr @sectors, align 8, !tbaa !5
  %18 = ptrtoint ptr %16 to i64
  %19 = ptrtoint ptr %17 to i64
  %20 = sub i64 %18, %19
  %21 = sdiv exact i64 %20, 128
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %6, align 4, !tbaa !17
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mobj_s, ptr %23, i32 0, i32 11
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.subsector_s, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  %28 = load ptr, ptr @sectors, align 8, !tbaa !5
  %29 = ptrtoint ptr %27 to i64
  %30 = ptrtoint ptr %28 to i64
  %31 = sub i64 %29, %30
  %32 = sdiv exact i64 %31, 128
  %33 = trunc i64 %32 to i32
  store i32 %33, ptr %7, align 4, !tbaa !17
  %34 = load i32, ptr %6, align 4, !tbaa !17
  %35 = load i32, ptr @numsectors, align 4, !tbaa !17
  %36 = mul nsw i32 %34, %35
  %37 = load i32, ptr %7, align 4, !tbaa !17
  %38 = add nsw i32 %36, %37
  store i32 %38, ptr %8, align 4, !tbaa !17
  %39 = load i32, ptr %8, align 4, !tbaa !17
  %40 = ashr i32 %39, 3
  store i32 %40, ptr %9, align 4, !tbaa !17
  %41 = load i32, ptr %8, align 4, !tbaa !17
  %42 = and i32 %41, 7
  %43 = shl i32 1, %42
  store i32 %43, ptr %10, align 4, !tbaa !17
  %44 = load ptr, ptr @rejectmatrix, align 8, !tbaa !5
  %45 = load i32, ptr %9, align 4, !tbaa !17
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %44, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !18
  %49 = zext i8 %48 to i32
  %50 = load i32, ptr %10, align 4, !tbaa !17
  %51 = and i32 %49, %50
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %53, label %56

53:                                               ; preds = %2
  %54 = load i32, ptr @sightcounts, align 4, !tbaa !17
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr @sightcounts, align 4, !tbaa !17
  store i32 0, ptr %3, align 4
  store i32 1, ptr %11, align 4
  br label %116

56:                                               ; preds = %2
  %57 = load i32, ptr getelementptr inbounds ([2 x i32], ptr @sightcounts, i64 0, i64 1), align 4, !tbaa !17
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr getelementptr inbounds ([2 x i32], ptr @sightcounts, i64 0, i64 1), align 4, !tbaa !17
  %59 = load i32, ptr @validcount, align 4, !tbaa !17
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr @validcount, align 4, !tbaa !17
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.mobj_s, ptr %61, i32 0, i32 3
  %63 = load i32, ptr %62, align 8, !tbaa !19
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.mobj_s, ptr %64, i32 0, i32 15
  %66 = load i32, ptr %65, align 4, !tbaa !20
  %67 = add nsw i32 %63, %66
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.mobj_s, ptr %68, i32 0, i32 15
  %70 = load i32, ptr %69, align 4, !tbaa !20
  %71 = ashr i32 %70, 2
  %72 = sub nsw i32 %67, %71
  store i32 %72, ptr @sightzstart, align 4, !tbaa !17
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.mobj_s, ptr %73, i32 0, i32 3
  %75 = load i32, ptr %74, align 8, !tbaa !19
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.mobj_s, ptr %76, i32 0, i32 15
  %78 = load i32, ptr %77, align 4, !tbaa !20
  %79 = add nsw i32 %75, %78
  %80 = load i32, ptr @sightzstart, align 4, !tbaa !17
  %81 = sub nsw i32 %79, %80
  store i32 %81, ptr @topslope, align 4, !tbaa !17
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.mobj_s, ptr %82, i32 0, i32 3
  %84 = load i32, ptr %83, align 8, !tbaa !19
  %85 = load i32, ptr @sightzstart, align 4, !tbaa !17
  %86 = sub nsw i32 %84, %85
  store i32 %86, ptr @bottomslope, align 4, !tbaa !17
  %87 = load ptr, ptr %4, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.mobj_s, ptr %87, i32 0, i32 1
  %89 = load i32, ptr %88, align 8, !tbaa !21
  store i32 %89, ptr @strace, align 4, !tbaa !22
  %90 = load ptr, ptr %4, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.mobj_s, ptr %90, i32 0, i32 2
  %92 = load i32, ptr %91, align 4, !tbaa !24
  store i32 %92, ptr getelementptr inbounds (%struct.divline_t, ptr @strace, i32 0, i32 1), align 4, !tbaa !25
  %93 = load ptr, ptr %5, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.mobj_s, ptr %93, i32 0, i32 1
  %95 = load i32, ptr %94, align 8, !tbaa !21
  store i32 %95, ptr @t2x, align 4, !tbaa !17
  %96 = load ptr, ptr %5, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.mobj_s, ptr %96, i32 0, i32 2
  %98 = load i32, ptr %97, align 4, !tbaa !24
  store i32 %98, ptr @t2y, align 4, !tbaa !17
  %99 = load ptr, ptr %5, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.mobj_s, ptr %99, i32 0, i32 1
  %101 = load i32, ptr %100, align 8, !tbaa !21
  %102 = load ptr, ptr %4, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.mobj_s, ptr %102, i32 0, i32 1
  %104 = load i32, ptr %103, align 8, !tbaa !21
  %105 = sub nsw i32 %101, %104
  store i32 %105, ptr getelementptr inbounds (%struct.divline_t, ptr @strace, i32 0, i32 2), align 4, !tbaa !26
  %106 = load ptr, ptr %5, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.mobj_s, ptr %106, i32 0, i32 2
  %108 = load i32, ptr %107, align 4, !tbaa !24
  %109 = load ptr, ptr %4, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.mobj_s, ptr %109, i32 0, i32 2
  %111 = load i32, ptr %110, align 4, !tbaa !24
  %112 = sub nsw i32 %108, %111
  store i32 %112, ptr getelementptr inbounds (%struct.divline_t, ptr @strace, i32 0, i32 3), align 4, !tbaa !27
  %113 = load i32, ptr @numnodes, align 4, !tbaa !17
  %114 = sub nsw i32 %113, 1
  %115 = call i32 @P_CrossBSPNode(i32 noundef %114)
  store i32 %115, ptr %3, align 4
  store i32 1, ptr %11, align 4
  br label %116

116:                                              ; preds = %56, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  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 4, ptr %6) #2
  %117 = load i32, ptr %3, align 4
  ret i32 %117
}

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, !6, i64 88}
!10 = !{!"mobj_s", !11, i64 0, !12, i64 24, !12, i64 28, !12, i64 32, !6, i64 40, !6, i64 48, !12, i64 56, !12, i64 60, !12, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !12, i64 96, !12, i64 100, !12, i64 104, !12, i64 108, !12, i64 112, !12, i64 116, !12, i64 120, !12, i64 124, !12, i64 128, !6, i64 136, !12, i64 144, !6, i64 152, !12, i64 160, !12, i64 164, !12, i64 168, !12, i64 172, !6, i64 176, !12, i64 184, !12, i64 188, !6, i64 192, !12, i64 200, !13, i64 204, !6, i64 216}
!11 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !14, i64 0, !14, i64 2, !14, i64 4, !14, i64 6, !14, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!16, !6, i64 0}
!16 = !{!"subsector_s", !6, i64 0, !14, i64 8, !14, i64 10}
!17 = !{!12, !12, i64 0}
!18 = !{!7, !7, i64 0}
!19 = !{!10, !12, i64 32}
!20 = !{!10, !12, i64 108}
!21 = !{!10, !12, i64 24}
!22 = !{!23, !12, i64 0}
!23 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12}
!24 = !{!10, !12, i64 28}
!25 = !{!23, !12, i64 4}
!26 = !{!23, !12, i64 8}
!27 = !{!23, !12, i64 12}


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

@validcount = external local_unnamed_addr global i32, align 4
@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
@sightzstart = external dso_local local_unnamed_addr global i32, align 4
@bottomslope = external dso_local local_unnamed_addr global i32, align 4
@topslope = external dso_local local_unnamed_addr global i32, align 4
@sectors = external local_unnamed_addr global ptr, align 8
@numsectors = external local_unnamed_addr global i32, align 4
@rejectmatrix = external local_unnamed_addr global ptr, align 8
@sightcounts = external dso_local local_unnamed_addr global [2 x i32], align 4
@numnodes = external local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local i32 @P_CheckSight(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 88
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = load ptr, ptr %4, align 8, !tbaa !14
  %6 = load ptr, ptr @sectors, align 8, !tbaa !16
  %7 = ptrtoint ptr %5 to i64
  %8 = ptrtoint ptr %6 to i64
  %9 = sub i64 %7, %8
  %10 = lshr exact i64 %9, 7
  %11 = trunc i64 %10 to i32
  %12 = getelementptr inbounds i8, ptr %1, i64 88
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %15 = ptrtoint ptr %14 to i64
  %16 = sub i64 %15, %8
  %17 = lshr exact i64 %16, 7
  %18 = trunc i64 %17 to i32
  %19 = load i32, ptr @numsectors, align 4, !tbaa !17
  %20 = mul nsw i32 %19, %11
  %21 = add nsw i32 %20, %18
  %22 = ashr i32 %21, 3
  %23 = and i32 %21, 7
  %24 = shl nuw nsw i32 1, %23
  %25 = load ptr, ptr @rejectmatrix, align 8, !tbaa !16
  %26 = sext i32 %22 to i64
  %27 = getelementptr inbounds i8, ptr %25, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !18
  %29 = zext i8 %28 to i32
  %30 = and i32 %24, %29
  %.not = icmp eq i32 %30, 0
  br i1 %.not, label %34, label %31

31:                                               ; preds = %2
  %32 = load i32, ptr @sightcounts, align 4, !tbaa !17
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr @sightcounts, align 4, !tbaa !17
  br label %65

34:                                               ; preds = %2
  %35 = load i32, ptr getelementptr inbounds (i8, ptr @sightcounts, i64 4), align 4, !tbaa !17
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr getelementptr inbounds (i8, ptr @sightcounts, i64 4), align 4, !tbaa !17
  %37 = load i32, ptr @validcount, align 4, !tbaa !17
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr @validcount, align 4, !tbaa !17
  %39 = getelementptr inbounds i8, ptr %0, i64 32
  %40 = load i32, ptr %39, align 8, !tbaa !19
  %41 = getelementptr inbounds i8, ptr %0, i64 108
  %42 = load i32, ptr %41, align 4, !tbaa !20
  %43 = add nsw i32 %42, %40
  %44 = ashr i32 %42, 2
  %45 = sub i32 %43, %44
  store i32 %45, ptr @sightzstart, align 4, !tbaa !17
  %46 = getelementptr inbounds i8, ptr %1, i64 32
  %47 = load i32, ptr %46, align 8, !tbaa !19
  %48 = getelementptr inbounds i8, ptr %1, i64 108
  %49 = load i32, ptr %48, align 4, !tbaa !20
  %50 = sub i32 %47, %45
  %51 = add i32 %50, %49
  store i32 %51, ptr @topslope, align 4, !tbaa !17
  store i32 %50, ptr @bottomslope, align 4, !tbaa !17
  %52 = getelementptr inbounds i8, ptr %0, i64 24
  %53 = load i32, ptr %52, align 8, !tbaa !21
  store i32 %53, ptr @strace, align 4, !tbaa !22
  %54 = getelementptr inbounds i8, ptr %0, i64 28
  %55 = load i32, ptr %54, align 4, !tbaa !24
  store i32 %55, ptr getelementptr inbounds (i8, ptr @strace, i64 4), align 4, !tbaa !25
  %56 = getelementptr inbounds i8, ptr %1, i64 24
  %57 = load i32, ptr %56, align 8, !tbaa !21
  store i32 %57, ptr @t2x, align 4, !tbaa !17
  %58 = getelementptr inbounds i8, ptr %1, i64 28
  %59 = load i32, ptr %58, align 4, !tbaa !24
  store i32 %59, ptr @t2y, align 4, !tbaa !17
  %60 = sub nsw i32 %57, %53
  store i32 %60, ptr getelementptr inbounds (i8, ptr @strace, i64 8), align 4, !tbaa !26
  %61 = sub nsw i32 %59, %55
  store i32 %61, ptr getelementptr inbounds (i8, ptr @strace, i64 12), align 4, !tbaa !27
  %62 = load i32, ptr @numnodes, align 4, !tbaa !17
  %63 = add nsw i32 %62, -1
  %64 = tail call i32 @P_CrossBSPNode(i32 noundef %63)
  br label %65

65:                                               ; preds = %34, %31
  %.0 = phi i32 [ 0, %31 ], [ %64, %34 ]
  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, !8, i64 88}
!6 = !{!"mobj_s", !7, i64 0, !11, i64 24, !11, i64 28, !11, i64 32, !8, i64 40, !8, i64 48, !11, i64 56, !11, i64 60, !11, i64 64, !8, i64 72, !8, i64 80, !8, i64 88, !11, i64 96, !11, i64 100, !11, i64 104, !11, i64 108, !11, i64 112, !11, i64 116, !11, i64 120, !11, i64 124, !11, i64 128, !8, i64 136, !11, i64 144, !8, i64 152, !11, i64 160, !11, i64 164, !11, i64 168, !11, i64 172, !8, i64 176, !11, i64 184, !11, i64 188, !8, i64 192, !11, i64 200, !12, i64 204, !8, i64 216}
!7 = !{!"thinker_s", !8, i64 0, !8, i64 8, !9, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!13 = !{!"short", !9, i64 0}
!14 = !{!15, !8, i64 0}
!15 = !{!"subsector_s", !8, i64 0, !13, i64 8, !13, i64 10}
!16 = !{!8, !8, i64 0}
!17 = !{!11, !11, i64 0}
!18 = !{!9, !9, i64 0}
!19 = !{!6, !11, i64 32}
!20 = !{!6, !11, i64 108}
!21 = !{!6, !11, i64 24}
!22 = !{!23, !11, i64 0}
!23 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12}
!24 = !{!6, !11, i64 28}
!25 = !{!23, !11, i64 4}
!26 = !{!23, !11, i64 8}
!27 = !{!23, !11, i64 12}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass