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

; 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.intercept_t = type { i32, i32, %union.anon }
%union.anon = type { ptr }

@intercept_p = external dso_local global ptr, align 8
@intercepts = external dso_local global [128 x %struct.intercept_t], 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

; Function Attrs: nounwind uwtable
define dso_local i32 @P_TraverseIntercepts(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %11 = load ptr, ptr @intercept_p, align 8, !tbaa !5
  %12 = ptrtoint ptr %11 to i64
  %13 = sub i64 %12, ptrtoint (ptr @intercepts to i64)
  %14 = sdiv exact i64 %13, 16
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %6, align 4, !tbaa !9
  store ptr null, ptr %9, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %51, %2
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = add nsw i32 %17, -1
  store i32 %18, ptr %6, align 4, !tbaa !9
  %19 = icmp ne i32 %17, 0
  br i1 %19, label %20, label %54

20:                                               ; preds = %16
  store i32 2147483647, ptr %7, align 4, !tbaa !9
  store ptr @intercepts, ptr %8, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %37, %20
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = load ptr, ptr @intercept_p, align 8, !tbaa !5
  %24 = icmp ult ptr %22, %23
  br i1 %24, label %25, label %40

25:                                               ; preds = %21
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.intercept_t, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 8, !tbaa !11
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %31, label %36

31:                                               ; preds = %25
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.intercept_t, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 8, !tbaa !11
  store i32 %34, ptr %7, align 4, !tbaa !9
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %35, ptr %9, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %31, %25
  br label %37

37:                                               ; preds = %36
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.intercept_t, ptr %38, i32 1
  store ptr %39, ptr %8, align 8, !tbaa !5
  br label %21, !llvm.loop !13

40:                                               ; preds = %21
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = icmp sgt i32 %41, %42
  br i1 %43, label %44, label %45

44:                                               ; preds = %40
  store i32 1, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %55

45:                                               ; preds = %40
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = call i32 %46(ptr noundef %47)
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %51, label %50

50:                                               ; preds = %45
  store i32 0, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %55

51:                                               ; preds = %45
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.intercept_t, ptr %52, i32 0, i32 0
  store i32 2147483647, ptr %53, align 8, !tbaa !11
  br label %16, !llvm.loop !15

54:                                               ; preds = %16
  store i32 1, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %55

55:                                               ; preds = %54, %50, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %56 = load i32, ptr %3, align 4
  ret i32 %56
}

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, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 4, !7, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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.intercept_t = type { i32, i32, %union.anon }
%union.anon = type { ptr }

@intercept_p = external dso_local local_unnamed_addr global ptr, align 8
@intercepts = external dso_local global [128 x %struct.intercept_t], align 16

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @P_TraverseIntercepts(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr @intercept_p, align 8, !tbaa !5
  %4 = ptrtoint ptr %3 to i64
  %5 = sub i64 %4, ptrtoint (ptr @intercepts to i64)
  %6 = lshr exact i64 %5, 4
  %7 = trunc i64 %6 to i32
  %.not24 = icmp eq i32 %7, 0
  br i1 %.not24, label %._crit_edge26, label %.preheader

.preheader:                                       ; preds = %2, %18
  %.in = phi i32 [ %8, %18 ], [ %7, %2 ]
  %.025 = phi ptr [ %.1.lcssa, %18 ], [ null, %2 ]
  %8 = add nsw i32 %.in, -1
  %9 = load ptr, ptr @intercept_p, align 8, !tbaa !5
  %10 = icmp ugt ptr %9, @intercepts
  br i1 %10, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.122 = phi ptr [ %spec.select19, %.lr.ph ], [ %.025, %.preheader ]
  %.01221 = phi ptr [ %13, %.lr.ph ], [ @intercepts, %.preheader ]
  %.01320 = phi i32 [ %spec.select, %.lr.ph ], [ 2147483647, %.preheader ]
  %11 = load i32, ptr %.01221, align 8, !tbaa !9
  %12 = icmp slt i32 %11, %.01320
  %spec.select = tail call i32 @llvm.smin.i32(i32 %11, i32 %.01320)
  %spec.select19 = select i1 %12, ptr %.01221, ptr %.122
  %13 = getelementptr inbounds i8, ptr %.01221, i64 16
  %14 = icmp ult ptr %13, %9
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.013.lcssa = phi i32 [ 2147483647, %.preheader ], [ %spec.select, %.lr.ph ]
  %.1.lcssa = phi ptr [ %.025, %.preheader ], [ %spec.select19, %.lr.ph ]
  %15 = icmp sgt i32 %.013.lcssa, %1
  br i1 %15, label %._crit_edge26, label %16

16:                                               ; preds = %._crit_edge
  %17 = tail call i32 %0(ptr noundef %.1.lcssa) #2
  %.not18 = icmp eq i32 %17, 0
  br i1 %.not18, label %._crit_edge26, label %18

18:                                               ; preds = %16
  store i32 2147483647, ptr %.1.lcssa, align 8, !tbaa !9
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %._crit_edge26, label %.preheader, !llvm.loop !14

._crit_edge26:                                    ; preds = %._crit_edge, %16, %18, %2
  %.016 = phi i32 [ 1, %2 ], [ 1, %18 ], [ 0, %16 ], [ 1, %._crit_edge ]
  ret i32 %.016
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 4, !7, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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