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

; 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.vissprite_s = type { ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, ptr, i32 }

@vissprites = external dso_local global [128 x %struct.vissprite_s], align 16
@vissprite_p = external dso_local global ptr, align 8
@vsprsortedhead = external dso_local global %struct.vissprite_s, align 8

; 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

; Function Attrs: nounwind uwtable
define dso_local void @R_SortVisSprites() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca %struct.vissprite_s, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 80, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %8 = load ptr, ptr @vissprite_p, align 8, !tbaa !5
  %9 = ptrtoint ptr %8 to i64
  %10 = sub i64 %9, ptrtoint (ptr @vissprites to i64)
  %11 = sdiv exact i64 %10, 80
  %12 = trunc i64 %11 to i32
  store i32 %12, ptr %2, align 4, !tbaa !9
  %13 = getelementptr inbounds %struct.vissprite_s, ptr %5, i32 0, i32 0
  store ptr %5, ptr %13, align 8, !tbaa !11
  %14 = getelementptr inbounds %struct.vissprite_s, ptr %5, i32 0, i32 1
  store ptr %5, ptr %14, align 8, !tbaa !13
  %15 = load i32, ptr %2, align 4, !tbaa !9
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %18, label %17

17:                                               ; preds = %0
  store i32 1, ptr %7, align 4
  br label %97

18:                                               ; preds = %0
  store ptr @vissprites, ptr %3, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %32, %18
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load ptr, ptr @vissprite_p, align 8, !tbaa !5
  %22 = icmp ult ptr %20, %21
  br i1 %22, label %23, label %35

23:                                               ; preds = %19
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.vissprite_s, ptr %24, i64 1
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.vissprite_s, ptr %26, i32 0, i32 1
  store ptr %25, ptr %27, align 8, !tbaa !13
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.vissprite_s, ptr %28, i64 -1
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.vissprite_s, ptr %30, i32 0, i32 0
  store ptr %29, ptr %31, align 8, !tbaa !11
  br label %32

32:                                               ; preds = %23
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.vissprite_s, ptr %33, i32 1
  store ptr %34, ptr %3, align 8, !tbaa !5
  br label %19, !llvm.loop !14

35:                                               ; preds = %19
  store ptr %5, ptr @vissprites, align 16, !tbaa !11
  %36 = getelementptr inbounds %struct.vissprite_s, ptr %5, i32 0, i32 1
  store ptr @vissprites, ptr %36, align 8, !tbaa !13
  %37 = load ptr, ptr @vissprite_p, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.vissprite_s, ptr %37, i64 -1
  %39 = getelementptr inbounds %struct.vissprite_s, ptr %38, i32 0, i32 1
  store ptr %5, ptr %39, align 8, !tbaa !13
  %40 = load ptr, ptr @vissprite_p, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.vissprite_s, ptr %40, i64 -1
  %42 = getelementptr inbounds %struct.vissprite_s, ptr %5, i32 0, i32 0
  store ptr %41, ptr %42, align 8, !tbaa !11
  store ptr null, ptr %4, align 8, !tbaa !5
  store ptr @vsprsortedhead, ptr @vsprsortedhead, align 8, !tbaa !11
  store ptr @vsprsortedhead, ptr getelementptr inbounds (%struct.vissprite_s, ptr @vsprsortedhead, i32 0, i32 1), align 8, !tbaa !13
  store i32 0, ptr %1, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %93, %35
  %44 = load i32, ptr %1, align 4, !tbaa !9
  %45 = load i32, ptr %2, align 4, !tbaa !9
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %47, label %96

47:                                               ; preds = %43
  store i32 2147483647, ptr %6, align 4, !tbaa !9
  %48 = getelementptr inbounds %struct.vissprite_s, ptr %5, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !13
  store ptr %49, ptr %3, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %65, %47
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = icmp ne ptr %51, %5
  br i1 %52, label %53, label %69

53:                                               ; preds = %50
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.vissprite_s, ptr %54, i32 0, i32 9
  %56 = load i32, ptr %55, align 4, !tbaa !16
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = icmp slt i32 %56, %57
  br i1 %58, label %59, label %64

59:                                               ; preds = %53
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.vissprite_s, ptr %60, i32 0, i32 9
  %62 = load i32, ptr %61, align 4, !tbaa !16
  store i32 %62, ptr %6, align 4, !tbaa !9
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %63, ptr %4, align 8, !tbaa !5
  br label %64

64:                                               ; preds = %59, %53
  br label %65

65:                                               ; preds = %64
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.vissprite_s, ptr %66, i32 0, i32 1
  %68 = load ptr, ptr %67, align 8, !tbaa !13
  store ptr %68, ptr %3, align 8, !tbaa !5
  br label %50, !llvm.loop !17

69:                                               ; preds = %50
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.vissprite_s, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !11
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.vissprite_s, ptr %73, i32 0, i32 1
  %75 = load ptr, ptr %74, align 8, !tbaa !13
  %76 = getelementptr inbounds %struct.vissprite_s, ptr %75, i32 0, i32 0
  store ptr %72, ptr %76, align 8, !tbaa !11
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.vissprite_s, ptr %77, i32 0, i32 1
  %79 = load ptr, ptr %78, align 8, !tbaa !13
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.vissprite_s, ptr %80, i32 0, i32 0
  %82 = load ptr, ptr %81, align 8, !tbaa !11
  %83 = getelementptr inbounds %struct.vissprite_s, ptr %82, i32 0, i32 1
  store ptr %79, ptr %83, align 8, !tbaa !13
  %84 = load ptr, ptr %4, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.vissprite_s, ptr %84, i32 0, i32 1
  store ptr @vsprsortedhead, ptr %85, align 8, !tbaa !13
  %86 = load ptr, ptr @vsprsortedhead, align 8, !tbaa !11
  %87 = load ptr, ptr %4, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.vissprite_s, ptr %87, i32 0, i32 0
  store ptr %86, ptr %88, align 8, !tbaa !11
  %89 = load ptr, ptr %4, align 8, !tbaa !5
  %90 = load ptr, ptr @vsprsortedhead, align 8, !tbaa !11
  %91 = getelementptr inbounds %struct.vissprite_s, ptr %90, i32 0, i32 1
  store ptr %89, ptr %91, align 8, !tbaa !13
  %92 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %92, ptr @vsprsortedhead, align 8, !tbaa !11
  br label %93

93:                                               ; preds = %69
  %94 = load i32, ptr %1, align 4, !tbaa !9
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %1, align 4, !tbaa !9
  br label %43, !llvm.loop !18

96:                                               ; preds = %43
  store i32 0, ptr %7, align 4
  br label %97

97:                                               ; preds = %96, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 80, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  %98 = load i32, ptr %7, align 4
  switch i32 %98, label %100 [
    i32 0, label %99
    i32 1, label %99
  ]

99:                                               ; preds = %97, %97
  ret void

100:                                              ; preds = %97
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !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 0}
!12 = !{!"vissprite_s", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !6, i64 64, !10, i64 72}
!13 = !{!12, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!12, !10, i64 44}
!17 = distinct !{!17, !15}
!18 = distinct !{!18, !15}


; 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.vissprite_s = type { ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, ptr, i32 }

@vissprites = external dso_local global [128 x %struct.vissprite_s], align 16
@vissprite_p = external dso_local local_unnamed_addr global ptr, align 8
@vsprsortedhead = external dso_local global %struct.vissprite_s, align 8

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

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

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @R_SortVisSprites() local_unnamed_addr #1 {
  %1 = alloca %struct.vissprite_s, align 8
  call void @llvm.lifetime.start.p0(i64 80, ptr nonnull %1) #3
  %2 = load ptr, ptr @vissprite_p, align 8, !tbaa !5
  %3 = ptrtoint ptr %2 to i64
  %4 = sub i64 %3, ptrtoint (ptr @vissprites to i64)
  %5 = sdiv exact i64 %4, 80
  %6 = trunc i64 %5 to i32
  %7 = getelementptr inbounds i8, ptr %1, i64 8
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %0
  %8 = icmp ugt ptr %2, @vissprites
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.02532 = phi ptr [ %9, %.lr.ph ], [ @vissprites, %.preheader ]
  %9 = getelementptr inbounds i8, ptr %.02532, i64 80
  %10 = getelementptr inbounds i8, ptr %.02532, i64 8
  store ptr %9, ptr %10, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %.02532, i64 -80
  store ptr %11, ptr %.02532, align 8, !tbaa !12
  %12 = icmp ult ptr %9, %2
  br i1 %12, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  store ptr %1, ptr @vissprites, align 16, !tbaa !12
  store ptr @vissprites, ptr %7, align 8, !tbaa !9
  %13 = getelementptr inbounds i8, ptr %2, i64 -72
  store ptr %1, ptr %13, align 8, !tbaa !9
  %14 = load ptr, ptr @vissprite_p, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 -80
  store ptr %15, ptr %1, align 8, !tbaa !12
  store ptr @vsprsortedhead, ptr @vsprsortedhead, align 8, !tbaa !12
  store ptr @vsprsortedhead, ptr getelementptr inbounds (i8, ptr @vsprsortedhead, i64 8), align 8, !tbaa !9
  %16 = icmp sgt i32 %6, 0
  br i1 %16, label %.lr.ph43, label %.loopexit

.lr.ph43:                                         ; preds = %._crit_edge, %._crit_edge39
  %.02341 = phi ptr [ %.124.lcssa, %._crit_edge39 ], [ null, %._crit_edge ]
  %.02740 = phi i32 [ %29, %._crit_edge39 ], [ 0, %._crit_edge ]
  %17 = load ptr, ptr %7, align 8, !tbaa !9
  %.not3033 = icmp eq ptr %17, %1
  br i1 %.not3033, label %._crit_edge39, label %.lr.ph38

.lr.ph38:                                         ; preds = %.lr.ph43, %.lr.ph38
  %.036 = phi i32 [ %spec.select31, %.lr.ph38 ], [ 2147483647, %.lr.ph43 ]
  %.12435 = phi ptr [ %spec.select, %.lr.ph38 ], [ %.02341, %.lr.ph43 ]
  %.12634 = phi ptr [ %22, %.lr.ph38 ], [ %17, %.lr.ph43 ]
  %18 = getelementptr inbounds i8, ptr %.12634, i64 44
  %19 = load i32, ptr %18, align 4, !tbaa !15
  %20 = icmp slt i32 %19, %.036
  %spec.select = select i1 %20, ptr %.12634, ptr %.12435
  %spec.select31 = call i32 @llvm.smin.i32(i32 %19, i32 %.036)
  %21 = getelementptr inbounds i8, ptr %.12634, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  %.not30 = icmp eq ptr %22, %1
  br i1 %.not30, label %._crit_edge39, label %.lr.ph38, !llvm.loop !16

._crit_edge39:                                    ; preds = %.lr.ph38, %.lr.ph43
  %.124.lcssa = phi ptr [ %.02341, %.lr.ph43 ], [ %spec.select, %.lr.ph38 ]
  %23 = load ptr, ptr %.124.lcssa, align 8, !tbaa !12
  %24 = getelementptr inbounds i8, ptr %.124.lcssa, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  store ptr %23, ptr %25, align 8, !tbaa !12
  %26 = getelementptr inbounds i8, ptr %23, i64 8
  store ptr %25, ptr %26, align 8, !tbaa !9
  store ptr @vsprsortedhead, ptr %24, align 8, !tbaa !9
  %27 = load ptr, ptr @vsprsortedhead, align 8, !tbaa !12
  store ptr %27, ptr %.124.lcssa, align 8, !tbaa !12
  %28 = getelementptr inbounds i8, ptr %27, i64 8
  store ptr %.124.lcssa, ptr %28, align 8, !tbaa !9
  store ptr %.124.lcssa, ptr @vsprsortedhead, align 8, !tbaa !12
  %29 = add nuw nsw i32 %.02740, 1
  %exitcond.not = icmp eq i32 %29, %6
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph43, !llvm.loop !17

.loopexit:                                        ; preds = %._crit_edge39, %._crit_edge, %0
  call void @llvm.lifetime.end.p0(i64 80, ptr nonnull %1) #3
  ret void
}

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"vissprite_s", !6, i64 0, !6, i64 8, !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, !6, i64 64, !11, i64 72}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!10, !11, i64 44}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !14}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass