; 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.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.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }
%struct.line_s = type { ptr, ptr, i32, i32, i16, i16, i16, [2 x i16], [4 x i32], i32, ptr, ptr, i32, ptr }
%struct.side_t = type { i32, i32, i16, i16, i16, ptr }

@validcount = external global i32, align 4
@soundtarget = external dso_local global ptr, align 8
@openrange = external global i32, align 4
@sides = external global ptr, align 8

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

14:                                               ; preds = %2
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.sector_t, ptr %15, i32 0, i32 7
  %17 = load i32, ptr %16, align 4, !tbaa !16
  %18 = load i32, ptr %4, align 4, !tbaa !9
  %19 = add nsw i32 %18, 1
  %20 = icmp sle i32 %17, %19
  br i1 %20, label %21, label %22

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

22:                                               ; preds = %14, %2
  %23 = load i32, ptr @validcount, align 4, !tbaa !9
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.sector_t, ptr %24, i32 0, i32 11
  store i32 %23, ptr %25, align 8, !tbaa !11
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.sector_t, ptr %28, i32 0, i32 7
  store i32 %27, ptr %29, align 4, !tbaa !16
  %30 = load ptr, ptr @soundtarget, align 8, !tbaa !5
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.sector_t, ptr %31, i32 0, i32 8
  store ptr %30, ptr %32, align 8, !tbaa !17
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %108, %22
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.sector_t, ptr %35, i32 0, i32 14
  %37 = load i32, ptr %36, align 8, !tbaa !18
  %38 = icmp slt i32 %34, %37
  br i1 %38, label %39, label %111

39:                                               ; preds = %33
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.sector_t, ptr %40, i32 0, i32 15
  %42 = load ptr, ptr %41, align 8, !tbaa !19
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds ptr, ptr %42, i64 %44
  %46 = load ptr, ptr %45, align 8, !tbaa !5
  store ptr %46, ptr %6, align 8, !tbaa !5
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.line_s, ptr %47, i32 0, i32 4
  %49 = load i16, ptr %48, align 8, !tbaa !20
  %50 = sext i16 %49 to i32
  %51 = and i32 %50, 4
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %54, label %53

53:                                               ; preds = %39
  br label %108

54:                                               ; preds = %39
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  call void @P_LineOpening(ptr noundef %55)
  %56 = load i32, ptr @openrange, align 4, !tbaa !9
  %57 = icmp sle i32 %56, 0
  br i1 %57, label %58, label %59

58:                                               ; preds = %54
  br label %108

59:                                               ; preds = %54
  %60 = load ptr, ptr @sides, align 8, !tbaa !5
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.line_s, ptr %61, i32 0, i32 7
  %63 = getelementptr inbounds [2 x i16], ptr %62, i64 0, i64 0
  %64 = load i16, ptr %63, align 2, !tbaa !22
  %65 = sext i16 %64 to i64
  %66 = getelementptr inbounds %struct.side_t, ptr %60, i64 %65
  %67 = getelementptr inbounds %struct.side_t, ptr %66, i32 0, i32 5
  %68 = load ptr, ptr %67, align 8, !tbaa !23
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = icmp eq ptr %68, %69
  br i1 %70, label %71, label %81

71:                                               ; preds = %59
  %72 = load ptr, ptr @sides, align 8, !tbaa !5
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.line_s, ptr %73, i32 0, i32 7
  %75 = getelementptr inbounds [2 x i16], ptr %74, i64 0, i64 1
  %76 = load i16, ptr %75, align 2, !tbaa !22
  %77 = sext i16 %76 to i64
  %78 = getelementptr inbounds %struct.side_t, ptr %72, i64 %77
  %79 = getelementptr inbounds %struct.side_t, ptr %78, i32 0, i32 5
  %80 = load ptr, ptr %79, align 8, !tbaa !23
  store ptr %80, ptr %7, align 8, !tbaa !5
  br label %91

81:                                               ; preds = %59
  %82 = load ptr, ptr @sides, align 8, !tbaa !5
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.line_s, ptr %83, i32 0, i32 7
  %85 = getelementptr inbounds [2 x i16], ptr %84, i64 0, i64 0
  %86 = load i16, ptr %85, align 2, !tbaa !22
  %87 = sext i16 %86 to i64
  %88 = getelementptr inbounds %struct.side_t, ptr %82, i64 %87
  %89 = getelementptr inbounds %struct.side_t, ptr %88, i32 0, i32 5
  %90 = load ptr, ptr %89, align 8, !tbaa !23
  store ptr %90, ptr %7, align 8, !tbaa !5
  br label %91

91:                                               ; preds = %81, %71
  %92 = load ptr, ptr %6, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.line_s, ptr %92, i32 0, i32 4
  %94 = load i16, ptr %93, align 8, !tbaa !20
  %95 = sext i16 %94 to i32
  %96 = and i32 %95, 64
  %97 = icmp ne i32 %96, 0
  br i1 %97, label %98, label %104

98:                                               ; preds = %91
  %99 = load i32, ptr %4, align 4, !tbaa !9
  %100 = icmp ne i32 %99, 0
  br i1 %100, label %103, label %101

101:                                              ; preds = %98
  %102 = load ptr, ptr %7, align 8, !tbaa !5
  call void @P_RecursiveSound(ptr noundef %102, i32 noundef 1)
  br label %103

103:                                              ; preds = %101, %98
  br label %107

104:                                              ; preds = %91
  %105 = load ptr, ptr %7, align 8, !tbaa !5
  %106 = load i32, ptr %4, align 4, !tbaa !9
  call void @P_RecursiveSound(ptr noundef %105, i32 noundef %106)
  br label %107

107:                                              ; preds = %104, %103
  br label %108

108:                                              ; preds = %107, %58, %53
  %109 = load i32, ptr %5, align 4, !tbaa !9
  %110 = add nsw i32 %109, 1
  store i32 %110, ptr %5, align 4, !tbaa !9
  br label %33, !llvm.loop !25

111:                                              ; preds = %33
  store i32 0, ptr %8, align 4
  br label %112

112:                                              ; preds = %111, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %113 = load i32, ptr %8, align 4
  switch i32 %113, label %115 [
    i32 0, label %114
    i32 1, label %114
  ]

114:                                              ; preds = %112, %112
  ret void

115:                                              ; preds = %112
  unreachable
}

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

declare void @P_LineOpening(ptr noundef) #2

; 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 = { "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 88}
!12 = !{!"", !10, i64 0, !10, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !10, i64 20, !6, i64 24, !7, i64 32, !14, i64 48, !10, i64 88, !6, i64 96, !6, i64 104, !10, i64 112, !6, i64 120}
!13 = !{!"short", !7, i64 0}
!14 = !{!"", !15, i64 0, !10, i64 24, !10, i64 28, !10, i64 32}
!15 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!16 = !{!12, !10, i64 20}
!17 = !{!12, !6, i64 24}
!18 = !{!12, !10, i64 112}
!19 = !{!12, !6, i64 120}
!20 = !{!21, !13, i64 24}
!21 = !{!"line_s", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !13, i64 24, !13, i64 26, !13, i64 28, !7, i64 30, !7, i64 36, !10, i64 52, !6, i64 56, !6, i64 64, !10, i64 72, !6, i64 80}
!22 = !{!13, !13, i64 0}
!23 = !{!24, !6, i64 16}
!24 = !{!"", !10, i64 0, !10, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !6, i64 16}
!25 = distinct !{!25, !26}
!26 = !{!"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.side_t = type { i32, i32, i16, i16, i16, ptr }

@validcount = external local_unnamed_addr global i32, align 4
@soundtarget = external dso_local local_unnamed_addr global ptr, align 8
@openrange = external local_unnamed_addr global i32, align 4
@sides = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @P_RecursiveSound(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 88
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = load i32, ptr @validcount, align 4, !tbaa !14
  %6 = icmp eq i32 %4, %5
  br i1 %6, label %7, label %._crit_edge

._crit_edge:                                      ; preds = %2
  %.pre = add nsw i32 %1, 1
  br label %11

7:                                                ; preds = %2
  %8 = getelementptr inbounds i8, ptr %0, i64 20
  %9 = load i32, ptr %8, align 4, !tbaa !15
  %10 = add nsw i32 %1, 1
  %.not = icmp sgt i32 %9, %10
  br i1 %.not, label %11, label %.loopexit

11:                                               ; preds = %._crit_edge, %7
  %.pre-phi = phi i32 [ %.pre, %._crit_edge ], [ %10, %7 ]
  store i32 %5, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %0, i64 20
  store i32 %.pre-phi, ptr %12, align 4, !tbaa !15
  %13 = load ptr, ptr @soundtarget, align 8, !tbaa !16
  %14 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr %13, ptr %14, align 8, !tbaa !17
  %15 = getelementptr inbounds i8, ptr %0, i64 112
  %16 = load i32, ptr %15, align 8, !tbaa !18
  %17 = icmp sgt i32 %16, 0
  br i1 %17, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %11
  %18 = getelementptr inbounds i8, ptr %0, i64 120
  %.not26 = icmp eq i32 %1, 0
  br i1 %.not26, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %44
  %indvars.iv30 = phi i64 [ %indvars.iv.next31, %44 ], [ 0, %.lr.ph ]
  %19 = load ptr, ptr %18, align 8, !tbaa !19
  %20 = getelementptr inbounds ptr, ptr %19, i64 %indvars.iv30
  %21 = load ptr, ptr %20, align 8, !tbaa !16
  %22 = getelementptr inbounds i8, ptr %21, i64 24
  %23 = load i16, ptr %22, align 8, !tbaa !20
  %24 = and i16 %23, 4
  %.not24.us = icmp eq i16 %24, 0
  br i1 %.not24.us, label %44, label %25

25:                                               ; preds = %.lr.ph.split.us
  tail call void @P_LineOpening(ptr noundef nonnull %21) #2
  %26 = load i32, ptr @openrange, align 4, !tbaa !14
  %27 = icmp slt i32 %26, 1
  br i1 %27, label %44, label %28

28:                                               ; preds = %25
  %29 = load ptr, ptr @sides, align 8, !tbaa !16
  %30 = getelementptr inbounds i8, ptr %21, i64 30
  %31 = load i16, ptr %30, align 2, !tbaa !22
  %32 = sext i16 %31 to i64
  %33 = getelementptr inbounds %struct.side_t, ptr %29, i64 %32, i32 5
  %34 = load ptr, ptr %33, align 8, !tbaa !23
  %35 = icmp eq ptr %34, %0
  br i1 %35, label %36, label %.sink.split

36:                                               ; preds = %28
  %37 = getelementptr inbounds i8, ptr %21, i64 32
  %38 = load i16, ptr %37, align 2, !tbaa !22
  %39 = sext i16 %38 to i64
  %40 = getelementptr inbounds %struct.side_t, ptr %29, i64 %39, i32 5
  %41 = load ptr, ptr %40, align 8, !tbaa !23
  br label %.sink.split

.sink.split:                                      ; preds = %36, %28
  %.0.us = phi ptr [ %41, %36 ], [ %34, %28 ]
  %42 = load i16, ptr %22, align 8, !tbaa !20
  %43 = lshr i16 %42, 6
  %.lobit = and i16 %43, 1
  %. = zext nneg i16 %.lobit to i32
  tail call void @P_RecursiveSound(ptr noundef %.0.us, i32 noundef %.)
  br label %44

44:                                               ; preds = %.sink.split, %25, %.lr.ph.split.us
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %45 = load i32, ptr %15, align 8, !tbaa !18
  %46 = sext i32 %45 to i64
  %47 = icmp slt i64 %indvars.iv.next31, %46
  br i1 %47, label %.lr.ph.split.us, label %.loopexit, !llvm.loop !25

.lr.ph.split:                                     ; preds = %.lr.ph, %75
  %indvars.iv = phi i64 [ %indvars.iv.next, %75 ], [ 0, %.lr.ph ]
  %48 = load ptr, ptr %18, align 8, !tbaa !19
  %49 = getelementptr inbounds ptr, ptr %48, i64 %indvars.iv
  %50 = load ptr, ptr %49, align 8, !tbaa !16
  %51 = getelementptr inbounds i8, ptr %50, i64 24
  %52 = load i16, ptr %51, align 8, !tbaa !20
  %53 = and i16 %52, 4
  %.not24 = icmp eq i16 %53, 0
  br i1 %.not24, label %75, label %54

54:                                               ; preds = %.lr.ph.split
  tail call void @P_LineOpening(ptr noundef nonnull %50) #2
  %55 = load i32, ptr @openrange, align 4, !tbaa !14
  %56 = icmp slt i32 %55, 1
  br i1 %56, label %75, label %57

57:                                               ; preds = %54
  %58 = load ptr, ptr @sides, align 8, !tbaa !16
  %59 = getelementptr inbounds i8, ptr %50, i64 30
  %60 = load i16, ptr %59, align 2, !tbaa !22
  %61 = sext i16 %60 to i64
  %62 = getelementptr inbounds %struct.side_t, ptr %58, i64 %61, i32 5
  %63 = load ptr, ptr %62, align 8, !tbaa !23
  %64 = icmp eq ptr %63, %0
  br i1 %64, label %65, label %71

65:                                               ; preds = %57
  %66 = getelementptr inbounds i8, ptr %50, i64 32
  %67 = load i16, ptr %66, align 2, !tbaa !22
  %68 = sext i16 %67 to i64
  %69 = getelementptr inbounds %struct.side_t, ptr %58, i64 %68, i32 5
  %70 = load ptr, ptr %69, align 8, !tbaa !23
  br label %71

71:                                               ; preds = %57, %65
  %.0 = phi ptr [ %70, %65 ], [ %63, %57 ]
  %72 = load i16, ptr %51, align 8, !tbaa !20
  %73 = and i16 %72, 64
  %.not25 = icmp eq i16 %73, 0
  br i1 %.not25, label %74, label %75

74:                                               ; preds = %71
  tail call void @P_RecursiveSound(ptr noundef %.0, i32 noundef %1)
  br label %75

75:                                               ; preds = %71, %74, %54, %.lr.ph.split
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %76 = load i32, ptr %15, align 8, !tbaa !18
  %77 = sext i32 %76 to i64
  %78 = icmp slt i64 %indvars.iv.next, %77
  br i1 %78, label %.lr.ph.split, label %.loopexit, !llvm.loop !25

.loopexit:                                        ; preds = %75, %44, %11, %7
  ret void
}

declare void @P_LineOpening(ptr noundef) local_unnamed_addr #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 = { "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, !7, i64 88}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 10, !10, i64 12, !10, i64 14, !10, i64 16, !7, i64 20, !11, i64 24, !8, i64 32, !12, i64 48, !7, i64 88, !11, i64 96, !11, i64 104, !7, i64 112, !11, i64 120}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"", !13, i64 0, !7, i64 24, !7, i64 28, !7, i64 32}
!13 = !{!"thinker_s", !11, i64 0, !11, i64 8, !8, i64 16}
!14 = !{!7, !7, i64 0}
!15 = !{!6, !7, i64 20}
!16 = !{!11, !11, i64 0}
!17 = !{!6, !11, i64 24}
!18 = !{!6, !7, i64 112}
!19 = !{!6, !11, i64 120}
!20 = !{!21, !10, i64 24}
!21 = !{!"line_s", !11, i64 0, !11, i64 8, !7, i64 16, !7, i64 20, !10, i64 24, !10, i64 26, !10, i64 28, !8, i64 30, !8, i64 36, !7, i64 52, !11, i64 56, !11, i64 64, !7, i64 72, !11, i64 80}
!22 = !{!10, !10, i64 0}
!23 = !{!24, !11, i64 16}
!24 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 10, !10, i64 12, !11, i64 16}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}


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