; 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.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }
%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.mapthing_t = type { i16, i16, i16, i16, i16 }

@thinkercap = external global %struct.thinker_s, align 8
@numbraintargets = external dso_local global i32, align 4
@braintargeton = external dso_local global i32, align 4
@braintargets = external dso_local global [32 x ptr], align 16

; 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 void @P_MobjThinker(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @A_BrainAwake(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  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
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 0, ptr @numbraintargets, align 4, !tbaa !9
  store i32 0, ptr @braintargeton, align 4, !tbaa !9
  %6 = load ptr, ptr getelementptr inbounds (%struct.thinker_s, ptr @thinkercap, i32 0, i32 1), align 8, !tbaa !11
  store ptr %6, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr getelementptr inbounds (%struct.thinker_s, ptr @thinkercap, i32 0, i32 1), align 8, !tbaa !11
  store ptr %7, ptr %3, align 8, !tbaa !5
  br label %8

8:                                                ; preds = %31, %1
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp ne ptr %9, @thinkercap
  br i1 %10, label %11, label %35

11:                                               ; preds = %8
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.thinker_s, ptr %12, i32 0, i32 2
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %15 = icmp ne ptr %14, @P_MobjThinker
  br i1 %15, label %16, label %17

16:                                               ; preds = %11
  br label %31

17:                                               ; preds = %11
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %18, ptr %4, align 8, !tbaa !5
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mobj_s, ptr %19, i32 0, i32 20
  %21 = load i32, ptr %20, align 8, !tbaa !14
  %22 = icmp eq i32 %21, 27
  br i1 %22, label %23, label %30

23:                                               ; preds = %17
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i32, ptr @numbraintargets, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [32 x ptr], ptr @braintargets, i64 0, i64 %26
  store ptr %24, ptr %27, align 8, !tbaa !5
  %28 = load i32, ptr @numbraintargets, align 4, !tbaa !9
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr @numbraintargets, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %23, %17
  br label %31

31:                                               ; preds = %30, %16
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.thinker_s, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !11
  store ptr %34, ptr %3, align 8, !tbaa !5
  br label %8, !llvm.loop !18

35:                                               ; preds = %8
  call void @S_StartSound(ptr noundef null, i32 noundef 96)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !10, i64 128}
!15 = !{!"mobj_s", !12, i64 0, !10, i64 24, !10, i64 28, !10, i64 32, !6, i64 40, !6, i64 48, !10, i64 56, !10, i64 60, !10, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !10, i64 96, !10, i64 100, !10, i64 104, !10, i64 108, !10, i64 112, !10, i64 116, !10, i64 120, !10, i64 124, !10, i64 128, !6, i64 136, !10, i64 144, !6, i64 152, !10, i64 160, !10, i64 164, !10, i64 168, !10, i64 172, !6, i64 176, !10, i64 184, !10, i64 188, !6, i64 192, !10, i64 200, !16, i64 204, !6, i64 216}
!16 = !{!"", !17, i64 0, !17, i64 2, !17, i64 4, !17, i64 6, !17, i64 8}
!17 = !{!"short", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"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.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }

@thinkercap = external global %struct.thinker_s, align 8
@numbraintargets = external dso_local local_unnamed_addr global i32, align 4
@braintargeton = external dso_local local_unnamed_addr global i32, align 4
@braintargets = external dso_local local_unnamed_addr global [32 x ptr], align 16

declare void @P_MobjThinker(ptr noundef) #0

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

; Function Attrs: nounwind uwtable
define dso_local void @A_BrainAwake(ptr nocapture noundef readnone %0) local_unnamed_addr #1 {
  store i32 0, ptr @numbraintargets, align 4, !tbaa !5
  store i32 0, ptr @braintargeton, align 4, !tbaa !5
  %.08 = load ptr, ptr getelementptr inbounds (i8, ptr @thinkercap, i64 8), align 8, !tbaa !9
  %.not9 = icmp eq ptr %.08, @thinkercap
  br i1 %.not9, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %13
  %.010 = phi ptr [ %.0, %13 ], [ %.08, %1 ]
  %2 = phi i32 [ %14, %13 ], [ 0, %1 ]
  %3 = getelementptr inbounds i8, ptr %.010, i64 16
  %4 = load ptr, ptr %3, align 8, !tbaa !12
  %.not7 = icmp eq ptr %4, @P_MobjThinker
  br i1 %.not7, label %5, label %13

5:                                                ; preds = %.lr.ph
  %6 = getelementptr inbounds i8, ptr %.010, i64 128
  %7 = load i32, ptr %6, align 8, !tbaa !13
  %8 = icmp eq i32 %7, 27
  br i1 %8, label %9, label %13

9:                                                ; preds = %5
  %10 = sext i32 %2 to i64
  %11 = getelementptr inbounds [32 x ptr], ptr @braintargets, i64 0, i64 %10
  store ptr %.010, ptr %11, align 8, !tbaa !17
  %12 = add nsw i32 %2, 1
  store i32 %12, ptr @numbraintargets, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %5, %9, %.lr.ph
  %14 = phi i32 [ %2, %5 ], [ %12, %9 ], [ %2, %.lr.ph ]
  %15 = getelementptr inbounds i8, ptr %.010, i64 8
  %.0 = load ptr, ptr %15, align 8, !tbaa !9
  %.not = icmp eq ptr %.0, @thinkercap
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %13, %1
  tail call void @S_StartSound(ptr noundef null, i32 noundef 96) #2
  ret void
}

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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"thinker_s", !11, i64 0, !11, i64 8, !7, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !6, i64 128}
!14 = !{!"mobj_s", !10, i64 0, !6, i64 24, !6, i64 28, !6, i64 32, !11, i64 40, !11, i64 48, !6, i64 56, !6, i64 60, !6, i64 64, !11, i64 72, !11, i64 80, !11, i64 88, !6, i64 96, !6, i64 100, !6, i64 104, !6, i64 108, !6, i64 112, !6, i64 116, !6, i64 120, !6, i64 124, !6, i64 128, !11, i64 136, !6, i64 144, !11, i64 152, !6, i64 160, !6, i64 164, !6, i64 168, !6, i64 172, !11, i64 176, !6, i64 184, !6, i64 188, !11, i64 192, !6, i64 200, !15, i64 204, !11, i64 216}
!15 = !{!"", !16, i64 0, !16, i64 2, !16, i64 4, !16, i64 6, !16, i64 8}
!16 = !{!"short", !7, i64 0}
!17 = !{!11, !11, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass