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

; 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.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 }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }

@nofit = external dso_local global i32, align 4
@crushchange = external dso_local 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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @PIT_ChangeSector(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @P_ChangeSector(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr @nofit, align 4, !tbaa !9
  %7 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %7, ptr @crushchange, align 4, !tbaa !9
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.sector_t, ptr %8, i32 0, i32 9
  %10 = getelementptr inbounds [4 x i32], ptr %9, i64 0, i64 2
  %11 = load i32, ptr %10, align 8, !tbaa !9
  store i32 %11, ptr %5, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %39, %2
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.sector_t, ptr %14, i32 0, i32 9
  %16 = getelementptr inbounds [4 x i32], ptr %15, i64 0, i64 3
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = icmp sle i32 %13, %17
  br i1 %18, label %19, label %42

19:                                               ; preds = %12
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.sector_t, ptr %20, i32 0, i32 9
  %22 = getelementptr inbounds [4 x i32], ptr %21, i64 0, i64 1
  %23 = load i32, ptr %22, align 4, !tbaa !9
  store i32 %23, ptr %6, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %35, %19
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.sector_t, ptr %26, i32 0, i32 9
  %28 = getelementptr inbounds [4 x i32], ptr %27, i64 0, i64 0
  %29 = load i32, ptr %28, align 8, !tbaa !9
  %30 = icmp sle i32 %25, %29
  br i1 %30, label %31, label %38

31:                                               ; preds = %24
  %32 = load i32, ptr %5, align 4, !tbaa !9
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = call i32 @P_BlockThingsIterator(i32 noundef %32, i32 noundef %33, ptr noundef @PIT_ChangeSector)
  br label %35

35:                                               ; preds = %31
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %6, align 4, !tbaa !9
  br label %24, !llvm.loop !11

38:                                               ; preds = %24
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %5, align 4, !tbaa !9
  br label %12, !llvm.loop !13

42:                                               ; preds = %12
  %43 = load i32, ptr @nofit, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret i32 %43
}

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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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"

@nofit = external dso_local local_unnamed_addr global i32, align 4
@crushchange = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @PIT_ChangeSector(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @P_ChangeSector(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #1 {
  store i32 0, ptr @nofit, align 4, !tbaa !5
  store i32 %1, ptr @crushchange, align 4, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 32
  %4 = getelementptr inbounds i8, ptr %0, i64 40
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 44
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %.not14 = icmp sgt i32 %5, %7
  br i1 %.not14, label %._crit_edge18, label %.lr.ph17

.lr.ph17:                                         ; preds = %2
  %8 = getelementptr inbounds i8, ptr %0, i64 36
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = load i32, ptr %3, align 8, !tbaa !5
  %11 = icmp sgt i32 %9, %10
  br i1 %11, label %._crit_edge18, label %.lr.ph17.split

.lr.ph17.split:                                   ; preds = %.lr.ph17, %._crit_edge
  %12 = phi i32 [ %18, %._crit_edge ], [ %7, %.lr.ph17 ]
  %13 = phi i32 [ %19, %._crit_edge ], [ %10, %.lr.ph17 ]
  %.01015 = phi i32 [ %20, %._crit_edge ], [ %5, %.lr.ph17 ]
  %14 = load i32, ptr %8, align 4, !tbaa !5
  %.not1112 = icmp sgt i32 %14, %13
  br i1 %.not1112, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph17.split, %.lr.ph
  %.013 = phi i32 [ %16, %.lr.ph ], [ %14, %.lr.ph17.split ]
  %15 = tail call i32 @P_BlockThingsIterator(i32 noundef %.01015, i32 noundef %.013, ptr noundef nonnull @PIT_ChangeSector) #2
  %16 = add nsw i32 %.013, 1
  %17 = load i32, ptr %3, align 8, !tbaa !5
  %.not11.not = icmp slt i32 %.013, %17
  br i1 %.not11.not, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !9

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i32, ptr %6, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.lr.ph17.split
  %18 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %12, %.lr.ph17.split ]
  %19 = phi i32 [ %17, %._crit_edge.loopexit ], [ %13, %.lr.ph17.split ]
  %20 = add nsw i32 %.01015, 1
  %.not.not = icmp slt i32 %.01015, %18
  br i1 %.not.not, label %.lr.ph17.split, label %._crit_edge18.loopexit19, !llvm.loop !11

._crit_edge18.loopexit19:                         ; preds = %._crit_edge
  %.pre20 = load i32, ptr @nofit, align 4, !tbaa !5
  br label %._crit_edge18

._crit_edge18:                                    ; preds = %.lr.ph17, %._crit_edge18.loopexit19, %2
  %21 = phi i32 [ %.pre20, %._crit_edge18.loopexit19 ], [ 0, %2 ], [ 0, %.lr.ph17 ]
  ret i32 %21
}

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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10, !12}
!12 = !{!"llvm.loop.unswitch.partial.disable"}


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