; 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 }
%struct.mobjinfo_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }

@finecosine = external global ptr, align 8
@finesine = external global [10240 x i32], 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 i32 @P_AproxDistance(i32 noundef, i32 noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local void @A_FaceTarget(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @FixedMul(i32 noundef, i32 noundef) #3

; Function Attrs: nounwind uwtable
define dso_local void @A_SkullAttack(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.mobj_s, ptr %7, i32 0, i32 28
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = icmp ne ptr %9, null
  br i1 %10, label %12, label %11

11:                                               ; preds = %1
  store i32 1, ptr %6, align 4
  br label %83

12:                                               ; preds = %1
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.mobj_s, ptr %13, i32 0, i32 28
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  store ptr %15, ptr %3, align 8, !tbaa !5
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mobj_s, ptr %16, i32 0, i32 24
  %18 = load i32, ptr %17, align 8, !tbaa !15
  %19 = or i32 %18, 16777216
  store i32 %19, ptr %17, align 8, !tbaa !15
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mobj_s, ptr %21, i32 0, i32 21
  %23 = load ptr, ptr %22, align 8, !tbaa !16
  %24 = getelementptr inbounds %struct.mobjinfo_t, ptr %23, i32 0, i32 6
  %25 = load i32, ptr %24, align 4, !tbaa !17
  call void @S_StartSound(ptr noundef %20, i32 noundef %25)
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  call void @A_FaceTarget(ptr noundef %26)
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mobj_s, ptr %27, i32 0, i32 6
  %29 = load i32, ptr %28, align 8, !tbaa !19
  %30 = lshr i32 %29, 19
  store i32 %30, ptr %4, align 4, !tbaa !20
  %31 = load ptr, ptr @finecosine, align 8, !tbaa !5
  %32 = load i32, ptr %4, align 4, !tbaa !20
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds i32, ptr %31, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !20
  %36 = call i32 @FixedMul(i32 noundef 1310720, i32 noundef %35)
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.mobj_s, ptr %37, i32 0, i32 16
  store i32 %36, ptr %38, align 8, !tbaa !21
  %39 = load i32, ptr %4, align 4, !tbaa !20
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !20
  %43 = call i32 @FixedMul(i32 noundef 1310720, i32 noundef %42)
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.mobj_s, ptr %44, i32 0, i32 17
  store i32 %43, ptr %45, align 4, !tbaa !22
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.mobj_s, ptr %46, i32 0, i32 1
  %48 = load i32, ptr %47, align 8, !tbaa !23
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mobj_s, ptr %49, i32 0, i32 1
  %51 = load i32, ptr %50, align 8, !tbaa !23
  %52 = sub nsw i32 %48, %51
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.mobj_s, ptr %53, i32 0, i32 2
  %55 = load i32, ptr %54, align 4, !tbaa !24
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.mobj_s, ptr %56, i32 0, i32 2
  %58 = load i32, ptr %57, align 4, !tbaa !24
  %59 = sub nsw i32 %55, %58
  %60 = call i32 @P_AproxDistance(i32 noundef %52, i32 noundef %59)
  store i32 %60, ptr %5, align 4, !tbaa !20
  %61 = load i32, ptr %5, align 4, !tbaa !20
  %62 = sdiv i32 %61, 1310720
  store i32 %62, ptr %5, align 4, !tbaa !20
  %63 = load i32, ptr %5, align 4, !tbaa !20
  %64 = icmp slt i32 %63, 1
  br i1 %64, label %65, label %66

65:                                               ; preds = %12
  store i32 1, ptr %5, align 4, !tbaa !20
  br label %66

66:                                               ; preds = %65, %12
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.mobj_s, ptr %67, i32 0, i32 3
  %69 = load i32, ptr %68, align 8, !tbaa !25
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.mobj_s, ptr %70, i32 0, i32 15
  %72 = load i32, ptr %71, align 4, !tbaa !26
  %73 = ashr i32 %72, 1
  %74 = add nsw i32 %69, %73
  %75 = load ptr, ptr %2, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.mobj_s, ptr %75, i32 0, i32 3
  %77 = load i32, ptr %76, align 8, !tbaa !25
  %78 = sub nsw i32 %74, %77
  %79 = load i32, ptr %5, align 4, !tbaa !20
  %80 = sdiv i32 %78, %79
  %81 = load ptr, ptr %2, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.mobj_s, ptr %81, i32 0, i32 18
  store i32 %80, ptr %82, align 8, !tbaa !27
  store i32 0, ptr %6, align 4
  br label %83

83:                                               ; preds = %66, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  %84 = load i32, ptr %6, align 4
  switch i32 %84, label %86 [
    i32 0, label %85
    i32 1, label %85
  ]

85:                                               ; preds = %83, %83
  ret void

86:                                               ; preds = %83
  unreachable
}

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 = { inlinehint 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 #4 = { 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 176}
!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 = !{!10, !12, i64 160}
!16 = !{!10, !6, i64 136}
!17 = !{!18, !12, i64 24}
!18 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52, !12, i64 56, !12, i64 60, !12, i64 64, !12, i64 68, !12, i64 72, !12, i64 76, !12, i64 80, !12, i64 84, !12, i64 88}
!19 = !{!10, !12, i64 56}
!20 = !{!12, !12, i64 0}
!21 = !{!10, !12, i64 112}
!22 = !{!10, !12, i64 116}
!23 = !{!10, !12, i64 24}
!24 = !{!10, !12, i64 28}
!25 = !{!10, !12, i64 32}
!26 = !{!10, !12, i64 108}
!27 = !{!10, !12, i64 120}


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

@finecosine = external local_unnamed_addr global ptr, align 8
@finesine = external local_unnamed_addr global [10240 x i32], align 16

declare i32 @P_AproxDistance(i32 noundef, i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @FixedMul(i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @A_SkullAttack(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 176
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %48, label %4

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 160
  %6 = load i32, ptr %5, align 8, !tbaa !14
  %7 = or i32 %6, 16777216
  store i32 %7, ptr %5, align 8, !tbaa !14
  %8 = getelementptr inbounds i8, ptr %0, i64 136
  %9 = load ptr, ptr %8, align 8, !tbaa !15
  %10 = getelementptr inbounds i8, ptr %9, i64 24
  %11 = load i32, ptr %10, align 4, !tbaa !16
  tail call void @S_StartSound(ptr noundef nonnull %0, i32 noundef %11) #4
  tail call void @A_FaceTarget(ptr noundef nonnull %0)
  %12 = getelementptr inbounds i8, ptr %0, i64 56
  %13 = load i32, ptr %12, align 8, !tbaa !18
  %14 = lshr i32 %13, 19
  %15 = load ptr, ptr @finecosine, align 8, !tbaa !19
  %16 = zext nneg i32 %14 to i64
  %17 = getelementptr inbounds i32, ptr %15, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !20
  %19 = tail call i32 @FixedMul(i32 noundef 1310720, i32 noundef %18)
  %20 = getelementptr inbounds i8, ptr %0, i64 112
  store i32 %19, ptr %20, align 8, !tbaa !21
  %21 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %16
  %22 = load i32, ptr %21, align 4, !tbaa !20
  %23 = tail call i32 @FixedMul(i32 noundef 1310720, i32 noundef %22)
  %24 = getelementptr inbounds i8, ptr %0, i64 116
  store i32 %23, ptr %24, align 4, !tbaa !22
  %25 = getelementptr inbounds i8, ptr %3, i64 24
  %26 = load i32, ptr %25, align 8, !tbaa !23
  %27 = getelementptr inbounds i8, ptr %0, i64 24
  %28 = load i32, ptr %27, align 8, !tbaa !23
  %29 = sub nsw i32 %26, %28
  %30 = getelementptr inbounds i8, ptr %3, i64 28
  %31 = load i32, ptr %30, align 4, !tbaa !24
  %32 = getelementptr inbounds i8, ptr %0, i64 28
  %33 = load i32, ptr %32, align 4, !tbaa !24
  %34 = sub nsw i32 %31, %33
  %35 = tail call i32 @P_AproxDistance(i32 noundef %29, i32 noundef %34) #4
  %36 = sdiv i32 %35, 1310720
  %spec.store.select = tail call i32 @llvm.smax.i32(i32 %36, i32 1)
  %37 = getelementptr inbounds i8, ptr %3, i64 32
  %38 = load i32, ptr %37, align 8, !tbaa !25
  %39 = getelementptr inbounds i8, ptr %3, i64 108
  %40 = load i32, ptr %39, align 4, !tbaa !26
  %41 = ashr i32 %40, 1
  %42 = add nsw i32 %41, %38
  %43 = getelementptr inbounds i8, ptr %0, i64 32
  %44 = load i32, ptr %43, align 8, !tbaa !25
  %45 = sub i32 %42, %44
  %46 = sdiv i32 %45, %spec.store.select
  %47 = getelementptr inbounds i8, ptr %0, i64 120
  store i32 %46, ptr %47, align 8, !tbaa !27
  br label %48

48:                                               ; preds = %1, %4
  ret void
}

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

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 = { inlinehint 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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, !8, i64 176}
!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 160}
!15 = !{!6, !8, i64 136}
!16 = !{!17, !11, i64 24}
!17 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68, !11, i64 72, !11, i64 76, !11, i64 80, !11, i64 84, !11, i64 88}
!18 = !{!6, !11, i64 56}
!19 = !{!8, !8, i64 0}
!20 = !{!11, !11, i64 0}
!21 = !{!6, !11, i64 112}
!22 = !{!6, !11, i64 116}
!23 = !{!6, !11, i64 24}
!24 = !{!6, !11, i64 28}
!25 = !{!6, !11, i64 32}
!26 = !{!6, !11, i64 108}
!27 = !{!6, !11, i64 120}


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