; 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_AproxDistance(i32 noundef, i32 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @P_CheckMissileRange(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.mobj_s, ptr %7, i32 0, i32 28
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = call i32 @P_CheckSight(ptr noundef %6, ptr noundef %9)
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %13, label %12

12:                                               ; preds = %1
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %121

13:                                               ; preds = %1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.mobj_s, ptr %14, i32 0, i32 24
  %16 = load i32, ptr %15, align 8, !tbaa !15
  %17 = and i32 %16, 64
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %24

19:                                               ; preds = %13
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mobj_s, ptr %20, i32 0, i32 24
  %22 = load i32, ptr %21, align 8, !tbaa !15
  %23 = and i32 %22, -65
  store i32 %23, ptr %21, align 8, !tbaa !15
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %121

24:                                               ; preds = %13
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.mobj_s, ptr %25, i32 0, i32 29
  %27 = load i32, ptr %26, align 8, !tbaa !16
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %24
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %121

30:                                               ; preds = %24
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mobj_s, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 8, !tbaa !17
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.mobj_s, ptr %34, i32 0, i32 28
  %36 = load ptr, ptr %35, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.mobj_s, ptr %36, i32 0, i32 1
  %38 = load i32, ptr %37, align 8, !tbaa !17
  %39 = sub nsw i32 %33, %38
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.mobj_s, ptr %40, i32 0, i32 2
  %42 = load i32, ptr %41, align 4, !tbaa !18
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.mobj_s, ptr %43, i32 0, i32 28
  %45 = load ptr, ptr %44, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.mobj_s, ptr %45, i32 0, i32 2
  %47 = load i32, ptr %46, align 4, !tbaa !18
  %48 = sub nsw i32 %42, %47
  %49 = call i32 @P_AproxDistance(i32 noundef %39, i32 noundef %48)
  %50 = sub nsw i32 %49, 4194304
  store i32 %50, ptr %4, align 4, !tbaa !19
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.mobj_s, ptr %51, i32 0, i32 21
  %53 = load ptr, ptr %52, align 8, !tbaa !20
  %54 = getelementptr inbounds %struct.mobjinfo_t, ptr %53, i32 0, i32 10
  %55 = load i32, ptr %54, align 4, !tbaa !21
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %60, label %57

57:                                               ; preds = %30
  %58 = load i32, ptr %4, align 4, !tbaa !19
  %59 = sub nsw i32 %58, 8388608
  store i32 %59, ptr %4, align 4, !tbaa !19
  br label %60

60:                                               ; preds = %57, %30
  %61 = load i32, ptr %4, align 4, !tbaa !19
  %62 = ashr i32 %61, 16
  store i32 %62, ptr %4, align 4, !tbaa !19
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.mobj_s, ptr %63, i32 0, i32 20
  %65 = load i32, ptr %64, align 8, !tbaa !23
  %66 = icmp eq i32 %65, 3
  br i1 %66, label %67, label %72

67:                                               ; preds = %60
  %68 = load i32, ptr %4, align 4, !tbaa !19
  %69 = icmp sgt i32 %68, 896
  br i1 %69, label %70, label %71

70:                                               ; preds = %67
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %121

71:                                               ; preds = %67
  br label %72

72:                                               ; preds = %71, %60
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.mobj_s, ptr %73, i32 0, i32 20
  %75 = load i32, ptr %74, align 8, !tbaa !23
  %76 = icmp eq i32 %75, 5
  br i1 %76, label %77, label %84

77:                                               ; preds = %72
  %78 = load i32, ptr %4, align 4, !tbaa !19
  %79 = icmp slt i32 %78, 196
  br i1 %79, label %80, label %81

80:                                               ; preds = %77
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %121

81:                                               ; preds = %77
  %82 = load i32, ptr %4, align 4, !tbaa !19
  %83 = ashr i32 %82, 1
  store i32 %83, ptr %4, align 4, !tbaa !19
  br label %84

84:                                               ; preds = %81, %72
  %85 = load ptr, ptr %3, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.mobj_s, ptr %85, i32 0, i32 20
  %87 = load i32, ptr %86, align 8, !tbaa !23
  %88 = icmp eq i32 %87, 21
  br i1 %88, label %99, label %89

89:                                               ; preds = %84
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.mobj_s, ptr %90, i32 0, i32 20
  %92 = load i32, ptr %91, align 8, !tbaa !23
  %93 = icmp eq i32 %92, 19
  br i1 %93, label %99, label %94

94:                                               ; preds = %89
  %95 = load ptr, ptr %3, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.mobj_s, ptr %95, i32 0, i32 20
  %97 = load i32, ptr %96, align 8, !tbaa !23
  %98 = icmp eq i32 %97, 18
  br i1 %98, label %99, label %102

99:                                               ; preds = %94, %89, %84
  %100 = load i32, ptr %4, align 4, !tbaa !19
  %101 = ashr i32 %100, 1
  store i32 %101, ptr %4, align 4, !tbaa !19
  br label %102

102:                                              ; preds = %99, %94
  %103 = load i32, ptr %4, align 4, !tbaa !19
  %104 = icmp sgt i32 %103, 200
  br i1 %104, label %105, label %106

105:                                              ; preds = %102
  store i32 200, ptr %4, align 4, !tbaa !19
  br label %106

106:                                              ; preds = %105, %102
  %107 = load ptr, ptr %3, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.mobj_s, ptr %107, i32 0, i32 20
  %109 = load i32, ptr %108, align 8, !tbaa !23
  %110 = icmp eq i32 %109, 21
  br i1 %110, label %111, label %115

111:                                              ; preds = %106
  %112 = load i32, ptr %4, align 4, !tbaa !19
  %113 = icmp sgt i32 %112, 160
  br i1 %113, label %114, label %115

114:                                              ; preds = %111
  store i32 160, ptr %4, align 4, !tbaa !19
  br label %115

115:                                              ; preds = %114, %111, %106
  %116 = call i32 @P_Random()
  %117 = load i32, ptr %4, align 4, !tbaa !19
  %118 = icmp slt i32 %116, %117
  br i1 %118, label %119, label %120

119:                                              ; preds = %115
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %121

120:                                              ; preds = %115
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %121

121:                                              ; preds = %120, %119, %80, %70, %29, %19, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %122 = load i32, ptr %2, align 4
  ret i32 %122
}

declare i32 @P_Random() #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, !6, i64 176}
!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 160}
!16 = !{!10, !12, i64 184}
!17 = !{!10, !12, i64 24}
!18 = !{!10, !12, i64 28}
!19 = !{!12, !12, i64 0}
!20 = !{!10, !6, i64 136}
!21 = !{!22, !12, i64 40}
!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}
!23 = !{!10, !12, i64 128}


; 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_AproxDistance(i32 noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @P_CheckMissileRange(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 176
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = tail call i32 @P_CheckSight(ptr noundef %0, ptr noundef %3) #3
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %47, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 160
  %7 = load i32, ptr %6, align 8, !tbaa !14
  %8 = and i32 %7, 64
  %.not32 = icmp eq i32 %8, 0
  br i1 %.not32, label %11, label %9

9:                                                ; preds = %5
  %10 = and i32 %7, -65
  store i32 %10, ptr %6, align 8, !tbaa !14
  br label %47

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %0, i64 184
  %13 = load i32, ptr %12, align 8, !tbaa !15
  %.not33 = icmp eq i32 %13, 0
  br i1 %.not33, label %14, label %47

14:                                               ; preds = %11
  %15 = getelementptr inbounds i8, ptr %0, i64 24
  %16 = load i32, ptr %15, align 8, !tbaa !16
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 24
  %19 = load i32, ptr %18, align 8, !tbaa !16
  %20 = sub nsw i32 %16, %19
  %21 = getelementptr inbounds i8, ptr %0, i64 28
  %22 = load i32, ptr %21, align 4, !tbaa !17
  %23 = getelementptr inbounds i8, ptr %17, i64 28
  %24 = load i32, ptr %23, align 4, !tbaa !17
  %25 = sub nsw i32 %22, %24
  %26 = tail call i32 @P_AproxDistance(i32 noundef %20, i32 noundef %25) #3
  %27 = getelementptr inbounds i8, ptr %0, i64 136
  %28 = load ptr, ptr %27, align 8, !tbaa !18
  %29 = getelementptr inbounds i8, ptr %28, i64 40
  %30 = load i32, ptr %29, align 4, !tbaa !19
  %.not34 = icmp eq i32 %30, 0
  %spec.select.v = select i1 %.not34, i32 -12582912, i32 -4194304
  %spec.select = add nsw i32 %spec.select.v, %26
  %31 = ashr i32 %spec.select, 16
  %32 = getelementptr inbounds i8, ptr %0, i64 128
  %33 = load i32, ptr %32, align 8, !tbaa !21
  %34 = icmp eq i32 %33, 3
  %35 = icmp sgt i32 %31, 896
  %or.cond3 = select i1 %34, i1 %35, i1 false
  br i1 %or.cond3, label %47, label %36

36:                                               ; preds = %14
  switch i32 %33, label %42 [
    i32 5, label %37
    i32 21, label %40
    i32 19, label %40
    i32 18, label %40
  ]

37:                                               ; preds = %36
  %38 = icmp slt i32 %31, 196
  br i1 %38, label %47, label %.thread

.thread:                                          ; preds = %37
  %39 = lshr i32 %31, 1
  br label %42

40:                                               ; preds = %36, %36, %36
  %41 = ashr i32 %spec.select, 17
  br label %42

42:                                               ; preds = %36, %.thread, %40
  %.2 = phi i32 [ %41, %40 ], [ %39, %.thread ], [ %31, %36 ]
  %spec.store.select = tail call i32 @llvm.smin.i32(i32 %.2, i32 200)
  %43 = icmp eq i32 %33, 21
  %44 = icmp sgt i32 %.2, 160
  %or.cond = select i1 %43, i1 %44, i1 false
  %spec.store.select4 = select i1 %or.cond, i32 160, i32 %spec.store.select
  %45 = tail call i32 @P_Random() #3
  %46 = icmp sge i32 %45, %spec.store.select4
  %. = zext i1 %46 to i32
  br label %47

47:                                               ; preds = %42, %37, %14, %11, %1, %9
  %.029 = phi i32 [ 1, %9 ], [ 0, %1 ], [ 0, %11 ], [ 0, %14 ], [ 0, %37 ], [ %., %42 ]
  ret i32 %.029
}

declare i32 @P_Random() local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !8, i64 176}
!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 160}
!15 = !{!6, !11, i64 184}
!16 = !{!6, !11, i64 24}
!17 = !{!6, !11, i64 28}
!18 = !{!6, !8, i64 136}
!19 = !{!20, !11, i64 40}
!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}
!21 = !{!6, !11, i64 128}


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