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

; 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.mapthing_t = type { i16, i16, i16, i16, i16 }
%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 }

@leveltime = external global i32, align 4
@itemrespawnque = external dso_local global [128 x %struct.mapthing_t], align 16
@iquehead = external dso_local global i32, align 4
@itemrespawntime = external dso_local global [128 x i32], align 16
@iquetail = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @P_RemoveMobj(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds %struct.mobj_s, ptr %3, i32 0, i32 24
  %5 = load i32, ptr %4, align 8, !tbaa !9
  %6 = and i32 %5, 1
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %8, label %45

8:                                                ; preds = %1
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.mobj_s, ptr %9, i32 0, i32 24
  %11 = load i32, ptr %10, align 8, !tbaa !9
  %12 = and i32 %11, 131072
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %45, label %14

14:                                               ; preds = %8
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.mobj_s, ptr %15, i32 0, i32 20
  %17 = load i32, ptr %16, align 8, !tbaa !15
  %18 = icmp ne i32 %17, 56
  br i1 %18, label %19, label %45

19:                                               ; preds = %14
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mobj_s, ptr %20, i32 0, i32 20
  %22 = load i32, ptr %21, align 8, !tbaa !15
  %23 = icmp ne i32 %22, 58
  br i1 %23, label %24, label %45

24:                                               ; preds = %19
  %25 = load i32, ptr @iquehead, align 4, !tbaa !16
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [128 x %struct.mapthing_t], ptr @itemrespawnque, i64 0, i64 %26
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mobj_s, ptr %28, i32 0, i32 33
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %27, ptr align 4 %29, i64 10, i1 false), !tbaa.struct !17
  %30 = load i32, ptr @leveltime, align 4, !tbaa !16
  %31 = load i32, ptr @iquehead, align 4, !tbaa !16
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [128 x i32], ptr @itemrespawntime, i64 0, i64 %32
  store i32 %30, ptr %33, align 4, !tbaa !16
  %34 = load i32, ptr @iquehead, align 4, !tbaa !16
  %35 = add nsw i32 %34, 1
  %36 = and i32 %35, 127
  store i32 %36, ptr @iquehead, align 4, !tbaa !16
  %37 = load i32, ptr @iquehead, align 4, !tbaa !16
  %38 = load i32, ptr @iquetail, align 4, !tbaa !16
  %39 = icmp eq i32 %37, %38
  br i1 %39, label %40, label %44

40:                                               ; preds = %24
  %41 = load i32, ptr @iquetail, align 4, !tbaa !16
  %42 = add nsw i32 %41, 1
  %43 = and i32 %42, 127
  store i32 %43, ptr @iquetail, align 4, !tbaa !16
  br label %44

44:                                               ; preds = %40, %24
  br label %45

45:                                               ; preds = %44, %19, %14, %8, %1
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  call void @P_UnsetThingPosition(ptr noundef %46)
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  call void @S_StopSound(ptr noundef %47)
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  call void @P_RemoveThinker(ptr noundef %48)
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare void @P_UnsetThingPosition(ptr noundef) #2

declare void @S_StopSound(ptr noundef) #2

declare void @P_RemoveThinker(ptr noundef) #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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" }

!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 160}
!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 128}
!16 = !{!12, !12, i64 0}
!17 = !{i64 0, i64 2, !18, i64 2, i64 2, !18, i64 4, i64 2, !18, i64 6, i64 2, !18, i64 8, i64 2, !18}
!18 = !{!14, !14, 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.mapthing_t = type { i16, i16, i16, i16, i16 }

@leveltime = external local_unnamed_addr global i32, align 4
@itemrespawnque = external dso_local local_unnamed_addr global [128 x %struct.mapthing_t], align 16
@iquehead = external dso_local local_unnamed_addr global i32, align 4
@itemrespawntime = external dso_local local_unnamed_addr global [128 x i32], align 16
@iquetail = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @P_RemoveMobj(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 160
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = and i32 %3, 131073
  %or.cond = icmp eq i32 %4, 1
  br i1 %or.cond, label %5, label %22

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 128
  %7 = load i32, ptr %6, align 8, !tbaa !14
  switch i32 %7, label %8 [
    i32 56, label %22
    i32 58, label %22
  ]

8:                                                ; preds = %5
  %9 = load i32, ptr @iquehead, align 4, !tbaa !15
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [128 x %struct.mapthing_t], ptr @itemrespawnque, i64 0, i64 %10
  %12 = getelementptr inbounds i8, ptr %0, i64 204
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 2 dereferenceable(10) %11, ptr noundef nonnull align 4 dereferenceable(10) %12, i64 10, i1 false), !tbaa.struct !16
  %13 = load i32, ptr @leveltime, align 4, !tbaa !15
  %14 = getelementptr inbounds [128 x i32], ptr @itemrespawntime, i64 0, i64 %10
  store i32 %13, ptr %14, align 4, !tbaa !15
  %15 = add nsw i32 %9, 1
  %16 = and i32 %15, 127
  store i32 %16, ptr @iquehead, align 4, !tbaa !15
  %17 = load i32, ptr @iquetail, align 4, !tbaa !15
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %22

19:                                               ; preds = %8
  %20 = add i32 %9, 2
  %21 = and i32 %20, 127
  store i32 %21, ptr @iquetail, align 4, !tbaa !15
  br label %22

22:                                               ; preds = %5, %5, %8, %19, %1
  tail call void @P_UnsetThingPosition(ptr noundef nonnull %0) #3
  tail call void @S_StopSound(ptr noundef nonnull %0) #3
  tail call void @P_RemoveThinker(ptr noundef nonnull %0) #3
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

declare void @P_UnsetThingPosition(ptr noundef) local_unnamed_addr #2

declare void @S_StopSound(ptr noundef) local_unnamed_addr #2

declare void @P_RemoveThinker(ptr noundef) local_unnamed_addr #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !11, i64 160}
!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 128}
!15 = !{!11, !11, i64 0}
!16 = !{i64 0, i64 2, !17, i64 2, i64 2, !17, i64 4, i64 2, !17, i64 6, i64 2, !17, i64 8, i64 2, !17}
!17 = !{!13, !13, i64 0}


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