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

; 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.castinfo_t = type { ptr, i32 }
%struct.state_t = type { i32, i64, i64, %union.actionf_t, i32, i64, i64 }
%union.actionf_t = type { ptr }
%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 }
%struct.event_t = type { i32, i32, i32, i32 }

@castorder = external dso_local global [18 x %struct.castinfo_t], align 16
@castnum = external dso_local global i32, align 4
@states = external global [967 x %struct.state_t], align 16
@mobjinfo = external global [137 x %struct.mobjinfo_t], align 16
@caststate = external dso_local global ptr, align 8
@casttics = external dso_local global i32, align 4
@castdeath = external dso_local global i32, align 4
@castframes = external dso_local global i32, align 4
@castattacking = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @F_CastResponder(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.event_t, ptr %4, i32 0, i32 0
  %6 = load i32, ptr %5, align 4, !tbaa !9
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  store i32 0, ptr %2, align 4
  br label %50

9:                                                ; preds = %1
  %10 = load i32, ptr @castdeath, align 4, !tbaa !12
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  store i32 1, ptr %2, align 4
  br label %50

13:                                               ; preds = %9
  store i32 1, ptr @castdeath, align 4, !tbaa !12
  %14 = load i32, ptr @castnum, align 4, !tbaa !12
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [18 x %struct.castinfo_t], ptr @castorder, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.castinfo_t, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 8, !tbaa !13
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds [137 x %struct.mobjinfo_t], ptr @mobjinfo, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.mobjinfo_t, ptr %20, i32 0, i32 12
  %22 = load i32, ptr %21, align 4, !tbaa !15
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [967 x %struct.state_t], ptr @states, i64 0, i64 %23
  store ptr %24, ptr @caststate, align 8, !tbaa !5
  %25 = load ptr, ptr @caststate, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.state_t, ptr %25, i32 0, i32 2
  %27 = load i64, ptr %26, align 8, !tbaa !17
  %28 = trunc i64 %27 to i32
  store i32 %28, ptr @casttics, align 4, !tbaa !12
  store i32 0, ptr @castframes, align 4, !tbaa !12
  store i32 0, ptr @castattacking, align 4, !tbaa !12
  %29 = load i32, ptr @castnum, align 4, !tbaa !12
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [18 x %struct.castinfo_t], ptr @castorder, i64 0, i64 %30
  %32 = getelementptr inbounds %struct.castinfo_t, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 8, !tbaa !13
  %34 = zext i32 %33 to i64
  %35 = getelementptr inbounds [137 x %struct.mobjinfo_t], ptr @mobjinfo, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.mobjinfo_t, ptr %35, i32 0, i32 14
  %37 = load i32, ptr %36, align 4, !tbaa !20
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %49

39:                                               ; preds = %13
  %40 = load i32, ptr @castnum, align 4, !tbaa !12
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [18 x %struct.castinfo_t], ptr @castorder, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.castinfo_t, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 8, !tbaa !13
  %45 = zext i32 %44 to i64
  %46 = getelementptr inbounds [137 x %struct.mobjinfo_t], ptr @mobjinfo, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.mobjinfo_t, ptr %46, i32 0, i32 14
  %48 = load i32, ptr %47, align 4, !tbaa !20
  call void @S_StartSound(ptr noundef null, i32 noundef %48)
  br label %49

49:                                               ; preds = %39, %13
  store i32 1, ptr %2, align 4
  br label %50

50:                                               ; preds = %49, %12, %8
  %51 = load i32, ptr %2, align 4
  ret i32 %51
}

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

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

!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, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !11, i64 8}
!14 = !{!"", !6, i64 0, !11, i64 8}
!15 = !{!16, !11, i64 48}
!16 = !{!"", !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}
!17 = !{!18, !19, i64 16}
!18 = !{!"", !11, i64 0, !19, i64 8, !19, i64 16, !7, i64 24, !11, i64 32, !19, i64 40, !19, i64 48}
!19 = !{!"long", !7, i64 0}
!20 = !{!16, !11, i64 56}


; 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.castinfo_t = type { ptr, i32 }
%struct.state_t = type { i32, i64, i64, %union.actionf_t, i32, i64, i64 }
%union.actionf_t = type { ptr }
%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 }

@castorder = external dso_local local_unnamed_addr global [18 x %struct.castinfo_t], align 16
@castnum = external dso_local local_unnamed_addr global i32, align 4
@states = external global [967 x %struct.state_t], align 16
@mobjinfo = external local_unnamed_addr global [137 x %struct.mobjinfo_t], align 16
@caststate = external dso_local local_unnamed_addr global ptr, align 8
@casttics = external dso_local local_unnamed_addr global i32, align 4
@castdeath = external dso_local local_unnamed_addr global i32, align 4
@castframes = external dso_local local_unnamed_addr global i32, align 4
@castattacking = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @F_CastResponder(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load i32, ptr %0, align 4, !tbaa !5
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %3, label %21

3:                                                ; preds = %1
  %4 = load i32, ptr @castdeath, align 4, !tbaa !10
  %.not2 = icmp eq i32 %4, 0
  br i1 %.not2, label %5, label %21

5:                                                ; preds = %3
  store i32 1, ptr @castdeath, align 4, !tbaa !10
  %6 = load i32, ptr @castnum, align 4, !tbaa !10
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [18 x %struct.castinfo_t], ptr @castorder, i64 0, i64 %7, i32 1
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = zext i32 %9 to i64
  %11 = getelementptr inbounds [137 x %struct.mobjinfo_t], ptr @mobjinfo, i64 0, i64 %10, i32 12
  %12 = load i32, ptr %11, align 4, !tbaa !14
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [967 x %struct.state_t], ptr @states, i64 0, i64 %13
  store ptr %14, ptr @caststate, align 8, !tbaa !16
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  %16 = load i64, ptr %15, align 8, !tbaa !17
  %17 = trunc i64 %16 to i32
  store i32 %17, ptr @casttics, align 4, !tbaa !10
  store i32 0, ptr @castframes, align 4, !tbaa !10
  store i32 0, ptr @castattacking, align 4, !tbaa !10
  %18 = getelementptr inbounds [137 x %struct.mobjinfo_t], ptr @mobjinfo, i64 0, i64 %10, i32 14
  %19 = load i32, ptr %18, align 4, !tbaa !20
  %.not3 = icmp eq i32 %19, 0
  br i1 %.not3, label %21, label %20

20:                                               ; preds = %5
  tail call void @S_StartSound(ptr noundef null, i32 noundef %19) #2
  br label %21

21:                                               ; preds = %5, %20, %3, %1
  %.0 = phi i32 [ 0, %1 ], [ 1, %3 ], [ 1, %20 ], [ 1, %5 ]
  ret i32 %.0
}

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

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 = { 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 0}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !7, i64 8}
!12 = !{!"", !13, i64 0, !7, i64 8}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!15, !7, i64 48}
!15 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !7, i64 32, !7, i64 36, !7, i64 40, !7, i64 44, !7, i64 48, !7, i64 52, !7, i64 56, !7, i64 60, !7, i64 64, !7, i64 68, !7, i64 72, !7, i64 76, !7, i64 80, !7, i64 84, !7, i64 88}
!16 = !{!13, !13, i64 0}
!17 = !{!18, !19, i64 16}
!18 = !{!"", !7, i64 0, !19, i64 8, !19, i64 16, !8, i64 24, !7, i64 32, !19, i64 40, !19, i64 48}
!19 = !{!"long", !8, i64 0}
!20 = !{!15, !7, i64 56}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass