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

@finesine = external global [10240 x i32], align 16
@viewangle = external dso_local global i32, align 4
@rw_normalangle = external global i32, align 4
@projection = external dso_local global i32, align 4
@rw_distance = 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: inlinehint nounwind uwtable
declare hidden i32 @FixedMul(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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @R_ScaleFromGlobalAngle(i32 noundef %0) #3 {
  %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 i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  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
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = load i32, ptr @viewangle, align 4, !tbaa !5
  %12 = sub i32 %10, %11
  %13 = add i32 1073741824, %12
  store i32 %13, ptr %4, align 4, !tbaa !5
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = load i32, ptr @rw_normalangle, align 4, !tbaa !5
  %16 = sub i32 %14, %15
  %17 = add i32 1073741824, %16
  store i32 %17, ptr %5, align 4, !tbaa !5
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = lshr i32 %18, 19
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  store i32 %22, ptr %6, align 4, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = lshr i32 %23, 19
  %25 = zext i32 %24 to i64
  %26 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  store i32 %27, ptr %7, align 4, !tbaa !5
  %28 = load i32, ptr @projection, align 4, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = call i32 @FixedMul(i32 noundef %28, i32 noundef %29)
  store i32 %30, ptr %8, align 4, !tbaa !5
  %31 = load i32, ptr @rw_distance, align 4, !tbaa !5
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = call i32 @FixedMul(i32 noundef %31, i32 noundef %32)
  store i32 %33, ptr %9, align 4, !tbaa !5
  %34 = load i32, ptr %9, align 4, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = ashr i32 %35, 16
  %37 = icmp sgt i32 %34, %36
  br i1 %37, label %38, label %51

38:                                               ; preds = %1
  %39 = load i32, ptr %8, align 4, !tbaa !5
  %40 = load i32, ptr %9, align 4, !tbaa !5
  %41 = call i32 @FixedDiv(i32 noundef %39, i32 noundef %40)
  store i32 %41, ptr %3, align 4, !tbaa !5
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = icmp sgt i32 %42, 4194304
  br i1 %43, label %44, label %45

44:                                               ; preds = %38
  store i32 4194304, ptr %3, align 4, !tbaa !5
  br label %50

45:                                               ; preds = %38
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = icmp slt i32 %46, 256
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  store i32 256, ptr %3, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %48, %45
  br label %50

50:                                               ; preds = %49, %44
  br label %52

51:                                               ; preds = %1
  store i32 4194304, ptr %3, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %51, %50
  %53 = load i32, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  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
  ret i32 %53
}

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 = { "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 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 #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"}


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

@finesine = external local_unnamed_addr global [10240 x i32], align 16
@viewangle = external dso_local local_unnamed_addr global i32, align 4
@rw_normalangle = external local_unnamed_addr global i32, align 4
@projection = external dso_local local_unnamed_addr global i32, align 4
@rw_distance = external local_unnamed_addr global i32, align 4

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -2147483648, 4194305) i32 @R_ScaleFromGlobalAngle(i32 noundef %0) local_unnamed_addr #2 {
  %2 = load i32, ptr @viewangle, align 4, !tbaa !5
  %3 = add i32 %0, 1073741824
  %4 = sub i32 %3, %2
  %5 = load i32, ptr @rw_normalangle, align 4, !tbaa !5
  %6 = sub i32 %3, %5
  %7 = lshr i32 %4, 19
  %8 = zext nneg i32 %7 to i64
  %9 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = lshr i32 %6, 19
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds [10240 x i32], ptr @finesine, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = load i32, ptr @projection, align 4, !tbaa !5
  %16 = tail call i32 @FixedMul(i32 noundef %15, i32 noundef %14)
  %17 = load i32, ptr @rw_distance, align 4, !tbaa !5
  %18 = tail call i32 @FixedMul(i32 noundef %17, i32 noundef %10)
  %19 = ashr i32 %16, 16
  %20 = icmp sgt i32 %18, %19
  br i1 %20, label %21, label %25

21:                                               ; preds = %1
  %22 = tail call i32 @FixedDiv(i32 noundef %16, i32 noundef %18) #4
  %23 = icmp sgt i32 %22, 4194304
  br i1 %23, label %25, label %24

24:                                               ; preds = %21
  %spec.store.select = tail call i32 @llvm.smax.i32(i32 %22, i32 256)
  br label %25

25:                                               ; preds = %1, %21, %24
  %.0 = phi i32 [ %spec.store.select, %24 ], [ 4194304, %21 ], [ 4194304, %1 ]
  ret i32 %.0
}

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

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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass