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

; 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.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 }
%struct.sector_t = type { i32, i32, i16, i16, i16, i16, i16, i32, ptr, [4 x i32], %struct.degenmobj_t, i32, ptr, ptr, i32, ptr }
%struct.degenmobj_t = type { %struct.thinker_s, i32, i32, i32 }

@bmaporgx = external global i32, align 4
@bmaporgy = external global i32, align 4
@bmapwidth = external global i32, align 4
@bmapheight = external global i32, align 4
@blocklinks = 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
define dso_local void @P_SetThingPosition(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.mobj_s, ptr %8, i32 0, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !9
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.mobj_s, ptr %11, i32 0, i32 2
  %13 = load i32, ptr %12, align 4, !tbaa !15
  %14 = call ptr @R_PointInSubsector(i32 noundef %10, i32 noundef %13)
  store ptr %14, ptr %3, align 8, !tbaa !5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mobj_s, ptr %16, i32 0, i32 11
  store ptr %15, ptr %17, align 8, !tbaa !16
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.mobj_s, ptr %18, i32 0, i32 24
  %20 = load i32, ptr %19, align 8, !tbaa !17
  %21 = and i32 %20, 8
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %48, label %23

23:                                               ; preds = %1
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.subsector_s, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !18
  store ptr %26, ptr %4, align 8, !tbaa !5
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mobj_s, ptr %27, i32 0, i32 5
  store ptr null, ptr %28, align 8, !tbaa !20
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.sector_t, ptr %29, i32 0, i32 12
  %31 = load ptr, ptr %30, align 8, !tbaa !21
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mobj_s, ptr %32, i32 0, i32 4
  store ptr %31, ptr %33, align 8, !tbaa !24
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.sector_t, ptr %34, i32 0, i32 12
  %36 = load ptr, ptr %35, align 8, !tbaa !21
  %37 = icmp ne ptr %36, null
  br i1 %37, label %38, label %44

38:                                               ; preds = %23
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.sector_t, ptr %40, i32 0, i32 12
  %42 = load ptr, ptr %41, align 8, !tbaa !21
  %43 = getelementptr inbounds %struct.mobj_s, ptr %42, i32 0, i32 5
  store ptr %39, ptr %43, align 8, !tbaa !20
  br label %44

44:                                               ; preds = %38, %23
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.sector_t, ptr %46, i32 0, i32 12
  store ptr %45, ptr %47, align 8, !tbaa !21
  br label %48

48:                                               ; preds = %44, %1
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mobj_s, ptr %49, i32 0, i32 24
  %51 = load i32, ptr %50, align 8, !tbaa !17
  %52 = and i32 %51, 16
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %112, label %54

54:                                               ; preds = %48
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.mobj_s, ptr %55, i32 0, i32 1
  %57 = load i32, ptr %56, align 8, !tbaa !9
  %58 = load i32, ptr @bmaporgx, align 4, !tbaa !25
  %59 = sub nsw i32 %57, %58
  %60 = ashr i32 %59, 23
  store i32 %60, ptr %5, align 4, !tbaa !25
  %61 = load ptr, ptr %2, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.mobj_s, ptr %61, i32 0, i32 2
  %63 = load i32, ptr %62, align 4, !tbaa !15
  %64 = load i32, ptr @bmaporgy, align 4, !tbaa !25
  %65 = sub nsw i32 %63, %64
  %66 = ashr i32 %65, 23
  store i32 %66, ptr %6, align 4, !tbaa !25
  %67 = load i32, ptr %5, align 4, !tbaa !25
  %68 = icmp sge i32 %67, 0
  br i1 %68, label %69, label %106

69:                                               ; preds = %54
  %70 = load i32, ptr %5, align 4, !tbaa !25
  %71 = load i32, ptr @bmapwidth, align 4, !tbaa !25
  %72 = icmp slt i32 %70, %71
  br i1 %72, label %73, label %106

73:                                               ; preds = %69
  %74 = load i32, ptr %6, align 4, !tbaa !25
  %75 = icmp sge i32 %74, 0
  br i1 %75, label %76, label %106

76:                                               ; preds = %73
  %77 = load i32, ptr %6, align 4, !tbaa !25
  %78 = load i32, ptr @bmapheight, align 4, !tbaa !25
  %79 = icmp slt i32 %77, %78
  br i1 %79, label %80, label %106

80:                                               ; preds = %76
  %81 = load ptr, ptr @blocklinks, align 8, !tbaa !5
  %82 = load i32, ptr %6, align 4, !tbaa !25
  %83 = load i32, ptr @bmapwidth, align 4, !tbaa !25
  %84 = mul nsw i32 %82, %83
  %85 = load i32, ptr %5, align 4, !tbaa !25
  %86 = add nsw i32 %84, %85
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds ptr, ptr %81, i64 %87
  store ptr %88, ptr %7, align 8, !tbaa !5
  %89 = load ptr, ptr %2, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.mobj_s, ptr %89, i32 0, i32 10
  store ptr null, ptr %90, align 8, !tbaa !26
  %91 = load ptr, ptr %7, align 8, !tbaa !5
  %92 = load ptr, ptr %91, align 8, !tbaa !5
  %93 = load ptr, ptr %2, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.mobj_s, ptr %93, i32 0, i32 9
  store ptr %92, ptr %94, align 8, !tbaa !27
  %95 = load ptr, ptr %7, align 8, !tbaa !5
  %96 = load ptr, ptr %95, align 8, !tbaa !5
  %97 = icmp ne ptr %96, null
  br i1 %97, label %98, label %103

98:                                               ; preds = %80
  %99 = load ptr, ptr %2, align 8, !tbaa !5
  %100 = load ptr, ptr %7, align 8, !tbaa !5
  %101 = load ptr, ptr %100, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.mobj_s, ptr %101, i32 0, i32 10
  store ptr %99, ptr %102, align 8, !tbaa !26
  br label %103

103:                                              ; preds = %98, %80
  %104 = load ptr, ptr %2, align 8, !tbaa !5
  %105 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %104, ptr %105, align 8, !tbaa !5
  br label %111

106:                                              ; preds = %76, %73, %69, %54
  %107 = load ptr, ptr %2, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.mobj_s, ptr %107, i32 0, i32 10
  store ptr null, ptr %108, align 8, !tbaa !26
  %109 = load ptr, ptr %2, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.mobj_s, ptr %109, i32 0, i32 9
  store ptr null, ptr %110, align 8, !tbaa !27
  br label %111

111:                                              ; preds = %106, %103
  br label %112

112:                                              ; preds = %111, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

declare ptr @R_PointInSubsector(i32 noundef, i32 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 24}
!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 = !{!10, !12, i64 28}
!16 = !{!10, !6, i64 88}
!17 = !{!10, !12, i64 160}
!18 = !{!19, !6, i64 0}
!19 = !{!"subsector_s", !6, i64 0, !14, i64 8, !14, i64 10}
!20 = !{!10, !6, i64 48}
!21 = !{!22, !6, i64 96}
!22 = !{!"", !12, i64 0, !12, i64 4, !14, i64 8, !14, i64 10, !14, i64 12, !14, i64 14, !14, i64 16, !12, i64 20, !6, i64 24, !7, i64 32, !23, i64 48, !12, i64 88, !6, i64 96, !6, i64 104, !12, i64 112, !6, i64 120}
!23 = !{!"", !11, i64 0, !12, i64 24, !12, i64 28, !12, i64 32}
!24 = !{!10, !6, i64 40}
!25 = !{!12, !12, i64 0}
!26 = !{!10, !6, i64 80}
!27 = !{!10, !6, i64 72}


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

@bmaporgx = external local_unnamed_addr global i32, align 4
@bmaporgy = external local_unnamed_addr global i32, align 4
@bmapwidth = external local_unnamed_addr global i32, align 4
@bmapheight = external local_unnamed_addr global i32, align 4
@blocklinks = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @P_SetThingPosition(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 24
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 28
  %5 = load i32, ptr %4, align 4, !tbaa !14
  %6 = tail call ptr @R_PointInSubsector(i32 noundef %3, i32 noundef %5) #3
  %7 = getelementptr inbounds i8, ptr %0, i64 88
  store ptr %6, ptr %7, align 8, !tbaa !15
  %8 = getelementptr inbounds i8, ptr %0, i64 160
  %9 = load i32, ptr %8, align 8, !tbaa !16
  %10 = and i32 %9, 8
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %11, label %20

11:                                               ; preds = %1
  %12 = load ptr, ptr %6, align 8, !tbaa !17
  %13 = getelementptr inbounds i8, ptr %0, i64 48
  store ptr null, ptr %13, align 8, !tbaa !19
  %14 = getelementptr inbounds i8, ptr %12, i64 96
  %15 = load ptr, ptr %14, align 8, !tbaa !20
  %16 = getelementptr inbounds i8, ptr %0, i64 40
  store ptr %15, ptr %16, align 8, !tbaa !23
  %.not37 = icmp eq ptr %15, null
  br i1 %.not37, label %19, label %17

17:                                               ; preds = %11
  %18 = getelementptr inbounds i8, ptr %15, i64 48
  store ptr %0, ptr %18, align 8, !tbaa !19
  br label %19

19:                                               ; preds = %17, %11
  store ptr %0, ptr %14, align 8, !tbaa !20
  br label %20

20:                                               ; preds = %19, %1
  %21 = and i32 %9, 16
  %.not38 = icmp eq i32 %21, 0
  br i1 %.not38, label %22, label %52

22:                                               ; preds = %20
  %23 = load i32, ptr %2, align 8, !tbaa !5
  %24 = load i32, ptr @bmaporgx, align 4, !tbaa !24
  %25 = sub nsw i32 %23, %24
  %26 = ashr i32 %25, 23
  %27 = load i32, ptr %4, align 4, !tbaa !14
  %28 = load i32, ptr @bmaporgy, align 4, !tbaa !24
  %29 = sub nsw i32 %27, %28
  %30 = ashr i32 %29, 23
  %31 = icmp sgt i32 %26, -1
  br i1 %31, label %32, label %50

32:                                               ; preds = %22
  %33 = load i32, ptr @bmapwidth, align 4, !tbaa !24
  %34 = icmp slt i32 %26, %33
  %35 = icmp sgt i32 %30, -1
  %or.cond = select i1 %34, i1 %35, i1 false
  %36 = load i32, ptr @bmapheight, align 4
  %37 = icmp slt i32 %30, %36
  %or.cond41 = select i1 %or.cond, i1 %37, i1 false
  br i1 %or.cond41, label %38, label %50

38:                                               ; preds = %32
  %39 = load ptr, ptr @blocklinks, align 8, !tbaa !25
  %40 = mul nsw i32 %33, %30
  %41 = add nuw nsw i32 %40, %26
  %42 = zext nneg i32 %41 to i64
  %43 = getelementptr inbounds ptr, ptr %39, i64 %42
  %44 = getelementptr inbounds i8, ptr %0, i64 80
  store ptr null, ptr %44, align 8, !tbaa !26
  %45 = load ptr, ptr %43, align 8, !tbaa !25
  %46 = getelementptr inbounds i8, ptr %0, i64 72
  store ptr %45, ptr %46, align 8, !tbaa !27
  %.not39 = icmp eq ptr %45, null
  br i1 %.not39, label %49, label %47

47:                                               ; preds = %38
  %48 = getelementptr inbounds i8, ptr %45, i64 80
  store ptr %0, ptr %48, align 8, !tbaa !26
  br label %49

49:                                               ; preds = %47, %38
  store ptr %0, ptr %43, align 8, !tbaa !25
  br label %52

50:                                               ; preds = %32, %22
  %51 = getelementptr inbounds i8, ptr %0, i64 72
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %51, i8 0, i64 16, i1 false)
  br label %52

52:                                               ; preds = %49, %50, %20
  ret void
}

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

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !11, i64 24}
!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 = !{!6, !11, i64 28}
!15 = !{!6, !8, i64 88}
!16 = !{!6, !11, i64 160}
!17 = !{!18, !8, i64 0}
!18 = !{!"subsector_s", !8, i64 0, !13, i64 8, !13, i64 10}
!19 = !{!6, !8, i64 48}
!20 = !{!21, !8, i64 96}
!21 = !{!"", !11, i64 0, !11, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !11, i64 20, !8, i64 24, !9, i64 32, !22, i64 48, !11, i64 88, !8, i64 96, !8, i64 104, !11, i64 112, !8, i64 120}
!22 = !{!"", !7, i64 0, !11, i64 24, !11, i64 28, !11, i64 32}
!23 = !{!6, !8, i64 40}
!24 = !{!11, !11, i64 0}
!25 = !{!8, !8, i64 0}
!26 = !{!6, !8, i64 80}
!27 = !{!6, !8, i64 72}


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