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

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

@solidsegs = external dso_local global [32 x %struct.cliprange_t], align 16

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

declare void @R_StoreWallRange(i32 noundef, i32 noundef) #1

; 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_ClipPassWallSegment(i32 noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store ptr @solidsegs, ptr %5, align 8, !tbaa !9
  br label %7

7:                                                ; preds = %14, %2
  %8 = load ptr, ptr %5, align 8, !tbaa !9
  %9 = getelementptr inbounds %struct.cliprange_t, ptr %8, i32 0, i32 1
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sub nsw i32 %11, 1
  %13 = icmp slt i32 %10, %12
  br i1 %13, label %14, label %17

14:                                               ; preds = %7
  %15 = load ptr, ptr %5, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.cliprange_t, ptr %15, i32 1
  store ptr %16, ptr %5, align 8, !tbaa !9
  br label %7, !llvm.loop !13

17:                                               ; preds = %7
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.cliprange_t, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %20, align 4, !tbaa !15
  %22 = icmp slt i32 %18, %21
  br i1 %22, label %23, label %39

23:                                               ; preds = %17
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = load ptr, ptr %5, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.cliprange_t, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 4, !tbaa !15
  %28 = sub nsw i32 %27, 1
  %29 = icmp slt i32 %24, %28
  br i1 %29, label %30, label %33

30:                                               ; preds = %23
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = load i32, ptr %4, align 4, !tbaa !5
  call void @R_StoreWallRange(i32 noundef %31, i32 noundef %32)
  store i32 1, ptr %6, align 4
  br label %80

33:                                               ; preds = %23
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.cliprange_t, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 4, !tbaa !15
  %38 = sub nsw i32 %37, 1
  call void @R_StoreWallRange(i32 noundef %34, i32 noundef %38)
  br label %39

39:                                               ; preds = %33, %17
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = load ptr, ptr %5, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.cliprange_t, ptr %41, i32 0, i32 1
  %43 = load i32, ptr %42, align 4, !tbaa !11
  %44 = icmp sle i32 %40, %43
  br i1 %44, label %45, label %46

45:                                               ; preds = %39
  store i32 1, ptr %6, align 4
  br label %80

46:                                               ; preds = %39
  br label %47

47:                                               ; preds = %73, %46
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = load ptr, ptr %5, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.cliprange_t, ptr %49, i64 1
  %51 = getelementptr inbounds %struct.cliprange_t, ptr %50, i32 0, i32 0
  %52 = load i32, ptr %51, align 4, !tbaa !15
  %53 = sub nsw i32 %52, 1
  %54 = icmp sge i32 %48, %53
  br i1 %54, label %55, label %74

55:                                               ; preds = %47
  %56 = load ptr, ptr %5, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.cliprange_t, ptr %56, i32 0, i32 1
  %58 = load i32, ptr %57, align 4, !tbaa !11
  %59 = add nsw i32 %58, 1
  %60 = load ptr, ptr %5, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.cliprange_t, ptr %60, i64 1
  %62 = getelementptr inbounds %struct.cliprange_t, ptr %61, i32 0, i32 0
  %63 = load i32, ptr %62, align 4, !tbaa !15
  %64 = sub nsw i32 %63, 1
  call void @R_StoreWallRange(i32 noundef %59, i32 noundef %64)
  %65 = load ptr, ptr %5, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.cliprange_t, ptr %65, i32 1
  store ptr %66, ptr %5, align 8, !tbaa !9
  %67 = load i32, ptr %4, align 4, !tbaa !5
  %68 = load ptr, ptr %5, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.cliprange_t, ptr %68, i32 0, i32 1
  %70 = load i32, ptr %69, align 4, !tbaa !11
  %71 = icmp sle i32 %67, %70
  br i1 %71, label %72, label %73

72:                                               ; preds = %55
  store i32 1, ptr %6, align 4
  br label %80

73:                                               ; preds = %55
  br label %47, !llvm.loop !16

74:                                               ; preds = %47
  %75 = load ptr, ptr %5, align 8, !tbaa !9
  %76 = getelementptr inbounds %struct.cliprange_t, ptr %75, i32 0, i32 1
  %77 = load i32, ptr %76, align 4, !tbaa !11
  %78 = add nsw i32 %77, 1
  %79 = load i32, ptr %4, align 4, !tbaa !5
  call void @R_StoreWallRange(i32 noundef %78, i32 noundef %79)
  store i32 0, ptr %6, align 4
  br label %80

80:                                               ; preds = %74, %72, %45, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %81 = load i32, ptr %6, align 4
  switch i32 %81, label %83 [
    i32 0, label %82
    i32 1, label %82
  ]

82:                                               ; preds = %80, %80
  ret void

83:                                               ; preds = %80
  unreachable
}

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 = !{!"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, !6, i64 4}
!12 = !{!"", !6, i64 0, !6, i64 4}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!12, !6, i64 0}
!16 = distinct !{!16, !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.cliprange_t = type { i32, i32 }

@solidsegs = external dso_local local_unnamed_addr global [32 x %struct.cliprange_t], align 16

declare void @R_StoreWallRange(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @R_ClipPassWallSegment(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = add nsw i32 %0, -1
  br label %4

4:                                                ; preds = %4, %2
  %.0 = phi ptr [ @solidsegs, %2 ], [ %8, %4 ]
  %5 = getelementptr inbounds i8, ptr %.0, i64 4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp slt i32 %6, %3
  %8 = getelementptr inbounds i8, ptr %.0, i64 8
  br i1 %7, label %4, label %9, !llvm.loop !10

9:                                                ; preds = %4
  %10 = getelementptr inbounds i8, ptr %.0, i64 4
  %11 = load i32, ptr %.0, align 4, !tbaa !12
  %12 = icmp sgt i32 %11, %0
  br i1 %12, label %13, label %17

13:                                               ; preds = %9
  %14 = add nsw i32 %11, -1
  %15 = icmp sgt i32 %14, %1
  br i1 %15, label %.loopexit.sink.split, label %16

16:                                               ; preds = %13
  tail call void @R_StoreWallRange(i32 noundef %0, i32 noundef %14) #2
  %.pre = load i32, ptr %10, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %16, %9
  %18 = phi i32 [ %.pre, %16 ], [ %6, %9 ]
  %.not = icmp slt i32 %18, %1
  br i1 %.not, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %17, %25
  %.1 = phi ptr [ %19, %25 ], [ %.0, %17 ]
  %19 = getelementptr inbounds i8, ptr %.1, i64 8
  %20 = load i32, ptr %19, align 4, !tbaa !12
  %21 = add nsw i32 %20, -1
  %.not27 = icmp sgt i32 %21, %1
  %22 = getelementptr inbounds i8, ptr %.1, i64 4
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  br i1 %.not27, label %.loopexit.sink.split, label %25

25:                                               ; preds = %.preheader
  tail call void @R_StoreWallRange(i32 noundef %24, i32 noundef %21) #2
  %26 = getelementptr inbounds i8, ptr %.1, i64 12
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %.not28 = icmp slt i32 %27, %1
  br i1 %.not28, label %.preheader, label %.loopexit, !llvm.loop !13

.loopexit.sink.split:                             ; preds = %.preheader, %13
  %.sink = phi i32 [ %0, %13 ], [ %24, %.preheader ]
  tail call void @R_StoreWallRange(i32 noundef %.sink, i32 noundef %1) #2
  br label %.loopexit

.loopexit:                                        ; preds = %25, %.loopexit.sink.split, %17
  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, !7, i64 4}
!6 = !{!"", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!6, !7, i64 0}
!13 = distinct !{!13, !11}


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