; 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 }

; 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_Random() #1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @A_BrainScream(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.mobj_s, ptr %7, i32 0, i32 1
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = sub nsw i32 %9, 12845056
  store i32 %10, ptr %3, align 4, !tbaa !15
  br label %11

11:                                               ; preds = %51, %1
  %12 = load i32, ptr %3, align 4, !tbaa !15
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.mobj_s, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 8, !tbaa !9
  %16 = add nsw i32 %15, 20971520
  %17 = icmp slt i32 %12, %16
  br i1 %17, label %18, label %54

18:                                               ; preds = %11
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mobj_s, ptr %19, i32 0, i32 2
  %21 = load i32, ptr %20, align 4, !tbaa !16
  %22 = sub nsw i32 %21, 20971520
  store i32 %22, ptr %4, align 4, !tbaa !15
  %23 = call i32 @P_Random()
  %24 = mul nsw i32 %23, 2
  %25 = mul nsw i32 %24, 65536
  %26 = add nsw i32 128, %25
  store i32 %26, ptr %5, align 4, !tbaa !15
  %27 = load i32, ptr %3, align 4, !tbaa !15
  %28 = load i32, ptr %4, align 4, !tbaa !15
  %29 = load i32, ptr %5, align 4, !tbaa !15
  %30 = call ptr @P_SpawnMobj(i32 noundef %27, i32 noundef %28, i32 noundef %29, i32 noundef 33)
  store ptr %30, ptr %6, align 8, !tbaa !5
  %31 = call i32 @P_Random()
  %32 = mul nsw i32 %31, 512
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.mobj_s, ptr %33, i32 0, i32 18
  store i32 %32, ptr %34, align 8, !tbaa !17
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = call i32 @P_SetMobjState(ptr noundef %35, i32 noundef 799)
  %37 = call i32 @P_Random()
  %38 = and i32 %37, 7
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.mobj_s, ptr %39, i32 0, i32 22
  %41 = load i32, ptr %40, align 8, !tbaa !18
  %42 = sub nsw i32 %41, %38
  store i32 %42, ptr %40, align 8, !tbaa !18
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.mobj_s, ptr %43, i32 0, i32 22
  %45 = load i32, ptr %44, align 8, !tbaa !18
  %46 = icmp slt i32 %45, 1
  br i1 %46, label %47, label %50

47:                                               ; preds = %18
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.mobj_s, ptr %48, i32 0, i32 22
  store i32 1, ptr %49, align 8, !tbaa !18
  br label %50

50:                                               ; preds = %47, %18
  br label %51

51:                                               ; preds = %50
  %52 = load i32, ptr %3, align 4, !tbaa !15
  %53 = add nsw i32 %52, 524288
  store i32 %53, ptr %3, align 4, !tbaa !15
  br label %11, !llvm.loop !19

54:                                               ; preds = %11
  call void @S_StartSound(ptr noundef null, i32 noundef 98)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, 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, !12, i64 24}
!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 = !{!12, !12, i64 0}
!16 = !{!10, !12, i64 28}
!17 = !{!10, !12, i64 120}
!18 = !{!10, !12, i64 144}
!19 = distinct !{!19, !20}
!20 = !{!"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"

declare i32 @P_Random() local_unnamed_addr #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @A_BrainScream(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
.lr.ph:
  %1 = getelementptr inbounds i8, ptr %0, i64 24
  %2 = load i32, ptr %1, align 8, !tbaa !5
  %3 = add nsw i32 %2, -12845056
  %4 = getelementptr inbounds i8, ptr %0, i64 28
  br label %5

5:                                                ; preds = %.lr.ph, %5
  %.012 = phi i32 [ %3, %.lr.ph ], [ %21, %5 ]
  %6 = load i32, ptr %4, align 4, !tbaa !14
  %7 = add nsw i32 %6, -20971520
  %8 = tail call i32 @P_Random() #3
  %9 = shl nsw i32 %8, 17
  %10 = or disjoint i32 %9, 128
  %11 = tail call ptr @P_SpawnMobj(i32 noundef %.012, i32 noundef %7, i32 noundef %10, i32 noundef 33) #3
  %12 = tail call i32 @P_Random() #3
  %13 = shl nsw i32 %12, 9
  %14 = getelementptr inbounds i8, ptr %11, i64 120
  store i32 %13, ptr %14, align 8, !tbaa !15
  %15 = tail call i32 @P_SetMobjState(ptr noundef %11, i32 noundef 799) #3
  %16 = tail call i32 @P_Random() #3
  %17 = and i32 %16, 7
  %18 = getelementptr inbounds i8, ptr %11, i64 144
  %19 = load i32, ptr %18, align 8, !tbaa !16
  %20 = sub nsw i32 %19, %17
  %spec.select = tail call i32 @llvm.smax.i32(i32 %20, i32 1)
  store i32 %spec.select, ptr %18, align 8, !tbaa !16
  %21 = add nsw i32 %.012, 524288
  %22 = load i32, ptr %1, align 8, !tbaa !5
  %23 = add nsw i32 %22, 20447232
  %24 = icmp slt i32 %.012, %23
  br i1 %24, label %5, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %5
  tail call void @S_StartSound(ptr noundef null, i32 noundef 98) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.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, !11, i64 24}
!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 28}
!15 = !{!6, !11, i64 120}
!16 = !{!6, !11, i64 144}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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