; 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.subsector_s = type { ptr, i16, i16 }
%struct.sector_t = type { i32, i32, i16, i16, i16, i16, i16, i32, ptr, [4 x i32], %struct.degenmobj_t, i32, ptr, ptr, i32, ptr }
%struct.degenmobj_t = type { %struct.thinker_s, i32, i32, i32 }
%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_CheckSight(ptr noundef, ptr noundef) #1

declare i32 @P_Random() #1

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

; Function Attrs: nounwind uwtable
define dso_local void @A_Look(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.mobj_s, ptr %6, i32 0, i32 30
  store i32 0, ptr %7, align 4, !tbaa !9
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.mobj_s, ptr %8, i32 0, i32 11
  %10 = load ptr, ptr %9, align 8, !tbaa !15
  %11 = getelementptr inbounds %struct.subsector_s, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 8, !tbaa !16
  %13 = getelementptr inbounds %struct.sector_t, ptr %12, i32 0, i32 8
  %14 = load ptr, ptr %13, align 8, !tbaa !18
  store ptr %14, ptr %3, align 8, !tbaa !5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %43

17:                                               ; preds = %1
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.mobj_s, ptr %18, i32 0, i32 24
  %20 = load i32, ptr %19, align 8, !tbaa !21
  %21 = and i32 %20, 4
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %43

23:                                               ; preds = %17
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.mobj_s, ptr %25, i32 0, i32 28
  store ptr %24, ptr %26, align 8, !tbaa !22
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mobj_s, ptr %27, i32 0, i32 24
  %29 = load i32, ptr %28, align 8, !tbaa !21
  %30 = and i32 %29, 32
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %41

32:                                               ; preds = %23
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.mobj_s, ptr %34, i32 0, i32 28
  %36 = load ptr, ptr %35, align 8, !tbaa !22
  %37 = call i32 @P_CheckSight(ptr noundef %33, ptr noundef %36)
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %40

39:                                               ; preds = %32
  br label %49

40:                                               ; preds = %32
  br label %42

41:                                               ; preds = %23
  br label %49

42:                                               ; preds = %40
  br label %43

43:                                               ; preds = %42, %17, %1
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = call i32 @P_LookForPlayers(ptr noundef %44, i32 noundef 0)
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %48, label %47

47:                                               ; preds = %43
  store i32 1, ptr %4, align 4
  br label %100

48:                                               ; preds = %43
  br label %49

49:                                               ; preds = %48, %41, %39
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.mobj_s, ptr %50, i32 0, i32 21
  %52 = load ptr, ptr %51, align 8, !tbaa !23
  %53 = getelementptr inbounds %struct.mobjinfo_t, ptr %52, i32 0, i32 4
  %54 = load i32, ptr %53, align 4, !tbaa !24
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %56, label %92

56:                                               ; preds = %49
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.mobj_s, ptr %57, i32 0, i32 21
  %59 = load ptr, ptr %58, align 8, !tbaa !23
  %60 = getelementptr inbounds %struct.mobjinfo_t, ptr %59, i32 0, i32 4
  %61 = load i32, ptr %60, align 4, !tbaa !24
  switch i32 %61, label %70 [
    i32 36, label %62
    i32 37, label %62
    i32 38, label %62
    i32 39, label %66
    i32 40, label %66
  ]

62:                                               ; preds = %56, %56, %56
  %63 = call i32 @P_Random()
  %64 = srem i32 %63, 3
  %65 = add nsw i32 36, %64
  store i32 %65, ptr %5, align 4, !tbaa !26
  br label %76

66:                                               ; preds = %56, %56
  %67 = call i32 @P_Random()
  %68 = srem i32 %67, 2
  %69 = add nsw i32 39, %68
  store i32 %69, ptr %5, align 4, !tbaa !26
  br label %76

70:                                               ; preds = %56
  %71 = load ptr, ptr %2, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.mobj_s, ptr %71, i32 0, i32 21
  %73 = load ptr, ptr %72, align 8, !tbaa !23
  %74 = getelementptr inbounds %struct.mobjinfo_t, ptr %73, i32 0, i32 4
  %75 = load i32, ptr %74, align 4, !tbaa !24
  store i32 %75, ptr %5, align 4, !tbaa !26
  br label %76

76:                                               ; preds = %70, %66, %62
  %77 = load ptr, ptr %2, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.mobj_s, ptr %77, i32 0, i32 20
  %79 = load i32, ptr %78, align 8, !tbaa !27
  %80 = icmp eq i32 %79, 19
  br i1 %80, label %86, label %81

81:                                               ; preds = %76
  %82 = load ptr, ptr %2, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.mobj_s, ptr %82, i32 0, i32 20
  %84 = load i32, ptr %83, align 8, !tbaa !27
  %85 = icmp eq i32 %84, 21
  br i1 %85, label %86, label %88

86:                                               ; preds = %81, %76
  %87 = load i32, ptr %5, align 4, !tbaa !26
  call void @S_StartSound(ptr noundef null, i32 noundef %87)
  br label %91

88:                                               ; preds = %81
  %89 = load ptr, ptr %2, align 8, !tbaa !5
  %90 = load i32, ptr %5, align 4, !tbaa !26
  call void @S_StartSound(ptr noundef %89, i32 noundef %90)
  br label %91

91:                                               ; preds = %88, %86
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %92

92:                                               ; preds = %91, %49
  %93 = load ptr, ptr %2, align 8, !tbaa !5
  %94 = load ptr, ptr %2, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.mobj_s, ptr %94, i32 0, i32 21
  %96 = load ptr, ptr %95, align 8, !tbaa !23
  %97 = getelementptr inbounds %struct.mobjinfo_t, ptr %96, i32 0, i32 3
  %98 = load i32, ptr %97, align 4, !tbaa !28
  %99 = call i32 @P_SetMobjState(ptr noundef %93, i32 noundef %98)
  store i32 0, ptr %4, align 4
  br label %100

100:                                              ; preds = %92, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %101 = load i32, ptr %4, align 4
  switch i32 %101, label %103 [
    i32 0, label %102
    i32 1, label %102
  ]

102:                                              ; preds = %100, %100
  ret void

103:                                              ; preds = %100
  unreachable
}

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

declare i32 @P_SetMobjState(ptr noundef, i32 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 188}
!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 88}
!16 = !{!17, !6, i64 0}
!17 = !{!"subsector_s", !6, i64 0, !14, i64 8, !14, i64 10}
!18 = !{!19, !6, i64 24}
!19 = !{!"", !12, i64 0, !12, i64 4, !14, i64 8, !14, i64 10, !14, i64 12, !14, i64 14, !14, i64 16, !12, i64 20, !6, i64 24, !7, i64 32, !20, i64 48, !12, i64 88, !6, i64 96, !6, i64 104, !12, i64 112, !6, i64 120}
!20 = !{!"", !11, i64 0, !12, i64 24, !12, i64 28, !12, i64 32}
!21 = !{!10, !12, i64 160}
!22 = !{!10, !6, i64 176}
!23 = !{!10, !6, i64 136}
!24 = !{!25, !12, i64 16}
!25 = !{!"", !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}
!26 = !{!12, !12, i64 0}
!27 = !{!10, !12, i64 128}
!28 = !{!25, !12, i64 12}


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

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

; Function Attrs: nounwind uwtable
define dso_local void @A_Look(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 188
  store i32 0, ptr %2, align 4, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 88
  %4 = load ptr, ptr %3, align 8, !tbaa !14
  %5 = load ptr, ptr %4, align 8, !tbaa !15
  %6 = getelementptr inbounds i8, ptr %5, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !17
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %19, label %8

8:                                                ; preds = %1
  %9 = getelementptr inbounds i8, ptr %7, i64 160
  %10 = load i32, ptr %9, align 8, !tbaa !20
  %11 = and i32 %10, 4
  %.not22 = icmp eq i32 %11, 0
  br i1 %.not22, label %19, label %12

12:                                               ; preds = %8
  %13 = getelementptr inbounds i8, ptr %0, i64 176
  store ptr %7, ptr %13, align 8, !tbaa !21
  %14 = getelementptr inbounds i8, ptr %0, i64 160
  %15 = load i32, ptr %14, align 8, !tbaa !20
  %16 = and i32 %15, 32
  %.not23 = icmp eq i32 %16, 0
  br i1 %.not23, label %21, label %17

17:                                               ; preds = %12
  %18 = tail call i32 @P_CheckSight(ptr noundef nonnull %0, ptr noundef nonnull %7) #2
  %.not24 = icmp eq i32 %18, 0
  br i1 %.not24, label %19, label %21

19:                                               ; preds = %17, %8, %1
  %20 = tail call i32 @P_LookForPlayers(ptr noundef nonnull %0, i32 noundef 0)
  %.not25 = icmp eq i32 %20, 0
  br i1 %.not25, label %40, label %21

21:                                               ; preds = %19, %12, %17
  %22 = getelementptr inbounds i8, ptr %0, i64 136
  %23 = load ptr, ptr %22, align 8, !tbaa !22
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %25 = load i32, ptr %24, align 4, !tbaa !23
  switch i32 %25, label %30 [
    i32 0, label %35
    i32 36, label %.sink.split
    i32 37, label %.sink.split
    i32 38, label %.sink.split
    i32 39, label %26
    i32 40, label %26
  ]

26:                                               ; preds = %21, %21
  br label %.sink.split

.sink.split:                                      ; preds = %21, %21, %21, %26
  %.sink29 = phi i32 [ 2, %26 ], [ 3, %21 ], [ 3, %21 ], [ 3, %21 ]
  %.sink27 = phi i32 [ 39, %26 ], [ 36, %21 ], [ 36, %21 ], [ 36, %21 ]
  %27 = tail call i32 @P_Random() #2
  %28 = srem i32 %27, %.sink29
  %29 = add nsw i32 %28, %.sink27
  br label %30

30:                                               ; preds = %.sink.split, %21
  %.0 = phi i32 [ %25, %21 ], [ %29, %.sink.split ]
  %31 = getelementptr inbounds i8, ptr %0, i64 128
  %32 = load i32, ptr %31, align 8, !tbaa !25
  switch i32 %32, label %34 [
    i32 19, label %33
    i32 21, label %33
  ]

33:                                               ; preds = %30, %30
  tail call void @S_StartSound(ptr noundef null, i32 noundef %.0) #2
  br label %35

34:                                               ; preds = %30
  tail call void @S_StartSound(ptr noundef nonnull %0, i32 noundef %.0) #2
  br label %35

35:                                               ; preds = %33, %34, %21
  %36 = load ptr, ptr %22, align 8, !tbaa !22
  %37 = getelementptr inbounds i8, ptr %36, i64 12
  %38 = load i32, ptr %37, align 4, !tbaa !26
  %39 = tail call i32 @P_SetMobjState(ptr noundef nonnull %0, i32 noundef %38) #2
  br label %40

40:                                               ; preds = %19, %35
  ret void
}

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

declare i32 @P_SetMobjState(ptr noundef, i32 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 188}
!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 88}
!15 = !{!16, !8, i64 0}
!16 = !{!"subsector_s", !8, i64 0, !13, i64 8, !13, i64 10}
!17 = !{!18, !8, i64 24}
!18 = !{!"", !11, i64 0, !11, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !11, i64 20, !8, i64 24, !9, i64 32, !19, i64 48, !11, i64 88, !8, i64 96, !8, i64 104, !11, i64 112, !8, i64 120}
!19 = !{!"", !7, i64 0, !11, i64 24, !11, i64 28, !11, i64 32}
!20 = !{!6, !11, i64 160}
!21 = !{!6, !8, i64 176}
!22 = !{!6, !8, i64 136}
!23 = !{!24, !11, i64 16}
!24 = !{!"", !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}
!25 = !{!6, !11, i64 128}
!26 = !{!24, !11, i64 12}


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