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

; 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.mobjinfo_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }

; 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

declare i32 @P_Random() #1

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

declare void @S_StartSound(ptr noundef, i32 noundef) #1

declare i32 @P_SetMobjState(ptr noundef, i32 noundef) #1

declare ptr @P_SpawnMobj(i32 noundef, i32 noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @A_SpawnFly(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %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
  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 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.mobj_s, ptr %9, i32 0, i32 29
  %11 = load i32, ptr %10, align 8, !tbaa !9
  %12 = add nsw i32 %11, -1
  store i32 %12, ptr %10, align 8, !tbaa !9
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %1
  store i32 1, ptr %8, align 4
  br label %113

15:                                               ; preds = %1
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mobj_s, ptr %16, i32 0, i32 28
  %18 = load ptr, ptr %17, align 8, !tbaa !15
  store ptr %18, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mobj_s, ptr %19, i32 0, i32 1
  %21 = load i32, ptr %20, align 8, !tbaa !16
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.mobj_s, ptr %22, i32 0, i32 2
  %24 = load i32, ptr %23, align 4, !tbaa !17
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.mobj_s, ptr %25, i32 0, i32 3
  %27 = load i32, ptr %26, align 8, !tbaa !18
  %28 = call ptr @P_SpawnMobj(i32 noundef %21, i32 noundef %24, i32 noundef %27, i32 noundef 29)
  store ptr %28, ptr %4, align 8, !tbaa !5
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  call void @S_StartSound(ptr noundef %29, i32 noundef 35)
  %30 = call i32 @P_Random()
  store i32 %30, ptr %6, align 4, !tbaa !19
  %31 = load i32, ptr %6, align 4, !tbaa !19
  %32 = icmp slt i32 %31, 50
  br i1 %32, label %33, label %34

33:                                               ; preds = %15
  store i32 11, ptr %7, align 4, !tbaa !19
  br label %80

34:                                               ; preds = %15
  %35 = load i32, ptr %6, align 4, !tbaa !19
  %36 = icmp slt i32 %35, 90
  br i1 %36, label %37, label %38

37:                                               ; preds = %34
  store i32 12, ptr %7, align 4, !tbaa !19
  br label %79

38:                                               ; preds = %34
  %39 = load i32, ptr %6, align 4, !tbaa !19
  %40 = icmp slt i32 %39, 120
  br i1 %40, label %41, label %42

41:                                               ; preds = %38
  store i32 13, ptr %7, align 4, !tbaa !19
  br label %78

42:                                               ; preds = %38
  %43 = load i32, ptr %6, align 4, !tbaa !19
  %44 = icmp slt i32 %43, 130
  br i1 %44, label %45, label %46

45:                                               ; preds = %42
  store i32 22, ptr %7, align 4, !tbaa !19
  br label %77

46:                                               ; preds = %42
  %47 = load i32, ptr %6, align 4, !tbaa !19
  %48 = icmp slt i32 %47, 160
  br i1 %48, label %49, label %50

49:                                               ; preds = %46
  store i32 14, ptr %7, align 4, !tbaa !19
  br label %76

50:                                               ; preds = %46
  %51 = load i32, ptr %6, align 4, !tbaa !19
  %52 = icmp slt i32 %51, 162
  br i1 %52, label %53, label %54

53:                                               ; preds = %50
  store i32 3, ptr %7, align 4, !tbaa !19
  br label %75

54:                                               ; preds = %50
  %55 = load i32, ptr %6, align 4, !tbaa !19
  %56 = icmp slt i32 %55, 172
  br i1 %56, label %57, label %58

57:                                               ; preds = %54
  store i32 5, ptr %7, align 4, !tbaa !19
  br label %74

58:                                               ; preds = %54
  %59 = load i32, ptr %6, align 4, !tbaa !19
  %60 = icmp slt i32 %59, 192
  br i1 %60, label %61, label %62

61:                                               ; preds = %58
  store i32 20, ptr %7, align 4, !tbaa !19
  br label %73

62:                                               ; preds = %58
  %63 = load i32, ptr %6, align 4, !tbaa !19
  %64 = icmp slt i32 %63, 222
  br i1 %64, label %65, label %66

65:                                               ; preds = %62
  store i32 8, ptr %7, align 4, !tbaa !19
  br label %72

66:                                               ; preds = %62
  %67 = load i32, ptr %6, align 4, !tbaa !19
  %68 = icmp slt i32 %67, 246
  br i1 %68, label %69, label %70

69:                                               ; preds = %66
  store i32 17, ptr %7, align 4, !tbaa !19
  br label %71

70:                                               ; preds = %66
  store i32 15, ptr %7, align 4, !tbaa !19
  br label %71

71:                                               ; preds = %70, %69
  br label %72

72:                                               ; preds = %71, %65
  br label %73

73:                                               ; preds = %72, %61
  br label %74

74:                                               ; preds = %73, %57
  br label %75

75:                                               ; preds = %74, %53
  br label %76

76:                                               ; preds = %75, %49
  br label %77

77:                                               ; preds = %76, %45
  br label %78

78:                                               ; preds = %77, %41
  br label %79

79:                                               ; preds = %78, %37
  br label %80

80:                                               ; preds = %79, %33
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.mobj_s, ptr %81, i32 0, i32 1
  %83 = load i32, ptr %82, align 8, !tbaa !16
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.mobj_s, ptr %84, i32 0, i32 2
  %86 = load i32, ptr %85, align 4, !tbaa !17
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.mobj_s, ptr %87, i32 0, i32 3
  %89 = load i32, ptr %88, align 8, !tbaa !18
  %90 = load i32, ptr %7, align 4, !tbaa !19
  %91 = call ptr @P_SpawnMobj(i32 noundef %83, i32 noundef %86, i32 noundef %89, i32 noundef %90)
  store ptr %91, ptr %3, align 8, !tbaa !5
  %92 = load ptr, ptr %3, align 8, !tbaa !5
  %93 = call i32 @P_LookForPlayers(ptr noundef %92, i32 noundef 1)
  %94 = icmp ne i32 %93, 0
  br i1 %94, label %95, label %103

95:                                               ; preds = %80
  %96 = load ptr, ptr %3, align 8, !tbaa !5
  %97 = load ptr, ptr %3, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.mobj_s, ptr %97, i32 0, i32 21
  %99 = load ptr, ptr %98, align 8, !tbaa !20
  %100 = getelementptr inbounds %struct.mobjinfo_t, ptr %99, i32 0, i32 3
  %101 = load i32, ptr %100, align 4, !tbaa !21
  %102 = call i32 @P_SetMobjState(ptr noundef %96, i32 noundef %101)
  br label %103

103:                                              ; preds = %95, %80
  %104 = load ptr, ptr %3, align 8, !tbaa !5
  %105 = load ptr, ptr %3, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.mobj_s, ptr %105, i32 0, i32 1
  %107 = load i32, ptr %106, align 8, !tbaa !16
  %108 = load ptr, ptr %3, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.mobj_s, ptr %108, i32 0, i32 2
  %110 = load i32, ptr %109, align 4, !tbaa !17
  %111 = call i32 @P_TeleportMove(ptr noundef %104, i32 noundef %107, i32 noundef %110)
  %112 = load ptr, ptr %2, align 8, !tbaa !5
  call void @P_RemoveMobj(ptr noundef %112)
  store i32 0, ptr %8, align 4
  br label %113

113:                                              ; preds = %103, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, 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
  %114 = load i32, ptr %8, align 4
  switch i32 %114, label %116 [
    i32 0, label %115
    i32 1, label %115
  ]

115:                                              ; preds = %113, %113
  ret void

116:                                              ; preds = %113
  unreachable
}

declare i32 @P_TeleportMove(ptr noundef, i32 noundef, i32 noundef) #1

declare void @P_RemoveMobj(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 184}
!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, !6, i64 176}
!16 = !{!10, !12, i64 24}
!17 = !{!10, !12, i64 28}
!18 = !{!10, !12, i64 32}
!19 = !{!12, !12, i64 0}
!20 = !{!10, !6, i64 136}
!21 = !{!22, !12, i64 12}
!22 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52, !12, i64 56, !12, i64 60, !12, i64 64, !12, i64 68, !12, i64 72, !12, i64 76, !12, i64 80, !12, i64 84, !12, i64 88}


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

declare i32 @P_Random() local_unnamed_addr #0

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

declare void @S_StartSound(ptr noundef, i32 noundef) local_unnamed_addr #0

declare i32 @P_SetMobjState(ptr noundef, i32 noundef) local_unnamed_addr #0

declare ptr @P_SpawnMobj(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @A_SpawnFly(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 184
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = add nsw i32 %3, -1
  store i32 %4, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %5, label %53

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 176
  %7 = load ptr, ptr %6, align 8, !tbaa !14
  %8 = getelementptr inbounds i8, ptr %7, i64 24
  %9 = load i32, ptr %8, align 8, !tbaa !15
  %10 = getelementptr inbounds i8, ptr %7, i64 28
  %11 = load i32, ptr %10, align 4, !tbaa !16
  %12 = getelementptr inbounds i8, ptr %7, i64 32
  %13 = load i32, ptr %12, align 8, !tbaa !17
  %14 = tail call ptr @P_SpawnMobj(i32 noundef %9, i32 noundef %11, i32 noundef %13, i32 noundef 29) #2
  tail call void @S_StartSound(ptr noundef %14, i32 noundef 35) #2
  %15 = tail call i32 @P_Random() #2
  %16 = icmp slt i32 %15, 50
  br i1 %16, label %35, label %17

17:                                               ; preds = %5
  %18 = icmp ult i32 %15, 90
  br i1 %18, label %35, label %19

19:                                               ; preds = %17
  %20 = icmp ult i32 %15, 120
  br i1 %20, label %35, label %21

21:                                               ; preds = %19
  %22 = icmp ult i32 %15, 130
  br i1 %22, label %35, label %23

23:                                               ; preds = %21
  %24 = icmp ult i32 %15, 160
  br i1 %24, label %35, label %25

25:                                               ; preds = %23
  %26 = icmp ult i32 %15, 162
  br i1 %26, label %35, label %27

27:                                               ; preds = %25
  %28 = icmp ult i32 %15, 172
  br i1 %28, label %35, label %29

29:                                               ; preds = %27
  %30 = icmp ult i32 %15, 192
  br i1 %30, label %35, label %31

31:                                               ; preds = %29
  %32 = icmp ult i32 %15, 222
  br i1 %32, label %35, label %33

33:                                               ; preds = %31
  %34 = icmp ult i32 %15, 246
  %. = select i1 %34, i32 17, i32 15
  br label %35

35:                                               ; preds = %33, %31, %29, %27, %25, %23, %21, %19, %17, %5
  %.0 = phi i32 [ 11, %5 ], [ 12, %17 ], [ 13, %19 ], [ 22, %21 ], [ 14, %23 ], [ 3, %25 ], [ 5, %27 ], [ 20, %29 ], [ 8, %31 ], [ %., %33 ]
  %36 = load i32, ptr %8, align 8, !tbaa !15
  %37 = load i32, ptr %10, align 4, !tbaa !16
  %38 = load i32, ptr %12, align 8, !tbaa !17
  %39 = tail call ptr @P_SpawnMobj(i32 noundef %36, i32 noundef %37, i32 noundef %38, i32 noundef %.0) #2
  %40 = tail call i32 @P_LookForPlayers(ptr noundef %39, i32 noundef 1)
  %.not27 = icmp eq i32 %40, 0
  br i1 %.not27, label %47, label %41

41:                                               ; preds = %35
  %42 = getelementptr inbounds i8, ptr %39, i64 136
  %43 = load ptr, ptr %42, align 8, !tbaa !18
  %44 = getelementptr inbounds i8, ptr %43, i64 12
  %45 = load i32, ptr %44, align 4, !tbaa !19
  %46 = tail call i32 @P_SetMobjState(ptr noundef %39, i32 noundef %45) #2
  br label %47

47:                                               ; preds = %41, %35
  %48 = getelementptr inbounds i8, ptr %39, i64 24
  %49 = load i32, ptr %48, align 8, !tbaa !15
  %50 = getelementptr inbounds i8, ptr %39, i64 28
  %51 = load i32, ptr %50, align 4, !tbaa !16
  %52 = tail call i32 @P_TeleportMove(ptr noundef %39, i32 noundef %49, i32 noundef %51) #2
  tail call void @P_RemoveMobj(ptr noundef nonnull %0) #2
  br label %53

53:                                               ; preds = %1, %47
  ret void
}

declare i32 @P_TeleportMove(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

declare void @P_RemoveMobj(ptr noundef) local_unnamed_addr #0

attributes #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 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, !11, i64 184}
!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, !8, i64 176}
!15 = !{!6, !11, i64 24}
!16 = !{!6, !11, i64 28}
!17 = !{!6, !11, i64 32}
!18 = !{!6, !8, i64 136}
!19 = !{!20, !11, i64 12}
!20 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68, !11, i64 72, !11, i64 76, !11, i64 80, !11, i64 84, !11, i64 88}


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