; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (p_mobj)

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

@attackrange = external global i32, align 4

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

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

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

declare i32 @P_Random() #2

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

; Function Attrs: nounwind uwtable
define dso_local void @P_SpawnPuff(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %8 = call i32 @P_Random()
  %9 = call i32 @P_Random()
  %10 = sub nsw i32 %8, %9
  %11 = shl i32 %10, 10
  %12 = load i32, ptr %6, align 4, !tbaa !5
  %13 = add nsw i32 %12, %11
  store i32 %13, ptr %6, align 4, !tbaa !5
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = call ptr @P_SpawnMobj(i32 noundef %14, i32 noundef %15, i32 noundef %16, i32 noundef 37)
  store ptr %17, ptr %7, align 8, !tbaa !9
  %18 = load ptr, ptr %7, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.mobj_s, ptr %18, i32 0, i32 18
  store i32 65536, ptr %19, align 8, !tbaa !11
  %20 = call i32 @P_Random()
  %21 = and i32 %20, 3
  %22 = load ptr, ptr %7, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.mobj_s, ptr %22, i32 0, i32 22
  %24 = load i32, ptr %23, align 8, !tbaa !16
  %25 = sub nsw i32 %24, %21
  store i32 %25, ptr %23, align 8, !tbaa !16
  %26 = load ptr, ptr %7, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.mobj_s, ptr %26, i32 0, i32 22
  %28 = load i32, ptr %27, align 8, !tbaa !16
  %29 = icmp slt i32 %28, 1
  br i1 %29, label %30, label %33

30:                                               ; preds = %3
  %31 = load ptr, ptr %7, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.mobj_s, ptr %31, i32 0, i32 22
  store i32 1, ptr %32, align 8, !tbaa !16
  br label %33

33:                                               ; preds = %30, %3
  %34 = load i32, ptr @attackrange, align 4, !tbaa !5
  %35 = icmp eq i32 %34, 4194304
  br i1 %35, label %36, label %39

36:                                               ; preds = %33
  %37 = load ptr, ptr %7, align 8, !tbaa !9
  %38 = call i32 @P_SetMobjState(ptr noundef %37, i32 noundef 95)
  br label %39

39:                                               ; preds = %36, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 120}
!12 = !{!"mobj_s", !13, i64 0, !6, i64 24, !6, i64 28, !6, i64 32, !10, i64 40, !10, i64 48, !6, i64 56, !6, i64 60, !6, i64 64, !10, i64 72, !10, i64 80, !10, 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, !10, i64 136, !6, i64 144, !10, i64 152, !6, i64 160, !6, i64 164, !6, i64 168, !6, i64 172, !10, i64 176, !6, i64 184, !6, i64 188, !10, i64 192, !6, i64 200, !14, i64 204, !10, i64 216}
!13 = !{!"thinker_s", !10, i64 0, !10, i64 8, !7, i64 16}
!14 = !{!"", !15, i64 0, !15, i64 2, !15, i64 4, !15, i64 6, !15, i64 8}
!15 = !{!"short", !7, i64 0}
!16 = !{!12, !6, i64 144}


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

@attackrange = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local i32 @P_SetMobjState(ptr noundef, i32 noundef) local_unnamed_addr #0

declare i32 @P_Random() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @P_SpawnMobj(i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @P_SpawnPuff(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = tail call i32 @P_Random() #3
  %5 = tail call i32 @P_Random() #3
  %6 = sub nsw i32 %4, %5
  %7 = shl i32 %6, 10
  %8 = add nsw i32 %7, %2
  %9 = tail call ptr @P_SpawnMobj(i32 noundef %0, i32 noundef %1, i32 noundef %8, i32 noundef 37)
  %10 = getelementptr inbounds i8, ptr %9, i64 120
  store i32 65536, ptr %10, align 8, !tbaa !5
  %11 = tail call i32 @P_Random() #3
  %12 = and i32 %11, 3
  %13 = getelementptr inbounds i8, ptr %9, i64 144
  %14 = load i32, ptr %13, align 8, !tbaa !14
  %15 = sub nsw i32 %14, %12
  %spec.select = tail call i32 @llvm.smax.i32(i32 %15, i32 1)
  store i32 %spec.select, ptr %13, align 8, !tbaa !14
  %16 = load i32, ptr @attackrange, align 4, !tbaa !15
  %17 = icmp eq i32 %16, 4194304
  br i1 %17, label %18, label %20

18:                                               ; preds = %3
  %19 = tail call i32 @P_SetMobjState(ptr noundef nonnull %9, i32 noundef 95)
  br label %20

20:                                               ; preds = %18, %3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

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 = { 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 120}
!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 144}
!15 = !{!11, !11, i64 0}


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