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

; 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.visplane_t = type { i32, i32, i32, i32, i32, i16, [320 x i16], i16, i16, [320 x i16], i16 }

@lastvisplane = external dso_local global ptr, 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @R_CheckPlane(ptr noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.visplane_t, ptr %15, i32 0, i32 3
  %17 = load i32, ptr %16, align 4, !tbaa !11
  %18 = icmp slt i32 %14, %17
  br i1 %18, label %19, label %24

19:                                               ; preds = %3
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.visplane_t, ptr %20, i32 0, i32 3
  %22 = load i32, ptr %21, align 4, !tbaa !11
  store i32 %22, ptr %8, align 4, !tbaa !9
  %23 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %23, ptr %10, align 4, !tbaa !9
  br label %29

24:                                               ; preds = %3
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.visplane_t, ptr %25, i32 0, i32 3
  %27 = load i32, ptr %26, align 4, !tbaa !11
  store i32 %27, ptr %10, align 4, !tbaa !9
  %28 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %28, ptr %8, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %24, %19
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.visplane_t, ptr %31, i32 0, i32 4
  %33 = load i32, ptr %32, align 4, !tbaa !14
  %34 = icmp sgt i32 %30, %33
  br i1 %34, label %35, label %40

35:                                               ; preds = %29
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.visplane_t, ptr %36, i32 0, i32 4
  %38 = load i32, ptr %37, align 4, !tbaa !14
  store i32 %38, ptr %9, align 4, !tbaa !9
  %39 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %39, ptr %11, align 4, !tbaa !9
  br label %45

40:                                               ; preds = %29
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.visplane_t, ptr %41, i32 0, i32 4
  %43 = load i32, ptr %42, align 4, !tbaa !14
  store i32 %43, ptr %11, align 4, !tbaa !9
  %44 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %44, ptr %9, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %40, %35
  %46 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %46, ptr %12, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %62, %45
  %48 = load i32, ptr %12, align 4, !tbaa !9
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = icmp sle i32 %48, %49
  br i1 %50, label %51, label %65

51:                                               ; preds = %47
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.visplane_t, ptr %52, i32 0, i32 6
  %54 = load i32, ptr %12, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [320 x i16], ptr %53, i64 0, i64 %55
  %57 = load i16, ptr %56, align 2, !tbaa !15
  %58 = zext i16 %57 to i32
  %59 = icmp ne i32 %58, 65535
  br i1 %59, label %60, label %61

60:                                               ; preds = %51
  br label %65

61:                                               ; preds = %51
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %12, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %12, align 4, !tbaa !9
  br label %47, !llvm.loop !16

65:                                               ; preds = %60, %47
  %66 = load i32, ptr %12, align 4, !tbaa !9
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = icmp sgt i32 %66, %67
  br i1 %68, label %69, label %77

69:                                               ; preds = %65
  %70 = load i32, ptr %10, align 4, !tbaa !9
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.visplane_t, ptr %71, i32 0, i32 3
  store i32 %70, ptr %72, align 4, !tbaa !11
  %73 = load i32, ptr %11, align 4, !tbaa !9
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.visplane_t, ptr %74, i32 0, i32 4
  store i32 %73, ptr %75, align 4, !tbaa !14
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %76, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %105

77:                                               ; preds = %65
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.visplane_t, ptr %78, i32 0, i32 0
  %80 = load i32, ptr %79, align 4, !tbaa !18
  %81 = load ptr, ptr @lastvisplane, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.visplane_t, ptr %81, i32 0, i32 0
  store i32 %80, ptr %82, align 4, !tbaa !18
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.visplane_t, ptr %83, i32 0, i32 1
  %85 = load i32, ptr %84, align 4, !tbaa !19
  %86 = load ptr, ptr @lastvisplane, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.visplane_t, ptr %86, i32 0, i32 1
  store i32 %85, ptr %87, align 4, !tbaa !19
  %88 = load ptr, ptr %5, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.visplane_t, ptr %88, i32 0, i32 2
  %90 = load i32, ptr %89, align 4, !tbaa !20
  %91 = load ptr, ptr @lastvisplane, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.visplane_t, ptr %91, i32 0, i32 2
  store i32 %90, ptr %92, align 4, !tbaa !20
  %93 = load ptr, ptr @lastvisplane, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.visplane_t, ptr %93, i32 1
  store ptr %94, ptr @lastvisplane, align 8, !tbaa !5
  store ptr %93, ptr %5, align 8, !tbaa !5
  %95 = load i32, ptr %6, align 4, !tbaa !9
  %96 = load ptr, ptr %5, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.visplane_t, ptr %96, i32 0, i32 3
  store i32 %95, ptr %97, align 4, !tbaa !11
  %98 = load i32, ptr %7, align 4, !tbaa !9
  %99 = load ptr, ptr %5, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.visplane_t, ptr %99, i32 0, i32 4
  store i32 %98, ptr %100, align 4, !tbaa !14
  %101 = load ptr, ptr %5, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.visplane_t, ptr %101, i32 0, i32 6
  %103 = getelementptr inbounds [320 x i16], ptr %102, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 2 %103, i8 -1, i64 640, i1 false)
  %104 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %104, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %105

105:                                              ; preds = %77, %69
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %106 = load ptr, ptr %4, align 8
  ret ptr %106
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!12, !10, i64 12}
!12 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !13, i64 20, !7, i64 22, !13, i64 662, !13, i64 664, !7, i64 666, !13, i64 1306}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !10, i64 16}
!15 = !{!13, !13, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !10, i64 0}
!19 = !{!12, !10, i64 4}
!20 = !{!12, !10, i64 8}


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

@lastvisplane = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @R_CheckPlane(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %0, i64 12
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %. = tail call i32 @llvm.smax.i32(i32 %5, i32 %1)
  %.47 = tail call i32 @llvm.smin.i32(i32 %5, i32 %1)
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %.036 = tail call i32 @llvm.smin.i32(i32 %7, i32 %2)
  %.034 = tail call i32 @llvm.smax.i32(i32 %7, i32 %2)
  %.not48 = icmp sgt i32 %., %.036
  br i1 %.not48, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 22
  %9 = tail call i32 @llvm.smax.i32(i32 %5, i32 %1)
  %smax = sext i32 %9 to i64
  %10 = add i32 %.036, %9
  %11 = add i32 %10, 1
  %12 = sub i32 %11, %.
  br label %14

13:                                               ; preds = %14
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %12, %lftr.wideiv
  br i1 %exitcond.not, label %._crit_edge, label %14, !llvm.loop !12

14:                                               ; preds = %.lr.ph, %13
  %indvars.iv = phi i64 [ %smax, %.lr.ph ], [ %indvars.iv.next, %13 ]
  %15 = getelementptr inbounds [320 x i16], ptr %8, i64 0, i64 %indvars.iv
  %16 = load i16, ptr %15, align 2, !tbaa !14
  %.not46 = icmp eq i16 %16, -1
  br i1 %.not46, label %13, label %17

._crit_edge:                                      ; preds = %13, %3
  store i32 %.47, ptr %4, align 4, !tbaa !5
  store i32 %.034, ptr %6, align 4, !tbaa !11
  br label %27

17:                                               ; preds = %14
  %18 = load ptr, ptr @lastvisplane, align 8, !tbaa !15
  %19 = load <2 x i32>, ptr %0, align 4, !tbaa !17
  store <2 x i32> %19, ptr %18, align 4, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %0, i64 8
  %21 = load i32, ptr %20, align 4, !tbaa !18
  %22 = getelementptr inbounds i8, ptr %18, i64 8
  store i32 %21, ptr %22, align 4, !tbaa !18
  %23 = getelementptr inbounds i8, ptr %18, i64 1308
  store ptr %23, ptr @lastvisplane, align 8, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %18, i64 12
  store i32 %1, ptr %24, align 4, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %18, i64 16
  store i32 %2, ptr %25, align 4, !tbaa !11
  %26 = getelementptr inbounds i8, ptr %18, i64 22
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(640) %26, i8 -1, i64 640, i1 false)
  br label %27

27:                                               ; preds = %17, %._crit_edge
  %.038 = phi ptr [ %0, %._crit_edge ], [ %18, %17 ]
  ret ptr %.038
}

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

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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) }

!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 12}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !10, i64 20, !8, i64 22, !10, i64 662, !10, i64 664, !8, i64 666, !10, i64 1306}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!6, !7, i64 16}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!10, !10, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"any pointer", !8, i64 0}
!17 = !{!7, !7, i64 0}
!18 = !{!6, !7, i64 8}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass