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

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

@colormaps = external global ptr, align 8
@zlight = external dso_local global [16 x [128 x ptr]], align 16

; 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

declare i32 @FixedDiv(i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @R_InitLightTables() #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) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %6

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

9:                                                ; preds = %6
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sub nsw i32 15, %10
  %12 = mul nsw i32 %11, 2
  %13 = mul nsw i32 %12, 32
  %14 = sdiv i32 %13, 16
  store i32 %14, ptr %4, align 4, !tbaa !5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %48, %9
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = icmp slt i32 %16, 128
  br i1 %17, label %18, label %51

18:                                               ; preds = %15
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  %21 = shl i32 %20, 20
  %22 = call i32 @FixedDiv(i32 noundef 10485760, i32 noundef %21)
  store i32 %22, ptr %5, align 4, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = ashr i32 %23, 12
  store i32 %24, ptr %5, align 4, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sdiv i32 %26, 2
  %28 = sub nsw i32 %25, %27
  store i32 %28, ptr %3, align 4, !tbaa !5
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = icmp slt i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %18
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %31, %18
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = icmp sge i32 %33, 32
  br i1 %34, label %35, label %36

35:                                               ; preds = %32
  store i32 31, ptr %3, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %35, %32
  %37 = load ptr, ptr @colormaps, align 8, !tbaa !9
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = mul nsw i32 %38, 256
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %37, i64 %40
  %42 = load i32, ptr %1, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [16 x [128 x ptr]], ptr @zlight, i64 0, i64 %43
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [128 x ptr], ptr %44, i64 0, i64 %46
  store ptr %41, ptr %47, align 8, !tbaa !9
  br label %48

48:                                               ; preds = %36
  %49 = load i32, ptr %2, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %2, align 4, !tbaa !5
  br label %15, !llvm.loop !11

51:                                               ; preds = %15
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %1, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !13

55:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@colormaps = external local_unnamed_addr global ptr, align 8
@zlight = external dso_local local_unnamed_addr global [16 x [128 x ptr]], align 16

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

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

1:                                                ; preds = %0, %15
  %indvars.iv19 = phi i64 [ 0, %0 ], [ %indvars.iv.next20, %15 ]
  %indvars.iv19.tr = trunc i64 %indvars.iv19 to i32
  %2 = shl i32 %indvars.iv19.tr, 6
  %3 = sub i32 960, %2
  %4 = lshr exact i32 %3, 4
  br label %5

5:                                                ; preds = %1, %5
  %indvars.iv = phi i64 [ 0, %1 ], [ %indvars.iv.next, %5 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %indvars.iv.next.tr = trunc i64 %indvars.iv.next to i32
  %6 = shl i32 %indvars.iv.next.tr, 20
  %7 = tail call i32 @FixedDiv(i32 noundef 10485760, i32 noundef %6) #3
  %8 = ashr i32 %7, 12
  %.neg = sdiv i32 %8, -2
  %9 = add nsw i32 %.neg, %4
  %spec.store.select = tail call i32 @llvm.smax.i32(i32 %9, i32 0)
  %spec.store.select1 = tail call i32 @llvm.umin.i32(i32 %spec.store.select, i32 31)
  %10 = load ptr, ptr @colormaps, align 8, !tbaa !5
  %11 = shl nuw nsw i32 %spec.store.select1, 8
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds i8, ptr %10, i64 %12
  %14 = getelementptr inbounds [16 x [128 x ptr]], ptr @zlight, i64 0, i64 %indvars.iv19, i64 %indvars.iv
  store ptr %13, ptr %14, align 8, !tbaa !5
  %exitcond.not = icmp eq i64 %indvars.iv.next, 128
  br i1 %exitcond.not, label %15, label %5, !llvm.loop !9

15:                                               ; preds = %5
  %indvars.iv.next20 = add nuw nsw i64 %indvars.iv19, 1
  %exitcond22.not = icmp eq i64 %indvars.iv.next20, 16
  br i1 %exitcond22.not, label %16, label %1, !llvm.loop !11

16:                                               ; preds = %15
  ret void
}

; 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.umin.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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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