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

@respawnmonsters = external global i32, align 4
@leveltime = external global i32, align 4

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

declare i32 @P_Random() #1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @P_MobjThinker(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 16
  %5 = load i32, ptr %4, align 8, !tbaa !9
  %6 = icmp ne i32 %5, 0
  br i1 %6, label %18, label %7

7:                                                ; preds = %1
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.mobj_s, ptr %8, i32 0, i32 17
  %10 = load i32, ptr %9, align 4, !tbaa !15
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %18, label %12

12:                                               ; preds = %7
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.mobj_s, ptr %13, i32 0, i32 24
  %15 = load i32, ptr %14, align 8, !tbaa !16
  %16 = and i32 %15, 16777216
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %27

18:                                               ; preds = %12, %7, %1
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  call void @P_XYMovement(ptr noundef %19)
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mobj_s, ptr %20, i32 0, i32 0
  %22 = getelementptr inbounds %struct.thinker_s, ptr %21, i32 0, i32 2
  %23 = load ptr, ptr %22, align 8, !tbaa !17
  %24 = icmp eq ptr %23, inttoptr (i64 -1 to ptr)
  br i1 %24, label %25, label %26

25:                                               ; preds = %18
  br label %107

26:                                               ; preds = %18
  br label %27

27:                                               ; preds = %26, %12
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mobj_s, ptr %28, i32 0, i32 3
  %30 = load i32, ptr %29, align 8, !tbaa !18
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mobj_s, ptr %31, i32 0, i32 12
  %33 = load i32, ptr %32, align 8, !tbaa !19
  %34 = icmp ne i32 %30, %33
  br i1 %34, label %40, label %35

35:                                               ; preds = %27
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mobj_s, ptr %36, i32 0, i32 18
  %38 = load i32, ptr %37, align 8, !tbaa !20
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %49

40:                                               ; preds = %35, %27
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  call void @P_ZMovement(ptr noundef %41)
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.mobj_s, ptr %42, i32 0, i32 0
  %44 = getelementptr inbounds %struct.thinker_s, ptr %43, i32 0, i32 2
  %45 = load ptr, ptr %44, align 8, !tbaa !17
  %46 = icmp eq ptr %45, inttoptr (i64 -1 to ptr)
  br i1 %46, label %47, label %48

47:                                               ; preds = %40
  br label %107

48:                                               ; preds = %40
  br label %49

49:                                               ; preds = %48, %35
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.mobj_s, ptr %50, i32 0, i32 22
  %52 = load i32, ptr %51, align 8, !tbaa !21
  %53 = icmp ne i32 %52, -1
  br i1 %53, label %54, label %75

54:                                               ; preds = %49
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.mobj_s, ptr %55, i32 0, i32 22
  %57 = load i32, ptr %56, align 8, !tbaa !21
  %58 = add nsw i32 %57, -1
  store i32 %58, ptr %56, align 8, !tbaa !21
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.mobj_s, ptr %59, i32 0, i32 22
  %61 = load i32, ptr %60, align 8, !tbaa !21
  %62 = icmp ne i32 %61, 0
  br i1 %62, label %74, label %63

63:                                               ; preds = %54
  %64 = load ptr, ptr %2, align 8, !tbaa !5
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.mobj_s, ptr %65, i32 0, i32 23
  %67 = load ptr, ptr %66, align 8, !tbaa !22
  %68 = getelementptr inbounds %struct.state_t, ptr %67, i32 0, i32 4
  %69 = load i32, ptr %68, align 8, !tbaa !23
  %70 = call i32 @P_SetMobjState(ptr noundef %64, i32 noundef %69)
  %71 = icmp ne i32 %70, 0
  br i1 %71, label %73, label %72

72:                                               ; preds = %63
  br label %107

73:                                               ; preds = %63
  br label %74

74:                                               ; preds = %73, %54
  br label %107

75:                                               ; preds = %49
  %76 = load ptr, ptr %2, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.mobj_s, ptr %76, i32 0, i32 24
  %78 = load i32, ptr %77, align 8, !tbaa !16
  %79 = and i32 %78, 4194304
  %80 = icmp ne i32 %79, 0
  br i1 %80, label %82, label %81

81:                                               ; preds = %75
  br label %107

82:                                               ; preds = %75
  %83 = load i32, ptr @respawnmonsters, align 4, !tbaa !26
  %84 = icmp ne i32 %83, 0
  br i1 %84, label %86, label %85

85:                                               ; preds = %82
  br label %107

86:                                               ; preds = %82
  %87 = load ptr, ptr %2, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.mobj_s, ptr %87, i32 0, i32 27
  %89 = load i32, ptr %88, align 4, !tbaa !27
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %88, align 4, !tbaa !27
  %91 = load ptr, ptr %2, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.mobj_s, ptr %91, i32 0, i32 27
  %93 = load i32, ptr %92, align 4, !tbaa !27
  %94 = icmp slt i32 %93, 420
  br i1 %94, label %95, label %96

95:                                               ; preds = %86
  br label %107

96:                                               ; preds = %86
  %97 = load i32, ptr @leveltime, align 4, !tbaa !26
  %98 = and i32 %97, 31
  %99 = icmp ne i32 %98, 0
  br i1 %99, label %100, label %101

100:                                              ; preds = %96
  br label %107

101:                                              ; preds = %96
  %102 = call i32 @P_Random()
  %103 = icmp sgt i32 %102, 4
  br i1 %103, label %104, label %105

104:                                              ; preds = %101
  br label %107

105:                                              ; preds = %101
  %106 = load ptr, ptr %2, align 8, !tbaa !5
  call void @P_NightmareRespawn(ptr noundef %106)
  br label %107

107:                                              ; preds = %105, %104, %100, %95, %85, %81, %74, %72, %47, %25
  ret void
}

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

!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 112}
!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 116}
!16 = !{!10, !12, i64 160}
!17 = !{!7, !7, i64 0}
!18 = !{!10, !12, i64 32}
!19 = !{!10, !12, i64 96}
!20 = !{!10, !12, i64 120}
!21 = !{!10, !12, i64 144}
!22 = !{!10, !6, i64 152}
!23 = !{!24, !12, i64 32}
!24 = !{!"", !12, i64 0, !25, i64 8, !25, i64 16, !7, i64 24, !12, i64 32, !25, i64 40, !25, i64 48}
!25 = !{!"long", !7, i64 0}
!26 = !{!12, !12, i64 0}
!27 = !{!10, !12, i64 172}


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

@respawnmonsters = external local_unnamed_addr global i32, align 4
@leveltime = external local_unnamed_addr global i32, align 4

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

declare i32 @P_Random() local_unnamed_addr #1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @P_MobjThinker(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 112
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %11

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 116
  %6 = load i32, ptr %5, align 4, !tbaa !14
  %.not20 = icmp eq i32 %6, 0
  br i1 %.not20, label %7, label %11

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 160
  %9 = load i32, ptr %8, align 8, !tbaa !15
  %10 = and i32 %9, 16777216
  %.not21 = icmp eq i32 %10, 0
  br i1 %.not21, label %15, label %11

11:                                               ; preds = %7, %4, %1
  tail call void @P_XYMovement(ptr noundef nonnull %0)
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !16
  %14 = icmp eq ptr %13, inttoptr (i64 -1 to ptr)
  br i1 %14, label %57, label %15

15:                                               ; preds = %11, %7
  %16 = getelementptr inbounds i8, ptr %0, i64 32
  %17 = load i32, ptr %16, align 8, !tbaa !17
  %18 = getelementptr inbounds i8, ptr %0, i64 96
  %19 = load i32, ptr %18, align 8, !tbaa !18
  %.not22 = icmp eq i32 %17, %19
  br i1 %.not22, label %20, label %23

20:                                               ; preds = %15
  %21 = getelementptr inbounds i8, ptr %0, i64 120
  %22 = load i32, ptr %21, align 8, !tbaa !19
  %.not23 = icmp eq i32 %22, 0
  br i1 %.not23, label %27, label %23

23:                                               ; preds = %20, %15
  tail call void @P_ZMovement(ptr noundef nonnull %0)
  %24 = getelementptr inbounds i8, ptr %0, i64 16
  %25 = load ptr, ptr %24, align 8, !tbaa !16
  %26 = icmp eq ptr %25, inttoptr (i64 -1 to ptr)
  br i1 %26, label %57, label %27

27:                                               ; preds = %23, %20
  %28 = getelementptr inbounds i8, ptr %0, i64 144
  %29 = load i32, ptr %28, align 8, !tbaa !20
  %.not24 = icmp eq i32 %29, -1
  br i1 %.not24, label %38, label %30

30:                                               ; preds = %27
  %31 = add nsw i32 %29, -1
  store i32 %31, ptr %28, align 8, !tbaa !20
  %.not26 = icmp eq i32 %31, 0
  br i1 %.not26, label %32, label %57

32:                                               ; preds = %30
  %33 = getelementptr inbounds i8, ptr %0, i64 152
  %34 = load ptr, ptr %33, align 8, !tbaa !21
  %35 = getelementptr inbounds i8, ptr %34, i64 32
  %36 = load i32, ptr %35, align 8, !tbaa !22
  %37 = tail call i32 @P_SetMobjState(ptr noundef nonnull %0, i32 noundef %36)
  br label %57

38:                                               ; preds = %27
  %39 = getelementptr inbounds i8, ptr %0, i64 160
  %40 = load i32, ptr %39, align 8, !tbaa !15
  %41 = and i32 %40, 4194304
  %42 = icmp ne i32 %41, 0
  %43 = load i32, ptr @respawnmonsters, align 4
  %44 = icmp ne i32 %43, 0
  %or.cond = select i1 %42, i1 %44, i1 false
  br i1 %or.cond, label %45, label %57

45:                                               ; preds = %38
  %46 = getelementptr inbounds i8, ptr %0, i64 172
  %47 = load i32, ptr %46, align 4, !tbaa !25
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %46, align 4, !tbaa !25
  %49 = icmp slt i32 %47, 419
  br i1 %49, label %57, label %50

50:                                               ; preds = %45
  %51 = load i32, ptr @leveltime, align 4, !tbaa !26
  %52 = and i32 %51, 31
  %.not25 = icmp eq i32 %52, 0
  br i1 %.not25, label %53, label %57

53:                                               ; preds = %50
  %54 = tail call i32 @P_Random() #2
  %55 = icmp sgt i32 %54, 4
  br i1 %55, label %57, label %56

56:                                               ; preds = %53
  tail call void @P_NightmareRespawn(ptr noundef nonnull %0)
  br label %57

57:                                               ; preds = %32, %53, %50, %45, %38, %30, %23, %11, %56
  ret void
}

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 = { 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 112}
!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 116}
!15 = !{!6, !11, i64 160}
!16 = !{!9, !9, i64 0}
!17 = !{!6, !11, i64 32}
!18 = !{!6, !11, i64 96}
!19 = !{!6, !11, i64 120}
!20 = !{!6, !11, i64 144}
!21 = !{!6, !8, i64 152}
!22 = !{!23, !11, i64 32}
!23 = !{!"", !11, i64 0, !24, i64 8, !24, i64 16, !9, i64 24, !11, i64 32, !24, i64 40, !24, i64 48}
!24 = !{!"long", !9, i64 0}
!25 = !{!6, !11, i64 172}
!26 = !{!11, !11, i64 0}


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