; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (am_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.mline_t = type { %struct.mpoint_t, %struct.mpoint_t }
%struct.mpoint_t = type { i32, i32 }

@m_w = external hidden global i32, align 4
@m_x = external hidden global i32, align 4
@m_h = external hidden global i32, align 4
@m_y = external hidden global i32, align 4
@bmaporgx = external global i32, align 4
@bmaporgy = 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
declare dso_local void @AM_drawMline(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @AM_drawGrid(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca %struct.mline_t, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #2
  %8 = load i32, ptr @m_x, align 4, !tbaa !5
  store i32 %8, ptr %5, align 4, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = load i32, ptr @bmaporgx, align 4, !tbaa !5
  %11 = sub nsw i32 %9, %10
  %12 = srem i32 %11, 8388608
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %14, label %22

14:                                               ; preds = %1
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = load i32, ptr @bmaporgx, align 4, !tbaa !5
  %17 = sub nsw i32 %15, %16
  %18 = srem i32 %17, 8388608
  %19 = sub nsw i32 8388608, %18
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = add nsw i32 %20, %19
  store i32 %21, ptr %5, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %14, %1
  %23 = load i32, ptr @m_x, align 4, !tbaa !5
  %24 = load i32, ptr @m_w, align 4, !tbaa !5
  %25 = add nsw i32 %23, %24
  store i32 %25, ptr %6, align 4, !tbaa !5
  %26 = load i32, ptr @m_y, align 4, !tbaa !5
  %27 = getelementptr inbounds %struct.mline_t, ptr %7, i32 0, i32 0
  %28 = getelementptr inbounds %struct.mpoint_t, ptr %27, i32 0, i32 1
  store i32 %26, ptr %28, align 4, !tbaa !9
  %29 = load i32, ptr @m_y, align 4, !tbaa !5
  %30 = load i32, ptr @m_h, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  %32 = getelementptr inbounds %struct.mline_t, ptr %7, i32 0, i32 1
  %33 = getelementptr inbounds %struct.mpoint_t, ptr %32, i32 0, i32 1
  store i32 %31, ptr %33, align 4, !tbaa !12
  %34 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %34, ptr %3, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %47, %22
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %39, label %50

39:                                               ; preds = %35
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = getelementptr inbounds %struct.mline_t, ptr %7, i32 0, i32 0
  %42 = getelementptr inbounds %struct.mpoint_t, ptr %41, i32 0, i32 0
  store i32 %40, ptr %42, align 4, !tbaa !13
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = getelementptr inbounds %struct.mline_t, ptr %7, i32 0, i32 1
  %45 = getelementptr inbounds %struct.mpoint_t, ptr %44, i32 0, i32 0
  store i32 %43, ptr %45, align 4, !tbaa !14
  %46 = load i32, ptr %2, align 4, !tbaa !5
  call void @AM_drawMline(ptr noundef %7, i32 noundef %46)
  br label %47

47:                                               ; preds = %39
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = add nsw i32 %48, 8388608
  store i32 %49, ptr %3, align 4, !tbaa !5
  br label %35, !llvm.loop !15

50:                                               ; preds = %35
  %51 = load i32, ptr @m_y, align 4, !tbaa !5
  store i32 %51, ptr %5, align 4, !tbaa !5
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = load i32, ptr @bmaporgy, align 4, !tbaa !5
  %54 = sub nsw i32 %52, %53
  %55 = srem i32 %54, 8388608
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %57, label %65

57:                                               ; preds = %50
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = load i32, ptr @bmaporgy, align 4, !tbaa !5
  %60 = sub nsw i32 %58, %59
  %61 = srem i32 %60, 8388608
  %62 = sub nsw i32 8388608, %61
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = add nsw i32 %63, %62
  store i32 %64, ptr %5, align 4, !tbaa !5
  br label %65

65:                                               ; preds = %57, %50
  %66 = load i32, ptr @m_y, align 4, !tbaa !5
  %67 = load i32, ptr @m_h, align 4, !tbaa !5
  %68 = add nsw i32 %66, %67
  store i32 %68, ptr %6, align 4, !tbaa !5
  %69 = load i32, ptr @m_x, align 4, !tbaa !5
  %70 = getelementptr inbounds %struct.mline_t, ptr %7, i32 0, i32 0
  %71 = getelementptr inbounds %struct.mpoint_t, ptr %70, i32 0, i32 0
  store i32 %69, ptr %71, align 4, !tbaa !13
  %72 = load i32, ptr @m_x, align 4, !tbaa !5
  %73 = load i32, ptr @m_w, align 4, !tbaa !5
  %74 = add nsw i32 %72, %73
  %75 = getelementptr inbounds %struct.mline_t, ptr %7, i32 0, i32 1
  %76 = getelementptr inbounds %struct.mpoint_t, ptr %75, i32 0, i32 0
  store i32 %74, ptr %76, align 4, !tbaa !14
  %77 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %77, ptr %4, align 4, !tbaa !5
  br label %78

78:                                               ; preds = %90, %65
  %79 = load i32, ptr %4, align 4, !tbaa !5
  %80 = load i32, ptr %6, align 4, !tbaa !5
  %81 = icmp slt i32 %79, %80
  br i1 %81, label %82, label %93

82:                                               ; preds = %78
  %83 = load i32, ptr %4, align 4, !tbaa !5
  %84 = getelementptr inbounds %struct.mline_t, ptr %7, i32 0, i32 0
  %85 = getelementptr inbounds %struct.mpoint_t, ptr %84, i32 0, i32 1
  store i32 %83, ptr %85, align 4, !tbaa !9
  %86 = load i32, ptr %4, align 4, !tbaa !5
  %87 = getelementptr inbounds %struct.mline_t, ptr %7, i32 0, i32 1
  %88 = getelementptr inbounds %struct.mpoint_t, ptr %87, i32 0, i32 1
  store i32 %86, ptr %88, align 4, !tbaa !12
  %89 = load i32, ptr %2, align 4, !tbaa !5
  call void @AM_drawMline(ptr noundef %7, i32 noundef %89)
  br label %90

90:                                               ; preds = %82
  %91 = load i32, ptr %4, align 4, !tbaa !5
  %92 = add nsw i32 %91, 8388608
  store i32 %92, ptr %4, align 4, !tbaa !5
  br label %78, !llvm.loop !17

93:                                               ; preds = %78
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

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


; 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.mline_t = type { %struct.mpoint_t, %struct.mpoint_t }
%struct.mpoint_t = type { i32, i32 }

@m_w = external hidden local_unnamed_addr global i32, align 4
@m_x = external hidden local_unnamed_addr global i32, align 4
@m_h = external hidden local_unnamed_addr global i32, align 4
@m_y = external hidden local_unnamed_addr global i32, align 4
@bmaporgx = external local_unnamed_addr global i32, align 4
@bmaporgy = external local_unnamed_addr global i32, align 4

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @AM_drawMline(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @AM_drawGrid(i32 noundef %0) local_unnamed_addr #1 {
  %2 = alloca %struct.mline_t, align 4
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #2
  %3 = load i32, ptr @m_x, align 4, !tbaa !5
  %4 = load i32, ptr @bmaporgx, align 4, !tbaa !5
  %5 = sub nsw i32 %3, %4
  %6 = srem i32 %5, 8388608
  %.not = icmp eq i32 %6, 0
  %reass.sub = add i32 %3, 8388608
  %7 = sub i32 %reass.sub, %6
  %.0 = select i1 %.not, i32 %3, i32 %7
  %8 = load i32, ptr @m_w, align 4, !tbaa !5
  %9 = add nsw i32 %8, %3
  %10 = load i32, ptr @m_y, align 4, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %2, i64 4
  store i32 %10, ptr %11, align 4, !tbaa !9
  %12 = load i32, ptr @m_h, align 4, !tbaa !5
  %13 = add nsw i32 %12, %10
  %14 = getelementptr inbounds i8, ptr %2, i64 8
  %15 = getelementptr inbounds i8, ptr %2, i64 12
  store i32 %13, ptr %15, align 4, !tbaa !12
  %16 = icmp slt i32 %.0, %9
  br i1 %16, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.02127 = phi i32 [ %17, %.lr.ph ], [ %.0, %1 ]
  store i32 %.02127, ptr %2, align 4, !tbaa !13
  store i32 %.02127, ptr %14, align 4, !tbaa !14
  call void @AM_drawMline(ptr noundef nonnull %2, i32 noundef %0)
  %17 = add nsw i32 %.02127, 8388608
  %18 = icmp slt i32 %17, %9
  br i1 %18, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i32, ptr @m_y, align 4, !tbaa !5
  %.pre32 = load i32, ptr @m_h, align 4, !tbaa !5
  %.pre33 = load i32, ptr @m_x, align 4, !tbaa !5
  %.pre34 = load i32, ptr @m_w, align 4, !tbaa !5
  %.pre35 = add nsw i32 %.pre32, %.pre
  %.pre36 = add nsw i32 %.pre34, %.pre33
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %1
  %.pre-phi37 = phi i32 [ %.pre36, %._crit_edge.loopexit ], [ %9, %1 ]
  %.pre-phi = phi i32 [ %.pre35, %._crit_edge.loopexit ], [ %13, %1 ]
  %19 = phi i32 [ %.pre33, %._crit_edge.loopexit ], [ %3, %1 ]
  %20 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %10, %1 ]
  %21 = load i32, ptr @bmaporgy, align 4, !tbaa !5
  %22 = sub nsw i32 %20, %21
  %23 = srem i32 %22, 8388608
  %.not24 = icmp eq i32 %23, 0
  %reass.sub25 = add i32 %20, 8388608
  %24 = sub i32 %reass.sub25, %23
  %.1 = select i1 %.not24, i32 %20, i32 %24
  store i32 %19, ptr %2, align 4, !tbaa !13
  store i32 %.pre-phi37, ptr %14, align 4, !tbaa !14
  %25 = icmp slt i32 %.1, %.pre-phi
  br i1 %25, label %.lr.ph30, label %._crit_edge31

.lr.ph30:                                         ; preds = %._crit_edge, %.lr.ph30
  %.02028 = phi i32 [ %26, %.lr.ph30 ], [ %.1, %._crit_edge ]
  store i32 %.02028, ptr %11, align 4, !tbaa !9
  store i32 %.02028, ptr %15, align 4, !tbaa !12
  call void @AM_drawMline(ptr noundef nonnull %2, i32 noundef %0)
  %26 = add nsw i32 %.02028, 8388608
  %27 = icmp slt i32 %26, %.pre-phi
  br i1 %27, label %.lr.ph30, label %._crit_edge31, !llvm.loop !17

._crit_edge31:                                    ; preds = %.lr.ph30, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #2
  ret void
}

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


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass