; 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.line_s = type { ptr, ptr, i32, i32, i16, i16, i16, [2 x i16], [4 x i32], i32, ptr, ptr, i32, 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

; 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

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

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

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

17:                                               ; preds = %12
  br label %38

18:                                               ; preds = %12
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %19, ptr %4, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = icmp ne ptr %20, %21
  br i1 %22, label %23, label %37

23:                                               ; preds = %18
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.mobj_s, ptr %24, i32 0, i32 20
  %26 = load i32, ptr %25, align 8, !tbaa !12
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mobj_s, ptr %27, i32 0, i32 20
  %29 = load i32, ptr %28, align 8, !tbaa !12
  %30 = icmp eq i32 %26, %29
  br i1 %30, label %31, label %37

31:                                               ; preds = %23
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mobj_s, ptr %32, i32 0, i32 25
  %34 = load i32, ptr %33, align 4, !tbaa !17
  %35 = icmp sgt i32 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %31
  store i32 1, ptr %6, align 4
  br label %45

37:                                               ; preds = %31, %23, %18
  br label %38

38:                                               ; preds = %37, %17
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.thinker_s, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !9
  store ptr %41, ptr %3, align 8, !tbaa !5
  br label %9, !llvm.loop !18

42:                                               ; preds = %9
  %43 = getelementptr inbounds %struct.line_s, ptr %5, i32 0, i32 6
  store i16 666, ptr %43, align 4, !tbaa !20
  %44 = call i32 @EV_DoDoor(ptr noundef %5, i32 noundef 3)
  store i32 0, ptr %6, align 4
  br label %45

45:                                               ; preds = %42, %36
  call void @llvm.lifetime.end.p0(i64 88, 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
  %46 = load i32, ptr %6, align 4
  switch i32 %46, label %48 [
    i32 0, label %47
    i32 1, label %47
  ]

47:                                               ; preds = %45, %45
  ret void

48:                                               ; preds = %45
  unreachable
}

; Function Attrs: nounwind uwtable
declare dso_local void @A_Fall(ptr noundef) #1

declare void @P_MobjThinker(ptr noundef) #2

declare i32 @EV_DoDoor(ptr noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 8}
!10 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !14, i64 128}
!13 = !{!"mobj_s", !10, i64 0, !14, i64 24, !14, i64 28, !14, i64 32, !6, i64 40, !6, i64 48, !14, i64 56, !14, i64 60, !14, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !14, i64 96, !14, i64 100, !14, i64 104, !14, i64 108, !14, i64 112, !14, i64 116, !14, i64 120, !14, i64 124, !14, i64 128, !6, i64 136, !14, i64 144, !6, i64 152, !14, i64 160, !14, i64 164, !14, i64 168, !14, i64 172, !6, i64 176, !14, i64 184, !14, i64 188, !6, i64 192, !14, i64 200, !15, i64 204, !6, i64 216}
!14 = !{!"int", !7, i64 0}
!15 = !{!"", !16, i64 0, !16, i64 2, !16, i64 4, !16, i64 6, !16, i64 8}
!16 = !{!"short", !7, i64 0}
!17 = !{!13, !14, i64 164}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!21, !16, i64 28}
!21 = !{!"line_s", !6, i64 0, !6, i64 8, !14, i64 16, !14, i64 20, !16, i64 24, !16, i64 26, !16, i64 28, !7, i64 30, !7, i64 36, !14, i64 52, !6, i64 56, !6, i64 64, !14, i64 72, !6, i64 80}


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

@thinkercap = external global %struct.thinker_s, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local void @A_KeenDie(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca %struct.line_s, align 8
  call void @llvm.lifetime.start.p0(i64 88, ptr nonnull %2) #3
  tail call void @A_Fall(ptr noundef %0)
  %.013 = load ptr, ptr getelementptr inbounds (i8, ptr @thinkercap, i64 8), align 8, !tbaa !5
  %.not14 = icmp eq ptr %.013, @thinkercap
  br i1 %.not14, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 128
  br label %4

4:                                                ; preds = %.lr.ph, %16
  %.015 = phi ptr [ %.013, %.lr.ph ], [ %.0, %16 ]
  %5 = getelementptr inbounds i8, ptr %.015, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !10
  %.not11 = icmp ne ptr %6, @P_MobjThinker
  %.not12 = icmp eq ptr %.015, %0
  %or.cond = or i1 %.not12, %.not11
  br i1 %or.cond, label %16, label %7

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %.015, i64 128
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = load i32, ptr %3, align 8, !tbaa !11
  %11 = icmp eq i32 %9, %10
  br i1 %11, label %12, label %16

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %.015, i64 164
  %14 = load i32, ptr %13, align 4, !tbaa !16
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.loopexit, label %16

16:                                               ; preds = %7, %12, %4
  %17 = getelementptr inbounds i8, ptr %.015, i64 8
  %.0 = load ptr, ptr %17, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, @thinkercap
  br i1 %.not, label %._crit_edge, label %4, !llvm.loop !17

._crit_edge:                                      ; preds = %16, %1
  %18 = getelementptr inbounds i8, ptr %2, i64 28
  store i16 666, ptr %18, align 4, !tbaa !19
  %19 = call i32 @EV_DoDoor(ptr noundef nonnull %2, i32 noundef 3) #3
  br label %.loopexit

.loopexit:                                        ; preds = %12, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 88, ptr nonnull %2) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @A_Fall(ptr noundef) local_unnamed_addr #1

declare void @P_MobjThinker(ptr noundef) #2

declare i32 @EV_DoDoor(ptr noundef, i32 noundef) local_unnamed_addr #2

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


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