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

; 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.strobe_t = type { %struct.thinker_s, ptr, i32, i32, i32, i32, i32 }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }
%struct.sector_t = type { i32, i32, i16, i16, i16, i16, i16, i32, ptr, [4 x i32], %struct.degenmobj_t, i32, ptr, ptr, i32, ptr }
%struct.degenmobj_t = type { %struct.thinker_s, i32, i32, i32 }

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

declare i32 @P_Random() #1

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

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

declare void @P_AddThinker(ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @P_SpawnStrobeFlash(ptr noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %8 = call ptr @Z_Malloc(i32 noundef 56, i32 noundef 51, ptr noundef null)
  store ptr %8, ptr %7, align 8, !tbaa !5
  %9 = load ptr, ptr %7, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.strobe_t, ptr %9, i32 0, i32 0
  call void @P_AddThinker(ptr noundef %10)
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.strobe_t, ptr %12, i32 0, i32 1
  store ptr %11, ptr %13, align 8, !tbaa !11
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.strobe_t, ptr %15, i32 0, i32 5
  store i32 %14, ptr %16, align 4, !tbaa !14
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.strobe_t, ptr %17, i32 0, i32 6
  store i32 5, ptr %18, align 8, !tbaa !15
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.strobe_t, ptr %19, i32 0, i32 0
  %21 = getelementptr inbounds %struct.thinker_s, ptr %20, i32 0, i32 2
  store ptr @T_StrobeFlash, ptr %21, align 8, !tbaa !16
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.sector_t, ptr %22, i32 0, i32 4
  %24 = load i16, ptr %23, align 4, !tbaa !17
  %25 = sext i16 %24 to i32
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.strobe_t, ptr %26, i32 0, i32 4
  store i32 %25, ptr %27, align 8, !tbaa !21
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.sector_t, ptr %29, i32 0, i32 4
  %31 = load i16, ptr %30, align 4, !tbaa !17
  %32 = sext i16 %31 to i32
  %33 = call i32 @P_FindMinSurroundingLight(ptr noundef %28, i32 noundef %32)
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.strobe_t, ptr %34, i32 0, i32 3
  store i32 %33, ptr %35, align 4, !tbaa !22
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.strobe_t, ptr %36, i32 0, i32 3
  %38 = load i32, ptr %37, align 4, !tbaa !22
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.strobe_t, ptr %39, i32 0, i32 4
  %41 = load i32, ptr %40, align 8, !tbaa !21
  %42 = icmp eq i32 %38, %41
  br i1 %42, label %43, label %46

43:                                               ; preds = %3
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.strobe_t, ptr %44, i32 0, i32 3
  store i32 0, ptr %45, align 4, !tbaa !22
  br label %46

46:                                               ; preds = %43, %3
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.sector_t, ptr %47, i32 0, i32 5
  store i16 0, ptr %48, align 2, !tbaa !23
  %49 = load i32, ptr %6, align 4, !tbaa !9
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %57, label %51

51:                                               ; preds = %46
  %52 = call i32 @P_Random()
  %53 = and i32 %52, 7
  %54 = add nsw i32 %53, 1
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.strobe_t, ptr %55, i32 0, i32 2
  store i32 %54, ptr %56, align 8, !tbaa !24
  br label %60

57:                                               ; preds = %46
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.strobe_t, ptr %58, i32 0, i32 2
  store i32 1, ptr %59, align 8, !tbaa !24
  br label %60

60:                                               ; preds = %57, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 24}
!12 = !{!"", !13, i64 0, !6, i64 24, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48}
!13 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!14 = !{!12, !10, i64 44}
!15 = !{!12, !10, i64 48}
!16 = !{!7, !7, i64 0}
!17 = !{!18, !19, i64 12}
!18 = !{!"", !10, i64 0, !10, i64 4, !19, i64 8, !19, i64 10, !19, i64 12, !19, i64 14, !19, i64 16, !10, i64 20, !6, i64 24, !7, i64 32, !20, i64 48, !10, i64 88, !6, i64 96, !6, i64 104, !10, i64 112, !6, i64 120}
!19 = !{!"short", !7, i64 0}
!20 = !{!"", !13, i64 0, !10, i64 24, !10, i64 28, !10, i64 32}
!21 = !{!12, !10, i64 40}
!22 = !{!12, !10, i64 36}
!23 = !{!18, !19, i64 14}
!24 = !{!12, !10, i64 32}


; 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 ptr @Z_Malloc(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

declare void @P_AddThinker(ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @P_SpawnStrobeFlash(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = tail call ptr @Z_Malloc(i32 noundef 56, i32 noundef 51, ptr noundef null) #2
  tail call void @P_AddThinker(ptr noundef %4) #2
  %5 = getelementptr inbounds i8, ptr %4, i64 24
  store ptr %0, ptr %5, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %4, i64 44
  store i32 %1, ptr %6, align 4, !tbaa !12
  %7 = getelementptr inbounds i8, ptr %4, i64 48
  store i32 5, ptr %7, align 8, !tbaa !13
  %8 = getelementptr inbounds i8, ptr %4, i64 16
  store ptr @T_StrobeFlash, ptr %8, align 8, !tbaa !14
  %9 = getelementptr inbounds i8, ptr %0, i64 12
  %10 = load i16, ptr %9, align 4, !tbaa !15
  %11 = sext i16 %10 to i32
  %12 = getelementptr inbounds i8, ptr %4, i64 40
  store i32 %11, ptr %12, align 8, !tbaa !19
  %13 = tail call i32 @P_FindMinSurroundingLight(ptr noundef %0, i32 noundef %11) #2
  %14 = getelementptr inbounds i8, ptr %4, i64 36
  %15 = load i32, ptr %12, align 8, !tbaa !19
  %16 = icmp eq i32 %13, %15
  %spec.store.select = select i1 %16, i32 0, i32 %13
  store i32 %spec.store.select, ptr %14, align 4
  %17 = getelementptr inbounds i8, ptr %0, i64 14
  store i16 0, ptr %17, align 2, !tbaa !20
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %18, label %22

18:                                               ; preds = %3
  %19 = tail call i32 @P_Random() #2
  %20 = and i32 %19, 7
  %21 = add nuw nsw i32 %20, 1
  br label %22

22:                                               ; preds = %3, %18
  %.sink = phi i32 [ %21, %18 ], [ 1, %3 ]
  %23 = getelementptr inbounds i8, ptr %4, i64 32
  store i32 %.sink, ptr %23, align 8
  ret void
}

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 = { 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 24}
!6 = !{!"", !7, i64 0, !8, i64 24, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48}
!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 = !{!6, !11, i64 44}
!13 = !{!6, !11, i64 48}
!14 = !{!9, !9, i64 0}
!15 = !{!16, !17, i64 12}
!16 = !{!"", !11, i64 0, !11, i64 4, !17, i64 8, !17, i64 10, !17, i64 12, !17, i64 14, !17, i64 16, !11, i64 20, !8, i64 24, !9, i64 32, !18, i64 48, !11, i64 88, !8, i64 96, !8, i64 104, !11, i64 112, !8, i64 120}
!17 = !{!"short", !9, i64 0}
!18 = !{!"", !7, i64 0, !11, i64 24, !11, i64 28, !11, i64 32}
!19 = !{!6, !11, i64 40}
!20 = !{!16, !17, i64 14}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,MergedLoadStoreMotionPass,SROAPass,SimplifyCFGPass,TailCallElimPass