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

@states = external global [967 x %struct.state_t], align 16

; Function Attrs: nounwind uwtable
define dso_local i32 @P_SetMobjState(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  br label %8

8:                                                ; preds = %52, %2
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %15

11:                                               ; preds = %8
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.mobj_s, ptr %12, i32 0, i32 23
  store ptr null, ptr %13, align 8, !tbaa !11
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  call void @P_RemoveMobj(ptr noundef %14)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %59

15:                                               ; preds = %8
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = zext i32 %16 to i64
  %18 = getelementptr inbounds [967 x %struct.state_t], ptr @states, i64 0, i64 %17
  store ptr %18, ptr %6, align 8, !tbaa !5
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mobj_s, ptr %20, i32 0, i32 23
  store ptr %19, ptr %21, align 8, !tbaa !11
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.state_t, ptr %22, i32 0, i32 2
  %24 = load i64, ptr %23, align 8, !tbaa !16
  %25 = trunc i64 %24 to i32
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mobj_s, ptr %26, i32 0, i32 22
  store i32 %25, ptr %27, align 8, !tbaa !19
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.state_t, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 8, !tbaa !20
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mobj_s, ptr %31, i32 0, i32 7
  store i32 %30, ptr %32, align 4, !tbaa !21
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.state_t, ptr %33, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !22
  %36 = trunc i64 %35 to i32
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.mobj_s, ptr %37, i32 0, i32 8
  store i32 %36, ptr %38, align 8, !tbaa !23
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.state_t, ptr %39, i32 0, i32 3
  %41 = load ptr, ptr %40, align 8, !tbaa !24
  %42 = icmp ne ptr %41, null
  br i1 %42, label %43, label %48

43:                                               ; preds = %15
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.state_t, ptr %44, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !24
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  call void %46(ptr noundef %47)
  br label %48

48:                                               ; preds = %43, %15
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.state_t, ptr %49, i32 0, i32 4
  %51 = load i32, ptr %50, align 8, !tbaa !25
  store i32 %51, ptr %5, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %48
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.mobj_s, ptr %53, i32 0, i32 22
  %55 = load i32, ptr %54, align 8, !tbaa !19
  %56 = icmp ne i32 %55, 0
  %57 = xor i1 %56, true
  br i1 %57, label %8, label %58, !llvm.loop !26

58:                                               ; preds = %52
  store i32 1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %59

59:                                               ; preds = %58, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %60 = load i32, ptr %3, align 4
  ret i32 %60
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local void @P_RemoveMobj(ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

@states = external global [967 x %struct.state_t], align 16

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @P_SetMobjState(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 152
  %4 = getelementptr inbounds i8, ptr %0, i64 144
  %5 = getelementptr inbounds i8, ptr %0, i64 60
  %6 = getelementptr inbounds i8, ptr %0, i64 64
  br label %7

7:                                                ; preds = %23, %2
  %.017 = phi i32 [ %1, %2 ], [ %26, %23 ]
  %8 = icmp eq i32 %.017, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %7
  store ptr null, ptr %3, align 8, !tbaa !5
  tail call void @P_RemoveMobj(ptr noundef %0)
  br label %.loopexit

10:                                               ; preds = %7
  %11 = zext i32 %.017 to i64
  %12 = getelementptr inbounds [967 x %struct.state_t], ptr @states, i64 0, i64 %11
  store ptr %12, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 16
  %14 = load i64, ptr %13, align 8, !tbaa !14
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %4, align 8, !tbaa !17
  %16 = load i32, ptr %12, align 8, !tbaa !18
  store i32 %16, ptr %5, align 4, !tbaa !19
  %17 = getelementptr inbounds i8, ptr %12, i64 8
  %18 = load i64, ptr %17, align 8, !tbaa !20
  %19 = trunc i64 %18 to i32
  store i32 %19, ptr %6, align 8, !tbaa !21
  %20 = getelementptr inbounds i8, ptr %12, i64 24
  %21 = load ptr, ptr %20, align 8, !tbaa !22
  %.not = icmp eq ptr %21, null
  br i1 %.not, label %23, label %22

22:                                               ; preds = %10
  tail call void %21(ptr noundef nonnull %0) #1
  %.pre = load i32, ptr %4, align 8, !tbaa !17
  br label %23

23:                                               ; preds = %22, %10
  %24 = phi i32 [ %.pre, %22 ], [ %15, %10 ]
  %25 = getelementptr inbounds i8, ptr %12, i64 32
  %26 = load i32, ptr %25, align 8, !tbaa !23
  %.not19 = icmp eq i32 %24, 0
  br i1 %.not19, label %7, label %.loopexit, !llvm.loop !24

.loopexit:                                        ; preds = %23, %9
  %.0 = phi i32 [ 0, %9 ], [ 1, %23 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local void @P_RemoveMobj(ptr noundef) local_unnamed_addr #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" }
attributes #1 = { 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, !8, i64 152}
!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, !16, i64 16}
!15 = !{!"", !11, i64 0, !16, i64 8, !16, i64 16, !9, i64 24, !11, i64 32, !16, i64 40, !16, i64 48}
!16 = !{!"long", !9, i64 0}
!17 = !{!6, !11, i64 144}
!18 = !{!15, !11, i64 0}
!19 = !{!6, !11, i64 60}
!20 = !{!15, !16, i64 8}
!21 = !{!6, !11, i64 64}
!22 = !{!9, !9, i64 0}
!23 = !{!15, !11, i64 32}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}


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