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

@m_x = external hidden global i32, align 4
@m_y = external hidden global i32, align 4
@f_w = external hidden global i32, align 4
@f_h = external hidden global i32, align 4
@scale_mtof = external hidden global i32, align 4
@markpoints = external hidden global [10 x %struct.mpoint_t], align 16
@marknums = external hidden global [10 x ptr], align 16
@f_y = external hidden global i32, align 4
@f_x = external hidden 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: inlinehint nounwind uwtable
declare hidden i32 @FixedMul(i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @AM_drawMarks() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %71, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 10
  br i1 %8, label %9, label %74

9:                                                ; preds = %6
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [10 x %struct.mpoint_t], ptr @markpoints, i64 0, i64 %11
  %13 = getelementptr inbounds %struct.mpoint_t, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !9
  %15 = icmp ne i32 %14, -1
  br i1 %15, label %16, label %70

16:                                               ; preds = %9
  store i32 5, ptr %4, align 4, !tbaa !5
  store i32 6, ptr %5, align 4, !tbaa !5
  %17 = load i32, ptr @f_x, align 4, !tbaa !5
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [10 x %struct.mpoint_t], ptr @markpoints, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.mpoint_t, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !9
  %23 = load i32, ptr @m_x, align 4, !tbaa !5
  %24 = sub nsw i32 %22, %23
  %25 = load i32, ptr @scale_mtof, align 4, !tbaa !5
  %26 = call i32 @FixedMul(i32 noundef %24, i32 noundef %25)
  %27 = ashr i32 %26, 16
  %28 = add nsw i32 %17, %27
  store i32 %28, ptr %2, align 4, !tbaa !5
  %29 = load i32, ptr @f_y, align 4, !tbaa !5
  %30 = load i32, ptr @f_h, align 4, !tbaa !5
  %31 = load i32, ptr %1, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [10 x %struct.mpoint_t], ptr @markpoints, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.mpoint_t, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 4, !tbaa !11
  %36 = load i32, ptr @m_y, align 4, !tbaa !5
  %37 = sub nsw i32 %35, %36
  %38 = load i32, ptr @scale_mtof, align 4, !tbaa !5
  %39 = call i32 @FixedMul(i32 noundef %37, i32 noundef %38)
  %40 = ashr i32 %39, 16
  %41 = sub nsw i32 %30, %40
  %42 = add nsw i32 %29, %41
  store i32 %42, ptr %3, align 4, !tbaa !5
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = load i32, ptr @f_x, align 4, !tbaa !5
  %45 = icmp sge i32 %43, %44
  br i1 %45, label %46, label %69

46:                                               ; preds = %16
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = load i32, ptr @f_w, align 4, !tbaa !5
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = sub nsw i32 %48, %49
  %51 = icmp sle i32 %47, %50
  br i1 %51, label %52, label %69

52:                                               ; preds = %46
  %53 = load i32, ptr %3, align 4, !tbaa !5
  %54 = load i32, ptr @f_y, align 4, !tbaa !5
  %55 = icmp sge i32 %53, %54
  br i1 %55, label %56, label %69

56:                                               ; preds = %52
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = load i32, ptr @f_h, align 4, !tbaa !5
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sub nsw i32 %58, %59
  %61 = icmp sle i32 %57, %60
  br i1 %61, label %62, label %69

62:                                               ; preds = %56
  %63 = load i32, ptr %2, align 4, !tbaa !5
  %64 = load i32, ptr %3, align 4, !tbaa !5
  %65 = load i32, ptr %1, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [10 x ptr], ptr @marknums, i64 0, i64 %66
  %68 = load ptr, ptr %67, align 8, !tbaa !12
  call void @V_DrawPatch(i32 noundef %63, i32 noundef %64, i32 noundef 0, ptr noundef %68)
  br label %69

69:                                               ; preds = %62, %56, %52, %46, %16
  br label %70

70:                                               ; preds = %69, %9
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %1, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !14

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

declare void @V_DrawPatch(i32 noundef, i32 noundef, i32 noundef, ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 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 = { "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 #4 = { 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 0}
!10 = !{!"", !6, i64 0, !6, i64 4}
!11 = !{!10, !6, i64 4}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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.mpoint_t = type { i32, i32 }

@m_x = external hidden local_unnamed_addr global i32, align 4
@m_y = external hidden local_unnamed_addr global i32, align 4
@f_w = external hidden local_unnamed_addr global i32, align 4
@f_h = external hidden local_unnamed_addr global i32, align 4
@scale_mtof = external hidden local_unnamed_addr global i32, align 4
@markpoints = external hidden local_unnamed_addr global [10 x %struct.mpoint_t], align 16
@marknums = external hidden local_unnamed_addr global [10 x ptr], align 16
@f_y = external hidden local_unnamed_addr global i32, align 4
@f_x = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @FixedMul(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @AM_drawMarks() local_unnamed_addr #1 {
  br label %1

1:                                                ; preds = %0, %34
  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next, %34 ]
  %2 = getelementptr inbounds [10 x %struct.mpoint_t], ptr @markpoints, i64 0, i64 %indvars.iv
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %3, -1
  br i1 %.not, label %34, label %4

4:                                                ; preds = %1
  %5 = load i32, ptr @f_x, align 4, !tbaa !10
  %6 = load i32, ptr @m_x, align 4, !tbaa !10
  %7 = sub nsw i32 %3, %6
  %8 = load i32, ptr @scale_mtof, align 4, !tbaa !10
  %9 = tail call i32 @FixedMul(i32 noundef %7, i32 noundef %8)
  %10 = ashr i32 %9, 16
  %11 = add nsw i32 %10, %5
  %12 = load i32, ptr @f_y, align 4, !tbaa !10
  %13 = load i32, ptr @f_h, align 4, !tbaa !10
  %14 = getelementptr inbounds i8, ptr %2, i64 4
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = load i32, ptr @m_y, align 4, !tbaa !10
  %17 = sub nsw i32 %15, %16
  %18 = load i32, ptr @scale_mtof, align 4, !tbaa !10
  %19 = tail call i32 @FixedMul(i32 noundef %17, i32 noundef %18)
  %20 = ashr i32 %19, 16
  %21 = sub nsw i32 %13, %20
  %22 = add nsw i32 %21, %12
  %23 = load i32, ptr @f_x, align 4, !tbaa !10
  %.not17 = icmp slt i32 %11, %23
  br i1 %.not17, label %34, label %24

24:                                               ; preds = %4
  %25 = load i32, ptr @f_w, align 4, !tbaa !10
  %26 = add nsw i32 %25, -5
  %.not18 = icmp sgt i32 %11, %26
  %27 = load i32, ptr @f_y, align 4
  %.not19 = icmp slt i32 %22, %27
  %or.cond = select i1 %.not18, i1 true, i1 %.not19
  br i1 %or.cond, label %34, label %28

28:                                               ; preds = %24
  %29 = load i32, ptr @f_h, align 4, !tbaa !10
  %30 = add nsw i32 %29, -6
  %.not20 = icmp sgt i32 %22, %30
  br i1 %.not20, label %34, label %31

31:                                               ; preds = %28
  %32 = getelementptr inbounds [10 x ptr], ptr @marknums, i64 0, i64 %indvars.iv
  %33 = load ptr, ptr %32, align 8, !tbaa !12
  tail call void @V_DrawPatch(i32 noundef %11, i32 noundef %22, i32 noundef 0, ptr noundef %33) #3
  br label %34

34:                                               ; preds = %1, %31, %28, %24, %4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 10
  br i1 %exitcond.not, label %35, label %1, !llvm.loop !14

35:                                               ; preds = %34
  ret void
}

declare void @V_DrawPatch(i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { inlinehint 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 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 = { "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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!7, !7, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass