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

@f_w = external hidden global i32, align 4
@f_h = external hidden global i32, align 4
@min_y = external hidden global i32, align 4
@min_x = external hidden global i32, align 4
@max_y = external hidden global i32, align 4
@max_x = external hidden global i32, align 4
@numvertexes = external global i32, align 4
@vertexes = external global ptr, align 8
@max_w = external hidden global i32, align 4
@max_h = external hidden global i32, align 4
@min_w = external hidden global i32, align 4
@min_h = external hidden global i32, align 4
@min_scale_mtof = external hidden global i32, align 4
@max_scale_mtof = 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

declare i32 @FixedDiv(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 @AM_findMinMaxBoundaries() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 2147483647, ptr @min_y, align 4, !tbaa !5
  store i32 2147483647, ptr @min_x, align 4, !tbaa !5
  store i32 -2147483647, ptr @max_y, align 4, !tbaa !5
  store i32 -2147483647, ptr @max_x, align 4, !tbaa !5
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %75, %0
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = load i32, ptr @numvertexes, align 4, !tbaa !5
  %7 = icmp slt i32 %5, %6
  br i1 %7, label %8, label %78

8:                                                ; preds = %4
  %9 = load ptr, ptr @vertexes, align 8, !tbaa !9
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds %struct.vertex_t, ptr %9, i64 %11
  %13 = getelementptr inbounds %struct.vertex_t, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %15 = load i32, ptr @min_x, align 4, !tbaa !5
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %17, label %24

17:                                               ; preds = %8
  %18 = load ptr, ptr @vertexes, align 8, !tbaa !9
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds %struct.vertex_t, ptr %18, i64 %20
  %22 = getelementptr inbounds %struct.vertex_t, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 4, !tbaa !11
  store i32 %23, ptr @min_x, align 4, !tbaa !5
  br label %41

24:                                               ; preds = %8
  %25 = load ptr, ptr @vertexes, align 8, !tbaa !9
  %26 = load i32, ptr %1, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.vertex_t, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.vertex_t, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 4, !tbaa !11
  %31 = load i32, ptr @max_x, align 4, !tbaa !5
  %32 = icmp sgt i32 %30, %31
  br i1 %32, label %33, label %40

33:                                               ; preds = %24
  %34 = load ptr, ptr @vertexes, align 8, !tbaa !9
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds %struct.vertex_t, ptr %34, i64 %36
  %38 = getelementptr inbounds %struct.vertex_t, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !11
  store i32 %39, ptr @max_x, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %33, %24
  br label %41

41:                                               ; preds = %40, %17
  %42 = load ptr, ptr @vertexes, align 8, !tbaa !9
  %43 = load i32, ptr %1, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds %struct.vertex_t, ptr %42, i64 %44
  %46 = getelementptr inbounds %struct.vertex_t, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %46, align 4, !tbaa !13
  %48 = load i32, ptr @min_y, align 4, !tbaa !5
  %49 = icmp slt i32 %47, %48
  br i1 %49, label %50, label %57

50:                                               ; preds = %41
  %51 = load ptr, ptr @vertexes, align 8, !tbaa !9
  %52 = load i32, ptr %1, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds %struct.vertex_t, ptr %51, i64 %53
  %55 = getelementptr inbounds %struct.vertex_t, ptr %54, i32 0, i32 1
  %56 = load i32, ptr %55, align 4, !tbaa !13
  store i32 %56, ptr @min_y, align 4, !tbaa !5
  br label %74

57:                                               ; preds = %41
  %58 = load ptr, ptr @vertexes, align 8, !tbaa !9
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds %struct.vertex_t, ptr %58, i64 %60
  %62 = getelementptr inbounds %struct.vertex_t, ptr %61, i32 0, i32 1
  %63 = load i32, ptr %62, align 4, !tbaa !13
  %64 = load i32, ptr @max_y, align 4, !tbaa !5
  %65 = icmp sgt i32 %63, %64
  br i1 %65, label %66, label %73

66:                                               ; preds = %57
  %67 = load ptr, ptr @vertexes, align 8, !tbaa !9
  %68 = load i32, ptr %1, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds %struct.vertex_t, ptr %67, i64 %69
  %71 = getelementptr inbounds %struct.vertex_t, ptr %70, i32 0, i32 1
  %72 = load i32, ptr %71, align 4, !tbaa !13
  store i32 %72, ptr @max_y, align 4, !tbaa !5
  br label %73

73:                                               ; preds = %66, %57
  br label %74

74:                                               ; preds = %73, %50
  br label %75

75:                                               ; preds = %74
  %76 = load i32, ptr %1, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %1, align 4, !tbaa !5
  br label %4, !llvm.loop !14

78:                                               ; preds = %4
  %79 = load i32, ptr @max_x, align 4, !tbaa !5
  %80 = load i32, ptr @min_x, align 4, !tbaa !5
  %81 = sub nsw i32 %79, %80
  store i32 %81, ptr @max_w, align 4, !tbaa !5
  %82 = load i32, ptr @max_y, align 4, !tbaa !5
  %83 = load i32, ptr @min_y, align 4, !tbaa !5
  %84 = sub nsw i32 %82, %83
  store i32 %84, ptr @max_h, align 4, !tbaa !5
  store i32 2097152, ptr @min_w, align 4, !tbaa !5
  store i32 2097152, ptr @min_h, align 4, !tbaa !5
  %85 = load i32, ptr @f_w, align 4, !tbaa !5
  %86 = shl i32 %85, 16
  %87 = load i32, ptr @max_w, align 4, !tbaa !5
  %88 = call i32 @FixedDiv(i32 noundef %86, i32 noundef %87)
  store i32 %88, ptr %2, align 4, !tbaa !5
  %89 = load i32, ptr @f_h, align 4, !tbaa !5
  %90 = shl i32 %89, 16
  %91 = load i32, ptr @max_h, align 4, !tbaa !5
  %92 = call i32 @FixedDiv(i32 noundef %90, i32 noundef %91)
  store i32 %92, ptr %3, align 4, !tbaa !5
  %93 = load i32, ptr %2, align 4, !tbaa !5
  %94 = load i32, ptr %3, align 4, !tbaa !5
  %95 = icmp slt i32 %93, %94
  br i1 %95, label %96, label %98

96:                                               ; preds = %78
  %97 = load i32, ptr %2, align 4, !tbaa !5
  br label %100

98:                                               ; preds = %78
  %99 = load i32, ptr %3, align 4, !tbaa !5
  br label %100

100:                                              ; preds = %98, %96
  %101 = phi i32 [ %97, %96 ], [ %99, %98 ]
  store i32 %101, ptr @min_scale_mtof, align 4, !tbaa !5
  %102 = load i32, ptr @f_h, align 4, !tbaa !5
  %103 = shl i32 %102, 16
  %104 = call i32 @FixedDiv(i32 noundef %103, i32 noundef 2097152)
  store i32 %104, ptr @max_scale_mtof, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

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 0}
!12 = !{!"", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 4}
!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.vertex_t = type { i32, i32 }

@f_w = external hidden local_unnamed_addr global i32, align 4
@f_h = external hidden local_unnamed_addr global i32, align 4
@min_y = external hidden local_unnamed_addr global i32, align 4
@min_x = external hidden local_unnamed_addr global i32, align 4
@max_y = external hidden local_unnamed_addr global i32, align 4
@max_x = external hidden local_unnamed_addr global i32, align 4
@numvertexes = external local_unnamed_addr global i32, align 4
@vertexes = external local_unnamed_addr global ptr, align 8
@max_w = external hidden local_unnamed_addr global i32, align 4
@max_h = external hidden local_unnamed_addr global i32, align 4
@min_w = external hidden local_unnamed_addr global i32, align 4
@min_h = external hidden local_unnamed_addr global i32, align 4
@min_scale_mtof = external hidden local_unnamed_addr global i32, align 4
@max_scale_mtof = external hidden local_unnamed_addr global i32, align 4

declare i32 @FixedDiv(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @AM_findMinMaxBoundaries() local_unnamed_addr #1 {
  store i32 2147483647, ptr @min_y, align 4, !tbaa !5
  store i32 2147483647, ptr @min_x, align 4, !tbaa !5
  store i32 -2147483647, ptr @max_y, align 4, !tbaa !5
  store i32 -2147483647, ptr @max_x, align 4, !tbaa !5
  %1 = load i32, ptr @numvertexes, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0
  %3 = load ptr, ptr @vertexes, align 8, !tbaa !9
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %4

4:                                                ; preds = %.lr.ph, %22
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %22 ]
  %5 = phi i32 [ 2147483647, %.lr.ph ], [ %16, %22 ]
  %6 = phi i32 [ -2147483647, %.lr.ph ], [ %15, %22 ]
  %7 = phi i32 [ 2147483647, %.lr.ph ], [ %24, %22 ]
  %8 = phi i32 [ -2147483647, %.lr.ph ], [ %23, %22 ]
  %9 = getelementptr inbounds %struct.vertex_t, ptr %3, i64 %indvars.iv
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = icmp slt i32 %10, %5
  br i1 %11, label %.sink.split, label %12

12:                                               ; preds = %4
  %13 = icmp sgt i32 %10, %6
  br i1 %13, label %.sink.split, label %14

.sink.split:                                      ; preds = %12, %4
  %max_x.sink = phi ptr [ @min_x, %4 ], [ @max_x, %12 ]
  %.ph = phi i32 [ %6, %4 ], [ %10, %12 ]
  %.ph22 = phi i32 [ %10, %4 ], [ %5, %12 ]
  store i32 %10, ptr %max_x.sink, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %.sink.split, %12
  %15 = phi i32 [ %6, %12 ], [ %.ph, %.sink.split ]
  %16 = phi i32 [ %5, %12 ], [ %.ph22, %.sink.split ]
  %17 = getelementptr inbounds %struct.vertex_t, ptr %3, i64 %indvars.iv, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %19 = icmp slt i32 %18, %7
  br i1 %19, label %.sink.split23, label %20

20:                                               ; preds = %14
  %21 = icmp sgt i32 %18, %8
  br i1 %21, label %.sink.split23, label %22

.sink.split23:                                    ; preds = %20, %14
  %min_y.sink = phi ptr [ @min_y, %14 ], [ @max_y, %20 ]
  %.ph24 = phi i32 [ %8, %14 ], [ %18, %20 ]
  %.ph25 = phi i32 [ %18, %14 ], [ %7, %20 ]
  store i32 %18, ptr %min_y.sink, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %.sink.split23, %20
  %23 = phi i32 [ %8, %20 ], [ %.ph24, %.sink.split23 ]
  %24 = phi i32 [ %7, %20 ], [ %.ph25, %.sink.split23 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %4, !llvm.loop !14

._crit_edge.loopexit:                             ; preds = %22
  %25 = sub nsw i32 %15, %16
  %26 = sub nsw i32 %23, %24
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %0
  %27 = phi i32 [ %25, %._crit_edge.loopexit ], [ 2, %0 ]
  %28 = phi i32 [ %26, %._crit_edge.loopexit ], [ 2, %0 ]
  store i32 %27, ptr @max_w, align 4, !tbaa !5
  store i32 %28, ptr @max_h, align 4, !tbaa !5
  store i32 2097152, ptr @min_w, align 4, !tbaa !5
  store i32 2097152, ptr @min_h, align 4, !tbaa !5
  %29 = load i32, ptr @f_w, align 4, !tbaa !5
  %30 = shl i32 %29, 16
  %31 = tail call i32 @FixedDiv(i32 noundef %30, i32 noundef %27) #3
  %32 = load i32, ptr @f_h, align 4, !tbaa !5
  %33 = shl i32 %32, 16
  %34 = load i32, ptr @max_h, align 4, !tbaa !5
  %35 = tail call i32 @FixedDiv(i32 noundef %33, i32 noundef %34) #3
  %36 = tail call i32 @llvm.smin.i32(i32 %31, i32 %35)
  store i32 %36, ptr @min_scale_mtof, align 4, !tbaa !5
  %37 = load i32, ptr @f_h, align 4, !tbaa !5
  %38 = shl i32 %37, 16
  %39 = tail call i32 @FixedDiv(i32 noundef %38, i32 noundef 2097152) #3
  store i32 %39, ptr @max_scale_mtof, align 4, !tbaa !5
  ret void
}

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

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 0}
!12 = !{!"", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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