; 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.line_s = type { ptr, ptr, i32, i32, i16, i16, i16, [2 x i16], [4 x i32], i32, ptr, ptr, i32, ptr }

@bmapwidth = external global i32, align 4
@bmapheight = external global i32, align 4
@blockmap = external global ptr, align 8
@blockmaplump = external global ptr, align 8
@lines = external global ptr, align 8
@validcount = external global i32, align 4

; 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_BlockLinesIterator(i32 noundef %0, i32 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %25, label %14

14:                                               ; preds = %3
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %25, label %17

17:                                               ; preds = %14
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = load i32, ptr @bmapwidth, align 4, !tbaa !5
  %20 = icmp sge i32 %18, %19
  br i1 %20, label %25, label %21

21:                                               ; preds = %17
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = load i32, ptr @bmapheight, align 4, !tbaa !5
  %24 = icmp sge i32 %22, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %21, %17, %14, %3
  store i32 1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %73

26:                                               ; preds = %21
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = load i32, ptr @bmapwidth, align 4, !tbaa !5
  %29 = mul nsw i32 %27, %28
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  store i32 %31, ptr %8, align 4, !tbaa !5
  %32 = load ptr, ptr @blockmap, align 8, !tbaa !9
  %33 = load i32, ptr %8, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i16, ptr %32, i64 %34
  %36 = load i16, ptr %35, align 2, !tbaa !11
  %37 = sext i16 %36 to i32
  store i32 %37, ptr %8, align 4, !tbaa !5
  %38 = load ptr, ptr @blockmaplump, align 8, !tbaa !9
  %39 = load i32, ptr %8, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i16, ptr %38, i64 %40
  store ptr %41, ptr %9, align 8, !tbaa !9
  br label %42

42:                                               ; preds = %69, %26
  %43 = load ptr, ptr %9, align 8, !tbaa !9
  %44 = load i16, ptr %43, align 2, !tbaa !11
  %45 = sext i16 %44 to i32
  %46 = icmp ne i32 %45, -1
  br i1 %46, label %47, label %72

47:                                               ; preds = %42
  %48 = load ptr, ptr @lines, align 8, !tbaa !9
  %49 = load ptr, ptr %9, align 8, !tbaa !9
  %50 = load i16, ptr %49, align 2, !tbaa !11
  %51 = sext i16 %50 to i64
  %52 = getelementptr inbounds %struct.line_s, ptr %48, i64 %51
  store ptr %52, ptr %10, align 8, !tbaa !9
  %53 = load ptr, ptr %10, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.line_s, ptr %53, i32 0, i32 12
  %55 = load i32, ptr %54, align 8, !tbaa !13
  %56 = load i32, ptr @validcount, align 4, !tbaa !5
  %57 = icmp eq i32 %55, %56
  br i1 %57, label %58, label %59

58:                                               ; preds = %47
  br label %69

59:                                               ; preds = %47
  %60 = load i32, ptr @validcount, align 4, !tbaa !5
  %61 = load ptr, ptr %10, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.line_s, ptr %61, i32 0, i32 12
  store i32 %60, ptr %62, align 8, !tbaa !13
  %63 = load ptr, ptr %7, align 8, !tbaa !9
  %64 = load ptr, ptr %10, align 8, !tbaa !9
  %65 = call i32 %63(ptr noundef %64)
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %68, label %67

67:                                               ; preds = %59
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %73

68:                                               ; preds = %59
  br label %69

69:                                               ; preds = %68, %58
  %70 = load ptr, ptr %9, align 8, !tbaa !9
  %71 = getelementptr inbounds i16, ptr %70, i32 1
  store ptr %71, ptr %9, align 8, !tbaa !9
  br label %42, !llvm.loop !15

72:                                               ; preds = %42
  store i32 1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %73

73:                                               ; preds = %72, %67, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %74 = load i32, ptr %4, align 4
  ret i32 %74
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !6, i64 72}
!14 = !{!"line_s", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 20, !12, i64 24, !12, i64 26, !12, i64 28, !7, i64 30, !7, i64 36, !6, i64 52, !10, i64 56, !10, i64 64, !6, i64 72, !10, i64 80}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; 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.line_s = type { ptr, ptr, i32, i32, i16, i16, i16, [2 x i16], [4 x i32], i32, ptr, ptr, i32, ptr }

@bmapwidth = external local_unnamed_addr global i32, align 4
@bmapheight = external local_unnamed_addr global i32, align 4
@blockmap = external local_unnamed_addr global ptr, align 8
@blockmaplump = external local_unnamed_addr global ptr, align 8
@lines = external local_unnamed_addr global ptr, align 8
@validcount = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @P_BlockLinesIterator(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = or i32 %1, %0
  %or.cond.not = icmp sgt i32 %4, -1
  br i1 %or.cond.not, label %5, label %.loopexit

5:                                                ; preds = %3
  %6 = load i32, ptr @bmapwidth, align 4, !tbaa !5
  %.not = icmp sgt i32 %6, %0
  %7 = load i32, ptr @bmapheight, align 4
  %.not21 = icmp sgt i32 %7, %1
  %or.cond = select i1 %.not, i1 %.not21, i1 false
  br i1 %or.cond, label %8, label %.loopexit

8:                                                ; preds = %5
  %9 = mul nsw i32 %6, %1
  %10 = add nuw nsw i32 %9, %0
  %11 = load ptr, ptr @blockmap, align 8, !tbaa !9
  %12 = zext nneg i32 %10 to i64
  %13 = getelementptr inbounds i16, ptr %11, i64 %12
  %14 = load i16, ptr %13, align 2, !tbaa !11
  %15 = load ptr, ptr @blockmaplump, align 8, !tbaa !9
  %16 = sext i16 %14 to i64
  %17 = getelementptr inbounds i16, ptr %15, i64 %16
  %18 = load i16, ptr %17, align 2, !tbaa !11
  %.not2224 = icmp eq i16 %18, -1
  br i1 %.not2224, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %8
  %.pre28 = load ptr, ptr @lines, align 8, !tbaa !9
  %.pre30 = load i32, ptr @validcount, align 4, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %29
  %19 = phi i32 [ %30, %29 ], [ %.pre30, %.lr.ph.preheader ]
  %20 = phi ptr [ %31, %29 ], [ %.pre28, %.lr.ph.preheader ]
  %21 = phi i16 [ %33, %29 ], [ %18, %.lr.ph.preheader ]
  %.01625 = phi ptr [ %32, %29 ], [ %17, %.lr.ph.preheader ]
  %22 = sext i16 %21 to i64
  %23 = getelementptr inbounds %struct.line_s, ptr %20, i64 %22
  %24 = getelementptr inbounds i8, ptr %23, i64 72
  %25 = load i32, ptr %24, align 8, !tbaa !13
  %26 = icmp eq i32 %25, %19
  br i1 %26, label %29, label %27

27:                                               ; preds = %.lr.ph
  store i32 %19, ptr %24, align 8, !tbaa !13
  %28 = tail call i32 %2(ptr noundef nonnull %23) #1
  %.not23 = icmp eq i32 %28, 0
  %.pre = load ptr, ptr @lines, align 8, !tbaa !9
  %.pre29 = load i32, ptr @validcount, align 4, !tbaa !5
  br i1 %.not23, label %.loopexit, label %29

29:                                               ; preds = %27, %.lr.ph
  %30 = phi i32 [ %.pre29, %27 ], [ %19, %.lr.ph ]
  %31 = phi ptr [ %.pre, %27 ], [ %20, %.lr.ph ]
  %32 = getelementptr inbounds i8, ptr %.01625, i64 2
  %33 = load i16, ptr %32, align 2, !tbaa !11
  %.not22 = icmp eq i16 %33, -1
  br i1 %.not22, label %.loopexit, label %.lr.ph, !llvm.loop !15

.loopexit:                                        ; preds = %27, %29, %8, %3, %5
  %.0 = phi i32 [ 1, %5 ], [ 1, %3 ], [ 1, %8 ], [ 0, %27 ], [ 1, %29 ]
  ret i32 %.0
}

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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !6, i64 72}
!14 = !{!"line_s", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 20, !12, i64 24, !12, i64 26, !12, i64 28, !7, i64 30, !7, i64 36, !6, i64 52, !10, i64 56, !10, i64 64, !6, i64 72, !10, i64 80}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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