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

declare i32 @P_Random() #0

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

8:                                                ; preds = %1
  br label %54

9:                                                ; preds = %1
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.lightflash_t, ptr %10, i32 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %13 = getelementptr inbounds %struct.sector_t, ptr %12, i32 0, i32 4
  %14 = load i16, ptr %13, align 4, !tbaa !14
  %15 = sext i16 %14 to i32
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.lightflash_t, ptr %16, i32 0, i32 3
  %18 = load i32, ptr %17, align 4, !tbaa !18
  %19 = icmp eq i32 %15, %18
  br i1 %19, label %20, label %37

20:                                               ; preds = %9
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.lightflash_t, ptr %21, i32 0, i32 4
  %23 = load i32, ptr %22, align 8, !tbaa !19
  %24 = trunc i32 %23 to i16
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.lightflash_t, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = getelementptr inbounds %struct.sector_t, ptr %27, i32 0, i32 4
  store i16 %24, ptr %28, align 4, !tbaa !14
  %29 = call i32 @P_Random()
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.lightflash_t, ptr %30, i32 0, i32 6
  %32 = load i32, ptr %31, align 8, !tbaa !20
  %33 = and i32 %29, %32
  %34 = add nsw i32 %33, 1
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.lightflash_t, ptr %35, i32 0, i32 2
  store i32 %34, ptr %36, align 8, !tbaa !9
  br label %54

37:                                               ; preds = %9
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.lightflash_t, ptr %38, i32 0, i32 3
  %40 = load i32, ptr %39, align 4, !tbaa !18
  %41 = trunc i32 %40 to i16
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.lightflash_t, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !13
  %45 = getelementptr inbounds %struct.sector_t, ptr %44, i32 0, i32 4
  store i16 %41, ptr %45, align 4, !tbaa !14
  %46 = call i32 @P_Random()
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.lightflash_t, ptr %47, i32 0, i32 5
  %49 = load i32, ptr %48, align 4, !tbaa !21
  %50 = and i32 %46, %49
  %51 = add nsw i32 %50, 1
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.lightflash_t, ptr %52, i32 0, i32 2
  store i32 %51, ptr %53, align 8, !tbaa !9
  br label %54

54:                                               ; preds = %37, %20, %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" }

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


; 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

; Function Attrs: nounwind uwtable
define dso_local void @T_LightFlash(ptr nocapture noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = add nsw i32 %3, -1
  store i32 %4, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %5, label %29

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %8 = getelementptr inbounds i8, ptr %7, i64 12
  %9 = load i16, ptr %8, align 4, !tbaa !13
  %10 = sext i16 %9 to i32
  %11 = getelementptr inbounds i8, ptr %0, i64 36
  %12 = load i32, ptr %11, align 4, !tbaa !17
  %13 = icmp eq i32 %12, %10
  br i1 %13, label %14, label %22

14:                                               ; preds = %5
  %15 = getelementptr inbounds i8, ptr %0, i64 40
  %16 = load i32, ptr %15, align 8, !tbaa !18
  %17 = trunc i32 %16 to i16
  store i16 %17, ptr %8, align 4, !tbaa !13
  %18 = tail call i32 @P_Random() #2
  %19 = getelementptr inbounds i8, ptr %0, i64 48
  %20 = load i32, ptr %19, align 8, !tbaa !19
  %21 = and i32 %20, %18
  br label %.sink.split

22:                                               ; preds = %5
  %23 = trunc i32 %12 to i16
  store i16 %23, ptr %8, align 4, !tbaa !13
  %24 = tail call i32 @P_Random() #2
  %25 = getelementptr inbounds i8, ptr %0, i64 44
  %26 = load i32, ptr %25, align 4, !tbaa !20
  %27 = and i32 %26, %24
  br label %.sink.split

.sink.split:                                      ; preds = %14, %22
  %.sink11 = phi i32 [ %27, %22 ], [ %21, %14 ]
  %28 = add nsw i32 %.sink11, 1
  store i32 %28, ptr %2, align 8, !tbaa !5
  br label %29

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


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